| 1 |
import { HWPrinter } from "@point_of_sale/app/utils/printer/hw_printer"; |
| 2 |
import { EventBus, reactive } from "@odoo/owl"; |
| 3 |
import { browser } from "@web/core/browser/browser"; |
| 4 |
import { rpc } from "@web/core/network/rpc"; |
| 5 |
import { registry } from "@web/core/registry"; |
| 6 |
import { deduceUrl } from "@point_of_sale/utils"; |
| 7 |
import { effect } from "@web/core/utils/reactive"; |
| 8 |
import { logPosMessage } from "../utils/pretty_console_log"; |
| 9 |
|
| 10 |
/** |
| 11 |
* This object interfaces with the local proxy to communicate to the various hardware devices |
| 12 |
* connected to the Point of Sale. As the communication only goes from the POS to the proxy, |
| 13 |
* methods are used both to signal an event, and to fetch information. Maybe could be improved |
| 14 |
* by using the bus for two-way communication? |
| 15 |
*/ |
| 16 |
export class HardwareProxy extends EventBus { |
| 17 |
static serviceDependencies = []; |
| 18 |
constructor() { |
| 19 |
super(); |
| 20 |
this.setup(...arguments); |
| 21 |
} |
| 22 |
setup() { |
| 23 |
this.host = ""; |
| 24 |
this.keptalive = false; |
| 25 |
this.connectionInfo = reactive({ status: "init", drivers: {} }); |
| 26 |
this.deviceControllers = {}; |
| 27 |
effect( |
| 28 |
(info) => { |
| 29 |
if (info.status === "connected" && this.printer) { |
| 30 |
this.printer.printReceipt(); |
| 31 |
} |
| 32 |
}, |
| 33 |
[this.connectionInfo] |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
setConnectionInfo(info) { |
| 38 |
Object.assign(this.connectionInfo, info); |
| 39 |
if (!info.drivers && this.connectionInfo.status === "disconnected") { |
| 40 |
this.connectionInfo.drivers = {}; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
disconnect() { |
| 45 |
if (this.connectionInfo.status !== "disconnected") { |
| 46 |
this.host = null; |
| 47 |
this.setConnectionInfo({ status: "disconnected" }); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
async connect() { |
| 52 |
if (this.pos.config.iface_print_via_proxy) { |
| 53 |
this.connectToPrinter(); |
| 54 |
} |
| 55 |
try { |
| 56 |
if (await this.message("handshake")) { |
| 57 |
this.setConnectionInfo({ status: "connected" }); |
| 58 |
localStorage.hw_proxy_url = this.host; |
| 59 |
this.keepAlive(); |
| 60 |
} else { |
| 61 |
this.setConnectionInfo({ status: "disconnected" }); |
| 62 |
logPosMessage("HardwareProxy", "printHtml", "Connection refused by the Proxy"); |
| 63 |
} |
| 64 |
} catch { |
| 65 |
this.setConnectionInfo({ status: "disconnected" }); |
| 66 |
logPosMessage("HardwareProxy", "printHtml", "Could not connect to the Proxy"); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
connectToPrinter() { |
| 71 |
this.printer = new HWPrinter({ url: this.host }); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Find a proxy and connects to it. |
| 76 |
* |
| 77 |
* @param {Object} [options] |
| 78 |
* @param {string} [options.force_ip] only try to connect to the specified ip. |
| 79 |
* @param {string} [options.port] |
| 80 |
* @returns {Promise} |
| 81 |
*/ |
| 82 |
async autoConnect(options) { |
| 83 |
this.setConnectionInfo({ status: "connecting", drivers: {} }); |
| 84 |
let url = options.force_ip || localStorage.hw_proxy_url; |
| 85 |
// Return a pending promise if there is no url to connect to |
| 86 |
// FIXME POSREF do something useful instead if this condition can happen, remove if not |
| 87 |
if (!url) { |
| 88 |
return new Promise(() => {}); |
| 89 |
} |
| 90 |
|
| 91 |
url = deduceUrl(url); |
| 92 |
|
| 93 |
if (await this.checkProxyAvailability(url)) { |
| 94 |
this.host = url; |
| 95 |
return this.connect(url); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// starts a loop that updates the connection status |
| 100 |
keepAlive() { |
| 101 |
const status = () => { |
| 102 |
const always = () => setTimeout(status, 5000); |
| 103 |
const xhr = new browser.XMLHttpRequest(); |
| 104 |
xhr.timeout = 2500; |
| 105 |
rpc(`${this.host}/hw_proxy/status_json`, {}, { silent: true, xhr }) |
| 106 |
.then( |
| 107 |
(drivers) => this.setConnectionInfo({ status: "connected", drivers }), |
| 108 |
() => { |
| 109 |
if (this.connectionInfo.status !== "connecting") { |
| 110 |
this.setConnectionInfo({ status: "disconnected" }); |
| 111 |
} |
| 112 |
} |
| 113 |
) |
| 114 |
.then(always, always); |
| 115 |
}; |
| 116 |
|
| 117 |
if (!this.keptalive) { |
| 118 |
this.keptalive = true; |
| 119 |
status(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @param {string} name |
| 125 |
* @param {Object} [params] |
| 126 |
* @returns {Promise} |
| 127 |
*/ |
| 128 |
message(name, params) { |
| 129 |
this.dispatchEvent(new CustomEvent(`send_message:${name}`)); |
| 130 |
if (this.connectionInfo.status === "disconnected") { |
| 131 |
return Promise.reject(); |
| 132 |
} |
| 133 |
return rpc(`${this.host}/hw_proxy/${name}`, params, { silent: true }); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Makes sure that the proxy is available by attempting to call the hello |
| 138 |
* route on the proxy. |
| 139 |
* |
| 140 |
* @param {string} url |
| 141 |
* @returns {Promise<void>} |
| 142 |
*/ |
| 143 |
async checkProxyAvailability(url) { |
| 144 |
this.setConnectionInfo({ status: "connecting" }); |
| 145 |
const maxRetries = 3; |
| 146 |
for (let i = 0; i <= maxRetries; i++) { |
| 147 |
const timeoutController = new AbortController(); |
| 148 |
setTimeout(() => timeoutController.abort(), 1000); |
| 149 |
const response = await browser |
| 150 |
.fetch(`${url}/hw_proxy/hello`, { |
| 151 |
signal: timeoutController.signal, |
| 152 |
targetAddressSpace: odoo.use_lna ? "local" : undefined, |
| 153 |
}) |
| 154 |
.catch(() => ({})); |
| 155 |
if (response.ok) { |
| 156 |
return true; |
| 157 |
} |
| 158 |
} |
| 159 |
this.setConnectionInfo({ status: "disconnected" }); |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
async openCashbox(action = false) { |
| 164 |
const isPrinterConnected = |
| 165 |
["connected", "init"].includes(this.connectionInfo.status) || |
| 166 |
this.pos.config.epson_printer_ip; |
| 167 |
if (this.pos.config.iface_cashdrawer && this.printer && isPrinterConnected) { |
| 168 |
this.printer.openCashbox(); |
| 169 |
if (action) { |
| 170 |
this.pos.logEmployeeMessage(action, "CASH_DRAWER_ACTION"); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
export const hardwareProxyService = { |
| 177 |
dependencies: HardwareProxy.serviceDependencies, |
| 178 |
start(env, deps) { |
| 179 |
return new HardwareProxy(deps); |
| 180 |
}, |
| 181 |
}; |
| 182 |
|
| 183 |
registry.category("services").add("hardware_proxy", hardwareProxyService); |