]> www.vanbest.org Git - motioneye-debian/commitdiff
motion detection frame indicator improvements
authorCalin Crisan <ccrisan@gmail.com>
Thu, 25 Dec 2014 13:26:09 +0000 (15:26 +0200)
committerCalin Crisan <ccrisan@gmail.com>
Thu, 25 Dec 2014 13:26:09 +0000 (15:26 +0200)
eventrelay.py
src/handlers.py
src/motionctl.py
src/wsswitch.py

index 69a7de459cdf480faa639d46721b35f05650ebd0..8be6f764913003e46b194ba618dbe93ae6f1e6bc 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 
-import base64
+import errno
 import logging
 import os.path
 import sys
-import urllib2
+import urllib
 
 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]),'src'))
 
-import config
 import settings
+import utils
 
 from motioneye import _configure_settings, _configure_logging
 
@@ -38,6 +38,64 @@ def print_usage():
     print 'Usage: eventrelay.py <event> <camera_id>'
 
 
+def get_admin_credentials():
+    # this shortcut function is a bit faster than using the config module functions
+    config_file_path = os.path.join(settings.CONF_PATH, 'motion.conf')
+    
+    logging.debug('reading main config from file %(path)s...' % {'path': config_file_path}) 
+
+    lines = None
+    try:
+        file = open(config_file_path, 'r')
+    
+    except IOError as e:
+        if e.errno == errno.ENOENT:  # file does not exist
+            logging.info('main config file %(path)s does not exist, using default values' % {'path': config_file_path})
+            
+            lines = []
+        
+        else:
+            logging.error('could not open main config file %(path)s: %(msg)s' % {
+                    'path': config_file_path, 'msg': unicode(e)})
+            
+            raise
+
+    if lines is None:
+        try:
+            lines = [l[:-1] for l in file.readlines()]
+        
+        except Exception as e:
+            logging.error('could not read main config file %(path)s: %(msg)s' % {
+                    'path': config_file_path, 'msg': unicode(e)})
+            
+            raise
+        
+        finally:
+            file.close()
+    
+    admin_username = 'admin'
+    admin_password = ''
+    for line in lines:
+        line = line.strip()
+        if not line.startswith('#'):
+            continue
+        
+        line = line[1:].strip()
+        if line.startswith('@admin_username'):
+            parts = line.split(' ', 1)
+            admin_username = parts[1]
+            
+            continue
+        
+        if line.startswith('@admin_password'):
+            parts = line.split(' ', 1)
+            admin_password = parts[1]
+
+            continue
+    
+    return admin_username, admin_password
+
+
 if __name__ == '__main__':
     if len(sys.argv) < 3:
         print_usage()
@@ -48,22 +106,21 @@ if __name__ == '__main__':
 
     logging.debug('event = %s' % event)
     logging.debug('camera_id = %s' % camera_id)
+    
+    admin_username, admin_password = get_admin_credentials()
 
-    url = 'http://127.0.0.1:%(port)s/config/%(camera_id)s/_relay_event/?event=%(event)s' % {
-            'port': settings.PORT,
+    uri = '/config/%(camera_id)s/_relay_event/?event=%(event)s&username=%(username)s' % {
+            'username': admin_username,
             'camera_id': camera_id,
             'event': event}
-
-    main_config = config.get_main()
     
-    username = main_config.get('@admin_username', '')
-    password = main_config.get('@admin_password', '')
+    signature = utils.compute_signature('POST', uri, '', admin_password)
     
-    request = urllib2.Request(url, '')
-    request.add_header('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (username, password)).replace('\n', ''))
+    url = 'http://127.0.0.1:%(port)s' + uri + '&signature=' + signature
+    url = url % {'port': settings.PORT}
     
     try:
-        urllib2.urlopen(request, timeout=settings.REMOTE_REQUEST_TIMEOUT)
+        urllib.urlopen(url, data='')
         logging.debug('event successfully relayed')
     
     except Exception as e:
index b91ed5a94a241cc75b0890021d1e9a70826e8ba6..b9380481f4a618780ae43040cd1e2dfc60b73345 100644 (file)
@@ -618,9 +618,18 @@ class ConfigHandler(BaseHandler):
     @BaseHandler.auth(admin=True)
     def _relay_event(self, camera_id):
         event = self.get_argument('event')
-        logging.debug('event %(event)s relayed for camera %(id)s' % {'event': event, 'id': camera_id})
+        logging.debug('event %(event)s relayed for camera with id %(id)s' % {'event': event, 'id': camera_id})
+        
+        camera_config = config.get_camera(camera_id)
+        if not utils.local_camera(camera_config):
+            logging.warn('ignoring event for remote 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':
index 506b5f9016fd80b590bd2a21f128b5dcb662bb7d..f03532245ece62714f9e293e8f66205eb85cf954 100644 (file)
@@ -223,6 +223,9 @@ def set_motion_detection(camera_id, enabled):
     if thread_id is None:
         return logging.error('could not find thread id for camera with id %s' % camera_id)
     
+    if not enabled:
+        _motion_detected[camera_id] = False
+    
     logging.debug('%(what)s motion detection for camera with id %(id)s' % {
             'what': ['disabling', 'enabling'][enabled],
             'id': camera_id})
index 15364ffb52e41d4a8491489d799053cc30fd9dad..66cd08324e42cb32a93ea5ced2a13c2bfba351da 100644 (file)
@@ -72,7 +72,7 @@ def _during_working_schedule(now, working_schedule):
 def _check_ws():
     # schedule the next call
     ioloop = tornado.ioloop.IOLoop.instance()
-    ioloop.add_timeout(datetime.timedelta(seconds=60), _check_ws)
+    ioloop.add_timeout(datetime.timedelta(seconds=10), _check_ws)
 
     if not motionctl.running():
         return