66__copyright__ = "Copyright 2024, Embeint Holdings Pty Ltd"
77
88import sys
9- from http import HTTPStatus
10- from json import loads
119from typing import Any
1210
1311from tabulate import tabulate
1412
13+ import infuse_iot .api_client .models as models
1514from infuse_iot .api_client import Client
1615from infuse_iot .api_client .api .board import (
1716 create_board ,
@@ -72,7 +71,7 @@ def list(self, client: Client):
7271 org_list = []
7372
7473 orgs = get_all_organisations .sync (client = client )
75- if isinstance (orgs , Error ) or orgs is None :
74+ if isinstance (orgs , models . Error ) or orgs is None :
7675 sys .exit (f"Organisation query failed { orgs } " )
7776 for o in orgs :
7877 org_list .append ([o .name , o .id ])
@@ -87,15 +86,15 @@ def list(self, client: Client):
8786 def create (self , client : Client ):
8887 rsp = create_organisation .sync_detailed (
8988 client = client ,
90- body = NewOrganisation (self .args .name ),
89+ body = models . NewOrganisation (self .args .name ),
9190 )
9291
93- if rsp .status_code == HTTPStatus .CREATED :
94- assert rsp .parsed is not None
95- print (f"Created organisation { rsp .parsed .name } with ID { rsp .parsed .id } " )
92+ if rsp .parsed is None :
93+ print (f"<{ rsp .status_code } >: { rsp .content .decode ('utf-8' )} " )
94+ elif isinstance (rsp .parsed , models .Error ):
95+ print (f"<{ rsp .status_code } >: { rsp .parsed .message } " )
9696 else :
97- c = loads (rsp .content .decode ("utf-8" ))
98- print (f"<{ rsp .status_code } >: { c ['message' ]} " )
97+ print (f"Created organisation { rsp .parsed .name } with ID { rsp .parsed .id } " )
9998
10099
101100class Boards (CloudSubCommand ):
@@ -124,11 +123,11 @@ def list(self, client: Client):
124123 board_list = []
125124
126125 orgs = get_all_organisations .sync (client = client )
127- if isinstance (orgs , Error ) or orgs is None :
126+ if isinstance (orgs , models . Error ) or orgs is None :
128127 sys .exit (f"Organisation query failed { orgs } " )
129128 for org in orgs :
130129 boards = get_boards .sync (client = client , organisation_id = org .id )
131- if isinstance (boards , Error ) or boards is None :
130+ if isinstance (boards , models . Error ) or boards is None :
132131 sys .exit (f"Boards query failed { boards } " )
133132
134133 for b in boards :
@@ -144,19 +143,20 @@ def list(self, client: Client):
144143 def create (self , client : Client ):
145144 rsp = create_board .sync_detailed (
146145 client = client ,
147- body = NewBoard (
146+ body = models . NewBoard (
148147 name = self .args .name ,
149148 description = self .args .desc ,
150149 soc = self .args .soc ,
151150 organisation_id = self .args .org ,
152151 ),
153152 )
154- if rsp .status_code == HTTPStatus .CREATED :
155- assert rsp .parsed is not None
156- print (f"Created board { rsp .parsed .name } with ID { rsp .parsed .id } " )
153+
154+ if rsp .parsed is None :
155+ print (f"<{ rsp .status_code } >: { rsp .content .decode ('utf-8' )} " )
156+ elif isinstance (rsp .parsed , models .Error ):
157+ print (f"<{ rsp .status_code } >: { rsp .parsed .message } " )
157158 else :
158- c = loads (rsp .content .decode ("utf-8" ))
159- print (f"<{ rsp .status_code } >: { c ['message' ]} " )
159+ print (f"Created board { rsp .parsed .name } with ID { rsp .parsed .id } " )
160160
161161
162162class Device (CloudSubCommand ):
@@ -185,6 +185,8 @@ def info(self, client: Client):
185185 info = get_device_by_device_id .sync (client = client , device_id = id_str )
186186 if info is None :
187187 sys .exit (f"No device with Infuse-IoT ID { id_str } found" )
188+ elif isinstance (info , models .Error ):
189+ sys .exit (f"<{ info .code } >: { info .message } " )
188190 metadata : list [tuple [str , Any ]] = []
189191 if info .metadata :
190192 metadata = [(f"Metadata.{ k } " , v ) for k , v in info .metadata .additional_properties .items ()]
@@ -198,13 +200,16 @@ def info(self, client: Client):
198200 table : list [tuple [str , Any ]] = [
199201 ("UUID" , info .id ),
200202 ("MCU ID" , info .mcu_id ),
201- ("Organisation" , f"{ info .organisation_id } ({ org .name if org else 'Unknown' } )" ),
202- ("Board" , f"{ info .board_id } ({ board .name if board else 'Unknown' } )" ),
203+ (
204+ "Organisation" ,
205+ f"{ info .organisation_id } ({ org .name if isinstance (org , models .Organisation ) else 'Unknown' } )" ,
206+ ),
207+ ("Board" , f"{ info .board_id } ({ board .name if isinstance (board , models .Board ) else 'Unknown' } )" ),
203208 ("Created" , info .created_at ),
204209 ("Updated" , info .updated_at ),
205210 * metadata ,
206211 ]
207- if state is not None :
212+ if isinstance ( state , models . DeviceState ) :
208213 v = state .application_version
209214
210215 table += [
@@ -215,7 +220,7 @@ def info(self, client: Client):
215220 table += [("Application ID" , f"0x{ state .application_id :08x} " )]
216221 if v :
217222 table += [("Version" , f"{ v .major } .{ v .minor } .{ v .revision } +{ v .build_num :08x} " )]
218- if route is not None :
223+ if isinstance ( route , models . UplinkRoute ) :
219224 table += [
220225 ("~~~Latest Route~~~" , "" ),
221226 ("Interface" , route .interface .upper ()),
@@ -263,7 +268,7 @@ def kv_state(self, client: Client):
263268 id_str = f"{ id_int :016x} "
264269
265270 kv_state = get_device_kv_entries_by_device_id .sync (client = client , device_id = id_str )
266- if kv_state is None :
271+ if not isinstance ( kv_state , list ) :
267272 print (f"Unable to query KV state for { id_str } " )
268273 return
269274
@@ -307,12 +312,14 @@ def run(self):
307312 def list (self , client : Client ):
308313 files = get_coap_files .sync (client = client )
309314
310- if isinstance (files , COAPFilesList ):
315+ if files is None :
316+ print ("Failed to retrieve file list (No response)" )
317+ elif isinstance (files , models .Error ):
318+ print (f"<{ files .code } >: { files .message } " )
319+ else :
311320 sorted_list : list [str ] = sorted (files .filenames )
312321 print ("CoAP Files:" )
313322 print ("\t " + "\n \t " .join (sorted_list ))
314- else :
315- print (f"Failed to retrieve file list { files } " )
316323
317324
318325class SubCommand (InfuseCommand ):
0 commit comments