|
33 | 33 | from infuse_iot.api_client.api.coap import get_coap_files |
34 | 34 | from infuse_iot.api_client.api.device import ( |
35 | 35 | create_device_application_update_by_device_id, |
| 36 | + create_device_kv_entry_update_by_device_id_and_key_id, |
36 | 37 | get_device_application_state_by_device_id, |
37 | 38 | get_device_application_updates_by_device_id, |
38 | 39 | get_device_by_device_id, |
|
49 | 50 | from infuse_iot.api_client.types import File, Unset |
50 | 51 | from infuse_iot.commands import InfuseCommand |
51 | 52 | from infuse_iot.credentials import get_api_key |
52 | | -from infuse_iot.util.argparse import InfuseDeviceId, ValidRelease |
| 53 | +from infuse_iot.util.argparse import HexString, InfuseDeviceId, ValidRelease |
53 | 54 | from infuse_iot.util.console import choose_one, user_confirm, user_response |
54 | 55 | from infuse_iot.util.version import Version |
55 | 56 |
|
@@ -200,6 +201,12 @@ def add_parser(cls, parser): |
200 | 201 | "--base64", action="store_true", help="Display values as base64 strings instead of decoding" |
201 | 202 | ) |
202 | 203 |
|
| 204 | + kv_update = tool_parser.add_parser("kv_update", help="Key-Value update") |
| 205 | + kv_update.set_defaults(command_fn=cls.kv_update) |
| 206 | + kv_update.add_argument("--id", type=InfuseDeviceId, required=True, help="Infuse-IoT device ID") |
| 207 | + kv_update.add_argument("--key", "-k", type=int, required=True, help="Key ID to update") |
| 208 | + kv_update.add_argument("--val", "-v", type=HexString, required=True, help="Key value as a hex string") |
| 209 | + |
203 | 210 | dfu_parser = tool_parser.add_parser("dfu", help="Manage device firmware upgrades") |
204 | 211 | dfu_parser.set_defaults(command_fn=cls.dfu) |
205 | 212 | dfu_parser.add_argument("--id", type=InfuseDeviceId, required=True, help="Infuse-IoT device ID") |
@@ -333,6 +340,28 @@ def kv_state(self, client: Client): |
333 | 340 |
|
334 | 341 | print(tabulate(table)) |
335 | 342 |
|
| 343 | + def kv_update(self, client: Client): |
| 344 | + id_str = f"{self.args.id:016x}" |
| 345 | + |
| 346 | + val_encoded = base64.b64encode(self.args.val).decode("utf-8") |
| 347 | + update = models.NewDeviceKVEntryUpdate(data=val_encoded) |
| 348 | + |
| 349 | + rsp = create_device_kv_entry_update_by_device_id_and_key_id.sync( |
| 350 | + client=client, |
| 351 | + device_id=id_str, |
| 352 | + key_id=self.args.key, |
| 353 | + body=update, |
| 354 | + ) |
| 355 | + if rsp is None: |
| 356 | + sys.exit("KV update: No response") |
| 357 | + elif isinstance(rsp, models.Error): |
| 358 | + sys.exit(f"<{rsp.code}>: {rsp.message}") |
| 359 | + elif isinstance(rsp, models.DeviceKVEntry): |
| 360 | + print(f"Device {id_str} key {self.args.key} already has value {self.args.val.hex()}") |
| 361 | + else: |
| 362 | + assert isinstance(rsp, models.DeviceKVEntryUpdate) |
| 363 | + print(f"Device {id_str} update scheduled with ID {rsp.id}") |
| 364 | + |
336 | 365 | def dfu(self, client: Client): |
337 | 366 | id_str = f"{self.args.id:016x}" |
338 | 367 |
|
|
0 commit comments