import subprocess
import time
-from tornado.httpclient import HTTPClient, AsyncHTTPClient, HTTPRequest
+from tornado import gen
+from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.ioloop import IOLoop
import config
return _started
+@gen.coroutine
def get_motion_detection(camera_id):
thread_id = camera_id_to_thread_id(camera_id)
if thread_id is None:
- return logging.error('could not find thread id for camera with id %s' % camera_id)
-
+ logging.error('could not find thread id for camera with id %s' % camera_id)
+ return
+
url = 'http://127.0.0.1:7999/%(id)s/detection/status' % {'id': thread_id}
request = HTTPRequest(url, connect_timeout=5, request_timeout=5)
- http_client = HTTPClient()
+ http_client = AsyncHTTPClient()
try:
- response = http_client.fetch(request)
+ response = yield http_client.fetch(request)
if response.error:
- raise response.error
-
+ raise response.error
+
except Exception as e:
logging.error('failed to get motion detection status for camera with id %(id)s: %(msg)s' % {
'id': camera_id,
'msg': unicode(e)})
+
+ return
- return None
-
enabled = bool(response.body.lower().count('active'))
logging.debug('motion detection is %(what)s for camera with id %(id)s' % {
'what': ['disabled', 'enabled'][enabled],
'id': camera_id})
-
- return enabled
+
+ raise gen.Return(enabled)
def set_motion_detection(camera_id, enabled):
import datetime
import logging
-import tornado
+
+from tornado import ioloop, gen
import config
import motionctl
def start():
- ioloop = tornado.ioloop.IOLoop.instance()
- ioloop.add_timeout(datetime.timedelta(seconds=10), _check_ws)
+ io_loop = ioloop.IOLoop.instance()
+ io_loop.add_timeout(datetime.timedelta(seconds=1), _check_ws)
def _during_working_schedule(now, working_schedule):
return True
+@gen.coroutine
def _check_ws():
# schedule the next call
- ioloop = tornado.ioloop.IOLoop.instance()
- ioloop.add_timeout(datetime.timedelta(seconds=10), _check_ws)
+ io_loop = ioloop.IOLoop.instance()
+ io_loop.add_timeout(datetime.timedelta(seconds=10), _check_ws)
if not motionctl.running():
return
-
+
now = datetime.datetime.now()
for camera_id in config.get_camera_ids():
camera_config = config.get_camera(camera_id)
now_during = _during_working_schedule(now, working_schedule)
must_be_enabled = (now_during and working_schedule_type == 'during') or (not now_during and working_schedule_type == 'outside')
- currently_enabled = motionctl.get_motion_detection(camera_id)
+ currently_enabled = yield motionctl.get_motion_detection(camera_id)
if currently_enabled is None: # could not detect current status
logging.warn('skipping motion detection status update for camera with id %(id)s' % {'id': camera_id})
continue