Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions scripts/mqtt_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,40 @@ class OutputFormat(enum.Enum):
JSON = "json"
TABLE = "table"


class ConnectionError(Exception):
pass


def get_enum_name(enum, value):
try:
return enum(value).name
except ValueError:
return None


def get_payload_type(payload_type):
return get_enum_name(InfuseType, payload_type)


def print_metadata_table(data):
metadata_table = [
["Device ID", data["deviceId"]],
["Packet ID", data["packetId"]],
["Timestamp", data["time"]],
["Payload Type", get_payload_type(data["payloadType"])],
["Sequence", data["sequence"]],
["Key ID", base64.b64decode(data["keyId"]).hex()]
["Key ID", base64.b64decode(data["keyId"]).hex()],
]

print("[Metadata]")
print(tabulate.tabulate(metadata_table, tablefmt="grid"))


def get_interface_type(interface_type):
return get_enum_name(InterfaceID, interface_type)


def print_route_table(route):
route_table = [
["Type", get_interface_type(route["type"])],
Expand All @@ -64,11 +70,11 @@ def print_route_table(route):
route_table.append(["UDP Address", route["udp"]["address"]])
route_table.append(["Arrival Time", route["udp"]["time"]])


print("[Route]")
print(tabulate.tabulate(route_table, tablefmt="grid"))

def flatten_tdf(tdf, parent_key=''):

def flatten_tdf(tdf, parent_key=""):
items = []
for k, v in tdf.items():
new_key = f"{parent_key}->{k}" if parent_key else k
Expand All @@ -80,6 +86,7 @@ def flatten_tdf(tdf, parent_key=''):
items.append((new_key, v))
return dict(items)


def print_tdfs_table(tdfs, packet_time):
table = []
for tdf in tdfs:
Expand All @@ -89,13 +96,12 @@ def print_tdfs_table(tdfs, packet_time):
tdf_time = tdf.get("time", packet_time)

for key, value in flatten_tdf(tdf["fields"]).items():
table.append(
[tdf_id, tdf_name, key, value, tdf_time]
)
table.append([tdf_id, tdf_name, key, value, tdf_time])

print("[TDFs]")
print(tabulate.tabulate(table, headers=["TDF ID", "Name", "Field", "Value", "Time"], tablefmt="grid"))


def print_table(data):
print_metadata_table(data)

Expand All @@ -107,6 +113,7 @@ def print_table(data):

print()


def on_message(client, userdata, message):
payload = message.payload.decode("utf-8")
output = userdata["output"]
Expand All @@ -116,6 +123,7 @@ def on_message(client, userdata, message):
data = json.loads(payload)
print_table(data)


def on_connect(client, userdata, flags, reason_code, properties):
if reason_code != 0:
raise ConnectionError(reason_code)
Expand All @@ -127,6 +135,7 @@ def on_connect(client, userdata, flags, reason_code, properties):
topic = userdata["topic"]
client.subscribe(topic)


def main(host, port, username, password, organisation, device, output):
topic = f"organisation/{organisation}"
if device:
Expand All @@ -150,49 +159,53 @@ def main(host, port, username, password, organisation, device, output):
except ConnectionError as e:
sys.exit(f"Connection failed: {e}")


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Read device packets from the Infuse-IoT Cloud MQTT broker",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)

parser.add_argument(
"--broker", "-b",
"--broker",
"-b",
type=str,
default="mqtt.dev.infuse-iot.com",
help="MQTT broker address",
)
parser.add_argument(
"--port", "-p",
"--port",
"-p",
type=int,
default=1883,
help="MQTT broker port",
)
parser.add_argument(
"--username", "-u",
"--username",
"-u",
type=str,
required=True,
help="MQTT username",
)
parser.add_argument("--password", "-P", type=str, required=True, help="MQTT password")
parser.add_argument(
"--password", "-P",
type=str,
required=True,
help="MQTT password")
parser.add_argument(
"--organisation", "--org", "-O",
"--organisation",
"--org",
"-O",
type=str,
required=True,
help="ID of organisation to read packets from",
)
parser.add_argument(
"--device", "-d",
"--device",
"-d",
type=lambda x: int(x, 0),
required=False,
help="Infuse ID of device to read packets from (in hex)",
)
parser.add_argument(
"--output", "-o",
"--output",
"-o",
type=OutputFormat,
default=OutputFormat.JSON,
choices=list(OutputFormat),
Expand Down
16 changes: 13 additions & 3 deletions src/infuse_iot/tools/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
get_board_by_id,
get_boards,
)
from infuse_iot.api_client.api.device import get_device_by_device_id, get_device_state_by_id
from infuse_iot.api_client.api.device import (
get_device_by_device_id,
get_device_last_route_by_device_id,
get_device_state_by_id,
)
from infuse_iot.api_client.api.organisation import (
create_organisation,
get_all_organisations,
Expand Down Expand Up @@ -180,6 +184,7 @@ def info(self, client):
org = get_organisation_by_id.sync(client=client, id=info.organisation_id)
board = get_board_by_id.sync(client=client, id=info.board_id)
state = get_device_state_by_id.sync(client=client, id=info.id)
route = get_device_last_route_by_device_id.sync(client=client, device_id=id_str)

table: list[tuple[str, Any]] = [
("UUID", info.id),
Expand All @@ -201,8 +206,13 @@ def info(self, client):
table += [("Application ID", f"0x{state.application_id:08x}")]
if v:
table += [("Version", f"{v.major}.{v.minor}.{v.revision}+{v.build_num:08x}")]
if state.last_route_interface:
table += [("Last Heard", state.last_route_interface)]
if route is not None:
table += [
("~~~Latest Route~~~", ""),
("Interface", route.interface.upper()),
]
if route.bt_adv:
table += [("BT Address", f"{route.bt_adv.address} ({route.bt_adv.type_})")]

print(tabulate(table))

Expand Down
4 changes: 2 additions & 2 deletions src/infuse_iot/tools/rpc_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def add_parser(cls, parser):
parser_queue = subparser.add_parser("queue", help="Queue a RPC to be sent")
parser_queue.set_defaults(_tool_action="queue")
parser_queue.add_argument("--id", required=True, type=lambda x: int(x, 0), help="Infuse ID to run command on")
parser_queue.add_argument("--timeout", type=int, default=600, help="Timeout to send command in seconds")
parser_queue.add_argument("--queue-timeout", type=int, default=600, help="Timeout to send command in seconds")
command_list_parser = parser_queue.add_subparsers(title="commands", metavar="<command>", required=True)

for _, name, _ in pkgutil.walk_packages(wrappers.__path__):
Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(self, args: argparse.Namespace):
def queue(self, client: Client):
infuse_id = f"{self._args.id:016x}"
command: InfuseRpcCommand = self._args.rpc_class(self._args)
timeout_ms = 1000 * self._args.timeout
timeout_ms = 1000 * self._args.queue_timeout

assert hasattr(command, "COMMAND_ID")

Expand Down
Loading