Skip to content

Commit c6b6889

Browse files
committed
tools: cloud: kv_state subcommand
Add a command to show the current KV state of the device as known by the cloud. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 172152e commit c6b6889

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/infuse_iot/tools/cloud.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from infuse_iot.api_client.api.coap import get_coap_files
2222
from infuse_iot.api_client.api.device import (
2323
get_device_by_device_id,
24+
get_device_kv_entries_by_device_id,
2425
get_device_last_route_by_device_id,
2526
get_device_state_by_id,
2627
)
@@ -30,6 +31,7 @@
3031
get_organisation_by_id,
3132
)
3233
from infuse_iot.api_client.models import COAPFilesList, Error, NewBoard, NewOrganisation
34+
from infuse_iot.api_client.types import Unset
3335
from infuse_iot.commands import InfuseCommand
3436
from infuse_iot.credentials import get_api_key
3537

@@ -167,6 +169,9 @@ def add_parser(cls, parser):
167169
info_parser = tool_parser.add_parser("info")
168170
info_parser.set_defaults(command_fn=cls.info)
169171
info_parser.add_argument("--id", type=str, help="Infuse-IoT device ID")
172+
info_parser = tool_parser.add_parser("kv_state")
173+
info_parser.set_defaults(command_fn=cls.kv_state)
174+
info_parser.add_argument("--id", type=str, help="Infuse-IoT device ID")
170175

171176
def run(self):
172177
with self.client() as client:
@@ -217,6 +222,36 @@ def info(self, client: Client):
217222

218223
print(tabulate(table))
219224

225+
def _kv_display(self, table: list[tuple[str, Any]], name_base: str, dictionary: dict):
226+
for name, value in dictionary.items():
227+
if isinstance(value, dict):
228+
self._kv_display(table, f"{name_base}.{name}", value)
229+
else:
230+
table.append((f"{name_base}.{name}", value))
231+
232+
def kv_state(self, client: Client):
233+
id_int = int(self.args.id, 0)
234+
id_str = f"{id_int:016x}"
235+
236+
kv_state = get_device_kv_entries_by_device_id.sync(client=client, device_id=id_str)
237+
if kv_state is None:
238+
print(f"Unable to query KV state for {id_str}")
239+
return
240+
241+
table: list[tuple[str, Any]] = []
242+
for element in kv_state:
243+
key = element.key_name if isinstance(element.key_name, str) else str(element.key_id)
244+
245+
if isinstance(element.data, Unset):
246+
table.append((key, "Not set"))
247+
else:
248+
if isinstance(element.decoded, Unset):
249+
table.append((key, element.data))
250+
else:
251+
self._kv_display(table, key, element.decoded.additional_properties)
252+
253+
print(tabulate(table))
254+
220255

221256
class Coap(CloudSubCommand):
222257
@classmethod

0 commit comments

Comments
 (0)