From 79cf0824e237e4acf0d3612e29cb85ecb5bb0a18 Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Wed, 2 Jul 2014 12:24:15 +0200 Subject: [PATCH] added reboot support for when system settings are changed --- motioneye.py | 7 +++++++ settings_default.py | 3 +++ src/handlers.py | 39 ++++++++++++++++++++++++++++++++++----- static/js/main.js | 27 ++++++++++++++++++++++++++- 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/motioneye.py b/motioneye.py index def4a64..c6d2a1e 100755 --- a/motioneye.py +++ b/motioneye.py @@ -49,7 +49,10 @@ def _configure_settings(): set_default_setting('LOG_LEVEL', logging.INFO) set_default_setting('LISTEN', '0.0.0.0') set_default_setting('PORT', 8765) + set_default_setting('WPA_SUPPLICANT_CONF', None) set_default_setting('SMB_SHARES', False) + set_default_setting('SMB_MOUNT_ROOT', '/media') + set_default_setting('ENABLE_REBOOT', False) set_default_setting('MOUNT_CHECK_INTERVAL', 300) set_default_setting('MOTION_CHECK_INTERVAL', 10) set_default_setting('CLEANUP_INTERVAL', 43200) @@ -130,6 +133,10 @@ def _test_requirements(): print('SMB_SHARES require root privileges') return False + if settings.ENABLE_REBOOT: + print('reboot requires root privileges') + return False + try: import tornado # @UnusedImport tornado = True diff --git a/settings_default.py b/settings_default.py index ba108a6..283447c 100644 --- a/settings_default.py +++ b/settings_default.py @@ -38,6 +38,9 @@ SMB_SHARES = False # the directory where the SMB mounts will be created SMB_MOUNT_ROOT = '/media' +# enables rebooting after changing system settings (such as wifi settings or system updates) +ENABLE_REBOOT = False + # interval in seconds at which motionEye checks the SMB mounts MOUNT_CHECK_INTERVAL = 300 diff --git a/src/handlers.py b/src/handlers.py index 610403c..f9e5f01 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -16,12 +16,14 @@ # along with this program. If not, see . import base64 +import datetime import json import logging import os import socket from tornado.web import RequestHandler, HTTPError, asynchronous +from tornado.ioloop import IOLoop import config import mediafiles @@ -280,20 +282,43 @@ class ConfigHandler(BaseHandler): main_config = config.main_ui_to_dict(ui_config) admin_credentials = main_config.get('@admin_username', '') + ':' + main_config.get('@admin_password', '') + wifi_changed = bool([k for k in ['@wifi_enabled', '@wifi_name', '@wifi_key'] if old_main_config.get(k) != main_config.get(k)]) + config.set_main(main_config) + reboot = False + reload = False + if admin_credentials != old_admin_credentials: logging.debug('admin credentials changed, reload needed') - return True # needs browser reload + reload = True + + if wifi_changed: + logging.debug('wifi settings changed, reboot needed') + + reboot = True - return False + return {'reload': reload, 'reboot': reboot} reload = False # indicates that browser should reload the page + reboot = [False] # indicates that the server will reboot immediately restart = [False] # indicates that the local motion instance was modified and needs to be restarted error = [None] def finish(): + if reboot[0]: + if settings.ENABLE_REBOOT: + def call_reboot(): + os.system('reboot') + + ioloop = IOLoop.instance() + ioloop.add_timeout(datetime.timedelta(seconds=2), call_reboot) + return self.finish({'reload': False, 'reboot': True, 'error': None}) + + else: + reboot[0] = False + if restart[0]: logging.debug('motion needs to be restarted') @@ -309,7 +334,7 @@ class ConfigHandler(BaseHandler): else: motionctl.start() - self.finish({'reload': reload, 'error': error[0]}) + self.finish({'reload': reload, 'reboot': reboot[0], 'error': error[0]}) if camera_id is not None: if camera_id == 0: # multiple camera configs @@ -327,7 +352,9 @@ class ConfigHandler(BaseHandler): for key, cfg in ui_config.items(): if key == 'main': - reload = set_main_config(cfg) or reload + result = set_main_config(cfg) + reload = result['reload'] or reload + reboot[0] = result['reboot'] or reboot[0] check_finished(None, reload) else: @@ -342,7 +369,9 @@ class ConfigHandler(BaseHandler): set_camera_config(camera_id, ui_config, on_finish) else: # main config - reload = set_main_config(ui_config) + result = set_main_config(ui_config) + reload = result['reload'] + reboot[0] = result['reboot'] @BaseHandler.auth(admin=True) def set_preview(self, camera_id): diff --git a/static/js/main.js b/static/js/main.js index 07fb3d3..e9bd2ba 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -946,6 +946,32 @@ function doApply() { return; } + if (data.reboot) { + var count = 0; + function checkServer() { + $.ajax({ + type: 'GET', + url: '/config/0/get/', + success: function () { + window.location.reload(true); + }, + error: function () { + if (count < 25) { + count += 1; + setTimeout(checkServer, 2000); + } + else { + window.location.reload(true); + } + } + }); + } + + setTimeout(checkServer, 15000); + + return; + } + if (data.reload) { window.location.reload(true); return; @@ -961,7 +987,6 @@ function doApply() { $('#camera' + key).find('span.camera-name').html(config.name); }); - pushConfigs = {}; endProgress(); -- 2.39.5