Skip to content

Commit cd33985

Browse files
committed
tools: rpc_cloud: optional command json handling
Enable RPC wrappers to handle json responses from the cloud directly. This allows nicer output than dumping the dictionary to the console. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 338908b commit cd33985

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/infuse_iot/commands.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,8 @@ def data_progress_cb(self, offset: int) -> None:
111111
def handle_response(self, return_code: int, response: ctypes.LittleEndianStructure | None) -> None:
112112
"""Handle RPC_RSP"""
113113
raise NotImplementedError
114+
115+
@classmethod
116+
def handle_json_response(cls, response: dict) -> None:
117+
"""Handle json response from cloud"""
118+
raise NotImplementedError

src/infuse_iot/tools/rpc_cloud.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from infuse_iot.api_client.api.rpc import get_rpc_by_id, send_rpc
2020
from infuse_iot.api_client.models import Error, NewRPCMessage, NewRPCReq, RPCParams, RPCReqDataHeader, RpcRsp
2121
from infuse_iot.api_client.models.downlink_message_status import DownlinkMessageStatus
22-
from infuse_iot.commands import InfuseCommand, InfuseRpcCommand
22+
from infuse_iot.commands import InfuseCommand, InfuseRpcCommand, wrapper_from_command_id
2323
from infuse_iot.credentials import get_api_key
2424
from infuse_iot.definitions.rpc import id_type_mapping
2525
from infuse_iot.zephyr.errno import errno
@@ -111,6 +111,11 @@ def query(self, client: Client):
111111
command_name = id_type_mapping[rpc_req.command_id].NAME
112112
except KeyError:
113113
command_name = "Unknown"
114+
try:
115+
command_wrapper = wrapper_from_command_id(rpc_req.command_id)
116+
except Exception:
117+
command_wrapper = None
118+
114119
print(f" RPC ID: {rpc_req.command_id} ({command_name})")
115120
print(f" To: {rsp.device.device_id}")
116121
# Manually detect downlink expiry, as the API doesn't do it
@@ -133,6 +138,12 @@ def query(self, client: Client):
133138
extra = f" ({errno(-rpc_rsp.return_code).name})" if rpc_rsp.return_code < 0 else ""
134139
print(f" Result: {rpc_rsp.return_code}{extra}")
135140
if rpc_rsp.params:
141+
try:
142+
if command_wrapper:
143+
command_wrapper.handle_json_response(rpc_rsp.params.additional_properties)
144+
return
145+
except NotImplementedError:
146+
pass
136147
print(json.dumps(rpc_rsp.params.additional_properties, indent=4))
137148
elif rpc_rsp.params_encoded:
138149
raw_rsp = base64.b64decode(rpc_rsp.params_encoded)

0 commit comments

Comments
 (0)