def find_room_listener(message) -> None: print("got message:", message) assert "room" in message assert message["room"] is not None returned_room = Room.from_json(message["room"]) expected_room = Room.from_json(room_dict["room"]) print("returned_room:", returned_room, "expected_room:", expected_room) assert returned_room == expected_room flag.append(True)
def find_all_rooms_listener(message) -> None: found_rooms = message["rooms"] for fr in found_rooms: rooms.append(Room.from_json(fr)) room_map = {room.room_id: room for room in rooms} first_id = room_dict["room"]["room_id"] second_id = room_dict2["room"]["room_id"] assert room_map[first_id] == Room.from_json(room_dict["room"]) assert room_map[second_id] == Room.from_json(room_dict2["room"])
def mock_room(): return Room.from_json({ "room_id": 1, "is_on": False, "is_veg_room": True, "brightness": 0 })
def return_all_entities_listener(message) -> None: print("Received message in entities_listener:", message) assert "rooms" in message assert "racks" in message assert "shelves" in message assert "grows" in message assert "recipes" in message assert "recipe_phases" in message found_rooms = [Room.from_json(r) for r in message["rooms"]] found_racks = [Rack.from_json(r) for r in message["racks"]] found_shelves = [Shelf.from_json(s) for s in message["shelves"]] found_grows = [Grow.from_json(g) for g in message["grows"]] found_recipes = [Recipe.from_json(r) for r in message["recipes"]] found_recipe_phases = [ RecipePhase.from_json(rp) for rp in message["recipe_phases"] ] assert collections.Counter(found_rooms) == collections.Counter(rooms) assert collections.Counter(found_racks) == collections.Counter(racks) assert collections.Counter(found_shelves) == collections.Counter( shelves) assert collections.Counter(found_grows) == collections.Counter(grows) assert collections.Counter(found_recipes) == collections.Counter( recipes) assert collections.Counter(found_recipe_phases) == collections.Counter( recipe_phases) flag.append(True)
def test_create_room_from_json(): room = Room.from_json({"room_id": 1}) assert room.room_id == 1 assert room.is_on == False # auto-initialized to False if not present assert room.is_veg_room == False # auto-initialized to False if not present assert room.brightness == None # auto-initialized to None
def return_all_entities_listener(message) -> None: message_dict = json.loads(message) print("Received message in entities_listener:", message_dict) assert "rooms" in message_dict assert "racks" in message_dict assert "shelves" in message_dict assert "grows" in message_dict assert "grow_phases" in message_dict assert "recipes" in message_dict assert "recipe_phases" in message_dict found_rooms = [Room.from_json(r) for r in message_dict["rooms"]] found_racks = [Rack.from_json(r) for r in message_dict["racks"]] found_shelves = [Shelf.from_json(s) for s in message_dict["shelves"]] found_grows = [Grow.from_json(g) for g in message_dict["grows"]] found_grow_phases = [ GrowPhase.from_json(g) for g in message_dict["grow_phases"] ] found_recipes = [Recipe.from_json(r) for r in message_dict["recipes"]] found_recipe_phases = [ RecipePhase.from_json(rp) for rp in message_dict["recipe_phases"] ] assert collections.Counter(found_rooms) == collections.Counter(rooms) assert collections.Counter(found_racks) == collections.Counter(racks) assert collections.Counter(found_shelves) == collections.Counter( shelves ) assert len(found_grows) > 0 x = len(found_grows) assert ( found_grows[x - 1].start_datetime.strftime("%Y-%m-%d %H:%M:%S") == start ) assert ( found_grows[x - 1].estimated_end_datetime.strftime( "%Y-%m-%d %H:%M:%S" ) == end ) i = len(found_grow_phases) assert ( found_grow_phases[i - 1].phase_start_datetime.strftime( "%Y-%m-%d %H:%M:%S" ) == start ) assert ( found_grow_phases[i - 1].phase_end_datetime.strftime( "%Y-%m-%d %H:%M:%S" ) == end ) j = len(found_recipes) assert found_recipe_phases[j - 1].power_level == p_level assert found_recipe_phases[j - 1].red_level == r_level assert found_recipe_phases[j - 1].blue_level == b_level flag.append(True) grow.append(found_grows[0].grow_id)
def message_sent(message): logging.debug("message sent:", message) entities_processed = [] print("message:", message) if "room" in message: # a room is contained in this update entities_processed.append("room") room_json = message["room"] room = Room.from_json(room_json) app_config.logger.debug(room) print("Saw room in message") app_config.db.write_room(room) if "rack" in message: # a rack is contained in this update entities_processed.append("rack") rack_json = message["rack"] rack = Rack.from_json(rack_json) app_config.logger.debug(rack) print("Saw rack in message") app_config.db.write_rack(rack) if "recipe" in message: # a recipe is contained in this update entities_processed.append("recipe") recipe_json = message["recipe"] recipe = Recipe.from_json(recipe_json) app_config.logger.debug(recipe) print("Saw recipe in message") app_config.db.write_recipe(recipe) if "shelf" in message: # a shelf is contained in this update entities_processed.append("shelf") shelf_json = message["shelf"] shelf = Shelf.from_json(shelf_json) app_config.logger.debug(shelf) print("Saw shelf in message") app_config.db.write_shelf(shelf) if "plant" in message: # a plant is contained in this update entities_processed.append("plant") plant_json = message["plant"] plant = Plant.from_json(plant_json) app_config.logger.debug(plant) print("Saw plant in message") app_config.db.write_plant(plant) send_message_to_namespace_if_specified( socketio, message, "message_received", {"processed": entities_processed})
def _test_send_room(sio): # send initial room update room_dict = { "room": { "room_id": 1, "is_on": False, "is_veg_room": True, "brightness": 5, } } sio.emit("message_sent", room_dict) sio.sleep(1) return [Room.from_json(room_dict["room"])]
def _test_send_room(sio): # send initial room update room_dict = { "room": { "room_id": 1, "is_on": False, "is_veg_room": True, "brightness": 5, }, NAMESPACE: INTEGRATION_NAMESPACE, } sio.emit("create_object", room_dict) sio.sleep(1) return [Room.from_json(room_dict["room"])]
def create_object(message): entities_processed = [] print("received message:", message) if "room" in message: # a room is contained in this update entities_processed.append("room") room_json = message["room"] room = Room.from_json(room_json) print("Saw room in message", room) app_config.db.write_room(room) if "rack" in message: # a rack is contained in this update entities_processed.append("rack") rack_json = message["rack"] rack = Rack.from_json(rack_json) print("Saw rack in message", rack) app_config.db.write_rack(rack) if "recipe" in message: # a recipe is contained in this update entities_processed.append("recipe") recipe_json = message["recipe"] recipe = Recipe.from_json(recipe_json) print("Saw recipe in message", recipe) app_config.db.write_recipe(recipe) if "shelf" in message: # a shelf is contained in this update entities_processed.append("shelf") shelf_json = message["shelf"] shelf = Shelf.from_json(shelf_json) print("Saw shelf in message", shelf) app_config.db.write_shelf(shelf) send_message_to_namespace_if_specified( socketio, message, "create_object_success", {"processed": entities_processed}, )
def _test_send_room(sio): # send initial room update room_dict = { "room": { "room_id": 1, "is_on": False, "is_veg_room": True, "brightness": 5 } } sio.emit("message_sent", room_dict) sio.sleep(1) flag = [] @sio.on("return_room") def find_room_listener(message) -> None: print("got message:", message) assert "room" in message assert message["room"] is not None returned_room = Room.from_json(message["room"]) expected_room = Room.from_json(room_dict["room"]) print("returned_room:", returned_room, "expected_room:", expected_room) assert returned_room == expected_room flag.append(True) sio.emit("read_room", room_dict) wait_for_event(sio, flag, 1, 5, "test_send_room.create_room") print("first room found and returned") # update same room to on room_dict["room"]["is_on"] = not room_dict["room"]["is_on"] sio.emit("message_sent", room_dict) sio.emit("read_room", room_dict) wait_for_event(sio, flag, 2, 5, "test_send_room.update_room") print("second room read and updated") # create new room room_dict2 = { "room": { "room_id": 2, "is_on": False, "is_veg_room": True, "brightness": 80 }, } sio.emit("message_sent", room_dict2) sio.sleep(1) # return both rooms rooms = [] @sio.on("return_rooms") def find_all_rooms_listener(message) -> None: found_rooms = message["rooms"] for fr in found_rooms: rooms.append(Room.from_json(fr)) room_map = {room.room_id: room for room in rooms} first_id = room_dict["room"]["room_id"] second_id = room_dict2["room"]["room_id"] assert room_map[first_id] == Room.from_json(room_dict["room"]) assert room_map[second_id] == Room.from_json(room_dict2["room"]) sio.emit("read_all_rooms", {}) wait_for_event(sio, rooms, 2, 5, "test_send_room.read_rooms") print("read all rooms") return [ Room.from_json(room_dict["room"]), Room.from_json(room_dict2["room"]) ]