From: Calin Crisan Date: Thu, 10 Jul 2014 14:15:40 +0000 (+0300) Subject: moved wifi config code to wifictl.py; timezone support is now fully X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=d2921fb9076232494c94757693a4347010656646;p=motioneye-debian moved wifi config code to wifictl.py; timezone support is now fully functional --- diff --git a/src/config.py b/src/config.py index 5e042e7..25a6ca6 100644 --- a/src/config.py +++ b/src/config.py @@ -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) diff --git a/src/handlers.py b/src/handlers.py index dbfb230..34fedf0 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -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 diff --git a/src/tzctl.py b/src/tzctl.py index 04e97af..6e19891 100644 --- a/src/tzctl.py +++ b/src/tzctl.py @@ -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 index 0000000..139434a --- /dev/null +++ b/src/wifictl.py @@ -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 . + +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() diff --git a/static/js/main.js b/static/js/main.js index e0e115d..d16b0dd 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -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']); diff --git a/templates/main.html b/templates/main.html index b9390cd..f65e658 100644 --- a/templates/main.html +++ b/templates/main.html @@ -66,7 +66,7 @@ Time Zone - {% for timezone in timezones %} {% endfor %}