def save_new_drink(self): print("Saved new Drink") name = self.setting_new_drink_frame_drink.get() print(name) ingredient_amounts = {} if (self.settings_drink_selected_one.get() != "Empty Slot") and (self.settings_slider_one.get() > 0): ingredient_amounts[self.settings_drink_selected_one.get( )] = self.settings_slider_one.get() if (self.settings_drink_selected_two.get() != "Empty Slot") and (self.settings_slider_two.get() > 0): ingredient_amounts[self.settings_drink_selected_two.get( )] = self.settings_slider_two.get() if (self.settings_drink_selected_three.get() != "Empty Slot") and (self.settings_slider_three.get() > 0): ingredient_amounts[self.settings_drink_selected_three.get( )] = self.settings_slider_three.get() if (self.settings_drink_selected_four.get() != "Empty Slot") and (self.settings_slider_four.get() > 0): ingredient_amounts[self.settings_drink_selected_four.get( )] = self.settings_slider_four.get() options = [] for key, value in ingredient_amounts.items(): options.append((key, value)) newRecipe = Recipe(name, options) State.add_recipe(newRecipe) jsonable_recipes = [ recipe.dict_obj() for name, recipe in State.recipes.items() ] self.message_handler.fireMessage(request_type="add_recipes", receiver='server', jsonable_obj=jsonable_recipes) self.full_update()
def process_request(self, data): request_sender = data[0] # Sender is first element of tuple request_type = data[1] # Request_type is second element of tuple request_data = data[2] # Data is third element of tuple, should already be json decoded print("received " + str(request_type)) print("from " + str(request_sender)) print("data " + str(request_data)) # Getter methods that return the current State values to the requester if request_type == "get_inventory": # Returns a JSON object of the slot information in the inventory dictionary self.message_handle.fireMessage(request_type="set_inventory",receiver=request_sender, jsonable_obj=State.inventory) elif request_type == "get_ingredients": # Returns a JSON object of the ingredients list self.message_handle.fireMessage(request_type="set_ingredients",receiver=request_sender, jsonable_obj=State.ingredients) elif request_type == "get_recipes": # Returns a JSON object of the recipes list # The recipe class is first converted into a dictionary jsonable_recipes = [recipe.dict_obj() for name, recipe in State.recipes.items()] self.message_handle.fireMessage(request_type="set_recipes", receiver=request_sender, jsonable_obj=jsonable_recipes) elif request_type == "make_drink": # request_data should contain a recipe_dict object self.send_drink_signals(request_data) # These should be the setter methods such as set_ingredients, set_inventory, set_recipes else: State.process_request(request_type, request_data)
def add_ingredient(self): print("Added drink ingredient") print(self.setting_new_drink_frame_ingredient.get()) State.add_ingredient(self.setting_new_drink_frame_ingredient.get()) self.message_handler.fireMessage(request_type="add_ingredients", receiver='server', jsonable_obj=State.ingredients) self.full_update()
def set_ingredient(self, dropdown, name, slot): dropdown.set(name) if (name == "Empty Slot"): State.set_inventory(slot, None) else: State.set_inventory(slot, name) self.message_handler.fireMessage(request_type="set_inventory", receiver='server', jsonable_obj=State.inventory) self.full_update()
def __init__(self): # Should load saved settings which are stored in the static State class State.load_state() # Start listener for drinks being sent to the pump self.DP = DrivePump() self.DP.start() self.message_handle = Message_Handler(localId='server', intendedReceiver='client') # Start listener for lcd requests self.LD = DriveLCD() self.LD.start() # Listen for requests and button inputs self.run()
def full_update(self): avalible_ingrediants = [] # Remove buttons/slders to start with self.home_drink_one.grid_remove() self.home_drink_two.grid_remove() self.home_drink_three.grid_remove() self.home_drink_four.grid_remove() self.home_slider_one.grid_remove() self.home_slider_two.grid_remove() self.home_slider_three.grid_remove() self.home_slider_four.grid_remove() # Update and re-add buttons/slders if necessary if (State.inventory["slot_one"] != None): self.home_drink_one.config(text=("Slot #1: " + State.inventory["slot_one"])) self.home_drink_one.grid() self.home_slider_one.config(label=State.inventory["slot_one"]) self.home_slider_one.grid() avalible_ingrediants.append(State.inventory["slot_one"]) if (State.inventory["slot_two"] != None): self.home_drink_two.config(text=("Slot #2: " + State.inventory["slot_two"])) self.home_drink_two.grid() self.home_slider_two.config(label=State.inventory["slot_two"]) self.home_slider_two.grid() avalible_ingrediants.append(State.inventory["slot_two"]) if (State.inventory["slot_three"] != None): self.home_drink_three.config(text=("Slot #3: " + State.inventory["slot_three"])) self.home_drink_three.grid() self.home_slider_three.config(label=State.inventory["slot_three"]) self.home_slider_three.grid() avalible_ingrediants.append(State.inventory["slot_three"]) if (State.inventory["slot_four"] != None): self.home_drink_four.config(text=("Slot #4: " + State.inventory["slot_four"])) self.home_drink_four.grid() self.home_slider_four.config(label=State.inventory["slot_four"]) self.home_slider_four.grid() avalible_ingrediants.append(State.inventory["slot_four"]) # Figure out which recipies are currently avalible self.home_drink_options = [] for recipe_name, recipe in State.recipes.items(): avalible = True for value in recipe.ingredients: if (value != "recipe_name"): if not (value in avalible_ingrediants): avalible = False if (avalible == True): self.home_drink_options.append(recipe.recipe_name) self.home_drink_selected.set("Select a drink") # Clear dropdown values self.home_drink_select["menu"].delete(0, 'end') # Add new values that call set selection for name in self.home_drink_options: self.home_drink_select["menu"].add_command( label=name, command=lambda name=name: self.set_selection(name)) # Setup drink setup drop downs self.settings_ingredient_select_one["menu"].delete(0, 'end') self.settings_ingredient_select_two["menu"].delete(0, 'end') self.settings_ingredient_select_three["menu"].delete(0, 'end') self.settings_ingredient_select_four["menu"].delete(0, 'end') for ingredient in State.ingredients: self.settings_ingredient_select_one["menu"].add_command( label=ingredient, command=lambda ingredient=ingredient: self.set_ingredient( self.settings_ingredient_selected_one, ingredient, "slot_one")) self.settings_ingredient_select_two["menu"].add_command( label=ingredient, command=lambda ingredient=ingredient: self.set_ingredient( self.settings_ingredient_selected_two, ingredient, "slot_two")) self.settings_ingredient_select_three["menu"].add_command( label=ingredient, command=lambda ingredient=ingredient: self.set_ingredient( self.settings_ingredient_selected_three, ingredient, "slot_three")) self.settings_ingredient_select_four["menu"].add_command( label=ingredient, command=lambda ingredient=ingredient: self.set_ingredient( self.settings_ingredient_selected_four, ingredient, "slot_four")) self.settings_ingredient_select_one["menu"].add_command( label="Empty Slot", command=lambda: self.set_ingredient( self.settings_ingredient_selected_one, "Empty Slot", "slot_one" )) self.settings_ingredient_select_two["menu"].add_command( label="Empty Slot", command=lambda: self.set_ingredient( self.settings_ingredient_selected_two, "Empty Slot", "slot_two" )) self.settings_ingredient_select_three["menu"].add_command( label="Empty Slot", command=lambda: self.set_ingredient( self.settings_ingredient_selected_three, "Empty Slot", "slot_three")) self.settings_ingredient_select_four["menu"].add_command( label="Empty Slot", command=lambda: self.set_ingredient( self.settings_ingredient_selected_four, "Empty Slot", "slot_four")) self.settings_ingredient_selected_one.set(State.inventory["slot_one"]) self.settings_ingredient_selected_two.set(State.inventory["slot_two"]) self.settings_ingredient_selected_three.set( State.inventory["slot_three"]) self.settings_ingredient_selected_four.set( State.inventory["slot_four"]) self.settings_drink_select_one["menu"].delete(0, 'end') self.settings_drink_select_two["menu"].delete(0, 'end') self.settings_drink_select_three["menu"].delete(0, 'end') self.settings_drink_select_four["menu"].delete(0, 'end') for ingredient in State.ingredients: self.settings_drink_select_one["menu"].add_command( label=ingredient, command=lambda ingredient= ingredient: self.set_recipe_ingredient( self.settings_drink_selected_one, ingredient)) self.settings_drink_select_two["menu"].add_command( label=ingredient, command=lambda ingredient= ingredient: self.set_recipe_ingredient( self.settings_drink_selected_two, ingredient)) self.settings_drink_select_three["menu"].add_command( label=ingredient, command=lambda ingredient= ingredient: self.set_recipe_ingredient( self.settings_drink_selected_three, ingredient)) self.settings_drink_select_four["menu"].add_command( label=ingredient, command=lambda ingredient= ingredient: self.set_recipe_ingredient( self.settings_drink_selected_four, ingredient)) self.settings_drink_select_one["menu"].add_command( label="Empty Slot", command=lambda: self.set_recipe_ingredient( self.settings_drink_selected_one, "Empty Slot")) self.settings_drink_select_two["menu"].add_command( label="Empty Slot", command=lambda: self.set_recipe_ingredient( self.settings_drink_selected_two, "Empty Slot")) self.settings_drink_select_three["menu"].add_command( label="Empty Slot", command=lambda: self.set_recipe_ingredient( self.settings_drink_selected_three, "Empty Slot")) self.settings_drink_select_four["menu"].add_command( label="Empty Slot", command=lambda: self.set_recipe_ingredient( self.settings_drink_selected_four, "Empty Slot")) print(State.to_string())
)] = self.settings_slider_one.get() if (self.settings_drink_selected_two.get() != "Empty Slot") and (self.settings_slider_two.get() > 0): ingredient_amounts[self.settings_drink_selected_two.get( )] = self.settings_slider_two.get() if (self.settings_drink_selected_three.get() != "Empty Slot") and (self.settings_slider_three.get() > 0): ingredient_amounts[self.settings_drink_selected_three.get( )] = self.settings_slider_three.get() if (self.settings_drink_selected_four.get() != "Empty Slot") and (self.settings_slider_four.get() > 0): ingredient_amounts[self.settings_drink_selected_four.get( )] = self.settings_slider_four.get() options = [] for key, value in ingredient_amounts.items(): options.append((key, value)) newRecipe = Recipe(name, options) State.add_recipe(newRecipe) jsonable_recipes = [ recipe.dict_obj() for name, recipe in State.recipes.items() ] self.message_handler.fireMessage(request_type="add_recipes", receiver='server', jsonable_obj=jsonable_recipes) self.full_update() if __name__ == "__main__": State.load_state() app = DrinkApp()
def message(self, pubnub, message): idValue = message.message.get('id') if (idValue == Message_Handler.local_id): # Add tuple of format (sender, request_type, data) to message_queue, The sender parameter allows # us to send information back. Message_Handler.message_queue.append( (message.message.get('sender'), message.message.get('request_type'), json.loads(message.message.get('data'))) ) if __name__ == "__main__": ''' This is a test to verify that sending State over pubnub is working. Assumed setup: Drink_server is running with name 'server'. State is currently cleared for both this client and server. ''' mh = Message_Handler("client", "server") State.clear_state() print("Cleared state") print(State.to_string()) print("Populating State") State.add_recipe(Recipe("Apple Juice", [("Water",5),("Apple",2)])) State.add_ingredient("Pineapple Juice") State.set_inventory("slot_one", "Orange Juice") print(State.to_string()) print("Sending State to server") mh.fireMessage(request_type="add_ingredients",receiver='server', jsonable_obj=State.ingredients) jsonable_recipes = [recipe.dict_obj() for name, recipe in State.recipes.items()] mh.fireMessage(request_type="add_recipes", receiver='server', jsonable_obj=jsonable_recipes) mh.fireMessage(request_type="set_inventory", receiver='server', jsonable_obj=State.inventory)