From 5274cf594a85b670fa437f6f4b7d458144115ad4 Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Tue, 26 Aug 2014 21:32:21 +0300 Subject: [PATCH] added a motion detection switch that, when disabled, explicitly disables motion's detection mechanism --- src/config.py | 25 +++++++++-------- src/handlers.py | 2 +- src/motionctl.py | 12 ++++++++ src/wsswitch.py | 10 ++++--- static/js/main.js | 46 +++++++++++++++++++----------- templates/main.html | 68 ++++++++++++++++++++++----------------------- 6 files changed, 97 insertions(+), 66 deletions(-) diff --git a/src/config.py b/src/config.py index da32dbc..dedad32 100644 --- a/src/config.py +++ b/src/config.py @@ -546,13 +546,8 @@ def camera_ui_to_dict(ui): 'snapshot_filename': '', '@preserve_pictures': int(ui['preserve_pictures']), - # movies - 'ffmpeg_output_movies': ui['motion_movies'], - 'movie_filename': ui['movie_file_name'], - 'ffmpeg_bps': 400000, - '@preserve_movies': int(ui['preserve_movies']), - # motion detection + '@motion_detection': ui['motion_detection'], 'text_changes': ui['show_frame_changes'], 'locate_motion_mode': ui['show_frame_changes'], 'noise_tune': ui['auto_noise_detect'], @@ -561,6 +556,12 @@ def camera_ui_to_dict(ui): 'pre_capture': int(ui['pre_capture']), 'post_capture': int(ui['post_capture']), + # movies + 'ffmpeg_output_movies': ui['motion_movies'], + 'movie_filename': ui['movie_file_name'], + 'ffmpeg_bps': 400000, + '@preserve_movies': int(ui['preserve_movies']), + # working schedule '@working_schedule': '', @@ -792,12 +793,8 @@ def camera_dict_to_ui(data): 'snapshot_interval': 0, 'preserve_pictures': data['@preserve_pictures'], - # motion movies - 'motion_movies': data['ffmpeg_output_movies'], - 'movie_file_name': data['movie_filename'], - 'preserve_movies': data['@preserve_movies'], - # motion detection + 'motion_detection': data['@motion_detection'], 'show_frame_changes': data['text_changes'] or data['locate_motion_mode'], 'auto_noise_detect': data['noise_tune'], 'noise_level': int(int(data['noise_level']) / 2.55), @@ -805,6 +802,11 @@ def camera_dict_to_ui(data): 'pre_capture': int(data['pre_capture']), 'post_capture': int(data['post_capture']), + # motion movies + 'motion_movies': data['ffmpeg_output_movies'], + 'movie_file_name': data['movie_filename'], + 'preserve_movies': data['@preserve_movies'], + # motion notifications 'email_notifications_enabled': False, 'web_hook_notifications_enabled': False, @@ -1293,6 +1295,7 @@ def _set_default_motion_camera(camera_id, data, old_motion=False): data.setdefault('text_right', '%Y-%m-%d\\n%T') data.setdefault('text_double', False) + data.setdefault('@motion_detection', True) data.setdefault('text_changes', False) if old_motion: data.setdefault('locate', False) diff --git a/src/handlers.py b/src/handlers.py index f03b4ce..d208de8 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -212,7 +212,7 @@ class ConfigHandler(BaseHandler): def on_response(remote_ui_config=None, error=None): if error: return self.finish_json({'error': 'Failed to get remote camera configuration for %(url)s: %(msg)s.' % { - 'url': remote.make_camera_url(local_config)}, 'msg': error}) + 'url': remote.make_camera_url(local_config), 'msg': error}}) for key, value in local_config.items(): remote_ui_config[key.replace('@', '')] = value diff --git a/src/motionctl.py b/src/motionctl.py index e96e735..bb244dc 100644 --- a/src/motionctl.py +++ b/src/motionctl.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import datetime import errno import logging import os.path @@ -24,6 +25,7 @@ import subprocess import time from tornado.httpclient import HTTPClient, AsyncHTTPClient, HTTPRequest +from tornado.ioloop import IOLoop import config import settings @@ -59,6 +61,14 @@ def find_motion(): return _motion_binary_cache +def _disable_motion_detection(): + for camera_id in config.get_camera_ids(): + camera_config = config.get_camera(camera_id) + if not camera_config['@motion_detection']: + logging.debug('motion detection disabled by config for camera with id %s' % camera_id) + set_motion_detection(camera_id, False) + + def start(): global _started @@ -106,6 +116,8 @@ def start(): # write the pid to file with open(motion_pid_path, 'w') as f: f.write(str(pid) + '\n') + + IOLoop.instance().add_timeout(datetime.timedelta(seconds=5), _disable_motion_detection) def stop(): diff --git a/src/wsswitch.py b/src/wsswitch.py index ffb1d17..b0e72cd 100644 --- a/src/wsswitch.py +++ b/src/wsswitch.py @@ -84,15 +84,17 @@ def _check_ws(): continue working_schedule = camera_config.get('@working_schedule') + motion_detection = camera_config.get('@motion_detection') working_schedule_type = camera_config.get('@working_schedule_type') or 'outside' - must_be_enabled = False if not working_schedule: # working schedule disabled, motion detection left untouched continue - else: - now_during = _during_working_schedule(now, working_schedule) - must_be_enabled = (now_during and working_schedule_type == 'during') or (not now_during and working_schedule_type == 'outside') + if not motion_detection: # motion detection explicitly disabled + continue + + now_during = _during_working_schedule(now, working_schedule) + must_be_enabled = (now_during and working_schedule_type == 'during') or (not now_during and working_schedule_type == 'outside') currently_enabled = motionctl.get_motion_detection(camera_id) if currently_enabled and not must_be_enabled: diff --git a/static/js/main.js b/static/js/main.js index dece045..0dfcd86 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -275,6 +275,7 @@ function initUI() { $('#streamingServerResizeSwitch').change(updateConfigUi); $('#stillImagesSwitch').change(updateConfigUi); $('#preservePicturesSelect').change(updateConfigUi); + $('#motionDetectionSwitch').change(updateConfigUi); $('#motionMoviesSwitch').change(updateConfigUi); $('#preserveMoviesSelect').change(updateConfigUi); $('#emailNotificationsSwitch').change(updateConfigUi); @@ -323,8 +324,8 @@ function initUI() { 'input.text-overlay, select.text-overlay, ' + 'input.streaming, select.streaming, ' + 'input.still-images, select.still-images, ' + - 'input.motion-movies, select.motion-movies, ' + 'input.motion-detection, select.motion-detection, ' + + 'input.motion-movies, select.motion-movies, ' + 'input.notifications, select.notifications, ' + 'input.working-schedule, select.working-schedule').change(pushCameraConfig); @@ -500,6 +501,17 @@ function updateConfigUi() { $('#picturesLifetime').parents('tr:eq(0)').each(markHide); } + /* motion detection switch */ + if (!$('#motionDetectionSwitch').get(0).checked) { + $('#motionDetectionSwitch').parent().next('table.settings').find('tr.settings-item').each(markHide); + $('#motionMoviesSwitch').parent().each(markHide); + $('#motionMoviesSwitch').parent().next('table.settings').each(markHide); + $('#emailNotificationsSwitch').parents('table.settings:eq(0)').each(markHide); + $('#emailNotificationsSwitch').parents('table.settings:eq(0)').prev().each(markHide); + $('#workingScheduleSwitch').parent().each(markHide); + $('#workingScheduleSwitch').parent().next('table.settings').each(markHide); + } + /* motion movies switch */ if (!$('#motionMoviesSwitch').get(0).checked) { $('#motionMoviesSwitch').parent().next('table.settings').find('tr.settings-item').each(markHide); @@ -676,13 +688,8 @@ function cameraUi2Dict() { 'snapshot_interval': $('#snapshotIntervalEntry').val(), 'preserve_pictures': $('#preservePicturesSelect').val() >= 0 ? $('#preservePicturesSelect').val() : $('#picturesLifetime').val(), - /* motion movies */ - 'motion_movies': $('#motionMoviesSwitch')[0].checked, - 'movie_file_name': $('#movieFileNameEntry').val(), - 'movie_quality': $('#movieQualitySlider').val(), - 'preserve_movies': $('#preserveMoviesSelect').val() >= 0 ? $('#preserveMoviesSelect').val() : $('#moviesLifetime').val(), - /* motion detection */ + 'motion_detection': $('#motionDetectionSwitch')[0].checked, 'show_frame_changes': $('#showFrameChangesSwitch')[0].checked, 'frame_change_threshold': $('#frameChangeThresholdSlider').val(), 'auto_noise_detect': $('#autoNoiseDetectSwitch')[0].checked, @@ -691,6 +698,12 @@ function cameraUi2Dict() { 'pre_capture': $('#preCaptureEntry').val(), 'post_capture': $('#postCaptureEntry').val(), + /* motion movies */ + 'motion_movies': $('#motionMoviesSwitch')[0].checked, + 'movie_file_name': $('#movieFileNameEntry').val(), + 'movie_quality': $('#movieQualitySlider').val(), + 'preserve_movies': $('#preserveMoviesSelect').val() >= 0 ? $('#preserveMoviesSelect').val() : $('#moviesLifetime').val(), + /* motion notifications */ 'email_notifications_enabled': $('#emailNotificationsSwitch')[0].checked, 'email_notifications_addresses': $('#emailAddressesEntry').val(), @@ -886,6 +899,16 @@ function dict2CameraUi(dict) { } $('#picturesLifetime').val(dict['preserve_pictures']); + /* motion detection */ + $('#motionDetectionSwitch')[0].checked = dict['motion_detection']; + $('#showFrameChangesSwitch')[0].checked = dict['show_frame_changes']; + $('#frameChangeThresholdSlider').val(dict['frame_change_threshold']); + $('#autoNoiseDetectSwitch')[0].checked = dict['auto_noise_detect']; + $('#noiseLevelSlider').val(dict['noise_level']); + $('#eventGapEntry').val(dict['event_gap']); + $('#preCaptureEntry').val(dict['pre_capture']); + $('#postCaptureEntry').val(dict['post_capture']); + /* motion movies */ $('#motionMoviesSwitch')[0].checked = dict['motion_movies']; $('#movieFileNameEntry').val(dict['movie_file_name']); @@ -896,15 +919,6 @@ function dict2CameraUi(dict) { } $('#moviesLifetime').val(dict['preserve_movies']); - /* motion detection */ - $('#showFrameChangesSwitch')[0].checked = dict['show_frame_changes']; - $('#frameChangeThresholdSlider').val(dict['frame_change_threshold']); - $('#autoNoiseDetectSwitch')[0].checked = dict['auto_noise_detect']; - $('#noiseLevelSlider').val(dict['noise_level']); - $('#eventGapEntry').val(dict['event_gap']); - $('#preCaptureEntry').val(dict['pre_capture']); - $('#postCaptureEntry').val(dict['post_capture']); - /* motion notifications */ $('#emailNotificationsSwitch')[0].checked = dict['email_notifications_enabled']; $('#emailAddressesEntry').val(dict['email_notifications_addresses']); diff --git a/templates/main.html b/templates/main.html index de0f447..0debe61 100644 --- a/templates/main.html +++ b/templates/main.html @@ -340,40 +340,7 @@ -
Motion Movies
- - - - - - - - - - - - - - - - - - - - - -
Movie File Name?
Movie Quality?
Preserve Movies - - ?
Movies Lifetimedays?
- -
Motion Detection
+
Motion Detection
@@ -415,6 +382,39 @@
Show Frame Changes
+
Motion Movies
+ + + + + + + + + + + + + + + + + + + + + +
Movie File Name?
Movie Quality?
Preserve Movies + + ?
Movies Lifetimedays?
+
Motion Notifications
-- 2.39.5