Skip to content

Commit 47b6814

Browse files
committed
trying to add weight system
1 parent 24017d2 commit 47b6814

File tree

2 files changed

+104
-9
lines changed

2 files changed

+104
-9
lines changed

weight_predict/stock.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"30:C6:F7:2A:0B:D0": [
3+
[
4+
{
5+
"id": 1,
6+
"quantity": 2
7+
}
8+
],
9+
[
10+
{
11+
"id": 1,
12+
"quantity": 2
13+
}
14+
],
15+
[
16+
{
17+
"id": 1,
18+
"quantity": 2
19+
}
20+
],
21+
[
22+
{
23+
"id": 1,
24+
"quantity": 2
25+
}
26+
]
27+
],
28+
"78:21:84:8E:83:28": [
29+
[
30+
{
31+
"id": 1,
32+
"quantity": 2
33+
}
34+
],
35+
[
36+
{
37+
"id": 1,
38+
"quantity": 2
39+
}
40+
],
41+
[
42+
{
43+
"id": 1,
44+
"quantity": 2
45+
}
46+
],
47+
[
48+
{
49+
"id": 1,
50+
"quantity": 2
51+
}
52+
]
53+
]
54+
}

weight_predict/weight_predict.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import database as db
88
import pandas as pd
99
import time
10+
from pathlib import Path
1011

1112
MAX_ITEM_REMOVALS_TO_CHECK = 3
1213
THRESHOLD_WEIGHT_PROBABILITY = 30
@@ -19,12 +20,14 @@ class Slot:
1920
_items: List[Item]
2021
_items_by_id: Dict[int, Item]
2122

22-
def __init__(self, starting_value: float = 0.0):
23-
self._previous_weight_value = starting_value
23+
def __init__(self, items: List[Item] = None):
2424
self._items = list()
25-
self._items = db.get_items()
26-
self._items_by_id = dict()
2725

26+
if items is not None:
27+
for item in items:
28+
self._items.append(item)
29+
30+
self._items_by_id = dict()
2831
for item in self._items:
2932
self._items_by_id[item.item_id] = item
3033

@@ -45,7 +48,7 @@ def predict_most_likely_item(self, weight_delta: float) -> List[Item]:
4548
# Store probabilities
4649
probabilities[item.item_id] = []
4750
# Iterate through all possible quantities
48-
for potential_quantity in range(1, MAX_ITEM_REMOVALS_TO_CHECK + 1):
51+
for potential_quantity in range(1, item.quantity + 1):
4952
expected_weight = item.avg_weight * potential_quantity
5053
scaled_std = item.std_weight * (potential_quantity ** 0.5)
5154

@@ -106,20 +109,57 @@ def predict_most_likely_item(self, weight_delta: float) -> List[Item]:
106109

107110
class Shelf:
108111

109-
_mac_address: str
112+
_mac_address: bytes
110113
slots: list[Slot]
111114

112-
def __init__(self, mac_address: str, num_slots = 4):
115+
def __init__(self, mac_address: bytes, slots: List[Slot] = None):
113116
self._mac_address = mac_address
114117
self.slots = list()
115118

116-
for i in range(num_slots):
117-
self.slots.append(Slot())
119+
if slots is not None:
120+
for slot in slots:
121+
self.slots.append(slot)
122+
123+
124+
def load_stock_file(file_path: Path = Path("stock.json")):
125+
126+
# Open stock file
127+
with open(file_path) as file:
128+
json_data = json.load(file)
129+
130+
slots = list()
131+
shelves = list()
132+
# Iterate through each shelf
133+
for shelf_mac in json_data:
134+
# Iterate through each slot
135+
for slot_i, json_slot in enumerate(json_data[shelf_mac]):
136+
137+
# Load in all items
138+
items = list()
139+
for json_item in json_slot:
140+
item = db.get_item(item_id=json_item["id"])
141+
item.quantity = json_item["quantity"]
142+
print("Loaded data from stock store")
143+
144+
# Create slot with items
145+
slot = Slot(items=items)
146+
slots.append(slot)
147+
# Create shelf with slots
148+
149+
shelf = Shelf(shelf_mac, slots=slots)
150+
shelves.append(shelf)
151+
return shelves
152+
153+
118154

119155

120156
def main():
121157

158+
# Create dictionary of known shelves
122159
mac_address_to_shelves = dict()
160+
known_shelves = load_stock_file()
161+
for shelf in known_shelves:
162+
mac_address_to_shelves[shelf._mac_address] = shelf
123163

124164
# Set up uart ports
125165
esp_uart_port = serial.Serial(
@@ -186,6 +226,7 @@ def main():
186226

187227
else:
188228
# New shelf
229+
print("Unknown shelf joined")
189230
shelf = Shelf(mac_address)
190231
mac_address_to_shelves[mac_address] = shelf
191232

0 commit comments

Comments
 (0)