@@ -136,37 +136,34 @@ def main():
136136 if not line :
137137 continue
138138
139- # Read weight data from ESP if available
140- if esp_uart_port .in_waiting > 0 :
141- data = esp_uart_port .read (esp_uart_port .in_waiting ).decode ('utf-8' , errors = 'ignore' ).strip ()
142- try :
143- json_data = json .loads (data )
144- except json .JSONDecodeError :
145- print ("Unable to decode JSON" , data )
146- continue
147-
148- # Check that json has necessary fields
149- if not 'mac_address' in json_data or not 'slot_weights_g' in json_data :
150- print ("JSON does not have mac_address or slot_weights_g" )
151- continue
152-
153- mac_address = json_data ['mac_address' ]
154- if mac_address in mac_address_to_shelves :
155-
156- shelf = mac_address_to_shelves [mac_address ]
157-
158- for i , new_weight in enumerate (json_data ['slot_weights_g' ]):
159- item_changes = shelf .slots [i ].predict_most_likely_item (new_weight )
160- for item_change in item_changes :
161- if item_change .quantity > 0 :
162- print (f"Remove { item_change .quantity } from cart" )
163- else :
164- print (f"Add { item_change .quantity } to cart" )
139+ try :
140+ json_data = json .loads (line )
141+ except json .JSONDecodeError :
142+ print ("Unable to decode JSON" , line )
143+ continue
165144
166- else :
167- # New shelf, just save this as the previous slot data
168- shelf = Shelf (mac_address , json_data ['slot_weights_g' ])
169- mac_address_to_shelves [mac_address ] = shelf
145+ # Check that json has necessary fields
146+ if not 'mac_address' in json_data or not 'slot_weights_g' in json_data :
147+ print ("JSON does not have mac_address or slot_weights_g" )
148+ continue
149+
150+ mac_address = json_data ['mac_address' ]
151+ if mac_address in mac_address_to_shelves :
152+
153+ shelf = mac_address_to_shelves [mac_address ]
154+
155+ for i , new_weight in enumerate (json_data ['slot_weights_g' ]):
156+ item_changes = shelf .slots [i ].predict_most_likely_item (new_weight )
157+ for item_change in item_changes :
158+ if item_change .quantity > 0 :
159+ print (f"Remove { item_change .quantity } from cart" )
160+ else :
161+ print (f"Add { item_change .quantity } to cart" )
162+
163+ else :
164+ # New shelf, just save this as the previous slot data
165+ shelf = Shelf (mac_address , json_data ['slot_weights_g' ])
166+ mac_address_to_shelves [mac_address ] = shelf
170167
171168
172169if __name__ == '__main__' :
0 commit comments