Skip to content

Commit

Permalink
[FIX] hw_drivers: Read weight from IoT Scale
Browse files Browse the repository at this point in the history
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#46411

Signed-off-by: Quentin Lejeune (qle) <[email protected]>
  • Loading branch information
aprieels committed Feb 27, 2020
1 parent a0e2afd commit a08c515
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions addons/hw_drivers/drivers/SerialScaleDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def scale_read(self):

class ScaleDriver(SerialDriver):
"""Abstract base class for scale drivers."""
last_sent_value = None

def __init__(self, device):
self._device_type = 'scale'
Expand Down Expand Up @@ -145,6 +146,7 @@ def _read_once_action(self, data):
"""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):
Expand Down Expand Up @@ -183,10 +185,11 @@ def _read_weight(self):
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):
Expand All @@ -200,7 +203,8 @@ def _take_measure(self):

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)


Expand Down Expand Up @@ -274,7 +278,8 @@ def _take_measure(self):
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)
Expand Down

0 comments on commit a08c515

Please sign in to comment.