Debian Codegraph @

addons/point_of_sale/static/src/customer_display/customer_display_data_service.js

raw
1 import { reactive } from "@odoo/owl";
2 import { getOnNotified } from "@point_of_sale/utils";
3 import { registry } from "@web/core/registry";
4 import { session } from "@web/session";
5 import { _t } from "@web/core/l10n/translation";
6
7 export const CustomerDisplayDataService = {
8 dependencies: ["bus_service", "notification"],
9 async start(env, { bus_service, notification }) {
10 const data = reactive({});
11 if (session.proxy_ip) {
12 const intervalId = setInterval(async () => {
13 try {
14 const response = await fetch(
15 `http://localhost:8069/hw_proxy/customer_facing_display`,
16 {
17 method: "POST",
18 headers: {
19 Accept: "application/json",
20 "Content-Type": "application/json",
21 },
22 body: JSON.stringify({
23 params: {
24 action: "get",
25 },
26 }),
27 }
28 );
29 const payload = await response.json();
30 Object.assign(data, payload.result?.data || payload.result);
31 } catch (error) {
32 notification.add(
33 _t(
34 "Make sure there is an IoT Box subscription associated with your Odoo database, then restart the IoT Box."
35 ),
36 {
37 title: _t("IoT Customer Display Error"),
38 type: "danger",
39 }
40 );
41 console.error("Error fetching data for the IoT customer display: %s", error);
42 clearInterval(intervalId);
43 }
44 }, 1000);
45 } else {
46 new BroadcastChannel("UPDATE_CUSTOMER_DISPLAY").onmessage = (event) => {
47 Object.assign(data, event.data);
48 };
49 getOnNotified(bus_service, session.access_token)(
50 `UPDATE_CUSTOMER_DISPLAY-${session.device_uuid}`,
51 (payload) => {
52 Object.assign(data, payload);
53 }
54 );
55 }
56 return data;
57 },
58 };
59
60 registry.category("services").add("customer_display_data", CustomerDisplayDataService);