Skip to content

Commit 33fe45f

Browse files
committed
tools: cloud: add coap list subcommand
Add a command to display all accessible files on the CoAP server. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 0dc665b commit 33fe45f

1 file changed

Lines changed: 34 additions & 6 deletions

File tree

src/infuse_iot/tools/cloud.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
get_board_by_id,
1919
get_boards,
2020
)
21+
from infuse_iot.api_client.api.coap import get_coap_files
2122
from infuse_iot.api_client.api.device import (
2223
get_device_by_device_id,
2324
get_device_last_route_by_device_id,
@@ -28,7 +29,7 @@
2829
get_all_organisations,
2930
get_organisation_by_id,
3031
)
31-
from infuse_iot.api_client.models import Error, NewBoard, NewOrganisation
32+
from infuse_iot.api_client.models import COAPFilesList, Error, NewBoard, NewOrganisation
3233
from infuse_iot.commands import InfuseCommand
3334
from infuse_iot.credentials import get_api_key
3435

@@ -64,7 +65,7 @@ def run(self):
6465
with self.client() as client:
6566
self.args.command_fn(self, client)
6667

67-
def list(self, client):
68+
def list(self, client: Client):
6869
org_list = []
6970

7071
orgs = get_all_organisations.sync(client=client)
@@ -80,7 +81,7 @@ def list(self, client):
8081
)
8182
)
8283

83-
def create(self, client):
84+
def create(self, client: Client):
8485
rsp = create_organisation.sync_detailed(
8586
client=client,
8687
body=NewOrganisation(self.args.name),
@@ -116,7 +117,7 @@ def run(self):
116117
with self.client() as client:
117118
self.args.command_fn(self, client)
118119

119-
def list(self, client):
120+
def list(self, client: Client):
120121
board_list = []
121122

122123
orgs = get_all_organisations.sync(client=client)
@@ -137,7 +138,7 @@ def list(self, client):
137138
)
138139
)
139140

140-
def create(self, client):
141+
def create(self, client: Client):
141142
rsp = create_board.sync_detailed(
142143
client=client,
143144
body=NewBoard(
@@ -171,7 +172,7 @@ def run(self):
171172
with self.client() as client:
172173
self.args.command_fn(self, client)
173174

174-
def info(self, client):
175+
def info(self, client: Client):
175176
id_int = int(self.args.id, 0)
176177
id_str = f"{id_int:016x}"
177178
info = get_device_by_device_id.sync(client=client, device_id=id_str)
@@ -217,6 +218,32 @@ def info(self, client):
217218
print(tabulate(table))
218219

219220

221+
class Coap(CloudSubCommand):
222+
@classmethod
223+
def add_parser(cls, parser):
224+
parser_coap = parser.add_parser("coap", help="CoAP file server")
225+
parser_coap.set_defaults(command_class=cls)
226+
227+
tool_parser = parser_coap.add_subparsers(title="commands", metavar="<command>", required=True)
228+
229+
list_parser = tool_parser.add_parser("list")
230+
list_parser.set_defaults(command_fn=cls.list)
231+
232+
def run(self):
233+
with self.client() as client:
234+
self.args.command_fn(self, client)
235+
236+
def list(self, client: Client):
237+
files = get_coap_files.sync(client=client)
238+
239+
if isinstance(files, COAPFilesList):
240+
sorted_list: list[str] = sorted(files.filenames)
241+
print("CoAP Files:")
242+
print("\t" + "\n\t".join(sorted_list))
243+
else:
244+
print(f"Failed to retrieve file list {files}")
245+
246+
220247
class SubCommand(InfuseCommand):
221248
NAME = "cloud"
222249
HELP = "Infuse-IoT cloud interaction"
@@ -229,6 +256,7 @@ def add_parser(cls, parser):
229256
Organisations.add_parser(subparser)
230257
Boards.add_parser(subparser)
231258
Device.add_parser(subparser)
259+
Coap.add_parser(subparser)
232260

233261
def __init__(self, args):
234262
self.tool = args.command_class(args)

0 commit comments

Comments
 (0)