From: Calin Crisan Date: Wed, 25 Jun 2014 14:40:43 +0000 (+0300) Subject: initial work on wifi settings X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=e266dc78b672c20815a632a6f59f2015ecce16d8;p=motioneye-debian initial work on wifi settings --- diff --git a/settings_default.py b/settings_default.py index 831ea31..d90f6bb 100644 --- a/settings_default.py +++ b/settings_default.py @@ -29,6 +29,9 @@ LISTEN = '0.0.0.0' # change the port according to your requirements/restrictions PORT = 8765 +# path to a wpa_supplicant.conf file if wifi settings UI is desired (requires root) +WPA_SUPPLICANT_CONF = None + # enable SMB shares (requires root) SMB_SHARES = False diff --git a/src/config.py b/src/config.py index bec11d3..f96d0a5 100644 --- a/src/config.py +++ b/src/config.py @@ -84,6 +84,10 @@ def get_main(as_lines=False): return lines main_config = _conf_to_dict(lines, list_names=['thread']) + + if settings.WPA_SUPPLICANT_CONF: + _get_wifi_settings(main_config) + _set_default_motion(main_config) _main_config_cache = main_config @@ -95,7 +99,11 @@ def set_main(main_config): global _main_config_cache _set_default_motion(main_config) - + _main_config_cache = dict(main_config) + + if settings.WPA_SUPPLICANT_CONF: + _set_wifi_settings(main_config) + config_file_path = os.path.join(settings.CONF_PATH, _MAIN_CONFIG_FILE_NAME) # read the actual configuration from file @@ -135,10 +143,6 @@ def set_main(main_config): finally: file.close() - _main_config_cache = main_config - - return main_config - def get_camera_ids(): global _camera_ids_cache @@ -377,8 +381,6 @@ def set_camera(camera_id, camera_config): finally: file.close() - return camera_config - def add_camera(device_details): global _camera_ids_cache @@ -461,29 +463,39 @@ def rem_camera(camera_id): def main_ui_to_dict(ui): return { '@enabled': ui['enabled'], + '@show_advanced': ui['show_advanced'], '@admin_username': ui['admin_username'], '@admin_password': ui['admin_password'], '@normal_username': ui['normal_username'], - '@normal_password': ui['normal_password'] + '@normal_password': ui['normal_password'], + + '@wifi_enabled': ui['wifi_enabled'], + '@wifi_name': ui['wifi_name'], + '@wifi_key': ui['wifi_key'], } def main_dict_to_ui(data): return { 'enabled': data['@enabled'], + 'show_advanced': data['@show_advanced'], 'admin_username': data['@admin_username'], 'admin_password': data['@admin_password'], 'normal_username': data['@normal_username'], - 'normal_password': data['@normal_password'] + 'normal_password': data['@normal_password'], + + 'wifi_enabled': data['@wifi_enabled'], + 'wifi_name': data['@wifi_name'], + 'wifi_key': data['@wifi_key'], } def camera_ui_to_dict(ui): if not ui['resolution']: # avoid errors for empty resolution setting ui['resolution'] = '352x288' - + width = int(ui['resolution'].split('x')[0]) height = int(ui['resolution'].split('x')[1]) threshold = int(float(ui['frame_change_threshold']) * width * height / 100) @@ -1041,12 +1053,17 @@ def _is_old_motion(): def _set_default_motion(data): data.setdefault('@enabled', True) + data.setdefault('@show_advanced', False) data.setdefault('@admin_username', 'admin') data.setdefault('@admin_password', '') data.setdefault('@normal_username', 'user') data.setdefault('@normal_password', '') + data.setdefault('@wifi_enabled', False) + data.setdefault('@wifi_name', '') + data.setdefault('@wifi_key', '') + def _set_default_motion_camera(camera_id, data, old_motion): data.setdefault('@name', 'Camera' + str(camera_id)) @@ -1135,3 +1152,37 @@ def _set_default_motion_camera(camera_id, data, old_motion): data.setdefault('@motion_notifications_emails', '') data.setdefault('@working_schedule', '') + + +def _get_wifi_settings(data): + 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 + + # TODO read settings from file + + conf_file.close() + + +def _set_wifi_settings(data): + wifi_enabled = data.pop('@wifi_enabled', False) + wifi_name = data.pop('@wifi_name', None) + wifi_key = data.pop('@wifi_key', None) + + 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 + + # TODO write settings to file + + conf_file.close() diff --git a/static/css/main.css b/static/css/main.css index fd89cf9..702d5e2 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -397,7 +397,7 @@ span.disk-usage-percent { div.hidden, tr.hidden { - display: none; + display: none !important; } diff --git a/static/js/main.js b/static/js/main.js index bb47f24..be75a96 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -193,6 +193,7 @@ function initUI() { /* text validators */ makeTextValidator($('#adminUsernameEntry'), true); makeTextValidator($('#normalUsernameEntry'), true); + makeTextValidator($('#wifiNameEntry'), true); makeTextValidator($('#deviceNameEntry'), true); makeTextValidator($('#networkServerEntry'), true); makeTextValidator($('#networkShareNameEntry'), true); @@ -250,6 +251,7 @@ function initUI() { $('#preserveMoviesSelect').change(updateConfigUi); $('#motionNotificationsSwitch').change(updateConfigUi); $('#workingScheduleSwitch').change(updateConfigUi); + $('#wifiSwitch').change(updateConfigUi); /* fetch & push handlers */ $('#videoDeviceSelect').change(function () { @@ -278,7 +280,8 @@ function initUI() { 'input.motion-movies, select.motion-movies, ' + 'input.motion-detection, select.motion-detection, ' + 'input.notifications, select.notifications, ' + - 'input.working-schedule, select.working-schedule').change(pushCameraConfig); + 'input.working-schedule, select.working-schedule, ' + + 'input.wifi, select.wifi').change(pushCameraConfig); /* preview controls */ $('#brightnessSlider').change(function () {pushPreview('brightness');}); @@ -356,6 +359,11 @@ function updateConfigUi() { objs.not($('#motionEyeSwitch').parents('div').get(0)).each(markHide); } + /* wifi switch */ + if (!$('#wifiSwitch').get(0).checked) { + $('#wifiSwitch').parent().next('table.settings').find('tr.settings-item').each(markHide); + } + if ($('#videoDeviceSelect').find('option').length < 2) { /* no camera configured */ $('#videoDeviceSwitch').parent().each(markHide); $('#videoDeviceSwitch').parent().nextAll('div.settings-section-title, table.settings').each(markHide); @@ -503,22 +511,32 @@ function configUiValid() { function mainUi2Dict() { return { 'enabled': $('#motionEyeSwitch')[0].checked, + 'show_advanced': $('#showAdvancedSwitch')[0].checked, 'admin_username': $('#adminUsernameEntry').val(), 'admin_password': $('#adminPasswordEntry').val(), 'normal_username': $('#normalUsernameEntry').val(), - 'normal_password': $('#normalPasswordEntry').val() + 'normal_password': $('#normalPasswordEntry').val(), + + 'wifi_enabled': $('#wifiSwitch')[0].checked, + 'wifi_name': $('#wifiNameEntry').val(), + 'wifi_key': $('#wifiKeyEntry').val() }; } function dict2MainUi(dict) { $('#motionEyeSwitch')[0].checked = dict['enabled']; + $('#showAdvancedSwitch')[0].checked = dict['show_advanced']; $('#adminUsernameEntry').val(dict['admin_username']); $('#adminPasswordEntry').val(dict['admin_password']); $('#normalUsernameEntry').val(dict['normal_username']); $('#normalPasswordEntry').val(dict['normal_password']); + $('#wifiSwitch')[0].checked = dict['wifi_enabled']; + $('#wifiNameEntry').val(dict['wifi_name']); + $('#wifiKeyEntry').val(dict['wifi_key']); + updateConfigUi(); } diff --git a/templates/main.html b/templates/main.html index 8ee9873..8f18eaf 100644 --- a/templates/main.html +++ b/templates/main.html @@ -76,6 +76,20 @@ +
Wireless Network
+ + + + + + + + + + + +
Network Name?
Network Key?
+
Video Device
@@ -464,6 +478,7 @@
?
+