From d4fa24c2bce7bfb0b01d244a66aed14542ed1bbb Mon Sep 17 00:00:00 2001 From: lejeune quentin Date: Tue, 10 Sep 2019 13:13:22 +0000 Subject: [PATCH] [IMP] hw_drivers: Add possibility to choose a wifi network on IoT Box Previously if you are wired connected we don't scan wifi network. Now we scan the wifi network and add possibiliyt to select a network and use them. --- addons/hw_drivers/tools/helpers.py | 12 +++- addons/hw_posbox_homepage/controllers/main.py | 25 +++------ .../tools/posbox/configuration/wireless_ap.sh | 55 ++++++++----------- 3 files changed, 42 insertions(+), 50 deletions(-) diff --git a/addons/hw_drivers/tools/helpers.py b/addons/hw_drivers/tools/helpers.py index 9a1b5e2064a..158e3022f07 100644 --- a/addons/hw_drivers/tools/helpers.py +++ b/addons/hw_drivers/tools/helpers.py @@ -74,7 +74,17 @@ def get_token(): return read_file_first_line('token') def get_version(): - return '19_07' + return '19.07' + +def get_wifi_essid(self): + wifi_options = [] + process_iwlist = subprocess.Popen(['sudo', 'iwlist', 'wlan0', 'scan'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + process_grep = subprocess.Popen(['grep', 'ESSID:"'], stdin=process_iwlist.stdout, stdout=subprocess.PIPE).stdout.readlines() + for ssid in process_grep: + essid = ssid.decode('utf-8').split('"')[1] + if essid not in wifi_options: + wifi_options.append(essid) + return wifi_options def load_certificate(): """ diff --git a/addons/hw_posbox_homepage/controllers/main.py b/addons/hw_posbox_homepage/controllers/main.py index e9344ae616e..0a940f3861f 100644 --- a/addons/hw_posbox_homepage/controllers/main.py +++ b/addons/hw_posbox_homepage/controllers/main.py @@ -97,7 +97,7 @@ class IoTboxHomepage(web.Home): 'title': 'Configure IoT Box', 'breadcrumb': 'Configure IoT Box', 'loading_message': 'Configuring your IoT Box', - 'ssid': self.get_wifi_essid(), + 'ssid': helpers.get_wifi_essid(), 'server': helpers.get_odoo_server_url(), 'hostname': subprocess.check_output('hostname').decode('utf-8'), }) @@ -145,27 +145,13 @@ class IoTboxHomepage(web.Home): subprocess.check_call(["sudo", "service", "odoo", "restart"]) return "" - def get_wifi_essid(self): - wifi_options = [] - try: - f = open('/tmp/scanned_networks.txt', 'r') - for line in f: - line = line.rstrip() - line = misc.html_escape(line) - if line not in wifi_options: - wifi_options.append(line) - f.close() - except IOError: - _logger.warning("No /tmp/scanned_networks.txt") - return wifi_options - @http.route('/wifi', type='http', auth='none', website=True) def wifi(self): return wifi_config_template.render({ 'title': 'Wifi configuration', 'breadcrumb': 'Configure Wifi', 'loading_message': 'Connecting to Wifi', - 'ssid': self.get_wifi_essid(), + 'ssid': helpers.get_wifi_essid(), }) @http.route('/wifi_connect', type='http', auth='none', cors='*', csrf=False) @@ -185,6 +171,11 @@ class IoTboxHomepage(web.Home): 'url': server, 'message': 'Redirect to Odoo Server' } + else: + res_payload['server'] = { + 'url': 'http://' + helpers.get_ip() + ':8069', + 'message': 'Redirect to IoT Box' + } return json.dumps(res_payload) @@ -229,7 +220,7 @@ class IoTboxHomepage(web.Home): 'title': 'Configure IoT Box', 'breadcrumb': 'Configure IoT Box', 'loading_message': 'Configuring your IoT Box', - 'ssid': self.get_wifi_essid(), + 'ssid': helpers.get_wifi_essid(), 'server': helpers.get_odoo_server_url(), 'hostname': subprocess.check_output('hostname').decode('utf-8').strip('\n'), }) diff --git a/addons/point_of_sale/tools/posbox/configuration/wireless_ap.sh b/addons/point_of_sale/tools/posbox/configuration/wireless_ap.sh index 1c6367c5b1c..2dbe31b938a 100755 --- a/addons/point_of_sale/tools/posbox/configuration/wireless_ap.sh +++ b/addons/point_of_sale/tools/posbox/configuration/wireless_ap.sh @@ -4,43 +4,34 @@ FORCE_HOST_AP="${1}" WIRED_IP=$(python3 -c "import netifaces as ni; print(ni.ifaddresses('eth0').get(ni.AF_INET) and ni.ifaddresses('eth0')[ni.AF_INET][0]['addr'] or '')") WIFI_NETWORK_FILE="/home/pi/wifi_network.txt" + +ifconfig wlan0 down +ifconfig wlan0 up + +# wait for wlan0 to come up +sleep 5 + # if there is no wired ip, attempt to start an AP through wireless interface if [ -z "${WIRED_IP}" ] ; then logger -t posbox_wireless_ap "No wired IP" - ifconfig wlan0 down - ifconfig wlan0 up - - # wait for wlan0 to come up - sleep 5 - - # we cannot scan for networks while in Master mode - # so first scan and save the networks to a list - iwlist wlan0 scan | grep 'ESSID:' | sed 's/.*ESSID:"\(.*\)"/\1/' > /tmp/scanned_networks.txt - - # only do it when there is a wireless interface - if [ -n "$(iw list)" ] ; then - if [ -f "${WIFI_NETWORK_FILE}" ] && [ -z "${FORCE_HOST_AP}" ] ; then - logger -t posbox_wireless_ap "Loading persistently saved setting" - /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/connect_to_wifi.sh & - else - logger -t posbox_wireless_ap "Starting AP" - - service hostapd restart - - ip addr add 10.11.12.1/24 dev wlan0 - - service dnsmasq restart - - service nginx stop - # We start nginx in another configuration than the default one with https - # as it needs to do redirect instead in case the IoT Box acts as an ap - nginx -c /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/nginx_ap.conf - - service odoo restart - fi - # no wired, no wireless + if [ -f "${WIFI_NETWORK_FILE}" ] && [ -z "${FORCE_HOST_AP}" ] ; then + logger -t posbox_wireless_ap "Loading persistently saved setting" + /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/connect_to_wifi.sh & else + logger -t posbox_wireless_ap "Starting AP" + + service hostapd restart + + ip addr add 10.11.12.1/24 dev wlan0 + + service dnsmasq restart + + service nginx stop + # We start nginx in another configuration than the default one with https + # as it needs to do redirect instead in case the IoT Box acts as an ap + nginx -c /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/nginx_ap.conf + service odoo restart fi # wired