From 2e54312941ab25239fce013e5e7f7b6c33dc9f3f Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Thu, 25 Dec 2014 15:26:09 +0200 Subject: [PATCH] motion detection frame indicator improvements --- eventrelay.py | 81 +++++++++++++++++++++++++++++++++++++++++------- src/handlers.py | 11 ++++++- src/motionctl.py | 3 ++ src/wsswitch.py | 2 +- 4 files changed, 83 insertions(+), 14 deletions(-) diff --git a/eventrelay.py b/eventrelay.py index 69a7de4..8be6f76 100755 --- a/eventrelay.py +++ b/eventrelay.py @@ -16,16 +16,16 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -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 ' +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: diff --git a/src/handlers.py b/src/handlers.py index b91ed5a..b938048 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -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': diff --git a/src/motionctl.py b/src/motionctl.py index 506b5f9..f035322 100644 --- a/src/motionctl.py +++ b/src/motionctl.py @@ -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}) diff --git a/src/wsswitch.py b/src/wsswitch.py index 15364ff..66cd083 100644 --- a/src/wsswitch.py +++ b/src/wsswitch.py @@ -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 -- 2.39.5