Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for history list is updated with last alert. #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)



Expand Down