diff --git a/ring.py b/ring.py index ac2209e..fa86169 100644 --- a/ring.py +++ b/ring.py @@ -108,27 +108,64 @@ 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) 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') + logger.debug("lastAlertID:"+str(lastAlertID)) + logger.debug("lastAlertKind:"+str(lastAlertKind)) + + if (lastAlertKind == 'ding') or (lastAlertKind == 'motion'): + 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 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) - 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 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('lastCaptureURL ' + str(dev.recording_url(dev.last_recording_id)), dev) + srRing('lastAlertType motion', dev) + setRing('motion', dev) + + if videoIsReadyForDownload: + srRing('lastCaptureURL ' + str(dev.recording_url(dev.last_recording_id)), dev)