Skip to content

Commit 53125cf

Browse files
committed
Adopt new weight delta format from ESPs
1 parent 7d2e7fd commit 53125cf

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed

weight_predict/weight_predict.py

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
class Slot:
1818

19-
_previous_weight_value: float
2019
_items: List[Item]
2120
_items_by_id: Dict[int, Item]
2221

@@ -30,13 +29,12 @@ def __init__(self, starting_value: float = 0.0):
3029
self._items_by_id[item.item_id] = item
3130

3231

33-
def predict_most_likely_item(self, new_weight: float) -> List[Item]:
32+
def predict_most_likely_item(self, weight_delta: float) -> List[Item]:
3433
"""
3534
Given a change in weight, predict the most likely item that could have been added/removed from the scale.
36-
:param new_weight: float new weight in grams
35+
:param weight_delta: float new weight in grams
3736
:return: Tuple: Item, Float; The item that is most likely
3837
"""
39-
weight_delta = new_weight - self._previous_weight_value
4038
direction = 1 if weight_delta > 0 else -1
4139
abs_weight_delta = abs(weight_delta)
4240

@@ -102,8 +100,7 @@ def predict_most_likely_item(self, new_weight: float) -> List[Item]:
102100
existing_item_obj.vision_class
103101
)
104102
)
105-
# Return resulting items, where the quantity matches the number predicted to be added/removed from the scale.
106-
self._previous_weight_value = new_weight
103+
# Return resulting items, where the quantity matches the number predicted to be added/removed from the scale.
107104
return items
108105

109106

@@ -112,13 +109,12 @@ class Shelf:
112109
_mac_address: str
113110
slots: list[Slot]
114111

115-
def __init__(self, mac_address: str, starting_slot_values: List[float]):
112+
def __init__(self, mac_address: str, num_slots = 4):
116113
self._mac_address = mac_address
117114
self.slots = list()
118115

119-
for i in range(len(starting_slot_values)):
120-
121-
self.slots.append(Slot(starting_value=starting_slot_values[i]))
116+
for i in range(num_slots):
117+
self.slots.append(Slot())
122118

123119

124120
def main():
@@ -159,37 +155,38 @@ def main():
159155
continue
160156

161157
# Check that json has necessary fields
162-
if not 'mac_address' in json_data or not 'slot_weights_g' in json_data:
163-
print("JSON does not have mac_address or slot_weights_g")
158+
if not 'shelf_mac' in json_data or not 'slot_id' in json_data or 'delta_g' not in json_data:
159+
print("JSON does not have shelf_mac, or slot_id, or delta_g")
164160
continue
165161

166-
mac_address = json_data['mac_address']
162+
mac_address = json_data['shelf_mac']
167163
if mac_address in mac_address_to_shelves:
168164

169165
shelf = mac_address_to_shelves[mac_address]
166+
slot = shelf.slots[json_data['slot_id']]
167+
168+
item_changes = slot.predict_most_likely_item(json_data['delta_g'])
169+
170+
for item_change in item_changes:
171+
# Construct out JSON
172+
json_data = {
173+
'id': item_change.item_id,
174+
'quantity': -item_change.quantity # Note that UI expects positive = add to cart, negative = remove from cart
175+
}
176+
json_str = json.dumps(json_data) + "\n"
170177

171-
for i, new_weight in enumerate(json_data['slot_weights_g']):
172-
item_changes = shelf.slots[i].predict_most_likely_item(new_weight)
173-
for item_change in item_changes:
174-
# Construct out JSON
175-
json_data = {
176-
'id': item_change.item_id,
177-
'quantity': -item_change.quantity # Note that UI expects positive = add to cart, negative = remove from cart
178-
}
179-
json_str = json.dumps(json_data) + "\n"
180-
181-
# Send it to pi
182-
pi_uart_port.write(json_str.encode('utf-8'))
183-
184-
time_str = time.strftime("%H:%M:%S.") + f"{int((time.time() * 1000) % 1000):03d}"
185-
if item_change.quantity > 0:
186-
print(f"{time_str}: Remove {abs(item_change.quantity)} {item_change.name} from cart")
187-
else:
188-
print(f"{time_str}: Add {abs(item_change.quantity)} {item_change.name} to cart")
178+
# Send it to pi
179+
pi_uart_port.write(json_str.encode('utf-8'))
180+
181+
time_str = time.strftime("%H:%M:%S.") + f"{int((time.time() * 1000) % 1000):03d}"
182+
if item_change.quantity > 0:
183+
print(f"{time_str}: Remove {abs(item_change.quantity)} {item_change.name} from cart")
184+
else:
185+
print(f"{time_str}: Add {abs(item_change.quantity)} {item_change.name} to cart")
189186

190187
else:
191-
# New shelf, just save this as the previous slot data
192-
shelf = Shelf(mac_address, json_data['slot_weights_g'])
188+
# New shelf
189+
shelf = Shelf(mac_address)
193190
mac_address_to_shelves[mac_address] = shelf
194191

195192

0 commit comments

Comments
 (0)