77import database as db
88import pandas as pd
99import time
10+ from pathlib import Path
1011
1112MAX_ITEM_REMOVALS_TO_CHECK = 3
1213THRESHOLD_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
107110class 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
120156def 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