Exemple #1
0
async def test_mqtt_client_schedule_legacy_without_version(mqtt_client: MQTTClient):
    from feeder.util.feeder import generate_feeder_hid
    from feeder.database.models import (
        KronosGateways,
        KronosDevices,
        Pet,
        FeedingSchedule,
    )
    from tests.test_database_models import SAMPLE_DEVICE, SAMPLE_GATEWAY

    # We need a legacy device to test the old scheduling API logic.
    # The default device created by "with_registered_device" is set to 2.8.0
    legacy_gateway = {
        **SAMPLE_GATEWAY,
        "softwareVersion": None,
        "uid": "smartfeeder-895ae773737d",
    }
    legacy_gateway_hid = generate_feeder_hid("smartfeeder-895ae773737d")
    await KronosGateways.create(**legacy_gateway)

    legacy_device = {
        **SAMPLE_DEVICE,
        "softwareVersion": None,
        "uid": "smartfeeder-895ae773737d-prod",
        "gatewayHid": legacy_gateway_hid,
    }
    await KronosDevices.create(**legacy_device)
    legacy_device_hid = generate_feeder_hid("smartfeeder-895ae773737d-prod")

    legacy_pet = await Pet.create(
        name="legacy",
        animal_type="dog",
        weight=1,
        birthday=1,
        activity_level=1,
        device_hid=legacy_device_hid,
    )

    await FeedingSchedule.create_event(
        pet_id=legacy_pet, name="legacy", time=3600, portion=0.0625
    )

    legacy_schedule = await FeedingSchedule.get_for_pet(pet_id=legacy_pet)
    await mqtt_client.send_cmd_schedule(
        legacy_gateway_hid, legacy_device_hid, events=legacy_schedule
    )

    message = await mqtt_client.deliver_message()
    packet = message.publish_packet
    assert packet.variable_header.topic_name == f"krs/cmd/stg/{legacy_gateway_hid}"
    payload = json.loads(packet.payload.data)
    assert payload["parameters"] == {
        "deviceHid": legacy_device_hid,
        "command": "schedule",
        "payload": (
            '[{"active": true, "automatic": true, '
            '"feeding_id": "93c346ad8d668ef6_feed1_1:00AM", "name": "FEED0", '
            '"portion": 0.0625, "reminder": true, "time": 3600}]'
        ),
    }
async def add_gateway(gateway: NewGateway):
    gateway_hid = generate_feeder_hid(gateway.uid)
    try:
        await KronosGateways.create(**gateway.dict())
        content = {"hid": gateway_hid, "message": "OK"}
    except IntegrityError:
        logger.debug("Gateway (%s) already registered!", gateway_hid)
        content = {
            "hid": gateway_hid,
            "message": "gateway is already registered"
        }
        return JSONResponse(content=content, headers=kronos_headers)

    return JSONResponse(content=content, headers=kronos_headers)
async def register_feeder(device: NewDevice):
    # Generate the feeder device HID
    device_hid = generate_feeder_hid(device.uid)

    try:
        await KronosDevices.create(**device.dict())
    except IntegrityError:
        logger.debug("Device (%s) already registered!", device_hid)

    content = {
        "hid": device_hid,
        "links": {},
        "message": "device is already registered",
        "pri": f"arw:krn:dev:{device_hid}",
    }
    return JSONResponse(content=content, headers=kronos_headers)
Exemple #4
0
def test_generate_feeder_hid():
    from feeder.util.feeder import generate_feeder_hid

    uid = "smartfeeder-1337f33d-123456"
    hid = generate_feeder_hid(uid)
    assert hid == "d9eae8344910399224153df53f12400ceb8c5707"
def handle_potential_registration(model: dict):
    if "hid" not in model and "uid" in model:
        model["hid"] = generate_feeder_hid(model["uid"])
    if "discoveredAt" not in model:
        model["discoveredAt"] = get_current_timestamp()
    return model