def unmarshall(bytes, extra=None): buf = ByteBuf(bytes) msg = MessageAudioData() msg.type = buf.read_uint8() # 文件类型 msg.total_size = buf.read_uint32() #文件总长 msg.check = buf.read_uint8() check_data = '' if msg.check == 0: # crc16 check_data = buf.read_uint16() if msg.check == 1: # md5 check_data = buf.read_bytes(16) msg.offset = buf.read_uint32() size = buf.read_uint16() msg.content = buf.read_bytes(size) if msg.type in (0,2): # 录音文件s # msg.flag = buf.read_bytes(6) y = 2000 + buf.read_uint8() m = buf.read_uint8() d = buf.read_uint8() h = buf.read_uint8() M = buf.read_uint8() s = buf.read_uint8() msg.ymdhms = '{}{:02d}{:02d} {:02d}:{:02d}:{:02d}'.format(y, m, d, h, M, s) elif msg.type == 1: #对应 SOS、声控 报警包序列号相同 msg.flag = buf.read_uint16() elif msg.type == 3: msg.flag = buf.read_uint32() msg.sequence = buf.read_uint16() return msg
def unmarshall(bytes, extra=None): buf = ByteBuf(bytes) msg = MessageWifiExtension() msg.extra = extra # y = 2000 + buf.read_uint8() # m = buf.read_uint8() # d = buf.read_uint8() # h = buf.read_uint8() # M = buf.read_uint8() # s = buf.read_uint8() # msg.ymdhms = '{}{:02d}{:02d} {:02d}:{:02d}:{:02d}'.format(y, m, d, h, M, s) msg.parse_time(buf) msg.mcc = buf.read_uint16() msg.mnc = buf.read_uint8() msg.lac = buf.read_uint16() msg.cell_id = tool_format_ci_value(buf.read_bytes(3)) msg.rssi = buf.read_uint8() msg.signal = msg.rssi msg.lac1 = buf.read_uint16() msg.ci1 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi1 = buf.read_uint8() msg.lac2 = buf.read_uint16() msg.ci2 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi2 = buf.read_uint8() msg.lac3 = buf.read_uint16() msg.ci3 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi3 = buf.read_uint8() msg.lac4 = buf.read_uint16() msg.ci4 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi4 = buf.read_uint8() msg.lac5 = buf.read_uint16() msg.ci5 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi5 = buf.read_uint8() msg.lac6 = buf.read_uint16() msg.ci6 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi6 = buf.read_uint8() msg.ahead = buf.read_uint8() msg.wifi_num = buf.read_uint8() for n in range(msg.wifi_num): wd = WifiData() mac = buf.read_bytes(6) wd.mac = tool_format_wifi_mac(mac) wd.signal = buf.read_uint8() msg.wifi_data.append(wd.dict()) # print str(wd) return msg
def unmarshall(bytes, extra=None): buf = ByteBuf(bytes) msg = MessageHeartBeat() msg.extra = extra msg.simple_info.parse(buf.read_uint8()) msg.voltage = buf.read_uint8() msg.gsm = buf.read_uint8() msg.lang = buf.read_uint16() return msg
def unmarshall(bytes, extra=None): buf = ByteBuf(bytes) msg = MessageLbsStationExtension() msg.extra = extra # y = 2000 + buf.read_uint8() # m = buf.read_uint8() # d = buf.read_uint8() # h = buf.read_uint8() # M = buf.read_uint8() # s = buf.read_uint8() # msg.ymdhms = '{}{:02d}{:02d} {:02d}:{:02d}:{:02d}'.format(y, m, d, h, M, s) msg.parse_time(buf) msg.mcc = buf.read_uint16() msg.mnc = buf.read_uint8() msg.lac = buf.read_uint16() msg.cell_id = tool_format_ci_value(buf.read_bytes(3)) msg.rssi = buf.read_uint8() msg.signal = msg.rssi msg.lac1 = buf.read_uint16() msg.ci1 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi1 = buf.read_uint8() msg.lac2 = buf.read_uint16() msg.ci2 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi2 = buf.read_uint8() msg.lac3 = buf.read_uint16() msg.ci3 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi3 = buf.read_uint8() msg.lac4 = buf.read_uint16() msg.ci4 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi4 = buf.read_uint8() msg.lac5 = buf.read_uint16() msg.ci5 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi5 = buf.read_uint8() msg.lac6 = buf.read_uint16() msg.ci6 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi6 = buf.read_uint8() msg.ahead = buf.read_uint8() msg.lang = buf.read_uint16() return msg
def unmarshall(bytes, extra=None): buf = ByteBuf(bytes) s = buf.read_bytes(8) msg = MessageLogin() msg.device_id = ''.join(map(hex, map(ord, s))).replace('0x', '') value = buf.read_uint16() msg.eastwest = (value >> 3) & 1 msg.timezone = (value >> 4) / 100 msg.extra = extra return msg
def unmarshall(bytes,extra=None): buf = ByteBuf(bytes) msg = MessageOnlineCommand() msg.extra = extra size = buf.read_uint8() body_size = size - 4 msg.sequence = buf.read_uint32() msg.content = buf.read_bytes(body_size) msg.lang = buf.read_uint16() return msg
def unmarshall(bytes,extra=None): buf = ByteBuf(bytes) s = buf.read_bytes(8) msg = MessageLogin() msg.device_id = ''.join(map(lambda _:'%02x'%_,map(ord,s))) if msg.device_id[0] == '0': msg.device_id = msg.device_id[1:] value = buf.read_uint16() msg.eastwest = (value>>3) & 1 msg.timezone = (value>>4) / 100 msg.extra = extra return msg
def unpack(self, bytes): index = 0 buf = ByteBuf(bytes) self.start_flag = buf.read_bytes(2) if self.start_flag == self.START_FLAG_1: self.fieldsize_width = 1 self.size = buf.read_uint8() if self.start_flag == self.START_FLAG_2: self.fieldsize_width = 2 self.size = buf.read_uint16() self.type = buf.read_uint8() # if len(bytes) - 4 - self.fieldsize_width != self.size: # print 'Packet Size Error. ' # return None buf.save() buf.move(len(bytes) - 6) self.sequence = buf.read_uint16() self.crc16 = buf.read_uint16() buf.restore() crc = self.calc_crc(bytes[2:len(bytes) - 4]) if crc != self.crc16: print 'Packet Crc Error.' return None msgcls = MessageClsDict.get(self.type) if not msgcls: print 'Message Type unKnown. value:{}'.format(self.type) return None payload = buf.read_bytes(self.size - 5) msg = msgcls.unmarshall(payload, self) # msg = msgcls.unmarshall(bytes[2+self.fieldsize_width+1, len(bytes)-6]) if not msg: print 'Message unmarshall Error. ', str(msgcls) return None return msg
def check_size(self, bytes): """计算这个包的大小""" buf = ByteBuf(bytes) self.start_flag = buf.read_bytes(2) if self.start_flag == self.START_FLAG_1: self.fieldsize_width = 1 self.size = buf.read_uint8() if self.start_flag == self.START_FLAG_2: self.fieldsize_width = 2 self.size = buf.read_uint16() self.type = buf.read_uint8() packet_size = self.size + 4 + self.fieldsize_width return packet_size
def unmarshall(bytes, extra=None): buf = ByteBuf(bytes) msg = MessageLbsStationExtension() msg.extra = extra msg.parse_time(buf) msg.mcc = buf.read_uint16() msg.mnc = buf.read_uint8() msg.lac = buf.read_uint16() msg.cell_id = tool_format_ci_value(buf.read_bytes(3)) msg.rssi = buf.read_uint8() msg.signal = msg.rssi msg.lac1 = buf.read_uint16() msg.ci1 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi1 = buf.read_uint8() msg.lac2 = buf.read_uint16() msg.ci2 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi2 = buf.read_uint8() msg.lac3 = buf.read_uint16() msg.ci3 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi3 = buf.read_uint8() msg.lac4 = buf.read_uint16() msg.ci4 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi4 = buf.read_uint8() msg.lac5 = buf.read_uint16() msg.ci5 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi5 = buf.read_uint8() msg.lac6 = buf.read_uint16() msg.ci6 = tool_format_ci_value(buf.read_bytes(3)) msg.rssi6 = buf.read_uint8() return msg