|
21 | 21 | from infuse_iot.api_client.api.coap import get_coap_files |
22 | 22 | from infuse_iot.api_client.api.device import ( |
23 | 23 | get_device_by_device_id, |
| 24 | + get_device_kv_entries_by_device_id, |
24 | 25 | get_device_last_route_by_device_id, |
25 | 26 | get_device_state_by_id, |
26 | 27 | ) |
|
30 | 31 | get_organisation_by_id, |
31 | 32 | ) |
32 | 33 | from infuse_iot.api_client.models import COAPFilesList, Error, NewBoard, NewOrganisation |
| 34 | +from infuse_iot.api_client.types import Unset |
33 | 35 | from infuse_iot.commands import InfuseCommand |
34 | 36 | from infuse_iot.credentials import get_api_key |
35 | 37 |
|
@@ -167,6 +169,9 @@ def add_parser(cls, parser): |
167 | 169 | info_parser = tool_parser.add_parser("info") |
168 | 170 | info_parser.set_defaults(command_fn=cls.info) |
169 | 171 | 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") |
170 | 175 |
|
171 | 176 | def run(self): |
172 | 177 | with self.client() as client: |
@@ -217,6 +222,36 @@ def info(self, client: Client): |
217 | 222 |
|
218 | 223 | print(tabulate(table)) |
219 | 224 |
|
| 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 | + |
220 | 255 |
|
221 | 256 | class Coap(CloudSubCommand): |
222 | 257 | @classmethod |
|
0 commit comments