]> www.vanbest.org Git - motioneye-debian/commitdiff
moved wifi config code to wifictl.py; timezone support is now fully
authorCalin Crisan <ccrisan@gmail.com>
Thu, 10 Jul 2014 14:15:40 +0000 (17:15 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Thu, 10 Jul 2014 14:15:40 +0000 (17:15 +0300)
functional

src/config.py
src/handlers.py
src/tzctl.py
src/wifictl.py [new file with mode: 0644]
static/js/main.js
templates/main.html

index 5e042e7d8550a7e1c7e3d6f37a76a2d00dc022f8..25a6ca62ecafea64aa98a01962b88e9a5a00a4e0 100644 (file)
@@ -30,6 +30,7 @@ import tzctl
 import update
 import utils
 import v4l2ctl
+import wifictl
 
 
 _CAMERA_CONFIG_FILE_NAME = 'thread-%(id)s.conf'
@@ -106,11 +107,8 @@ def set_main(main_config):
     _set_default_motion(main_config)
     _main_config_cache = dict(main_config)
 
-    if settings.WPA_SUPPLICANT_CONF:
-        _set_wifi_settings(main_config)
-        
-    if settings.LOCAL_TIME_FILE:
-        _set_localtime_settings(main_config)
+    _set_wifi_settings(main_config)
+    _set_localtime_settings(main_config)
         
     config_file_path = os.path.join(settings.CONF_PATH, _MAIN_CONFIG_FILE_NAME)
     
@@ -496,6 +494,7 @@ def main_ui_to_dict(ui):
         '@admin_password': ui['admin_password'],
         '@normal_username': ui['normal_username'],
         '@normal_password': ui['normal_password'],
+        '@time_zone': ui['time_zone'],
         
         '@wifi_enabled': ui['wifi_enabled'],
         '@wifi_name': ui['wifi_name'],
@@ -512,6 +511,7 @@ def main_dict_to_ui(data):
         'admin_password': data['@admin_password'],
         'normal_username': data['@normal_username'],
         'normal_password': data['@normal_password'],
+        'time_zone': data['@time_zone'],
     
         'wifi_enabled': data['@wifi_enabled'],
         'wifi_name': data['@wifi_name'],
@@ -1109,7 +1109,7 @@ def _set_default_motion(data):
     data.setdefault('@admin_password', '')
     data.setdefault('@normal_username', 'user')
     data.setdefault('@normal_password', '')
-    data.setdefault('@timezone', 'UTC')
+    data.setdefault('@time_zone', 'UTC')
 
     data.setdefault('@wifi_enabled', False)
     data.setdefault('@wifi_name', '')
@@ -1206,142 +1206,25 @@ def _set_default_motion_camera(camera_id, data, old_motion):
 
 
 def _get_wifi_settings(data):
-    # will return the first configured network
-    
-    logging.debug('reading wifi settings from %s' % settings.WPA_SUPPLICANT_CONF)
-    
-    try:
-        conf_file = open(settings.WPA_SUPPLICANT_CONF, 'r')
-    
-    except Exception as e:
-        logging.error('could open wifi settings file %(path)s: %(msg)s' % {
-                'path': settings.WPA_SUPPLICANT_CONF, 'msg': unicode(e)})
-        
-        return
-    
-    lines = conf_file.readlines()
-    conf_file.close()
-    
-    ssid = psk = ''
-    in_section = False
-    for line in lines:
-        line = line.strip()
-        if line.startswith('#'):
-            continue
-        
-        if '{' in line:
-            in_section = True
-            
-        elif '}' in line:
-            in_section = False
-            break
-            
-        elif in_section:
-            m = re.search('ssid\s*=\s*"(.*?)"', line)
-            if m:
-                ssid = m.group(1)
-    
-            m = re.search('psk\s*=\s*"(.*?)"', line)
-            if m:
-                psk = m.group(1)
+    wifi_settings = wifictl.get_wifi_settings()
 
-    data['@wifi_enabled'] = bool(ssid)
-    data['@wifi_name'] = ssid
-    data['@wifi_key'] = psk
-    
-    if ssid:
-        logging.debug('wifi is enabled (name = "%s")' % ssid)
-    
-    else:
-        logging.debug('wifi is disabled')
+    data['@wifi_enabled'] = bool(wifi_settings['ssid'])
+    data['@wifi_name'] = wifi_settings['ssid']
+    data['@wifi_key'] = wifi_settings['psk']
     
 
 def _set_wifi_settings(data):
-    # will update the first configured network
-    
-    logging.debug('writing wifi settings to %s' % settings.WPA_SUPPLICANT_CONF)
-    
     wifi_enabled = data.pop('@wifi_enabled', False)
     wifi_name = data.pop('@wifi_name', '')
     wifi_key = data.pop('@wifi_key', '')
     
-    try:
-        conf_file = open(settings.WPA_SUPPLICANT_CONF, 'r')
-    
-    except Exception as e:
-        logging.error('could open wifi settings file %(path)s: %(msg)s' % {
-                'path': settings.WPA_SUPPLICANT_CONF, 'msg': unicode(e)})
-
-        return
-    
-    lines = conf_file.readlines()
-    conf_file.close()
-    
-    in_section = False
-    found_ssid = False
-    found_psk = False
-    i = 0
-    while i < len(lines):
-        line = lines[i].strip()
-        if line.startswith('#'):
-            i += 1
-            continue
-        
-        if '{' in line:
-            in_section = True
-            
-        elif '}' in line:
-            in_section = False
-            if wifi_enabled and wifi_name and not found_ssid:
-                lines.insert(i, '    ssid="' + wifi_name + '"\n')
-            if wifi_enabled and wifi_key and not found_psk:
-                lines.insert(i, '    psk="' + wifi_key + '"\n')
-            
-            found_psk = found_ssid = True
-            
-            break
-            
-        elif in_section:
-            if wifi_enabled:
-                if re.match('ssid\s*=\s*".*?"', line):
-                    lines[i] = '    ssid="' + wifi_name + '"\n'
-                    found_ssid = True
-                
-                elif re.match('psk\s*=\s*".*?"', line):
-                    if wifi_key:
-                        lines[i] = '    psk="' + wifi_key + '"\n'
-                        found_psk = True
-                
-                    else:
-                        lines.pop(i)
-                        i -= 1
-        
-            else: # wifi disabled
-                if re.match('ssid\s*=\s*".*?"', line) or re.match('psk\s*=\s*".*?"', line):
-                    lines.pop(i)
-                    i -= 1
+    if settings.WPA_SUPPLICANT_CONF:
+        s = {
+            'ssid': wifi_enabled and wifi_name,
+            'psk': wifi_key
+        }
         
-        i += 1
-
-    if wifi_enabled and not found_ssid:
-        lines.append('network={\n')
-        lines.append('    ssid="' + wifi_name + '"\n')
-        lines.append('    psk="' + wifi_key + '"\n')
-        lines.append('}\n\n')
-
-    try:
-        conf_file = open(settings.WPA_SUPPLICANT_CONF, 'w')
-    
-    except Exception as e:
-        logging.error('could open wifi settings file %(path)s: %(msg)s' % {
-                'path': settings.WPA_SUPPLICANT_CONF, 'msg': unicode(e)})
-
-        return
-    
-    for line in lines:
-        conf_file.write(line)
-
-    conf_file.close()
+        wifictl.set_wifi_settings(s)
 
 
 def _get_localtime_settings(data):
@@ -1351,5 +1234,5 @@ def _get_localtime_settings(data):
 
 def _set_localtime_settings(data):
     time_zone = data.pop('@time_zone')
-    if time_zone:
+    if time_zone and settings.LOCAL_TIME_FILE:
         tzctl.set_time_zone(time_zone)
index dbfb230d5966be01a717b17f61775eb4194a4ac6..34fedf0271655556c59c424af06099b63f7bfc86 100644 (file)
@@ -352,6 +352,11 @@ class ConfigHandler(BaseHandler):
                 if len(ui_config) > 1:
                     logging.debug('setting multiple configs')
                 
+                elif len(ui_config) == 0:
+                    logging.warn('no configuration to set')
+                    
+                    self.finish()
+                
                 so_far = [0]
                 def check_finished(e, r):
                     restart[0] = restart[0] or r
index 04e97af481c19a8ab5d59b639a8bc07c322333c7..6e19891050cbcaaf8acadda00b2e658563b9290d 100644 (file)
@@ -33,7 +33,7 @@ def _get_time_zone_symlink():
             break
     
     if file and file.startswith('/usr/share/zoneinfo/'):
-        file = file[21:]
+        file = file[20:]
     
     else:
         file = None
diff --git a/src/wifictl.py b/src/wifictl.py
new file mode 100644 (file)
index 0000000..139434a
--- /dev/null
@@ -0,0 +1,163 @@
+
+# Copyright (c) 2013 Calin Crisan
+# This file is part of motionEye.
+#
+# motionEye is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# 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 logging
+import re
+import settings
+
+
+def get_wifi_settings():
+    # will return the first configured network
+    
+    logging.debug('reading wifi settings from %s' % settings.WPA_SUPPLICANT_CONF)
+    
+    try:
+        conf_file = open(settings.WPA_SUPPLICANT_CONF, 'r')
+    
+    except Exception as e:
+        logging.error('could open wifi settings file %(path)s: %(msg)s' % {
+                'path': settings.WPA_SUPPLICANT_CONF, 'msg': unicode(e)})
+        
+        return {
+            'ssid': None,
+            'psk': None
+        }
+    
+    lines = conf_file.readlines()
+    conf_file.close()
+    
+    ssid = psk = ''
+    in_section = False
+    for line in lines:
+        line = line.strip()
+        if line.startswith('#'):
+            continue
+        
+        if '{' in line:
+            in_section = True
+            
+        elif '}' in line:
+            in_section = False
+            break
+            
+        elif in_section:
+            m = re.search('ssid\s*=\s*"(.*?)"', line)
+            if m:
+                ssid = m.group(1)
+    
+            m = re.search('psk\s*=\s*"(.*?)"', line)
+            if m:
+                psk = m.group(1)
+
+    if ssid:
+        logging.debug('wifi is enabled (ssid = "%s")' % ssid)
+    
+    else:
+        logging.debug('wifi is disabled')
+
+    return {
+        'ssid': ssid,
+        'psk': psk
+    }
+
+
+def set_wifi_settings(s):
+    # will update the first configured network
+    
+    logging.debug('writing wifi settings to %s' % settings.WPA_SUPPLICANT_CONF)
+    
+    enabled = bool(s['ssid'])
+    ssid = s['ssid']
+    psk = s['psk']
+    
+    try:
+        conf_file = open(settings.WPA_SUPPLICANT_CONF, 'r')
+    
+    except Exception as e:
+        logging.error('could open wifi settings file %(path)s: %(msg)s' % {
+                'path': settings.WPA_SUPPLICANT_CONF, 'msg': unicode(e)})
+
+        return
+    
+    lines = conf_file.readlines()
+    conf_file.close()
+    
+    in_section = False
+    found_ssid = False
+    found_psk = False
+    i = 0
+    while i < len(lines):
+        line = lines[i].strip()
+        if line.startswith('#'):
+            i += 1
+            continue
+        
+        if '{' in line:
+            in_section = True
+            
+        elif '}' in line:
+            in_section = False
+            if enabled and ssid and not found_ssid:
+                lines.insert(i, '    ssid="' + ssid + '"\n')
+            if enabled and psk and not found_psk:
+                lines.insert(i, '    psk="' + psk + '"\n')
+            
+            found_psk = found_ssid = True
+            
+            break
+            
+        elif in_section:
+            if enabled:
+                if re.match('ssid\s*=\s*".*?"', line):
+                    lines[i] = '    ssid="' + ssid + '"\n'
+                    found_ssid = True
+                
+                elif re.match('psk\s*=\s*".*?"', line):
+                    if psk:
+                        lines[i] = '    psk="' + psk + '"\n'
+                        found_psk = True
+                
+                    else:
+                        lines.pop(i)
+                        i -= 1
+        
+            else: # wifi disabled
+                if re.match('ssid\s*=\s*".*?"', line) or re.match('psk\s*=\s*".*?"', line):
+                    lines.pop(i)
+                    i -= 1
+        
+        i += 1
+
+    if enabled and not found_ssid:
+        lines.append('network={\n')
+        lines.append('    ssid="' + ssid + '"\n')
+        lines.append('    psk="' + psk + '"\n')
+        lines.append('}\n\n')
+
+    try:
+        conf_file = open(settings.WPA_SUPPLICANT_CONF, 'w')
+    
+    except Exception as e:
+        logging.error('could open wifi settings file %(path)s: %(msg)s' % {
+                'path': settings.WPA_SUPPLICANT_CONF, 'msg': unicode(e)})
+
+        return
+    
+    for line in lines:
+        conf_file.write(line)
+
+    conf_file.close()
index e0e115d4f27845b56ba22dbad166fb4722db6a99..d16b0dddfb51463cc7d11ff810fcebf65b4c4ed9 100644 (file)
@@ -288,7 +288,7 @@ function initUI() {
             fetchCurrentCameraConfig(endProgress);
         }
     });
-    $('input.general').change(pushMainConfig);
+    $('input.general, select.general').change(pushMainConfig);
     $('input.wifi').change(pushMainConfig);
     $('input.device, select.device, ' +
       'input.storage, select.storage, ' +
@@ -530,6 +530,7 @@ function mainUi2Dict() {
         'admin_password': $('#adminPasswordEntry').val(),
         'normal_username': $('#normalUsernameEntry').val(),
         'normal_password': $('#normalPasswordEntry').val(),
+        'time_zone': $('#timeZoneSelect').val(),
         
         'wifi_enabled': $('#wifiSwitch')[0].checked,
         'wifi_name': $('#wifiNameEntry').val(),
@@ -545,6 +546,7 @@ function dict2MainUi(dict) {
     $('#adminPasswordEntry').val(dict['admin_password']);
     $('#normalUsernameEntry').val(dict['normal_username']);
     $('#normalPasswordEntry').val(dict['normal_password']);
+    $('#timeZoneSelect').val(dict['time_zone']);
     
     $('#wifiSwitch')[0].checked = dict['wifi_enabled'];
     $('#wifiNameEntry').val(dict['wifi_name']);
index b9390cd0d69bb4494420be61b79b5f549d882269..f65e65812fa864273e7c59a5082e8ee2f3399191 100644 (file)
@@ -66,7 +66,7 @@
                     <tr class="settings-item {% if not timezones %}hidden{% endif %}">
                         <td class="settings-item-label"><span class="settings-item-label">Time Zone</span></td>
                         <td class="settings-item-value">
-                            <select class="styled motion-movies" id="timeZoneSelect">
+                            <select class="styled general" id="timeZoneSelect">
                                 {% for timezone in timezones %}
                                 <option value="{{timezone}}">{{timezone}}</option>
                                 {% endfor %}