Debian Codegraph @

addons/point_of_sale/static/src/app/services/barcode_reader_service.js

raw
1 import { _t } from "@web/core/l10n/translation";
2 import { registry } from "@web/core/registry";
3 import { Mutex } from "@web/core/utils/concurrency";
4 import { session } from "@web/session";
5 import { AlertDialog } from "@web/core/confirmation_dialog/confirmation_dialog";
6 import { BarcodeParser } from "@barcodes/js/barcode_parser";
7 import { GS1BarcodeError } from "@barcodes_gs1_nomenclature/js/barcode_parser";
8 import { logPosMessage } from "../utils/pretty_console_log";
9
10 export class BarcodeReader {
11 static serviceDependencies = ["dialog", "hardware_proxy", "notification", "action", "orm"];
12 constructor(parser, { dialog, hardware_proxy, notification, action, orm }) {
13 this.parser = parser;
14 this.dialog = dialog;
15 this.action = action;
16 this.orm = orm;
17 this.hardwareProxy = hardware_proxy;
18 this.notification = notification;
19 this.setup();
20 }
21
22 setup() {
23 this.mutex = new Mutex();
24 this.cbMaps = new Set();
25 // FIXME POSREF: When LoginScreen becomes a normal screen, we can remove this exclusive callback handling.
26 this.exclusiveCbMap = null;
27 this.remoteScanning = false;
28 this.remoteActive = 0;
29 }
30
31 register(cbMap, exclusive) {
32 if (exclusive) {
33 this.exclusiveCbMap = cbMap;
34 } else {
35 this.cbMaps.add(cbMap);
36 }
37 return () => {
38 if (exclusive) {
39 this.exclusiveCbMap = null;
40 } else {
41 this.cbMaps.delete(cbMap);
42 }
43 };
44 }
45
46 scan(code) {
47 return this.mutex.exec(() => this._scan(code));
48 }
49 async _scan(code) {
50 if (!code) {
51 return;
52 }
53
54 const cbMaps = this.exclusiveCbMap ? [this.exclusiveCbMap] : [...this.cbMaps];
55
56 let parseBarcode;
57 try {
58 parseBarcode = this.parser.parse_barcode(code);
59 if (
60 Array.isArray(parseBarcode) &&
61 !parseBarcode.some((element) => element.type === "product")
62 ) {
63 throw new GS1BarcodeError("The GS1 barcode must contain a product.");
64 }
65 } catch (error) {
66 if (error instanceof GS1BarcodeError) {
67 if (this.fallbackParser) {
68 parseBarcode = this.fallbackParser.parse_barcode(code);
69 } else {
70 this.showGS1IncompatibleBarcodeWarning();
71 return;
72 }
73 } else {
74 throw error;
75 }
76 }
77 if (Array.isArray(parseBarcode)) {
78 cbMaps.map((cb) => cb.gs1?.(parseBarcode));
79 } else {
80 const cbs = cbMaps.map((cbMap) => cbMap[parseBarcode.type]).filter(Boolean);
81 if (cbs.length === 0) {
82 this.showNotFoundNotification(parseBarcode);
83 }
84 for (const cb of cbs) {
85 await cb(parseBarcode);
86 }
87 }
88 }
89 showNotFoundNotification(code) {
90 this.notification.add(
91 _t(
92 "The Point of Sale could not find any product, customer, employee or action associated with the scanned barcode."
93 ),
94 {
95 type: "warning",
96 title: _t(`Unknown Barcode`) + " " + this.codeRepr(code),
97 }
98 );
99 }
100
101 codeRepr(parsedBarcode) {
102 if (parsedBarcode.code.length > 32) {
103 return parsedBarcode.code.substring(0, 29) + "...";
104 } else {
105 return parsedBarcode.code;
106 }
107 }
108
109 showGS1IncompatibleBarcodeWarning() {
110 this.notification.add(
111 _t(
112 "This barcode is not compatible with the GS1 standard. Consider configuring a fallback barcode parser from the PoS settings."
113 ),
114 {
115 type: "warning",
116 title: _t("Unsupported Barcode Format"),
117 }
118 );
119 }
120
121 // the barcode scanner will listen on the hw_proxy/scanner interface for
122 // scan events until disconnectFromProxy is called
123 connectToProxy() {
124 this.remoteScanning = true;
125 if (this.remoteActive >= 1) {
126 return;
127 }
128 this.remoteActive = 1;
129 this.waitForBarcode();
130 }
131
132 async waitForBarcode() {
133 const barcode = await this.hardwareProxy.message("scanner").catch(() => {});
134 if (!this.remoteScanning) {
135 this.remoteActive = 0;
136 return;
137 }
138 this.scan(barcode);
139 this.waitForBarcode();
140 }
141
142 // the barcode scanner will stop listening on the hw_proxy/scanner remote interface
143 disconnectFromProxy() {
144 this.remoteScanning = false;
145 }
146 }
147
148 export const barcodeReaderService = {
149 dependencies: [...BarcodeReader.serviceDependencies, "dialog", "barcode", "orm"],
150 async start(env, deps) {
151 const { dialog, barcode, orm } = deps;
152 let barcodeReader = null;
153
154 try {
155 if (session.nomenclature_id) {
156 const nomenclature = await BarcodeParser.fetchNomenclature(
157 orm,
158 session.nomenclature_id
159 );
160 const parser = new BarcodeParser({ nomenclature });
161 barcodeReader = new BarcodeReader(parser, deps);
162 }
163
164 if (session.fallback_nomenclature_id && barcodeReader) {
165 const fallbackNomenclature = await BarcodeParser.fetchNomenclature(
166 orm,
167 session.fallback_nomenclature_id
168 );
169 barcodeReader.fallbackParser = new BarcodeParser({
170 nomenclature: fallbackNomenclature,
171 });
172 }
173 } catch (error) {
174 logPosMessage(
175 "BarcodeReaderService",
176 "start",
177 "Failed to start barcode reader",
178 false,
179 [error]
180 );
181 }
182
183 barcode.bus.addEventListener("barcode_scanned", (ev) => {
184 if (barcodeReader) {
185 barcodeReader.scan(ev.detail.barcode);
186 } else {
187 dialog.add(AlertDialog, {
188 title: _t("Unable to parse barcode"),
189 body: _t(
190 "No barcode nomenclature has been configured. This can be changed in the configuration settings."
191 ),
192 });
193 }
194 });
195
196 return barcodeReader;
197 },
198 };
199
200 registry.category("services").add("barcode_reader", barcodeReaderService);