Improve catching polling errors

try/expect around http requests
This commit is contained in:
loosrob 2021-07-30 17:55:08 +02:00
parent 2b82773070
commit 54e21a4f21

View File

@ -48,8 +48,7 @@ class ZeroconfListener(object):
return return
pollTimer = None pollTimer = None
pollFrequency = 40
playPoll = False
def discoverThings(info): def discoverThings(info):
if info.thingClassId == receiverThingClassId: if info.thingClassId == receiverThingClassId:
@ -314,7 +313,7 @@ def setupThing(info):
return return
def pollReceiver(info): def pollReceiver(info):
global playPoll global pollFrequency
if info.thingClassId == zoneThingClassId: if info.thingClassId == zoneThingClassId:
# get parent receiver thing, needed to get deviceIp # get parent receiver thing, needed to get deviceIp
for possibleParent in myThings(): for possibleParent in myThings():
@ -333,7 +332,15 @@ def pollReceiver(info):
rUrl = 'http://' + deviceIp + ':80/YamahaRemoteControl/ctrl' rUrl = 'http://' + deviceIp + ':80/YamahaRemoteControl/ctrl'
body = bodyStart + '<Basic_Status>GetParam</Basic_Status>' + bodyEnd body = bodyStart + '<Basic_Status>GetParam</Basic_Status>' + bodyEnd
headers = {'Content-Type': 'text/xml', 'Accept': '*/*'} headers = {'Content-Type': 'text/xml', 'Accept': '*/*'}
try:
pr = requests.post(rUrl, headers=headers, data=body) pr = requests.post(rUrl, headers=headers, data=body)
polled = True
except:
logger.log("Device didn't respond to http request to get basic status", deviceIp, info.name)
polled = False
logger.log("Temporarily reducing pollfrequency to give the device some rest")
pollFrequency = 120
if polled == True:
pollResponse = pr.text pollResponse = pr.text
# add distinction between receiver & zone # add distinction between receiver & zone
if info.thingClassId == receiverThingClassId: if info.thingClassId == receiverThingClassId:
@ -443,7 +450,15 @@ def pollReceiver(info):
# Get player info # Get player info
body = '<YAMAHA_AV cmd="GET"><' + inputSource + '><Play_Info>GetParam</Play_Info></' + inputSource + '></YAMAHA_AV>' body = '<YAMAHA_AV cmd="GET"><' + inputSource + '><Play_Info>GetParam</Play_Info></' + inputSource + '></YAMAHA_AV>'
headers = {'Content-Type': 'text/xml', 'Accept': '*/*'} headers = {'Content-Type': 'text/xml', 'Accept': '*/*'}
try:
plr = requests.post(rUrl, headers=headers, data=body) plr = requests.post(rUrl, headers=headers, data=body)
polled = True
except:
logger.log("Device didn't respond to http request to get player status", deviceIp, info.name)
polled = False
logger.log("Temporarily reducing pollfrequency to give the device some rest")
pollFrequency = 120
if polled == True:
if plr.status_code == requests.codes.ok and powerState == True: if plr.status_code == requests.codes.ok and powerState == True:
playerResponse = plr.text playerResponse = plr.text
# Get repeat state # Get repeat state
@ -468,13 +483,13 @@ def pollReceiver(info):
responseExtract = playerResponse[stringIndex1+15:stringIndex2] responseExtract = playerResponse[stringIndex1+15:stringIndex2]
if responseExtract == "Play": if responseExtract == "Play":
playStatus = "Playing" playStatus = "Playing"
playPoll = True or playPoll pollFrequency = min(10, pollFrequency)
elif responseExtract == "Pause": elif responseExtract == "Pause":
playStatus = "Paused" playStatus = "Paused"
playPoll = True or playPoll pollFrequency = min(10, pollFrequency)
else: else:
playStatus = "Stopped" playStatus = "Stopped"
playPoll = False or playPoll pollFrequency = min(30, pollFrequency)
receiver.setStateValue(receiverPlaybackStatusStateTypeId, playStatus) receiver.setStateValue(receiverPlaybackStatusStateTypeId, playStatus)
# Get meta info # Get meta info
stringIndex1 = playerResponse.find("<Artist>") stringIndex1 = playerResponse.find("<Artist>")
@ -551,7 +566,15 @@ def pollReceiver(info):
# Get player info # Get player info
body = '<YAMAHA_AV cmd="GET"><' + inputSource + '><Play_Info>GetParam</Play_Info></' + inputSource + '></YAMAHA_AV>' body = '<YAMAHA_AV cmd="GET"><' + inputSource + '><Play_Info>GetParam</Play_Info></' + inputSource + '></YAMAHA_AV>'
headers = {'Content-Type': 'text/xml', 'Accept': '*/*'} headers = {'Content-Type': 'text/xml', 'Accept': '*/*'}
try:
plr = requests.post(rUrl, headers=headers, data=body) plr = requests.post(rUrl, headers=headers, data=body)
polled = True
except:
logger.log("Device didn't respond to http request to get player status", deviceIp, info.name)
polled = False
logger.log("Temporarily reducing pollfrequency to give the device some rest")
pollFrequency = 120
if polled == True:
if plr.status_code == requests.codes.ok and powerState == True: if plr.status_code == requests.codes.ok and powerState == True:
playerResponse = plr.text playerResponse = plr.text
# Get repeat state # Get repeat state
@ -576,13 +599,13 @@ def pollReceiver(info):
responseExtract = playerResponse[stringIndex1+15:stringIndex2] responseExtract = playerResponse[stringIndex1+15:stringIndex2]
if responseExtract == "Play": if responseExtract == "Play":
playStatus = "Playing" playStatus = "Playing"
playPoll = True or playPoll pollFrequency = min(10, pollFrequency)
elif responseExtract == "Pause": elif responseExtract == "Pause":
playStatus = "Paused" playStatus = "Paused"
playPoll = True or playPoll pollFrequency = min(10, pollFrequency)
else: else:
playStatus = "Stopped" playStatus = "Stopped"
playPoll = False or playPoll pollFrequency = min(30, pollFrequency)
zone.setStateValue(zonePlaybackStatusStateTypeId, playStatus) zone.setStateValue(zonePlaybackStatusStateTypeId, playStatus)
# Get meta info # Get meta info
stringIndex1 = playerResponse.find("<Artist>") stringIndex1 = playerResponse.find("<Artist>")
@ -617,17 +640,12 @@ def pollReceiver(info):
def pollService(): def pollService():
logger.log("pollService!!!") logger.log("pollService!!!")
global pollTimer global pollTimer
global playPoll global pollFrequency
# restart the timer for next poll (if player was playing at previous poll, increase poll frequency) # restart the timer for next poll
# we start the timer before polling the receivers to test if this avoids the timer not restarting due to request errors in pollReceiver logger.log("Restarting timer @ pollService, with frequency", pollFrequency)
if playPoll == True: pollTimer = threading.Timer(pollFrequency, pollService)
interval = 10
else:
interval = 30
logger.log("Restarting timer @ pollService")
pollTimer = threading.Timer(interval, pollService)
pollTimer.start() pollTimer.start()
playPoll = False pollFrequency = 30
# Poll all receivers we know # Poll all receivers we know
for thing in myThings(): for thing in myThings():
if thing.thingClassId == receiverThingClassId or thing.thingClassId == zoneThingClassId: if thing.thingClassId == receiverThingClassId or thing.thingClassId == zoneThingClassId: