Debian Codegraph @

addons/point_of_sale/static/src/app/screens/scale_screen/scale_service.js

raw
1 import { _t } from "@web/core/l10n/translation";
2 import { registry } from "@web/core/registry";
3 import { roundDecimals, formatFloat } from "@web/core/utils/numbers";
4 import { Reactive } from "@web/core/utils/reactive";
5
6 const MEASURING_DELAY_MS = 500;
7 const TARE_TIMEOUT_MS = 3000;
8
9 export class PosScaleService extends Reactive {
10 constructor(env, deps) {
11 super(...arguments);
12 this.setup(env, deps);
13 }
14
15 setup(env, deps) {
16 this.env = env;
17 this.hardwareProxy = deps.hardware_proxy;
18 this.lastWeight = null;
19 this.weight = 0;
20 this.reset();
21 }
22
23 start(errorCallback) {
24 this.onError = errorCallback;
25 if (!this.isManualMeasurement) {
26 this.isMeasuring = true;
27 this._readWeightContinuously();
28 }
29 }
30
31 reset() {
32 this.tare = 0;
33 this.tareRequested = false;
34 this.loading = false;
35 this.isMeasuring = false;
36 this.product = null;
37 this.onError = null;
38 }
39
40 confirmWeight() {
41 this.lastWeight = this.weight;
42 return this.netWeight;
43 }
44
45 async _readWeightContinuously() {
46 if (!this.isMeasuring) {
47 return;
48 }
49 await this.readWeight();
50 setTimeout(() => this._readWeightContinuously(), MEASURING_DELAY_MS);
51 }
52
53 async _getWeightFromScale() {
54 const { weight } = await this.hardwareProxy.message("scale_read");
55 return weight;
56 }
57
58 async readWeight() {
59 this.loading = true;
60 try {
61 this._checkScaleIsConnected();
62 this.weight = await this._getWeightFromScale();
63 this._clearLastWeightIfValid();
64 } catch (error) {
65 this.isMeasuring = false;
66 this.onError?.(error.message);
67 }
68 this.loading = false;
69 this._setTareIfRequested();
70 }
71
72 setProduct(product, decimalAccuracy, unitPrice) {
73 this.product = {
74 name: product.display_name || _t("Unnamed Product"),
75 unitOfMeasure: product.product_tmpl_id?.uom_id?.name || "kg",
76 decimalAccuracy,
77 unitPrice,
78 };
79 }
80
81 _setTareIfRequested() {
82 if (this.tareRequested) {
83 this.tare = this.weight;
84 this.tareRequested = false;
85 }
86 }
87
88 _clearLastWeightIfValid() {
89 if (this.lastWeight && this.isWeightValid) {
90 this.lastWeight = null;
91 }
92 }
93
94 requestTare() {
95 this.tareRequested = true;
96 if (this.isManualMeasurement && !this.loading) {
97 this.readWeight();
98 } else {
99 setTimeout(() => this._setTareIfRequested(), TARE_TIMEOUT_MS);
100 }
101 }
102
103 get isWeightValid() {
104 // LNE requires that the weight changes from the previously
105 // added value before another product is allowed to be added.
106 return (
107 !this.lastWeight ||
108 roundDecimals(this.weight, this.product.decimalAccuracy) !==
109 roundDecimals(this.lastWeight, this.product.decimalAccuracy)
110 );
111 }
112
113 get isManualMeasurement() {
114 // In Community we don't know anything about the connected scale,
115 // so we assume automatic measurement.
116 return false;
117 }
118
119 get netWeight() {
120 return roundDecimals(this.weight - (this.tare || 0), this.product.decimalAccuracy);
121 }
122
123 get netWeightString() {
124 const weightString = formatFloat(this.netWeight, {
125 digits: [0, this.product.decimalAccuracy],
126 });
127 return `${weightString} ${this.product.unitOfMeasure}`;
128 }
129
130 get tareWeightString() {
131 const weightString = formatFloat(this.tare || 0, {
132 digits: [0, this.product.decimalAccuracy],
133 });
134 return `${weightString} ${this.product.unitOfMeasure}`;
135 }
136
137 get grossWeightString() {
138 const weightString = formatFloat(this.weight, {
139 digits: [0, this.product.decimalAccuracy],
140 });
141 return `${weightString} ${this.product.unitOfMeasure}`;
142 }
143
144 get unitPriceString() {
145 const priceString = this.env.utils.formatCurrency(this.product.unitPrice);
146 return `${priceString} / ${this.product.unitOfMeasure}`;
147 }
148
149 get totalPriceString() {
150 const priceString = this.env.utils.formatCurrency(this.netWeight * this.product.unitPrice);
151 return priceString;
152 }
153
154 _checkScaleIsConnected() {
155 if (this.hardwareProxy.connectionInfo.status !== "connected") {
156 throw new Error(_t("Cannot weigh product - IoT Box is disconnected"));
157 }
158 if (this.hardwareProxy.connectionInfo.drivers.scale?.status !== "connected") {
159 throw new Error(_t("Cannot weigh product - Scale is not connected to IoT Box"));
160 }
161 }
162 }
163
164 export const posScaleService = {
165 dependencies: ["hardware_proxy"],
166 start(env, deps) {
167 return new PosScaleService(env, deps);
168 },
169 };
170
171 registry.category("services").add("pos_scale", posScaleService);