From 98bebfbfa56013ff4939bdf67348a6c40c110599 Mon Sep 17 00:00:00 2001 From: Antoine Prieels Date: Tue, 16 Apr 2019 12:29:53 +0000 Subject: [PATCH] [IMP] point_of_sale: Per device screen_url The screen URL is not linked to the Box anymore but to the Device. We don't need to fetch a screen_url at the boot, it'll be done inside each device. TaskID: 1893493 --- addons/hw_drivers/drivers/DisplayDriver.py | 49 +++++++++++++++---- .../etc/xdg/openbox/autostart | 30 ------------ 2 files changed, 39 insertions(+), 40 deletions(-) delete mode 100755 addons/point_of_sale/tools/posbox/overwrite_after_init/etc/xdg/openbox/autostart diff --git a/addons/hw_drivers/drivers/DisplayDriver.py b/addons/hw_drivers/drivers/DisplayDriver.py index a80a7cf8f4d..35547fbcbe1 100644 --- a/addons/hw_drivers/drivers/DisplayDriver.py +++ b/addons/hw_drivers/drivers/DisplayDriver.py @@ -3,12 +3,15 @@ import json import logging import netifaces as ni import os +from pathlib import Path import subprocess import threading import time +import urllib3 from odoo import http -from odoo.addons.hw_drivers.controllers.driver import Driver, event_manager, iot_devices, get_odoo_server_url, get_mac_address +from odoo.addons.hw_drivers.tools import helpers +from odoo.addons.hw_drivers.controllers.driver import Driver, event_manager, iot_devices path = os.path.realpath(os.path.join(os.path.dirname(__file__), '../views')) loader = jinja2.FileSystemLoader(path) @@ -20,13 +23,6 @@ pos_display_template = jinja_env.get_template('pos_display.html') _logger = logging.getLogger(__name__) -# Get screen_url from the Box for backward compatibility -box_url = False -if get_odoo_server_url(): - result = requests.get("%s/iot/box/%s/screen_url" % (get_odoo_server_url(), get_mac_address()), {}) - if result.status_code == 200: - box_url = result.text - class DisplayDriver(Driver): connection_type = 'display' @@ -39,7 +35,7 @@ class DisplayDriver(Driver): self.event_data = threading.Event() self.owner = False self.rendered_html = '' - self.url = 'http://localhost:8069/point_of_sale/display/' + self.load_url() @property def device_identifier(self): @@ -55,7 +51,9 @@ class DisplayDriver(Driver): return len(displays) and iot_devices[displays[0]] def action(self, data): - if data.get('action') == "display_refresh": + if data.get('action') == "update_url": + self.update_url(data.get('url')) + elif data.get('action') == "display_refresh": self.call_xdotools('F5') elif data.get('action') == "take_control": self.take_control(self.data['owner'], data.get('html')) @@ -68,6 +66,37 @@ class DisplayDriver(Driver): } event_manager.device_changed(self) + def run(self): + while True: + if self.url != 'http://localhost:8069/point_of_sale/display/': + # Refresh the page every minute + time.sleep(60) + self.call_xdotools('F5') + + def update_url(self, url=None): + firefox_env = os.environ.copy() + firefox_env['HOME'] = '/tmp' + firefox_env['DISPLAY'] = ':0.0' + firefox_env['XAUTHORITY'] = '/run/lightdm/pi/xauthority' + self.url = url or 'http://localhost:8069/point_of_sale/display/' + if self.device_identifier != 'distant_display': + subprocess.Popen(['firefox', self.url], env=firefox_env) + + def load_url(self): + if helpers.get_odoo_server_url(): + # disable certifiacte verification + urllib3.disable_warnings() + http = urllib3.PoolManager(cert_reqs='CERT_NONE') + try: + response = http.request('GET', "%s/iot/box/%s/screen_url" % (helpers.get_odoo_server_url(), helpers.get_mac_address())) + urls = json.loads(response.data.decode('utf8')) + return self.update_url(urls[self.device_identifier]) + except json.decoder.JSONDecodeError: + return self.update_url(response.data.decode('utf8')) + except Exception: + pass + return self.update_url() + def call_xdotools(self, keystroke): os.environ['DISPLAY'] = ":0.0" os.environ['XAUTHORITY'] = "/run/lightdm/pi/xauthority" diff --git a/addons/point_of_sale/tools/posbox/overwrite_after_init/etc/xdg/openbox/autostart b/addons/point_of_sale/tools/posbox/overwrite_after_init/etc/xdg/openbox/autostart deleted file mode 100755 index d7b5349a74f..00000000000 --- a/addons/point_of_sale/tools/posbox/overwrite_after_init/etc/xdg/openbox/autostart +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash -xset s off -xset -dpms - -export HOME=/tmp - -DISPLAY_URL='http://localhost:8069/point_of_sale/display' -URL=${DISPLAY_URL} - -maciotbox=$(/sbin/ifconfig eth0 |grep -Eo ..\(\:..\){5}) - -if [ -e /home/pi/odoo-remote-server.conf ] - then - URL_SERVER=$(grep "" /home/pi/odoo-remote-server.conf)/iot/box/${maciotbox}/screen_url - var=0 - while [ ${var} -lt 30 ]; do - var=$((var+1)) - sleep 1 - status_code=$(wget ${URL_SERVER} --server-response 2>&1|grep 'HTTP/'|awk '{print $2}') - if [ ${status_code} -eq 200 ] - then - URL=$(wget ${URL_SERVER} -q -O -) - break - fi - done -fi -# wait until odoo is ready, otherwise you will have to plug a -# keyboard into the posbox just to refresh firefox -until wget -q "${DISPLAY_URL}" -O /dev/null ; do sleep 1 ; done -/usr/bin/firefox "${URL}" & \ No newline at end of file