[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
This commit is contained in:
Antoine Prieels
2019-09-04 13:08:12 +00:00
parent f03026a3d7
commit 98bebfbfa5
2 changed files with 39 additions and 40 deletions
+39 -10
View File
@@ -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"
@@ -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}" &