]> www.vanbest.org Git - motioneye-debian/commitdiff
added reboot support for when system settings are changed
authorCalin Crisan <ccrisan@gmail.com>
Wed, 2 Jul 2014 10:24:15 +0000 (12:24 +0200)
committerCalin Crisan <ccrisan@gmail.com>
Wed, 2 Jul 2014 10:24:15 +0000 (12:24 +0200)
motioneye.py
settings_default.py
src/handlers.py
static/js/main.js

index def4a642b41420c52475cabfb19be79b141f35bf..c6d2a1ecd01770b13cfb68386f5bddb6b063976d 100755 (executable)
@@ -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
index ba108a68a23aa47ad3ab36b63747f4d9ada3e60c..283447c0e097639f25173fb2458332550fbd354d 100644 (file)
@@ -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
 
index 610403c6ea301947a549f09de1fbf875784e0328..f9e5f012b7eea07f02f66eea06fc6559e5642d42 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 
 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):
index 07fb3d36a532a20fbe4c42a6d39b00be220ec42f..e9bd2ba2eae6801914e664197e3e35423ff360f1 100644 (file)
@@ -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();