| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
import json |
| 4 |
import logging |
| 5 |
import os |
| 6 |
import requests |
| 7 |
import subprocess |
| 8 |
import time |
| 9 |
import werkzeug |
| 10 |
|
| 11 |
from odoo import http |
| 12 |
from odoo.addons.iot_drivers.browser import Browser, BrowserState |
| 13 |
from odoo.addons.iot_drivers.driver import Driver |
| 14 |
from odoo.addons.iot_drivers.main import iot_devices |
| 15 |
from odoo.addons.iot_drivers.tools import helpers, route |
| 16 |
from odoo.addons.iot_drivers.tools.helpers import Orientation |
| 17 |
|
| 18 |
_logger = logging.getLogger(__name__) |
| 19 |
|
| 20 |
|
| 21 |
class DisplayDriver(Driver): |
| 22 |
connection_type = 'display' |
| 23 |
|
| 24 |
def __init__(self, identifier, device): |
| 25 |
super().__init__(identifier, device) |
| 26 |
self.device_type = 'display' |
| 27 |
self.device_connection = 'hdmi' |
| 28 |
self.device_name = device['name'] |
| 29 |
self.owner = False |
| 30 |
self.customer_display_data = {} |
| 31 |
|
| 32 |
saved_url, self.orientation = helpers.load_browser_state() |
| 33 |
self._x_screen = device.get('x_screen', '0') |
| 34 |
self.url = saved_url or self.get_url_from_db() or 'http://localhost:8069/status/' |
| 35 |
self.browser = Browser(self.url, self._x_screen, os.environ.copy()) |
| 36 |
self.set_orientation(self.orientation) |
| 37 |
|
| 38 |
self._actions.update({ |
| 39 |
'update_url': self._action_update_url, |
| 40 |
'display_refresh': self._action_display_refresh, |
| 41 |
'open_kiosk': self._action_open_kiosk, |
| 42 |
'rotate_screen': self._action_rotate_screen, |
| 43 |
'open': self._action_open_customer_display, |
| 44 |
'close': self._action_close_customer_display, |
| 45 |
'set': self._action_set_customer_display, |
| 46 |
}) |
| 47 |
|
| 48 |
@classmethod |
| 49 |
def supported(cls, device): |
| 50 |
return True # All devices with connection_type == 'display' are supported |
| 51 |
|
| 52 |
@classmethod |
| 53 |
def get_default_display(cls): |
| 54 |
displays = list(filter(lambda d: iot_devices[d].device_type == 'display', iot_devices)) |
| 55 |
return len(displays) and iot_devices[displays[0]] |
| 56 |
|
| 57 |
def run(self): |
| 58 |
while not self._stopped.is_set() and "pos_customer_display" not in self.url: |
| 59 |
time.sleep(60) |
| 60 |
if self.url != 'http://localhost:8069/status/' and self.browser.state != BrowserState.KIOSK: |
| 61 |
# Refresh the page every minute |
| 62 |
self.browser.refresh() |
| 63 |
|
| 64 |
def update_url(self, url=None): |
| 65 |
self.url = ( |
| 66 |
url |
| 67 |
or helpers.load_browser_state()[0] |
| 68 |
or 'http://localhost:8069/status/' |
| 69 |
) |
| 70 |
|
| 71 |
browser_state = BrowserState.KIOSK if "/pos-self/" in self.url else BrowserState.FULLSCREEN |
| 72 |
self.browser.open_browser(self.url, browser_state) |
| 73 |
|
| 74 |
@helpers.require_db |
| 75 |
def get_url_from_db(self, server_url=None): |
| 76 |
"""Get the display URL provided by the connected database. |
| 77 |
|
| 78 |
:param server_url: The URL of the connected database (provided by decorator). |
| 79 |
:return: URL to display or None. |
| 80 |
""" |
| 81 |
try: |
| 82 |
response = requests.get(f"{server_url}/iot/box/{helpers.get_identifier()}/display_url", timeout=5) |
| 83 |
response.raise_for_status() |
| 84 |
data = json.loads(response.content.decode()) |
| 85 |
return data.get(self.device_identifier) |
| 86 |
except requests.exceptions.RequestException: |
| 87 |
_logger.exception("Failed to get display URL from server") |
| 88 |
except json.decoder.JSONDecodeError: |
| 89 |
return response.content.decode('utf8') |
| 90 |
|
| 91 |
def _action_update_url(self, data): |
| 92 |
helpers.save_browser_state(url=data.get('url')) |
| 93 |
self.update_url(data.get('url')) |
| 94 |
|
| 95 |
def _action_display_refresh(self, data): |
| 96 |
self.browser.refresh() |
| 97 |
|
| 98 |
def _action_open_kiosk(self, data): |
| 99 |
origin = helpers.get_odoo_server_url() |
| 100 |
self.update_url(f"{origin}/pos-self/{data.get('pos_id')}?access_token={data.get('access_token')}") |
| 101 |
self.set_orientation(Orientation.RIGHT) |
| 102 |
|
| 103 |
def _action_rotate_screen(self, data): |
| 104 |
orientation = data.get('orientation', 'NORMAL').upper() |
| 105 |
self.set_orientation(Orientation[orientation]) |
| 106 |
|
| 107 |
def _action_open_customer_display(self, data): |
| 108 |
if not data.get('pos_id') or not data.get('access_token'): |
| 109 |
return |
| 110 |
|
| 111 |
origin = helpers.get_odoo_server_url() or http.request.httprequest.origin |
| 112 |
self.update_url(f"{origin}/pos_customer_display/{data['pos_id']}/{data['access_token']}") |
| 113 |
|
| 114 |
def _action_close_customer_display(self, data): |
| 115 |
helpers.update_conf({"browser_url": "", "screen_orientation": ""}) |
| 116 |
self.browser.disable_kiosk_mode() |
| 117 |
self.update_url() |
| 118 |
|
| 119 |
def _action_set_customer_display(self, data): |
| 120 |
if not data.get('data'): |
| 121 |
return |
| 122 |
|
| 123 |
self.data['customer_display_data'] = data['data'] |
| 124 |
|
| 125 |
def set_orientation(self, orientation=Orientation.NORMAL): |
| 126 |
if type(orientation) is not Orientation: |
| 127 |
raise TypeError("orientation must be of type Orientation") |
| 128 |
|
| 129 |
subprocess.run(['wlr-randr', '--output', self.device_identifier, '--transform', orientation.value], check=True) |
| 130 |
# Update touchscreen mapping to this display |
| 131 |
subprocess.run( |
| 132 |
['sed', '-i', f's/HDMI-A-[12]/{self.device_identifier}/', '/home/odoo/.config/labwc/rc.xml'], |
| 133 |
check=False, |
| 134 |
) |
| 135 |
# Tell labwc to reload its configuration |
| 136 |
subprocess.run(['pkill', '-HUP', 'labwc'], check=False) |
| 137 |
helpers.save_browser_state(orientation=orientation) |
| 138 |
|
| 139 |
|
| 140 |
class DisplayController(http.Controller): |
| 141 |
@route.iot_route('/hw_proxy/customer_facing_display', type='jsonrpc', cors='*') |
| 142 |
def customer_facing_display(self): |
| 143 |
display = self.ensure_display() |
| 144 |
return display.data.get('customer_display_data', {}) |
| 145 |
|
| 146 |
def ensure_display(self): |
| 147 |
display: DisplayDriver = DisplayDriver.get_default_display() |
| 148 |
if not display: |
| 149 |
raise werkzeug.exceptions.ServiceUnavailable(description="No display connected") |
| 150 |
return display |