def print_usage():
- print 'Usage: eventrelay.py <event> <camera_id>'
+ print 'Usage: eventrelay.py <event> <thread_id>'
def get_admin_credentials():
sys.exit(-1)
event = sys.argv[1]
- camera_id = sys.argv[2]
+ thread_id = sys.argv[2]
logging.debug('event = %s' % event)
- logging.debug('camera_id = %s' % camera_id)
+ logging.debug('thread_id = %s' % thread_id)
admin_username, admin_password = get_admin_credentials()
- uri = '/config/%(camera_id)s/_relay_event/?event=%(event)s&_username=%(username)s' % {
+ uri = '/_relay_event/?event=%(event)s&thread_id=%(thread_id)s&_username=%(username)s' % {
'username': admin_username,
- 'camera_id': camera_id,
+ 'thread_id': thread_id,
'event': event}
signature = compute_signature('POST', uri, '', admin_password)
elif op == 'restore':
self.restore()
- elif op == '_relay_event':
- self._relay_event(camera_id)
-
else:
raise HTTPError(400, 'unknown operation')
else:
self.finish_json({'ok': False})
- @BaseHandler.auth(admin=True)
- def _relay_event(self, camera_id):
- event = self.get_argument('event')
- logging.debug('event %(event)s relayed for camera with id %(id)s' % {'event': event, 'id': camera_id})
-
- try:
- camera_config = config.get_camera(camera_id)
-
- except:
- logging.warn('ignoring event for remote camera with id %s (probably removed)' % camera_id)
- return self.finish_json()
-
- if not utils.local_motion_camera(camera_config):
- logging.warn('ignoring event for non-local camera with id %s' % camera_id)
- return self.finish_json()
-
- if event == 'start':
- if not camera_config['@motion_detection']:
- logging.debug('ignoring start event for camera with id %s and motion detection disabled' % camera_id)
- return self.finish_json()
-
- motionctl._motion_detected[camera_id] = True
-
- elif event == 'stop':
- motionctl._motion_detected[camera_id] = False
-
- else:
- logging.warn('unknown event %s' % event)
-
- self.finish_json()
-
class PictureHandler(BaseHandler):
@asynchronous
raise HTTPError(400, 'unknown operation')
+class RelayEventHandler(BaseHandler):
+ @BaseHandler.auth(admin=True)
+ def post(self):
+ event = self.get_argument('event')
+ thread_id = int(self.get_argument('thread_id'))
+ logging.debug('recevied relayed event %(event)s for thread id %(id)s' % {'event': event, 'id': thread_id})
+
+ camera_id = motionctl.thread_id_to_camera_id(thread_id)
+ try:
+ camera_config = config.get_camera(camera_id)
+
+ except:
+ logging.warn('ignoring event for remote camera with id %s (probably removed)' % camera_id)
+ return self.finish_json()
+
+ if not utils.local_motion_camera(camera_config):
+ logging.warn('ignoring event for non-local camera with id %s' % camera_id)
+ return self.finish_json()
+
+ if event == 'start':
+ if not camera_config['@motion_detection']:
+ logging.debug('ignoring start event for camera with id %s and motion detection disabled' % camera_id)
+ return self.finish_json()
+
+ motionctl.set_motion_detected(camera_id, True)
+
+ elif event == 'stop':
+ motionctl.set_motion_detected(camera_id, False)
+
+ else:
+ logging.warn('unknown event %s' % event)
+
+ self.finish_json()
+
+
class LogHandler(BaseHandler):
LOGS = {
'motion': (os.path.join(settings.LOG_PATH, 'motion.log'), 'motion.log'),
def get_motion_detection(camera_id):
- thread_id = _get_thread_id(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)
def set_motion_detection(camera_id, enabled):
- thread_id = _get_thread_id(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)
return _motion_detected.get(camera_id, False)
-def _get_thread_id(camera_id):
+def set_motion_detected(camera_id, motion_detected):
+ if motion_detected:
+ logging.debug('marking motion detected for camera with id %s' % camera_id)
+
+ else:
+ logging.debug('clearing motion detected for camera with id %s' % camera_id)
+
+ _motion_detected[camera_id] = motion_detected
+
+
+def camera_id_to_thread_id(camera_id):
# find the corresponding thread_id
# (which can be different from camera_id)
camera_ids = config.get_camera_ids()
thread_id += 1
if cid == camera_id:
- break
-
- else:
- return None
+ return thread_id or None
+
+ return None
- if thread_id == 0:
- return None
+
+def thread_id_to_camera_id(thread_id):
+ # find the corresponding camera_id
+ # (which can be different from thread_id)
+ camera_ids = config.get_camera_ids()
+ tid = 0
+ for cid in camera_ids:
+ camera_config = config.get_camera(cid)
+ if utils.local_motion_camera(camera_config):
+ tid += 1
+ if tid == thread_id:
+ return cid
- return thread_id
+ return None
def _get_pid():
[
(r'^/$', handlers.MainHandler),
(r'^/config/main/(?P<op>set|get)/?$', handlers.ConfigHandler),
- (r'^/config/(?P<camera_id>\d+)/(?P<op>get|set|rem|set_preview|_relay_event)/?$', handlers.ConfigHandler),
+ (r'^/config/(?P<camera_id>\d+)/(?P<op>get|set|rem|set_preview)/?$', handlers.ConfigHandler),
(r'^/config/(?P<op>add|list|list_devices|backup|restore)/?$', handlers.ConfigHandler),
(r'^/picture/(?P<camera_id>\d+)/(?P<op>current|list|frame)/?$', handlers.PictureHandler),
(r'^/picture/(?P<camera_id>\d+)/(?P<op>download|preview|delete)/(?P<filename>.+?)/?$', handlers.PictureHandler),
(r'^/movie/(?P<camera_id>\d+)/(?P<op>list)/?$', handlers.MovieHandler),
(r'^/movie/(?P<camera_id>\d+)/(?P<op>download|preview|delete)/(?P<filename>.+?)/?$', handlers.MovieHandler),
(r'^/movie/(?P<camera_id>\d+)/(?P<op>delete_all)/(?P<group>.+?)/?$', handlers.MovieHandler),
+ (r'^/_relay_event/?$', handlers.RelayEventHandler),
(r'^/log/(?P<name>\w+)/?$', handlers.LogHandler),
(r'^/update/?$', handlers.UpdateHandler),
(r'^/power/(?P<op>shutdown|reboot)/?$', handlers.PowerHandler),