[FIX] hw_drivers: Read weight from IoT Scale

We had two issues when reading the weight from a scale connected to
the IoT Box :

When using the '/hw_proxy/scale_read' route, if the same weight was
read twice, the value in PoS was reset to 0.
If the product was placed on the scale before opening the weight
screen, the weight was never read.

closes odoo/odoo#46432

X-original-commit: a08c5157f0b042e2ee250fc7b431b90451c7781a
Signed-off-by: Antoine Prieëls <aprieels@users.noreply.github.com>
This commit is contained in:
Antoine Prieels
2020-02-27 12:36:27 +00:00
parent 888cf9a5eb
commit cad11eed98
+11 -6
View File
@@ -93,6 +93,7 @@ class ScaleReadOldRoute(http.Controller):
class ScaleDriver(SerialDriver):
"""Abstract base class for scale drivers."""
last_sent_value = None
def __init__(self, device):
self._device_type = 'scale'
@@ -145,6 +146,7 @@ class ScaleDriver(SerialDriver):
"""Reads the scale current weight value and pushes it to the frontend."""
self._read_weight()
self.last_sent_value = self.data['value']
event_manager.device_changed(self)
def _set_zero_action(self, data):
@@ -183,10 +185,11 @@ class ScaleDriver(SerialDriver):
self._connection.write(protocol.measureCommand + protocol.commandTerminator)
answer = self._get_raw_response(self._connection)
match = re.search(self._protocol.measureRegexp, answer)
self.data = {
'value': float(match.group(1)) if match else None,
'status': self._status
}
if match:
self.data = {
'value': float(match.group(1)),
'status': self._status
}
# Ensures compatibility with older versions of Odoo
def _scale_read_old_route(self):
@@ -200,7 +203,8 @@ class ScaleDriver(SerialDriver):
with self._device_lock:
self._read_weight()
if self.data['value'] is not None or self._status['status'] == self.STATUS_ERROR:
if self.data['value'] != self.last_sent_value or self._status['status'] == self.STATUS_ERROR:
self.last_sent_value = self.data['value']
event_manager.device_changed(self)
@@ -274,7 +278,8 @@ class AdamEquipmentDriver(ScaleDriver):
with self._device_lock:
self._read_weight()
self._check_last_weight_time()
if self.data['value'] is not None or self._status['status'] == self.STATUS_ERROR:
if self.data['value'] != self.last_sent_value or self._status['status'] == self.STATUS_ERROR:
self.last_sent_value = self.data['value']
event_manager.device_changed(self)
else:
time.sleep(0.5)