2828 default_multicast_address ,
2929)
3030from infuse_iot .time import InfuseTime
31- from infuse_iot .util .argparse import ValidFile
31+ from infuse_iot .util .argparse import InfuseDeviceId , ValidFile
3232from infuse_iot .util .console import choose_one
3333from infuse_iot .zephyr .errno import errno
3434
@@ -37,12 +37,14 @@ class LabelType(enum.Enum):
3737 CUSTOM = "custom"
3838 MANUAL = "manual"
3939
40+
4041class TimeCheckType (enum .Enum ):
4142 NONE = "none"
4243 FORCE = "force"
4344 AUTO = "auto"
4445 DEFAULT = "default"
4546
47+
4648class SubCommand (InfuseCommand ):
4749 NAME = "annotate_events"
4850 HELP = "Annotate events on Infuse Tags"
@@ -58,44 +60,66 @@ class SubCommand(InfuseCommand):
5860 def add_parser (cls , parser ):
5961 # Logger Selection parameters.
6062 logger_parser = parser .add_mutually_exclusive_group (required = True )
61- logger_parser .add_argument ("--onboard" , dest = "logger" , action = "store_const" ,
62- const = rpc_enum_data_logger .FLASH_ONBOARD )
63- logger_parser .add_argument ("--external" , dest = "logger" , action = "store_const" ,
64- const = rpc_enum_data_logger .FLASH_REMOVABLE )
65- logger_parser .add_argument ("--logger" , "-l" , type = annotate_wrapper .parse_logger ,
66- help = "TDF Data Logger to write the event to" )
63+ logger_parser .add_argument (
64+ "--onboard" , dest = "logger" , action = "store_const" , const = rpc_enum_data_logger .FLASH_ONBOARD
65+ )
66+ logger_parser .add_argument (
67+ "--external" , dest = "logger" , action = "store_const" , const = rpc_enum_data_logger .FLASH_REMOVABLE
68+ )
69+ logger_parser .add_argument (
70+ "--logger" , "-l" , type = annotate_wrapper .parse_logger , help = "TDF Data Logger to write the event to"
71+ )
6772
6873 # Label selection parameters.
6974 label_group = parser .add_mutually_exclusive_group (required = True )
7075 label_group .add_argument (
71- "--preset-labels" , "-p" , dest = "labels" , type = ValidFile ,
72- help = "JSON file containing labels"
76+ "--preset-labels" , "-p" , dest = "labels" , type = ValidFile , help = "JSON file containing labels"
7377 )
7478 label_group .add_argument (
75- "--custom-labels" , "-c" , dest = "labels" , action = "store_const" , const = LabelType .CUSTOM ,
76- help = "Specify custom labels at runtime"
79+ "--custom-labels" ,
80+ "-c" ,
81+ dest = "labels" ,
82+ action = "store_const" ,
83+ const = LabelType .CUSTOM ,
84+ help = "Specify custom labels at runtime" ,
7785 )
7886 label_group .add_argument (
79- "--manual-labels" , "-m" , dest = "labels" , action = "store_const" , const = LabelType .MANUAL ,
80- help = "Manually enter labels for each event"
87+ "--manual-labels" ,
88+ "-m" ,
89+ dest = "labels" ,
90+ action = "store_const" ,
91+ const = LabelType .MANUAL ,
92+ help = "Manually enter labels for each event" ,
8193 )
8294
8395 # Time sync parameters.
8496 time_group = parser .add_mutually_exclusive_group ()
8597 time_group .add_argument (
86- "--force-time" , "-f" , dest = "time" , action = "store_const" , const = TimeCheckType .FORCE ,
87- help = "Forcibly update the tag's time before writing annotations"
98+ "--force-time" ,
99+ "-f" ,
100+ dest = "time" ,
101+ action = "store_const" ,
102+ const = TimeCheckType .FORCE ,
103+ help = "Forcibly update the tag's time before writing annotations" ,
88104 )
89105 time_group .add_argument (
90- "--auto-time" , "-a" , dest = "time" , action = "store_const" , const = TimeCheckType .AUTO ,
91- help = "Automatically update the tag's time if it is not current"
106+ "--auto-time" ,
107+ "-a" ,
108+ dest = "time" ,
109+ action = "store_const" ,
110+ const = TimeCheckType .AUTO ,
111+ help = "Automatically update the tag's time if it is not current" ,
92112 )
93113 time_group .add_argument (
94- "--skip-time" , "-s" , dest = "time" , action = "store_const" , const = TimeCheckType .NONE ,
95- help = "Do not update the tag's time before writing annotations"
114+ "--skip-time" ,
115+ "-s" ,
116+ dest = "time" ,
117+ action = "store_const" ,
118+ const = TimeCheckType .NONE ,
119+ help = "Do not update the tag's time before writing annotations" ,
96120 )
97121
98- parser .add_argument ("--id" , type = lambda x : int ( x , 0 ) , help = "Device to log events to" )
122+ parser .add_argument ("--id" , type = InfuseDeviceId , help = "Device to log events to" )
99123
100124 def __init__ (self , args ):
101125 self ._label_type = args .labels
@@ -147,18 +171,14 @@ def load_tag_time(self):
147171 sync_request_sent = datetime .now ()
148172 assert self .rpc_client is not None
149173 hdr , rsp = self .rpc_client .run_standard_cmd (
150- time_get .COMMAND_ID ,
151- Auth .DEVICE ,
152- bytes (params ),
153- time_get .response .from_buffer_copy
174+ time_get .COMMAND_ID , Auth .DEVICE , bytes (params ), time_get .response .from_buffer_copy
154175 )
155176 sync_response_received = datetime .now ()
156177
157178 if hdr is None :
158179 raise RuntimeError ("Failed to get time from tag" )
159180 if hdr .return_code != 0 :
160- raise RuntimeError (f"Error getting time from tag ({ hdr .return_code } ): "
161- f"{ errno .strerror (- hdr .return_code )} " )
181+ raise RuntimeError (f"Error getting time from tag ({ hdr .return_code } ): { errno .strerror (- hdr .return_code )} " )
162182
163183 assert isinstance (rsp , time_get .response )
164184 time_response : time_get .response = rsp
@@ -179,7 +199,7 @@ def check_tag_needs_sync(self) -> bool:
179199 f"Tag's clock is out of sync. Update the tag's time?\n "
180200 f"Tag: { tag_datetime_now } \n "
181201 f"System: { self ._time_of_sync } " ,
182- ["Yes" , "No" ]
202+ ["Yes" , "No" ],
183203 )
184204 update = not bool (selection )
185205 except IndexError :
@@ -190,25 +210,19 @@ def check_tag_needs_sync(self) -> bool:
190210 def sync_tag_time (self ):
191211 # Update the tag's time to the current time.
192212 now = datetime .now ().timestamp ()
193- params = time_set .request (
194- InfuseTime .epoch_time_from_unix (now )
195- )
213+ params = time_set .request (InfuseTime .epoch_time_from_unix (now ))
196214
197215 sync_request_sent = datetime .now ()
198216 assert self .rpc_client is not None
199217 hdr , _ = self .rpc_client .run_standard_cmd (
200- time_set .COMMAND_ID ,
201- Auth .DEVICE ,
202- bytes (params ),
203- time_set .response .from_buffer_copy
218+ time_set .COMMAND_ID , Auth .DEVICE , bytes (params ), time_set .response .from_buffer_copy
204219 )
205220
206221 sync_response_received = datetime .now ()
207222 if hdr is None :
208223 raise RuntimeError ("Failed to set time on tag" )
209224 if hdr .return_code != 0 :
210- raise RuntimeError (f"Error setting time on tag ({ hdr .return_code } ): "
211- f"{ errno .strerror (- hdr .return_code )} " )
225+ raise RuntimeError (f"Error setting time on tag ({ hdr .return_code } ): { errno .strerror (- hdr .return_code )} " )
212226
213227 # Update sync point to reflect new time on tag, assuming the tag's time doesn't change for
214228 # the duration of the connection.
@@ -249,8 +263,11 @@ def connection_listener(self):
249263 evt = self ._client .receive ()
250264 if evt is None :
251265 continue
252- if isinstance (evt , ClientNotificationConnectionDropped ) and \
253- evt .infuse_id == self ._device_id and not self .complete :
266+ if (
267+ isinstance (evt , ClientNotificationConnectionDropped )
268+ and evt .infuse_id == self ._device_id
269+ and not self .complete
270+ ):
254271 # Ensure the connection wasn't caused by the script existing.
255272 print ("\n " * (len (self ._labels ))) # Clear any pending input lines
256273 print (f"Lost connection to { self ._device_id :016x} " )
@@ -267,10 +284,10 @@ def run(self):
267284 cl .start ()
268285
269286 while not self .complete :
270- with Live ( self . draw_connecting (), refresh_per_second = 4 ) as live , \
271- self ._client . connection (
272- self ._device_id , GatewayRequestConnectionRequest .DataType .COMMAND
273- ) as mtu :
287+ with (
288+ Live ( self .draw_connecting (), refresh_per_second = 4 ) as live ,
289+ self ._client . connection ( self . _device_id , GatewayRequestConnectionRequest .DataType .COMMAND ) as mtu ,
290+ ) :
274291 self .connected = True
275292 live .transient = True
276293 live .stop ()
@@ -293,15 +310,10 @@ def run(self):
293310 params = annotate_wrapper .annotate_factory (self ._logger , timestamp , label )
294311
295312 hdr , _ = self .rpc_client .run_standard_cmd (
296- annotate .COMMAND_ID ,
297- Auth .DEVICE ,
298- bytes (params ),
299- annotate .response .from_buffer_copy
313+ annotate .COMMAND_ID , Auth .DEVICE , bytes (params ), annotate .response .from_buffer_copy
300314 )
301315
302316 if hdr is None :
303317 print ("Failed to send annotation event to tag" )
304318 continue
305- annotate_wrapper .handle_response_generic (
306- hdr .return_code , self ._logger , now , label
307- )
319+ annotate_wrapper .handle_response_generic (hdr .return_code , self ._logger , now , label )
0 commit comments