Skip to content

Commit

Permalink
Merge pull request #49 from mbillow/update-version-info
Browse files Browse the repository at this point in the history
Update stored firmware version on checkin
  • Loading branch information
mbillow committed Feb 7, 2021
2 parents 055552c + 4415fc6 commit 7f372ef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions feeder/api/routers/kronos.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ async def add_gateway(gateway: NewGateway):
content = {"hid": gateway_hid, "message": "OK"}
except IntegrityError:
logger.debug("Gateway (%s) already registered!", gateway_hid)
await KronosGateways.update(
gateway_hid=gateway_hid, firmware_version=gateway.softwareVersion
)
content = {"hid": gateway_hid, "message": "gateway is already registered"}
return JSONResponse(content=content, headers=kronos_headers)

Expand All @@ -66,6 +69,9 @@ async def register_feeder(device: NewDevice):
await KronosDevices.create(**device.dict())
except IntegrityError:
logger.debug("Device (%s) already registered!", device_hid)
await KronosDevices.update(
device_hid=device_hid, firmware_version=device.softwareVersion
)

content = {
"hid": device_hid,
Expand Down
18 changes: 18 additions & 0 deletions feeder/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ async def get_or_insert(cls, *, gateway_hid):
results = await db.fetch_all(query)
return results[0]

@classmethod
async def update(cls, *, gateway_hid: str, firmware_version: str):
query = (
gateways.update()
.where(gateways.c.hid == gateway_hid)
.values(softwareVersion=firmware_version)
)
await db.execute(query)
device_query = (
devices.update()
.where(devices.c.gatewayHid == gateway_hid)
.values(softwareVersion=firmware_version)
)
await db.execute(device_query)


devices = Table(
"kronos_device",
Expand Down Expand Up @@ -167,6 +182,7 @@ async def update(
front_button: bool = None,
recipe_id: int = None,
black: bool = None,
firmware_version: str = None,
):
values = {}
if name is not None:
Expand All @@ -179,6 +195,8 @@ async def update(
values["currentRecipe"] = recipe_id
if black is not None:
values["black"] = black
if firmware_version is not None:
values["softwareVersion"] = firmware_version
query = devices.update().where(devices.c.hid == device_hid).values(**values)
results = await db.execute(query)
return results
Expand Down
28 changes: 28 additions & 0 deletions tests/test_database_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ async def test_get_or_insert_gateway():
assert len(all_gateways) == 2


@pytest.mark.asyncio
async def test_set_gateway_firmware(with_registered_device: None):
from feeder.database.models import KronosGateways, KronosDevices

device = await KronosDevices.get(gateway_hid=SAMPLE_GATEWAY_HID)
gateway = await KronosGateways.get(gateway_hid=SAMPLE_GATEWAY_HID)

assert gateway[0].softwareVersion != "2.9.0"
assert device[0].softwareVersion != "2.9.0"

await KronosGateways.update(
gateway_hid=SAMPLE_GATEWAY_HID, firmware_version="2.9.0"
)

device = await KronosDevices.get(gateway_hid=SAMPLE_GATEWAY_HID)
gateway = await KronosGateways.get(gateway_hid=SAMPLE_GATEWAY_HID)

assert gateway[0].softwareVersion == "2.9.0"
assert device[0].softwareVersion == "2.9.0"


@pytest.mark.asyncio
async def test_get_device_parameters(with_registered_device: None):
from feeder.database.models import KronosDevices
Expand Down Expand Up @@ -197,6 +218,7 @@ async def test_update_device_single_call(with_registered_device: None):
front_button=False,
recipe_id=1,
black=True,
firmware_version="2.9.0",
)

device = await KronosDevices.get(device_hid=SAMPLE_DEVICE_HID)
Expand All @@ -205,6 +227,7 @@ async def test_update_device_single_call(with_registered_device: None):
assert device[0][10] is False
assert device[0][11] == 1
assert device[0][12] is True
assert device[0].softwareVersion == "2.9.0"


@pytest.mark.asyncio
Expand Down Expand Up @@ -238,13 +261,18 @@ async def test_update_device_multiple_call(with_registered_device: None):
assert rows_updated == 1
rows_updated = await KronosDevices.update(device_hid=SAMPLE_DEVICE_HID, black=True)
assert rows_updated == 1
rows_updated = await KronosDevices.update(
device_hid=SAMPLE_DEVICE_HID, firmware_version="2.9.0"
)
assert rows_updated == 1

device = await KronosDevices.get(device_hid=SAMPLE_DEVICE_HID)
assert device[0][1] == "testing"
assert device[0][9] == "America/Chicago"
assert device[0][10] is False
assert device[0][11] == 1
assert device[0][12] is True
assert device[0].softwareVersion == "2.9.0"


@pytest.mark.asyncio
Expand Down

0 comments on commit 7f372ef

Please sign in to comment.