async def ping(cls, *, gateway_hid, device_hid): await KronosDevices.get_or_insert(gateway_hid=gateway_hid, device_hid=device_hid) query = devices.update().where(devices.c.hid == device_hid).values( lastPingedAt=get_current_timestamp()) results = await db.execute(query) return results
async def report(cls, *, gateway_hid: str, device_hid: str, voltage: float, usb_power: bool, charging: bool, ir: bool, rssi: int): sensors = { "timestamp": get_current_timestamp(), "voltage": voltage, "usb_power": usb_power, "charging": charging, "ir": ir, "rssi": rssi } query_last_report = sensor_data.select().where( sensor_data.c.device_hid == device_hid) last_report = await db.fetch_all(query_last_report) if last_report: query = sensor_data.update().where( sensor_data.c.device_hid == device_hid).values(**sensors) else: # We may have never seen this device before, so make sure the device exists. device = await KronosDevices.get_or_insert(device_hid=device_hid, gateway_hid=gateway_hid) query = sensor_data.insert().values(device_hid=device.hid, **sensors) results = await db.execute(query) return results
async def report(cls, *, device_hid: str, start_time: int, end_time: int, pour: int, full: int, grams_expected: int, grams_actual: int, hopper_start: int, hopper_end: int, recipe_id: str, fail: bool, source=None, trip=None, lrg=None, vol=None, bowl=None, error=None): query = feeding_event.insert().values( device_hid=device_hid, timestamp=get_current_timestamp(), start_time=start_time * 1000000, end_time=end_time * 1000000, pour=pour, full=full, grams_expected=grams_expected, grams_actual=grams_actual, hopper_start=hopper_start, hopper_end=hopper_end, source=source, fail=fail, trip=trip, lrg=lrg, vol=vol, bowl=bowl, recipe_id=recipe_id, error=error ) try: return await db.execute(query) except IntegrityError: logger.exception("Unable to save feed result!")
async def set(cls, *, device_id: str, level: int): target_device = await KronosDevices.get(device_hid=device_id) query = hopper_level_references.insert().values( device_hid=target_device[0].hid, timestamp=get_current_timestamp(), level=level ) return await db.execute(query)
async def test_set_hopper_level(with_registered_device: None): from feeder.database.models import ( HopperLevelRef, StoredRecipe, KronosDevices, FeedingResult, ) from feeder.util import get_current_timestamp with pytest.raises(HTTPException) as exc: await HopperLevelRef.get(device_id=SAMPLE_DEVICE_HID) assert exc.value.status_code == 404 assert exc.value.detail == f"Hopper level not set for {SAMPLE_DEVICE_HID}" await HopperLevelRef.set(device_id=SAMPLE_DEVICE_HID, level=100) with pytest.raises(HTTPException) as exc: await HopperLevelRef.get(device_id=SAMPLE_DEVICE_HID) assert exc.value.status_code == 400 assert ( exc.value.detail == "No recipe set for device, cannot calculate hopper level!" ) await StoredRecipe.create( name="sample", g_per_tbsp=10, tbsp_per_feeding=1, budget_tbsp=3 ) await KronosDevices.update(device_hid=SAMPLE_DEVICE_HID, recipe_id=1) level = await HopperLevelRef.get(device_id=SAMPLE_DEVICE_HID) assert level == 100 for index in range(100): row_mod = await FeedingResult.report( device_hid=SAMPLE_DEVICE_HID, start_time=int(get_current_timestamp() / 1000000) + index, end_time=2, pour=3, full=4, grams_expected=5, grams_actual=10, hopper_start=7, hopper_end=8, recipe_id="E000001", fail=False, ) assert row_mod == index + 1 level = await HopperLevelRef.get(device_id=SAMPLE_DEVICE_HID) assert int(level) == 84
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
def test_current_timestamp(): from feeder.util import get_current_timestamp timestamp = get_current_timestamp() assert isinstance(timestamp, int) assert int(math.log10(timestamp)) + 1 == 16