1
1
import logging
2
2
import struct
3
- import time
4
-
5
3
try :
6
4
import queue
7
5
except ImportError :
@@ -55,10 +53,9 @@ def send_switch_mode_global(self, mode):
55
53
:param int mode:
56
54
CONFIGURATION_MODE or NORMAL_MODE
57
55
"""
58
- # LSS messages are always a full 8 bytes long.
56
+ # LSS messages are always a full 8 bytes long.
59
57
# Unused bytes are reserved and should be initialized with 0.
60
-
61
- message = [0 ]* 8
58
+ message = bytearray (8 )
62
59
63
60
if self ._mode_state != mode :
64
61
message [0 ] = SWITCH_MODE_GLOBAL
@@ -68,19 +65,18 @@ def send_switch_mode_global(self, mode):
68
65
69
66
def __send_inquire_node_id (self ):
70
67
"""
71
- :return: int current node id
68
+ :return: Current node id
69
+ :rtype: int
72
70
"""
73
- message = [ 0 ] * 8
71
+ message = bytearray ( 8 )
74
72
message [0 ] = INQUIRE_NODE_ID
75
73
current_node_id , nothing = self .__send_command (message )
76
74
77
75
return current_node_id
78
76
79
77
def __send_configure (self , key , value1 = 0 , value2 = 0 ):
80
- """Send a message to set a key with values
81
- :return: int error_code, int error_extension
82
- """
83
- message = [0 ]* 8
78
+ """Send a message to set a key with values"""
79
+ message = bytearray (8 )
84
80
message [0 ] = key
85
81
message [1 ] = value1
86
82
message [2 ] = value2
@@ -130,13 +126,13 @@ def store_configuration(self):
130
126
def __send_command (self , message ):
131
127
"""Send a LSS operation code to the network
132
128
133
- :param byte list message:
129
+ :param bytearray message:
134
130
LSS request message.
135
131
"""
136
132
137
133
retries_left = self .MAX_RETRIES
138
134
139
- message_str = "" .join (["{:02x} " .format (x ) for x in message ])
135
+ message_str = " " .join (["{:02x}" .format (x ) for x in message ])
140
136
logger .info (
141
137
"Sending LSS message %s" , message_str )
142
138
@@ -165,17 +161,15 @@ def __send_command(self, message):
165
161
raise LssError ("No LSS response received" )
166
162
if retries_left < self .MAX_RETRIES :
167
163
logger .warning ("There were some issues while communicating with the node" )
168
- res_command , = struct .unpack ( "B " , response [ 0 : 1 ] )
164
+ res_command , message1 , message2 = struct .unpack_from ( "BBB " , response )
169
165
if res_command != message [0 ]:
170
- raise LssError (abort_code )
171
- else :
172
- self ._mode_state = self .CONFIGURATION_MODE
173
- message1 , = struct .unpack ("B" , response [1 :2 ])
174
- message2 , = struct .unpack ("B" , response [2 :3 ])
175
- return message1 , message2
166
+ raise LssError ("Unexpected response (%d)" % res_command )
167
+ self ._mode_state = self .CONFIGURATION_MODE
168
+ return message1 , message2
176
169
177
170
def on_message_received (self , can_id , data , timestamp ):
178
171
self .responses .put (bytes (data ))
179
172
173
+
180
174
class LssError (Exception ):
181
175
"""Some LSS operation failed."""
0 commit comments