55 def __init__(self, header = None, datas = None):
63 if not header ==
None:
83 for i
in range(len(strArg)):
84 res.append(ord(strArg[i]))
96 while len(strArg) > 0:
97 res.append( int(strArg[0:2],16) )
109 if type(header)
is str:
110 self.
header = P2Msg.str2list(header)
111 elif type(header)
is list:
114 raise TypeError(
"Waiting str or list but got "+str(type(header)))
131 if type(recvBuff)
is str:
132 self.
dataSz = ord(recvBuff[0])
133 elif type(recvBuff)
is int:
136 raise TypeError(
"Waiting str or int but got ",type(recvBuff))
139 raise Exception(
"Data size doesn't match actuel size of data in the message")
154 """ DEACTIVATE TEST !!! TO REACTIVATE LATER
155 if sizeChk and self.dataSz != len(datas):
159 if type(datas)
is str:
160 self.
datas = P2Msg.str2list(datas)
161 elif type(datas)
is list:
164 raise TypeError(
"Waiting str or list but got ",type(datas))
166 if not sizeChk
or self.
dataSz ==
None:
184 if len(checksum) != 2:
187 elif type(checksum)
is str
or type(checksum)
is list:
188 self.
checksum = ord(checksum[1]) + (ord(checksum[0]) * 0x100)
189 elif type(checksum)
is int:
192 raise TypeError(
"Waiting int, str or list but got : ",type(checksum))
198 logger.warn(
"Warning invalid checksum for : "+self.
getStr())
211 def prepare(self, header = None, data = None):
247 if fmt == P2Msg.FMT_INT:
251 elif fmt == P2Msg.FMT_RAW_STR:
254 return str(bytearray([self.
dataSz]))
255 elif fmt == P2Msg.FMT_HEX_STR:
258 return "%02X" % self.
dataSz
260 raise TypeError(
"Unknow or invalid format")
272 if fmt == P2Msg.FMT_INT:
274 elif fmt == P2Msg.FMT_RAW_STR:
276 elif fmt == P2Msg.FMT_HEX_STR:
278 elif fmt == P2Msg.FMT_LIST:
281 raise TypeError(
"Unknow format")
284 Return a list attribute in the requested format
298 if fmt == P2Msg.FMT_LIST:
300 elif fmt == P2Msg.FMT_HEX_STR:
307 elif fmt == P2Msg.FMT_RAW_STR:
310 return str(bytearray(lst))
312 raise TypeError(
"Unknow or invalid format")
322 return P2Msg.formatList(self.
header, fmt)
332 return P2Msg.formatList(self.
datas, fmt)
347 if self.
datas !=
None:
348 res +=
"%02X" % len(self.
datas)
349 for data
in self.
datas:
352 res +=
"%02X" % (self.
checksum / 0x100)
353 res +=
"%02X" % (self.
checksum % 0x100)
363 res += self.
getData(P2Msg.FMT_RAW_STR)
366 logger.warn(
"message marked as invalid while getting raw string")
368 logger.warn(
"invalid message's chekcsum while getting raw string")
373 print "Header :\t"+str(self.
header)
374 print "Data size =\t"+str(self.
dataSz)
375 print "Datas :\t"+str(self.
datas)
377 print "Message marked as",
396 if c >= ord(chrRange[0])
and c <= ord(chrRange[1]):
408 res =
"[0x%02X 0x%02X]" % (self.
header[0],self.
header[1])
412 if self.
header[0] == ord(
'M'):
414 if hid == ord(
'A')
or hid == ord(
'B')
or hid == ord(
'M'):
415 for d
in self.
datas[:5]:
418 elif hid == ord(
'D'):
419 res += chr(self.
datas[0])+
" "
420 for d
in self.
datas[1:7]:
423 elif hid == ord(
'T'):
424 res+=
"0x%02X "%self.
datas[0]
426 elif hid == ord(
'L'):
427 for d
in self.
datas[:11]:
430 elif hid == ord(
'F')
or hid == ord(
'W'):
431 for d
in self.
datas[:3]:
447 return (
not self.
valid)
458 for data
in self.
datas: