[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.
This commit is contained in:
lejeune quentin
2019-09-10 14:14:27 +00:00
parent 9c10d7a28e
commit d4fa24c2bc
3 changed files with 42 additions and 50 deletions
+11 -1
View File
@@ -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():
"""
+8 -17
View File
@@ -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 "<meta http-equiv='refresh' content='20; url=http://" + helpers.get_ip() + ":8069'>"
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'),
})
@@ -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