From 77f862f904dcccdf5c352ba453f94aed8163c7f6 Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Sat, 16 May 2015 17:06:04 +0300 Subject: [PATCH] motion event relay mechanism has been rewritten to work with thread ids rather than camera ids --- eventrelay.py | 10 +++---- src/handlers.py | 69 ++++++++++++++++++++++++------------------------ src/motionctl.py | 39 ++++++++++++++++++++------- src/server.py | 3 ++- 4 files changed, 71 insertions(+), 50 deletions(-) diff --git a/eventrelay.py b/eventrelay.py index ca53da0..e09142b 100755 --- a/eventrelay.py +++ b/eventrelay.py @@ -37,7 +37,7 @@ _configure_logging() def print_usage(): - print 'Usage: eventrelay.py ' + print 'Usage: eventrelay.py ' def get_admin_credentials(): @@ -116,16 +116,16 @@ if __name__ == '__main__': 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) diff --git a/src/handlers.py b/src/handlers.py index d05dcba..1e160da 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -200,9 +200,6 @@ class ConfigHandler(BaseHandler): elif op == 'restore': self.restore() - elif op == '_relay_event': - self._relay_event(camera_id) - else: raise HTTPError(400, 'unknown operation') @@ -689,37 +686,6 @@ class ConfigHandler(BaseHandler): 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 @@ -1378,6 +1344,41 @@ class MovieHandler(BaseHandler): 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'), diff --git a/src/motionctl.py b/src/motionctl.py index c6c4fd9..1783fda 100644 --- a/src/motionctl.py +++ b/src/motionctl.py @@ -203,7 +203,7 @@ def started(): 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) @@ -233,7 +233,7 @@ def get_motion_detection(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) @@ -269,7 +269,17 @@ def is_motion_detected(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() @@ -280,15 +290,24 @@ def _get_thread_id(camera_id): 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(): diff --git a/src/server.py b/src/server.py index 49b3280..9258198 100644 --- a/src/server.py +++ b/src/server.py @@ -42,7 +42,7 @@ application = Application( [ (r'^/$', handlers.MainHandler), (r'^/config/main/(?Pset|get)/?$', handlers.ConfigHandler), - (r'^/config/(?P\d+)/(?Pget|set|rem|set_preview|_relay_event)/?$', handlers.ConfigHandler), + (r'^/config/(?P\d+)/(?Pget|set|rem|set_preview)/?$', handlers.ConfigHandler), (r'^/config/(?Padd|list|list_devices|backup|restore)/?$', handlers.ConfigHandler), (r'^/picture/(?P\d+)/(?Pcurrent|list|frame)/?$', handlers.PictureHandler), (r'^/picture/(?P\d+)/(?Pdownload|preview|delete)/(?P.+?)/?$', handlers.PictureHandler), @@ -50,6 +50,7 @@ application = Application( (r'^/movie/(?P\d+)/(?Plist)/?$', handlers.MovieHandler), (r'^/movie/(?P\d+)/(?Pdownload|preview|delete)/(?P.+?)/?$', handlers.MovieHandler), (r'^/movie/(?P\d+)/(?Pdelete_all)/(?P.+?)/?$', handlers.MovieHandler), + (r'^/_relay_event/?$', handlers.RelayEventHandler), (r'^/log/(?P\w+)/?$', handlers.LogHandler), (r'^/update/?$', handlers.UpdateHandler), (r'^/power/(?Pshutdown|reboot)/?$', handlers.PowerHandler), -- 2.39.5