[FIX] 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.

Description of the issue/feature this PR addresses:

Current behavior before PR:

Desired behavior after PR is merged:

--
I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr

closes odoo/odoo#36634

Signed-off-by: Quentin Lejeune (qle) <qle@odoo.com>
This commit is contained in:
Odoo's Mergebot
2019-09-11 11:57:20 +02:00
committed by GitHub
7 changed files with 48 additions and 75 deletions
@@ -181,9 +181,7 @@ class KeyboardUSBDriver(Driver):
else:
data = {}
data[self.device_identifier] = layout
subprocess.check_call(["sudo", "mount", "-o", "remount,rw", "/"])
file_path.write_text(json.dumps(data))
subprocess.check_call(["sudo", "mount", "-o", "remount,ro", "/"])
helpers.write_file('odoo-keyboard-layouts.conf', json.dumps(data))
def load_layout(self):
"""Read the layout from the saved filed and set it as current layout.
+15 -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():
"""
@@ -109,6 +119,7 @@ def load_certificate():
Path('/root_bypass_ramdisks/etc/ssl/private/nginx-cert.key').write_text(result['private_key_pem'])
subprocess.check_call(["sudo", "mount", "-o", "remount,ro", "/"])
subprocess.check_call(["sudo", "mount", "-o", "remount,ro", "/root_bypass_ramdisks/"])
subprocess.check_call(["sudo", "mount", "-o", "remount,rw", "/root_bypass_ramdisks/etc/cups"])
subprocess.check_call(["sudo", "service", "nginx", "restart"])
def download_drivers(auto=True):
@@ -127,6 +138,7 @@ def download_drivers(auto=True):
zip_file = zipfile.ZipFile(io.BytesIO(resp.data))
zip_file.extractall(get_resource_path('hw_drivers', 'drivers'))
subprocess.check_call(["sudo", "mount", "-o", "remount,ro", "/"])
subprocess.check_call(["sudo", "mount", "-o", "remount,rw", "/root_bypass_ramdisks/etc/cups"])
except Exception as e:
_logger.error('Could not reach configured server')
_logger.error('A error encountered : %s ' % e)
@@ -145,9 +157,11 @@ def unlink_file(filename):
if path.exists():
path.unlink()
subprocess.check_call(["sudo", "mount", "-o", "remount,ro", "/"])
subprocess.check_call(["sudo", "mount", "-o", "remount,rw", "/root_bypass_ramdisks/etc/cups"])
def write_file(filename, text):
subprocess.check_call(["sudo", "mount", "-o", "remount,rw", "/"])
path = Path.home() / filename
path.write_text(text)
subprocess.check_call(["sudo", "mount", "-o", "remount,ro", "/"])
subprocess.check_call(["sudo", "mount", "-o", "remount,rw", "/root_bypass_ramdisks/etc/cups"])
+8 -36
View File
@@ -49,12 +49,6 @@ list_credential_template = jinja_env.get_template('list_credential.html')
class IoTboxHomepage(web.Home):
def get_pos_device_status(self):
statuses = {}
for driver in hw_proxy.drivers:
statuses[driver] = hw_proxy.drivers[driver].get_status()
return statuses
def get_six_terminal(self):
terminal_id = helpers.read_file_first_line('odoo-six-payment-terminal.conf')
return terminal_id or 'Not Configured'
@@ -75,18 +69,7 @@ class IoTboxHomepage(web.Home):
else:
network = 'Not Connected'
pos_device = self.get_pos_device_status()
iot_device = []
if not iot_devices:
for status in pos_device:
if pos_device[status]['status'] == 'connected':
iot_device.append({
'name': status,
'type': 'device',
'message': ' '.join(pos_device[status]['messages'])
})
for device in iot_devices:
iot_device.append({
'name': iot_devices[device].device_name + ' : ' + str(iot_devices[device].data['value']),
@@ -114,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'),
})
@@ -157,34 +140,18 @@ class IoTboxHomepage(web.Home):
@http.route('/clear_credential', type='http', auth='none', cors='*', csrf=False)
def clear_credential(self):
subprocess.check_call(["sudo", "mount", "-o", "remount,rw", "/"])
helpers.unlink_file('odoo-db-uuid.conf')
helpers.unlink_file('odoo-enterprise-code.conf')
subprocess.check_call(["sudo", "service", "odoo", "restart"])
subprocess.check_call(["sudo", "mount", "-o", "remount,ro", "/"])
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)
@@ -204,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)
@@ -248,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'),
})
@@ -117,7 +117,6 @@ function connect () {
if [ ${WIFI_WAS_LOST} -eq 0 ] ; then
touch "${LOST_WIFI_FILE}"
fi
wget -q "http://localhost:8069/hw_drivers/send_iot_box" -O /dev/null
logger -t posbox_connect_to_wifi "Starting wifi keep alive script"
/home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/keep_wifi_alive.sh &
@@ -81,7 +81,6 @@ function connect () {
if [ ${WIFI_WAS_LOST} -eq 0 ] ; then
touch "${LOST_WIFI_FILE}"
fi
wget -q "http://localhost:8069/hw_drivers/send_iot_box" -O /dev/null
logger -t posbox_connect_to_wifi "Starting wifi keep alive script"
/home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/keep_wifi_alive.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
@@ -1,6 +1,6 @@
#!/bin/sh
wget 'http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-11-15/2018-11-13-raspbian-stretch-lite.zip' -O raspbian.img.zip
wget 'http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-04-09/2019-04-08-raspbian-stretch-lite.zip' -O raspbian.img.zip
unzip raspbian.img.zip
wget 'https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.14.79-stretch' -O kernel-qemu
wget 'https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb'