Skip to content

Commit e81cd46

Browse files
authored
Capture tidy up (#117)
* Tidy up capture code. Better naming and double check work actually needs doing. * Tidy up capture code. Better naming and double check work actually needs doing. * Tidy up capture code. Better naming and double check work actually needs doing. * Tidy up capture code. Better naming and double check work actually needs doing.
1 parent 77c202b commit e81cd46

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

changelog

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.8.0b7:
2+
Fix missing event issue.
3+
Tidy capture code.
4+
Specify Python 3
15
0.8.0b6: Simplify state checking; bring API up to date and remove deprecated
26
calls
37
Add back the packet docs.

pyaarlo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
from pyaarlo.main import main_func
44

pyaarlo/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
_LOGGER = logging.getLogger("pyaarlo")
4747

48-
__version__ = "0.8.0b6"
48+
__version__ = "0.8.0b7"
4949

5050

5151
class PyArlo(object):

pyaarlo/camera.py

+30-25
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def _dump_activities(self, msg):
146146
)
147147

148148
# Media library has updated, reload today's events.
149-
def _update_media(self):
149+
def _update_from_media_library(self):
150150
self.debug("reloading cache for " + self._name)
151151
count, videos = self._arlo.ml.videos_for(self)
152152
if videos:
@@ -163,7 +163,7 @@ def _update_media(self):
163163
self._cache_count = count
164164
self._cached_videos = videos
165165

166-
# update latest video details
166+
# Update latest video details.
167167
if videos:
168168
if videos[0].created_at != self._load(LAST_VIDEO_CREATED_KEY):
169169
self._save_and_do_callbacks(LAST_VIDEO_CREATED_KEY, videos[0].created_at)
@@ -172,7 +172,7 @@ def _update_media(self):
172172
self._save_and_do_callbacks(LAST_VIDEO_OBJECT_TYPE, videos[0].object_type)
173173
self._save_and_do_callbacks(LAST_VIDEO_OBJECT_REGION, videos[0].object_region)
174174

175-
# signal video up!
175+
# Tell anyone listening about the new capture and how it affects things.
176176
self._save_and_do_callbacks(CAPTURED_TODAY_KEY, captured_today)
177177
if last_captured is not None:
178178
self._save_and_do_callbacks(LAST_CAPTURE_KEY, last_captured)
@@ -181,18 +181,24 @@ def _update_media(self):
181181
# new snapshot?
182182
snapshot = self._arlo.ml.snapshot_for(self)
183183
if snapshot is not None:
184-
self.debug("snapshot updated for media " + self.name)
185-
self._save(SNAPSHOT_KEY, snapshot.image_url)
186-
self._arlo.bg.run_low(self._update_snapshot)
184+
if self._load(SNAPSHOT_KEY, None) != snapshot.image_url:
185+
self.debug("snapshot updated for media " + self.name)
186+
self._save(SNAPSHOT_KEY, snapshot.image_url)
187+
self._arlo.bg.run_low(self._update_image_from_snapshot)
188+
else:
189+
self.debug("snapshot already done for " + self.name)
187190

188-
# new image?
191+
# New image? Then fetch it an update image details.
189192
if last_image is not None:
190-
self.debug("image updated for media " + self.name)
191-
self._save(LAST_IMAGE_KEY, last_image)
192-
self._arlo.bg.run_low(self._update_image)
193+
if self._load(LAST_IMAGE_KEY, None) != last_image:
194+
self.debug("image updated for media " + self.name)
195+
self._save(LAST_IMAGE_KEY, last_image)
196+
self._arlo.bg.run_low(self._update_image_from_capture)
197+
else:
198+
self.debug("image already done for " + self.name)
193199

194200
# Update last captured image.
195-
def _update_image(self):
201+
def _update_image_from_capture(self):
196202
# Get image and date, if fails ignore
197203
img, date = http_get_img(self._load(LAST_IMAGE_KEY, None))
198204
if img is None:
@@ -205,14 +211,13 @@ def _update_image(self):
205211
date = date.strftime(self._arlo.cfg.last_format)
206212
self.debug(f"updating image for {self.name} ({date})")
207213
self._save_and_do_callbacks(LAST_IMAGE_SRC_KEY, "capture/" + date)
208-
self._save_and_do_callbacks(LAST_CAPTURE_KEY, date)
209214
self._save_and_do_callbacks(LAST_IMAGE_DATA_KEY, img)
210215
else:
211216
date = date.strftime(self._arlo.cfg.last_format)
212217
self.vdebug(f"ignoring image for {self.name} ({date})")
213218

214219
# Update the last snapshot
215-
def _update_snapshot(self, ignore_date=False):
220+
def _update_image_from_snapshot(self, ignore_date=False):
216221
# Get image and date, if fails ignore.
217222
img, date = http_get_img(self._load(SNAPSHOT_KEY, None), ignore_date)
218223
if img is None:
@@ -270,7 +275,7 @@ def _queue_media_updates(self):
270275
for retry in self._arlo.cfg.media_retry:
271276
self.debug("queueing update in {}".format(retry))
272277
self._arlo.bg.run_in(
273-
self._arlo.ml.queue_update, retry, cb=self._update_media
278+
self._arlo.ml.queue_update, retry, cb=self._update_from_media_library
274279
)
275280

276281
def _mark_as_idle(self):
@@ -371,20 +376,20 @@ def _event_handler(self, resource, event):
371376
if LAST_IMAGE_KEY in event:
372377
if not self.is_taking_snapshot:
373378
self.debug("{} -> thumbnail changed".format(self.name))
374-
self._arlo.bg.run_low(self._update_image)
379+
self._arlo.bg.run_low(self._update_image_from_capture)
375380
else:
376381
self.debug(
377382
"{} -> snapshot(thumbnail) ready".format(self.name)
378383
)
379384
self._save(SNAPSHOT_KEY, event.get(LAST_IMAGE_KEY, ""))
380-
self._arlo.bg.run_low(self._update_snapshot, ignore_date=True)
385+
self._arlo.bg.run_low(self._update_image_from_snapshot, ignore_date=True)
381386

382387
# Recording has stopped so a new video is available. Queue an
383388
# media update, this could later trigger a snapshot or image
384389
# update.
385390
if event.get(RECORDING_STOPPED_KEY, False):
386391
self.debug("{} -> recording stopped".format(self.name))
387-
self._arlo.ml.queue_update(self._update_media)
392+
self._arlo.ml.queue_update(self._update_from_media_library)
388393

389394
# Examine the URL passed; snapshots contain `/snapshots/` and
390395
# recordings contain `recordings`. For snapshot, save URL and queue
@@ -394,7 +399,7 @@ def _event_handler(self, resource, event):
394399
if "/snapshots/" in value:
395400
self.debug("{} -> snapshot1 ready".format(self.name))
396401
self._save(SNAPSHOT_KEY, value)
397-
self._arlo.bg.run_low(self._update_snapshot)
402+
self._arlo.bg.run_low(self._update_image_from_snapshot)
398403
if "/recordings/" in value:
399404
self.debug("{} -> new recording ready".format(self.name))
400405

@@ -443,15 +448,15 @@ def _event_handler(self, resource, event):
443448
if value is not None:
444449
self.debug("{} -> snapshot2 ready".format(self.name))
445450
self._save(SNAPSHOT_KEY, value)
446-
self._arlo.bg.run_low(self._update_snapshot)
451+
self._arlo.bg.run_low(self._update_image_from_snapshot)
447452

448453
# Non subscription...
449454
if event.get("action", "") == "lastImageSnapshotAvailable":
450455
value = event.get("properties", {}).get("presignedLastImageUrl", None)
451456
if value is not None:
452457
self.debug("{} -> snapshot3 ready".format(self.name))
453458
self._save(SNAPSHOT_KEY, value)
454-
self._arlo.bg.run_low(self._update_snapshot)
459+
self._arlo.bg.run_low(self._update_image_from_snapshot)
455460

456461
# Ambient sensors update, decode and push changes.
457462
if resource.endswith("/ambientSensors/history"):
@@ -704,10 +709,10 @@ def update_media(self, wait=None):
704709
wait = self._arlo.cfg.synchronous_mode
705710
if wait:
706711
self.debug("doing media update")
707-
self._update_media()
712+
self._update_from_media_library()
708713
else:
709714
self.debug("queueing media update")
710-
self._arlo.bg.run_low(self._update_media)
715+
self._arlo.bg.run_low(self._update_from_media_library)
711716

712717
def update_last_image(self, wait=None):
713718
"""Requests last thumbnail from the backend server.
@@ -721,10 +726,10 @@ def update_last_image(self, wait=None):
721726
wait = self._arlo.cfg.synchronous_mode
722727
if wait:
723728
self.debug("doing image update")
724-
self._update_image()
729+
self._update_image_from_capture()
725730
else:
726731
self.debug("queueing image update")
727-
self._arlo.bg.run_low(self._update_image)
732+
self._arlo.bg.run_low(self._update_image_from_capture)
728733

729734
def update_ambient_sensors(self):
730735
"""Requests the latest temperature, humidity and air quality settings.
@@ -812,7 +817,7 @@ def request_snapshot(self):
812817
for check in self._arlo.cfg.snapshot_checks:
813818
self.debug("queueing snapshot check in {}".format(check))
814819
self._arlo.bg.run_in(
815-
self._arlo.ml.queue_update, check, cb=self._update_media
820+
self._arlo.ml.queue_update, check, cb=self._update_from_media_library
816821
)
817822

818823
self.vdebug("handle dodgy cameras")

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def readme():
1111
setup(
1212

1313
name='pyaarlo',
14-
version='0.8.0b6',
14+
version='0.8.0b7',
1515
packages=['pyaarlo'],
1616

1717
python_requires='>=3.6',

0 commit comments

Comments
 (0)