Skip to content

Commit

Permalink
Update requests are now throttled to prevent unintentional hammering …
Browse files Browse the repository at this point in the history
…of Logi API.
  • Loading branch information
evanjd committed Sep 10, 2018
1 parent 20b77b9 commit 15f0068
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ loop.close()
- `set_streaming_mode` now accepts a boolean instead of string.
- Removed `model_type` property, replaced with `mount` and `model_generation` properties.
- Added `battery_saving` property and `set_battery_saving_mode` method
- 0.1.7
- Implemented rudimentary throttling on `update()` requests

## Meta

Expand Down
23 changes: 16 additions & 7 deletions logi_circle/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
# vim:sw=4:ts=4:et:
import logging
import os
from datetime import datetime
from datetime import datetime, timedelta
from subprocess import CalledProcessError
from tempfile import NamedTemporaryFile
import pytz
from aiohttp.client_exceptions import ClientResponseError
from .const import (
PROTOCOL, ACCESSORIES_ENDPOINT, ACTIVITIES_ENDPOINT, IMAGES_ENDPOINT, JPEG_CONTENT_TYPE, FEATURES,
MODEL_GEN_1, MODEL_GEN_2, MODEL_GEN_1_NAME, MODEL_GEN_2_NAME, MODEL_GEN_UNKNOWN_NAME,
MODEL_GEN_1_MOUNT, MODEL_GEN_2_MOUNT_WIRED, MODEL_GEN_2_MOUNT_WIRELESS)
MODEL_GEN_1_MOUNT, MODEL_GEN_2_MOUNT_WIRED, MODEL_GEN_2_MOUNT_WIRELESS,
UPDATE_REQUEST_THROTTLE)
from .activity import Activity
from .live_stream import LiveStream
from .utils import (_stream_to_file, _write_to_file, _delete_quietly, _get_file_duration,
Expand All @@ -28,6 +29,8 @@ def __init__(self, logi, camera):
"""Initialise Logi Camera object."""
self._logi = logi
self._attrs = {}
# Internal prop to determine when the next update is allowed.
self._next_update_time = datetime.utcnow()

self._set_attributes(camera)

Expand Down Expand Up @@ -80,15 +83,21 @@ def _set_attributes(self, camera):

self._local_tz = pytz.timezone(self._attrs['timezone'])

async def update(self):
async def update(self, force=False):
"""Poll API for changes to camera properties."""
_LOGGER.debug('Updating properties for camera %s', self.name)

url = '%s/%s' % (ACCESSORIES_ENDPOINT, self.id)
camera = await self._logi._fetch(
url=url, method='GET')
if force is True or datetime.utcnow() >= self._next_update_time:
url = '%s/%s' % (ACCESSORIES_ENDPOINT, self.id)
camera = await self._logi._fetch(
url=url, method='GET')

self._set_attributes(camera)
self._set_attributes(camera)
self._next_update_time = datetime.utcnow(
) + timedelta(seconds=UPDATE_REQUEST_THROTTLE)
else:
_LOGGER.debug('Request to update ignored, requested too soon after previous update. Throttle interval is %s.',
UPDATE_REQUEST_THROTTLE)

def supported_features(self):
"""Returns an array of supported sensors for this camera."""
Expand Down
1 change: 1 addition & 0 deletions logi_circle/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
except (AttributeError, TypeError):
CACHE_FILE = os.path.join('.', '.logi_circle-session.cache')
COOKIE_NAME = 'prod_session'
UPDATE_REQUEST_THROTTLE = 5 # seconds

# Date formats
# Yes, we're hard-coding the timezone. "%z" is a py37 only feature,
Expand Down
2 changes: 1 addition & 1 deletion logi_circle/live_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def _get_mpd(self):
"""Gets the MPD XML and extracts the data required to download segments"""

# Force an update to get the latest node ID
await self._camera.update()
await self._camera.update(force=True)

# Get MPD XML and save to raw_xml var
url = self._get_mpd_url()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def readme():
setup(
name='logi_circle',
packages=['logi_circle'],
version='0.1.6',
version='0.1.7',
description='A Python library to communicate with Logi Circle cameras',
long_description=readme(),
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 15f0068

Please sign in to comment.