]> www.vanbest.org Git - motioneye-debian/commitdiff
motion event relay mechanism has been rewritten to work with thread ids
authorCalin Crisan <ccrisan@gmail.com>
Sat, 16 May 2015 14:06:04 +0000 (17:06 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sat, 16 May 2015 14:06:04 +0000 (17:06 +0300)
rather than camera ids

eventrelay.py
src/handlers.py
src/motionctl.py
src/server.py

index ca53da0801c7b1f3df25ff707b791b179ac205a5..e09142b28812834bfed4423f6097259d1dcc5b20 100755 (executable)
@@ -37,7 +37,7 @@ _configure_logging()
 
 
 def print_usage():
-    print 'Usage: eventrelay.py <event> <camera_id>'
+    print 'Usage: eventrelay.py <event> <thread_id>'
 
 
 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)
index d05dcbaa30e6e7132ee25e6bc2b74174bad1898f..1e160dab4b20bfb72770c03f5877f52c4d6b3b90 100644 (file)
@@ -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'),
index c6c4fd9d5cf2e7219d77624bd0966970858a9fcf..1783fda68cc23ee43f59d92f8b4a29bdf67b8862 100644 (file)
@@ -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():
index 49b3280cbd0ff5889d7308482e74b1a69d0af823..92581983afb3af3734b9e71039478ae9cc114254 100644 (file)
@@ -42,7 +42,7 @@ application = Application(
     [
         (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),
@@ -50,6 +50,7 @@ application = Application(
         (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),