Esempio n. 1
0
def parse_shelves_from_message(message) -> List[Shelf]:
    shelf_json_list: List[Any] = message["shelves"]

    shelves: List[Shelf] = [
        Shelf.from_json(shelf_json) for shelf_json in shelf_json_list
    ]
    return shelves
    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 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)
Esempio n. 4
0
def read_shelves_in_rack(conn, rack_id: int) -> List[Shelf]:
    sql = "SELECT shelf_id, rack_id FROM shelves WHERE rack_id=%s"
    with conn.cursor() as cursor:
        cursor.execute(sql, (rack_id))
        all_shelves = cursor.fetchall()
        shelves = [
            Shelf(shelf_id, rack_id) for (shelf_id, rack_id) in all_shelves
        ]
        cursor.close()
        return shelves
Esempio n. 5
0
def read_all_shelves(conn) -> List[Shelf]:
    sql = "SELECT shelf_id, rack_id FROM shelves"
    with conn.cursor() as cursor:
        cursor.execute(sql)
        all_shelves = cursor.fetchall()
        shelves = [
            Shelf(shelf_id, rack_id) for (shelf_id, rack_id) in all_shelves
        ]
        cursor.close()
        return shelves
Esempio n. 6
0
    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_shelf(sio, rack, recipe):
    shelf_dict = {
        "shelf": {
            "shelf_id": 1,
            "rack_id": rack.rack_id,
        },
    }
    sio.emit("message_sent", shelf_dict)
    sio.sleep(1)
    expected = Shelf.from_json(shelf_dict["shelf"])

    return expected
def _test_send_shelf(sio, rack):
    shelf_dict = {
        "shelf": {
            "shelf_id": 1,
            "rack_id": rack.rack_id,
            "room_id": rack.room_id,
        },
        NAMESPACE: INTEGRATION_NAMESPACE,
    }
    sio.emit("create_object", shelf_dict)
    sio.sleep(1)
    expected = Shelf.from_json(shelf_dict["shelf"])

    return expected
Esempio n. 9
0
    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},
        )
Esempio n. 10
0
def validate_all_shelves_free(app_config: AppConfig,
                              planned_shelves: List[Shelf]) -> List[Shelf]:
    # find all current grows and the shelves used to grow them
    all_current_grows: List[Grow] = app_config.db.read_current_grows()
    for g in all_current_grows:
        assert g.grow_id != None
    all_grow_ids: List[Optional[int]] = [g.grow_id for g in all_current_grows]
    all_current_used_shelf_grows: List[
        ShelfGrow] = app_config.db.read_shelves_with_grows(all_grow_ids)

    # verify that none of the shelf grows are the same as the shelf grows currently in use

    # convert the shelf grows to shelves so we can ignore the grow_id attribute
    current_shelves_dict: Dict[Shelf, bool] = {
        Shelf(sg.shelf_id, sg.room_id, sg.rack_id): True
        for sg in all_current_used_shelf_grows
    }

    in_use_shelves: List[Shelf] = []
    for shelf in planned_shelves:
        if shelf in current_shelves_dict:
            in_use_shelves.append(shelf)

    return in_use_shelves
Esempio n. 11
0
def test__eq__fail():
    shelf = Shelf(1, 2, 3)
    shelf2 = Shelf(4, 5, 6)
    assert not shelf == shelf2
Esempio n. 12
0
def test__eq__():
    shelf = Shelf(1, 2, 3)
    shelf2 = Shelf(1, 2, 3)
    assert shelf == shelf2
Esempio n. 13
0
def test__hash__fail():
    shelf = Shelf(1, 2, 3)
    shelf2 = Shelf(12, 6, 4)
    assert not hash(shelf) == hash(shelf2)
Esempio n. 14
0
def test__hash__():
    shelf = Shelf(1, 2, 3)
    shelf2 = Shelf(1, 2, 3)
    assert hash(shelf2) == hash(shelf)
Esempio n. 15
0
def test_create_shelf():
    shelf = Shelf(1, 2, 3)

    assert shelf.shelf_id == 1
    assert shelf.room_id == 2
    assert shelf.rack_id == 3
Esempio n. 16
0
def test_create_shelf():
    shelf = Shelf(1, 2)

    assert shelf.shelf_id == 1
    assert shelf.rack_id == 2
Esempio n. 17
0
def test_create_shelf_from_json():
    shelf = Shelf.from_json({"shelf_id": 1, "room_id": 2, "rack_id": 3})

    assert shelf.shelf_id == 1
    assert shelf.room_id == 2
    assert shelf.rack_id == 3
Esempio n. 18
0
def mock_shelf():
    return Shelf.from_json({
        "shelf_id": 1,
        "rack_id": 2,
    })
Esempio n. 19
0
def test_to_json():
    shelf = Shelf(1, 2, 3)
    assert shelf.to_json() == {"shelf_id": 1, "room_id": 2, "rack_id": 3}
Esempio n. 20
0
def test__str__():
    shelf = Shelf(1, 2, 3)
    shelf2 = Shelf(1, 2, 3)
    assert str(shelf2) == str(shelf)