From 8f9749d759751b6bc35df069caef6af325b3ab63 Mon Sep 17 00:00:00 2001 From: Niels Hetzke Date: Fri, 11 Jan 2019 15:01:00 +0100 Subject: [PATCH 1/2] Wait for history list is updated with last alert. --- ring.py | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/ring.py b/ring.py index ac2209e..db41e74 100644 --- a/ring.py +++ b/ring.py @@ -114,21 +114,40 @@ def alertDevice(dev,alert): srRing('lastAlertDeviceName ' + str(dev.name), dev) srRing('lastAlertSipTo ' + str(alert.get('sip_to')), dev) srRing('lastAlertSipToken ' + str(alert.get('sip_token')), dev) - if(alert.get('kind') == 'ding'): - setRing('ring', dev) - srRing('lastAlertType ring', dev) - his = dev.history(limit=100,kind='ding') - if(len(his)>0): - dev.recording_download(his[0]['id'], filename=fhem_path + 'last_ding_video.mp4',override=True) + + lastAlertID = alert.get('id') + lastAlertKind = alert.get('kind') + + if (lastAlertKind == 'ding') or (lastAlertKind == 'motion'): + lastHistoryID = dev.history(limit=1,kind=lastAlertKind)[0]['id'] + + #Wait and check history until new alert is added + i=0 + while lastHistoryID != lastAlertID: + logger.debug("Wait and check history until new alert is added lastHistoryID != lastAlertID") + logger.debug(" lastHistoryID:"+str(lastHistoryID)) + logger.debug(" lastAlertID:"+str(lastAlertID)) + time.sleep(POLLS) + lastHistoryID = dev.history(limit=1,kind=lastAlertKind)[0]['id'] + + i+=1 + if i>60: # break when no history object can be found + break + + if (lastAlertKind == 'ding') and i<=60: + dev.recording_download(lastAlertID, filename=fhem_path + 'last_ding_video.mp4',override=True) srRing('lastDingVideo ' + fhem_path + 'last_ding_video.mp4', dev) - elif(alert.get('kind') == 'motion'): - setRing('motion', dev) - srRing('lastAlertType motion', dev) - his = dev.history(limit=100,kind='motion') - if(len(his)>0): - dev.recording_download(his[0]['id'], filename=fhem_path + 'last_motion_video.mp4',override=True) + setRing('ring', dev) + srRing('lastAlertType ring', dev) + + elif (lastAlertKind == 'motion') and i<=60: + dev.recording_download(lastAlertID, filename=fhem_path + 'last_motion_video.mp4',override=True) srRing('lastMotionVideo ' + fhem_path + 'last_motion_video.mp4', dev) - srRing('lastCaptureURL ' + str(dev.recording_url(dev.last_recording_id)), dev) + srRing('lastAlertType motion', dev) + setRing('motion', dev) + + if i<=60: + srRing('lastCaptureURL ' + str(dev.recording_url(dev.last_recording_id)), dev) From ba177d13334ce804ced881265dfe1d882c6184d2 Mon Sep 17 00:00:00 2001 From: Niels Hetzke Date: Fri, 11 Jan 2019 22:14:01 +0100 Subject: [PATCH 2/2] Improv the downloading process --- ring.py | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/ring.py b/ring.py index db41e74..fa86169 100644 --- a/ring.py +++ b/ring.py @@ -108,6 +108,15 @@ def pollDevices(): if i>600: break +def findHistoryItem(historyArry,id): + ret = None + for singleItem in historyArry: + if (singleItem['id']==id): + ret = singleItem + break + return ret + + def alertDevice(dev,alert): srRing('lastAlertDeviceID ' + str(dev.id), dev) srRing('lastAlertDeviceAccountID ' + str(dev.account_id), dev) @@ -117,36 +126,45 @@ def alertDevice(dev,alert): lastAlertID = alert.get('id') lastAlertKind = alert.get('kind') + logger.debug("lastAlertID:"+str(lastAlertID)) + logger.debug("lastAlertKind:"+str(lastAlertKind)) if (lastAlertKind == 'ding') or (lastAlertKind == 'motion'): - lastHistoryID = dev.history(limit=1,kind=lastAlertKind)[0]['id'] - - #Wait and check history until new alert is added - i=0 - while lastHistoryID != lastAlertID: - logger.debug("Wait and check history until new alert is added lastHistoryID != lastAlertID") - logger.debug(" lastHistoryID:"+str(lastHistoryID)) - logger.debug(" lastAlertID:"+str(lastAlertID)) - time.sleep(POLLS) - lastHistoryID = dev.history(limit=1,kind=lastAlertKind)[0]['id'] - - i+=1 - if i>60: # break when no history object can be found + videoIsReadyForDownload = None + counti = 1 + while (videoIsReadyForDownload is None): + logger.debug(str(counti) + ". Try to find hitory and video in history data list") + logger.debug(" hitoryID:"+str(lastAlertID)) + try: + singleHistoryItem = findHistoryItem(dev.history(limit=10,kind=lastAlertKind),lastAlertID) + if singleHistoryItem and singleHistoryItem['id'] == lastAlertID : + logger.debug("History item found!") + if singleHistoryItem['recording']['status'] == 'ready': + logger.debug("Video is now ready to downloading") + videoIsReadyForDownload = True + except Exception as inst: + logger.debug("Repeating...") + time.sleep(1) + counti+=1 + if (counti>240): + logger.debug("Stop trying to find history and video data") break - if (lastAlertKind == 'ding') and i<=60: + if (lastAlertKind == 'ding') and videoIsReadyForDownload: + logger.debug("Start downloading new ding video now") dev.recording_download(lastAlertID, filename=fhem_path + 'last_ding_video.mp4',override=True) srRing('lastDingVideo ' + fhem_path + 'last_ding_video.mp4', dev) setRing('ring', dev) srRing('lastAlertType ring', dev) - elif (lastAlertKind == 'motion') and i<=60: + elif (lastAlertKind == 'motion') and videoIsReadyForDownload: + logger.debug("Start downloading new motion video now") dev.recording_download(lastAlertID, filename=fhem_path + 'last_motion_video.mp4',override=True) srRing('lastMotionVideo ' + fhem_path + 'last_motion_video.mp4', dev) srRing('lastAlertType motion', dev) setRing('motion', dev) - if i<=60: + if videoIsReadyForDownload: srRing('lastCaptureURL ' + str(dev.recording_url(dev.last_recording_id)), dev)