11import base64
22import logging
3- import socket
4-
53from time import sleep
64from typing import List
75
119
1210logger = logging .getLogger (__name__ )
1311
14- QUIT_MSG = Message ('q' )
15- L_MSG = Message ('l' )
12+ QUIT_MSG = Message ("q" )
13+ L_MSG = Message ("l" )
1614L_REPLY_CMD = L_MSG .reply_cmd ()
1715
18- UPDATE_TIMEOUT = Timeout (' update' , 3.0 )
19- CONNECT_TIMEOUT = Timeout (' connect' , 3.0 )
20- FLUSH_INPUT_TIMEOUT = Timeout (' flush-input' , 0 )
21- SEND_RADIO_MSG_TIMEOUT = Timeout (' send-radio-msg' , 30.0 )
22- CMD_REPLY_TIMEOUT = Timeout (' cmd-reply' , 2.0 )
16+ UPDATE_TIMEOUT = Timeout (" update" , 3.0 )
17+ CONNECT_TIMEOUT = Timeout (" connect" , 3.0 )
18+ FLUSH_INPUT_TIMEOUT = Timeout (" flush-input" , 0 )
19+ SEND_RADIO_MSG_TIMEOUT = Timeout (" send-radio-msg" , 30.0 )
20+ CMD_REPLY_TIMEOUT = Timeout (" cmd-reply" , 2.0 )
2321
2422
2523class Commander (object ):
@@ -34,8 +32,10 @@ def disconnect(self):
3432 if self .__connection :
3533 try :
3634 self .__connection .send (QUIT_MSG )
37- except :
38- logger .debug ('Unable to properly shutdown MAX Cube connection. Resetting it...' )
35+ except Exception :
36+ logger .debug (
37+ "Unable to properly shutdown MAX Cube connection. Resetting it..."
38+ )
3939 finally :
4040 self .__close ()
4141
@@ -51,7 +51,7 @@ def update(self) -> List[Message]:
5151 response = self .__call (L_MSG , deadline )
5252 if response :
5353 self .__unsolicited_messages .append (response )
54- except :
54+ except Exception :
5555 self .__connect (deadline )
5656 else :
5757 self .__connect (deadline )
@@ -61,7 +61,9 @@ def update(self) -> List[Message]:
6161
6262 def send_radio_msg (self , hex_radio_msg : str ) -> bool :
6363 deadline = SEND_RADIO_MSG_TIMEOUT .deadline ()
64- request = Message ('s' , base64 .b64encode (bytearray .fromhex (hex_radio_msg )).decode ('utf-8' ))
64+ request = Message (
65+ "s" , base64 .b64encode (bytearray .fromhex (hex_radio_msg )).decode ("utf-8" )
66+ )
6567 while not deadline .is_expired ():
6668 if self .__cmd_send_radio_msg (request , deadline ):
6769 return True
@@ -70,15 +72,17 @@ def send_radio_msg(self, hex_radio_msg: str) -> bool:
7072 def __cmd_send_radio_msg (self , request : Message , deadline : Deadline ) -> bool :
7173 try :
7274 response = self .__call (request , deadline )
73- duty_cycle , status_code , free_slots = response .arg .split (',' , 3 )
75+ duty_cycle , status_code , free_slots = response .arg .split ("," , 3 )
7476 if int (status_code ) == 0 :
7577 return True
76- logger .debug ('Radio message %s was not send [DutyCycle:%s, StatusCode:%s, FreeSlots:%s]' %
77- (request , duty_cycle , status_code , free_slots ))
78+ logger .debug (
79+ "Radio message %s was not send [DutyCycle:%s, StatusCode:%s, FreeSlots:%s]"
80+ % (request , duty_cycle , status_code , free_slots )
81+ )
7882 if int (duty_cycle ) == 100 and int (free_slots ) == 0 :
7983 sleep (deadline .remaining (upper_bound = 10.0 ))
8084 except Exception as ex :
81- logger .error (' Error sending radio message to Max! Cube: ' + str (ex ))
85+ logger .error (" Error sending radio message to Max! Cube: " + str (ex ))
8286 return False
8387
8488 def __call (self , msg : Message , deadline : Deadline ) -> Message :
@@ -97,7 +101,7 @@ def __call(self, msg: Message, deadline: Deadline) -> Message:
97101 raise TimeoutError (str (subdeadline ))
98102 return result
99103
100- except :
104+ except Exception :
101105 self .__close ()
102106 if already_connected :
103107 return self .__call (msg , deadline )
@@ -114,7 +118,9 @@ def __is_connected(self) -> bool:
114118 def __connect (self , deadline : Deadline ):
115119 self .__unsolicited_messages = []
116120 self .__connection = Connection (self .__host , self .__port )
117- reply = self .__wait_for_reply (L_REPLY_CMD , deadline .subtimeout (CMD_REPLY_TIMEOUT ))
121+ reply = self .__wait_for_reply (
122+ L_REPLY_CMD , deadline .subtimeout (CMD_REPLY_TIMEOUT )
123+ )
118124 if reply :
119125 self .__unsolicited_messages .append (reply )
120126
0 commit comments