Skip to content

Commit 1ea1ef7

Browse files
committed
scripts: device_kv_update: added
Add an example script for batch queuing a KV update to multiple devices. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent a3fb476 commit 1ea1ef7

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

scripts/device_kv_update.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
3+
import base64
4+
5+
from infuse_iot.api_client import Client, models
6+
from infuse_iot.api_client.api.device import (
7+
create_device_kv_entry_update_by_device_id_and_key_id,
8+
get_device_kv_entry_by_device_id_and_key_id,
9+
)
10+
from infuse_iot.credentials import get_api_key
11+
12+
13+
class ScriptConfig:
14+
def __init__(self, devices: list[int], kv_key: int, kv_value: str):
15+
self.devices = devices
16+
self.kv_key = kv_key
17+
self.kv_value = kv_value
18+
19+
20+
# Example sets APPLICATION_ACTIVE to 0x00 for all listed devices
21+
config = ScriptConfig(
22+
[
23+
0x12345678ABCDEF01,
24+
],
25+
# APPLICATION_ACTIVE == 6
26+
6,
27+
# Base64 string of desired value (0x00)
28+
base64.b64encode(b"\x00").decode("utf-8"),
29+
)
30+
31+
if __name__ == "__main__":
32+
api_client = Client(base_url="https://api.infuse-iot.com").with_headers({"x-api-key": f"Bearer {get_api_key()}"})
33+
34+
with api_client as client:
35+
# Value doesn't change per device
36+
params = models.NewDeviceKVEntryUpdate(data=config.kv_value)
37+
38+
# Get device state
39+
for device in config.devices:
40+
id_str = f"{device:016x}"
41+
42+
entry = get_device_kv_entry_by_device_id_and_key_id.sync(
43+
client=client, device_id=id_str, key_id=config.kv_key
44+
)
45+
46+
if isinstance(entry, models.DeviceKVEntry) and entry.data == config.kv_value:
47+
print(f"{id_str}: Already has desired value")
48+
continue
49+
50+
update = create_device_kv_entry_update_by_device_id_and_key_id.sync(
51+
client=client,
52+
device_id=id_str,
53+
key_id=config.kv_key,
54+
body=params,
55+
)
56+
if isinstance(update, models.DeviceKVEntry):
57+
print(f"{id_str}: Already has desired value (not caught above?)")
58+
elif isinstance(update, models.DeviceKVEntryUpdate):
59+
print(f"{id_str}: Update scheduled ({update.id})")
60+
elif isinstance(update, models.Error):
61+
print(f"{id_str}: <{update.code}> {update.message}")
62+
else:
63+
assert update is None
64+
print(f"{id_str}: No response?")

0 commit comments

Comments
 (0)