def parse_query_terminal_para(data): msg_body = data[12:-1] serial_no = data[10:12] para_num = big2num(byte2str(msg_body[0:1])) para_body = msg_body[1:] para_id_list = [] para_data_list = '' for n in range(para_num): para_id = byte2str(para_body[n * 4:(n + 1) * 4]) para_id_list.append(para_id) para_data = para_data_content.get(para_id) para_data_list = para_data_list + para_id + num2big( len(para_data) // 2, 1) + para_data log_data.debug('—————— 查询终端参数 ——————') log_data.debug('参数总数 {}'.format(para_num)) log_data.debug('参数列表 {}'.format(', '.join(para_id_list))) log_data.debug('—————— END ——————') msg_body = byte2str(serial_no) + num2big(para_num, 1) + para_data_list body = '0104' + calc_length_su_ter(msg_body) + communication_id + num2big( get_serial_no()) + msg_body data = '7E' + body + calc_check_code(body) + '7E' send_queue.put(data)
def comm_reply_su_ter(data, reply_result): device_id = data[4:10] msg_id = data[0:2] serial_no = data[10:12] result = reply_result msg_body = byte2str(serial_no) + byte2str(msg_id) + result body = '0001' + calc_length_su_ter(msg_body) + byte2str( device_id) + num2big(get_serial_no()) + msg_body data = '7E' + body + calc_check_code(body) + '7E' return data
def parse_data_file(self): log_file.debug("【 File Server 】 Parse Thread start...") while not getattr(self.connection, '_closed', False): try: data = rec_queue_file.get_nowait() except queue.Empty: data = None if data: data_rec = data text = byte2str(data) text_hex = ' '.join(text[i:i + 2] for i in range(0, len(text), 2)) if len(text_hex) > 500: text_hex = text_hex[:500] log_file.debug( '%s%s%s%s%s' % ("RECV DATA: ", 'lens: ', str(len(data_rec)).ljust( 5, ' '), ' data: || ', text_hex)) # 进入解析过程 command = data[1:3] func = parse_su_ter.parse_su_ter_command.get(command) if func: func(data[1:-1]) time.sleep(0.1)
def parse_tts(data): reply_data = comm_reply_su_ter(data, '00') send_queue.put(reply_data) msg_body = data[12:-1] flag = msg_body[0:1] txt = msg_body[1:] flag_content = '' flag_int = big2num(byte2str(flag)) if flag_int & 0b100000: flag_content += 'CAN故障码信息 ' else: flag_content += '中心导航信息 ' if flag_int & 0b1: flag_content += '紧急 ' if flag_int & 0b100: flag_content += '终端显示器显示 ' if flag_int & 0b1000: flag_content += '终端TTS播读 ' if flag_int & 0b10000: flag_content += '广告屏显示 ' log_data.debug('—————— 文本信息 ——————') log_data.debug('标志: {}'.format(flag_content)) log_data.debug('文本信息: {}'.format(txt.decode('gbk'))) log_data.debug('—————— END ——————')
def send_driver_face_data(self, file): file_name_len = num2big(len(file), 1) file_name = byte2str(file.encode('utf-8')) file_type = '00' path = os.path.join(self.file_path, file) with open(path, 'rb') as f: file_content = f.read() file_size = num2big(len(file_content), 4) code = num2big(sum(file_content))[-2:] msg_body = '033D' + '65' + 'E8' + file_name_len + file_name + file_type + file_size + code data = '7E' + calc_check_code(msg_body) + num2big( GlobalVar.get_serial_no()) + msg_body + '7E' send_queue.put(data) event.clear() event.wait() file_size_int = big2num(file_size) piece = 65536 r = file_size_int % piece pkg_num = file_size_int // piece if r == 0 else (file_size_int // piece) + 1 for x in range(pkg_num): offset = x * piece if x == pkg_num - 1: piece = piece if r == 0 else r pkg_no = x file_content_piece = file_content[offset:offset + piece] msg_body = '033D' + '65' + 'E7' + file_name_len + file_name + num2big( pkg_num, 2) + num2big(pkg_no, 2) + num2big(offset, 4) + num2big( piece, 4) + byte2str(file_content_piece) data = '7E' + calc_check_code(msg_body) + num2big( GlobalVar.get_serial_no()) + msg_body + '7E' logger.debug( '—————— 人脸数据下发 {} 文件大小 {} 包总数 {} 包序号 {} 偏移量 {} 数据长度 {} ——————' .format(file, file_size_int, pkg_num, pkg_no, offset, piece)) send_queue.put(data) time.sleep(0.1) msg_body = '033D' + '65' + 'E6' + file_name_len + file_name + file_type + file_size data = '7E' + calc_check_code(msg_body) + num2big( GlobalVar.get_serial_no()) + msg_body + '7E' send_queue.put(data)
def parse_upload_finish_reply(data): msg_body = data[12:-1] file_name_len = big2num(byte2str(msg_body[0:1])) file_name = msg_body[1:1 + file_name_len].decode('gbk') file_type = byte2str(msg_body[1 + file_name_len:2 + file_name_len]) result = byte2str(msg_body[2 + file_name_len:3 + file_name_len]) retransmission_num = big2num( byte2str(msg_body[3 + file_name_len:4 + file_name_len])) log_file.debug('—————— 文件上传完成应答 ——————') log_file.debug('文件名称: {}'.format(file_name)) log_file.debug('文件类型: {}'.format(file_type)) log_file.debug('上传结果: {}'.format(result)) log_file.debug('补传数量: {}'.format(retransmission_num)) if not retransmission_num == 0: log_data.debug('补传内容: {}'.format(byte2str(msg_body[4 + file_name_len:]))) log_file.debug('—————— END ——————') event.set()
def file_data_upload(self, file): file_size = os.path.getsize(os.path.join(self.file_path, file)) piece = 65536 n = file_size // piece r = file_size % piece with open(os.path.join(self.file_path, file), 'rb') as f: file_data = f.read() for x in range(n): offset = x * piece file_data_piece = file_data[offset:offset + piece] data = '30316364' + str2hex(file, 50) + num2big( offset, 4) + num2big(piece, 4) + byte2str(file_data_piece) log_file.debug('—————— 文件数据上传 {} 偏移量 {} 数据长度 {} ——————'.format( file, offset, piece)) send_queue_file.put(data) offset = n * piece if r: piece = r file_data_piece = file_data[offset:offset + piece] data = '30316364' + str2hex(file, 50) + num2big( offset, 4) + num2big(piece, 4) + byte2str(file_data_piece) log_file.debug('—————— 文件数据上传 {} 偏移量 {} 数据长度 {} ——————'.format( file, offset, piece)) send_queue_file.put(data) file_name_len = len(file) file_name = str2hex(file, file_name_len) file_type = os.path.splitext(file)[-1] if file_type == '.jpg': file_type = '00' elif file_type == '.mp4': file_type = '02' import time time.sleep(0.5) msg_body = num2big(file_name_len, 1) + file_name + file_type + num2big( file_size, 4) body = '1212' + calc_length_su_ter( msg_body) + communication_id + num2big(get_serial_no()) + msg_body data = '7E' + body + calc_check_code(body) + '7E' log_file.debug('—————— 文件上传完成 {} ——————'.format(file)) send_queue_file.put(data)
def parse_terminal_control(data): reply_data = comm_reply_su_ter(data, '00') send_queue.put(reply_data) msg_body = data[12:-1] command = big2num(byte2str(msg_body[:1])) log_data.debug('—————— 终端控制 ——————') log_data.debug('命令字 {}'.format(command)) if command == 1 or command == 2: command_para = msg_body[1:] log_data.debug('命令参数 {}'.format(command_para.decode('gbk'))) log_data.debug('—————— END ——————')
def parse_attachment_upload(data): reply_data = comm_reply_su_ter(data, '00') send_queue.put(reply_data) msg_body = data[12:-1] address_len = big2num(byte2str(msg_body[0:1])) address = msg_body[1:1 + address_len].decode('gbk') port_tcp = big2num(byte2str(msg_body[1 + address_len:3 + address_len])) port_udp = big2num(byte2str(msg_body[3 + address_len:5 + address_len])) alarm_flag = byte2str(msg_body[5 + address_len:21 + address_len]) alarm_no = byte2str(msg_body[21 + address_len:53 + address_len]) log_data.debug('—————— 收到服务器地址 ——————') log_data.debug('服务器IP: {}'.format(address)) log_data.debug('TCP端口: {}'.format(port_tcp)) log_data.debug('UDP端口: {}'.format(port_udp)) log_data.debug('报警标识: {}'.format(alarm_flag)) log_data.debug('报警编号: {}'.format(alarm_no)) log_data.debug('—————— END ——————') upload_attachment_thread = UploadAttachment( 'Upload Attachment Thread ... ', address, port_tcp, alarm_flag, alarm_no) upload_attachment_thread.start()
def produce(buf, remain): data = remain + buf st_list = [m.start() for m in re.finditer(b"\x7e", data)] current = -1 for x in range(len(st_list)): if st_list[x] <= current: continue if not st_list[x] == st_list[-1]: if st_list[x] + 1 == st_list[x + 1]: log_data.info(data[st_list[x]:st_list[x] + 2]) continue for y in range(x + 1, len(st_list)): data_piece = data[st_list[x]:st_list[y] + 1] if len(data_piece) > 2 and b'\x7e' not in data_piece[1:-1]: data_piece = rec_translate(data_piece) rec_queue.put(data_piece) current = st_list[y] break else: log_data.error("未能解析的7E数据" + byte2str(data_piece)) remain = data[current + 1:] return remain
def parse_set_terminal_para(data): reply_data = comm_reply_su_ter(data, '00') send_queue.put(reply_data) msg_body = data[12:-1] para_num = big2num(byte2str(msg_body[0:1])) para_body = msg_body[1:] start = 0 log_data.debug('—————— 设置终端参数 ——————') for _ in range(para_num): para_id = byte2str(para_body[start:start + 4]) para_len = big2num(byte2str(para_body[start + 4:start + 5])) para_data = para_body[start + 5:start + 5 + para_len] start = start + 5 + para_len if para_id == '0000F400': log_data.debug('## {} ##'.format(para_id_name.get(para_id))) log_data.debug('') for n in range(len(para_data) // 2): log_data.debug('第 {} 个参数 {}'.format( n + 1, (byte2str(para_data[n * 2:(n + 1) * 2])))) elif para_id == '0000F401': log_data.debug('## {} ##'.format(para_id_name.get(para_id))) log_data.debug('') for n in range(len(para_data) // 4): log_data.debug('第 {} 个参数 {}'.format( n + 1, (byte2str(para_data[n * 4:(n + 1) * 4])))) elif para_id == '0000F402': log_data.debug('## {} ##'.format(para_id_name.get(para_id))) log_data.debug('') for n in range(len(para_data) // 4): log_data.debug('第 {} 个参数 {}'.format( n + 1, (byte2str(para_data[n * 4:(n + 1) * 4])))) else: log_data.debug('{} {}'.format( para_id_name.get(para_id), para_data_deal_method.get(para_id)(para_data))) log_data.debug('—————— END ——————')
def parse_query_status(data): msg_body = data[12:-1] msg_type = byte2str(msg_body[:1]) log_data.debug('—————— 查询基本信息 ——————') msg_num = big2num(byte2str(msg_body[1:2])) peripheral_id = byte2str(msg_body[2:3]) if msg_type == 'F8': log_data.debug('参数总数 {}'.format(msg_num)) log_data.debug('== 查询信息 ==') company_name_len = 32 company_name = str2hex('Reconova', company_name_len) product_model_len = 32 product_model = str2hex('RN-CA-P500', product_model_len) hw_len = 32 hw = str2hex('This is HardWare.0900', hw_len) sw_len = 32 sw = str2hex('This is SoftWare.', sw_len) device_id_len = 32 device_id = str2hex('123456_F8', device_id_len) client_code_len = 32 client_code = str2hex('ABCDEFG', client_code_len) msg_info = num2big(company_name_len, 1) + company_name + num2big(product_model_len, 1) + product_model + \ num2big(hw_len, 1) + hw + num2big(sw_len, 1) + sw + num2big(device_id_len, 1) + device_id + \ num2big(client_code_len, 1) + client_code msg_body = msg_type + '01' + peripheral_id + num2big( len(msg_info) // 2, 1) + msg_info body = '0900' + calc_length_su_ter( msg_body) + communication_id + num2big(get_serial_no()) + msg_body data = '7E' + body + calc_check_code(body) + '7E' send_queue.put(data) elif msg_type == 'F9': log_data.debug('参数总数 {}'.format(msg_num)) log_data.debug('== 查询硬件信息 ==') msg_info = '00000000' msg_body = msg_type + '01' + peripheral_id + num2big( len(msg_info) // 2, 1) + msg_info body = '0900' + calc_length_su_ter( msg_body) + communication_id + num2big(get_serial_no()) + msg_body data = '7E' + body + calc_check_code(body) + '7E' send_queue.put(data) elif msg_type == 'FA': log_data.debug('参数总数 {}'.format(msg_num)) log_data.debug('== 查询软件信息 ==') terminal_sim_len = 11 terminal_sim = str2hex('13811111111', terminal_sim_len) signal_level = '00' mcu_len = 14 mcu = str2hex('RN-CA-P500-MCU', mcu_len) device_id_len = 32 device_id = str2hex('123456_FA', device_id_len) communicate_id_len = 32 communicate_id = str2hex('123456', communicate_id_len) msg_info = num2big(terminal_sim_len, 1) + terminal_sim + signal_level + num2big(mcu_len, 1) + mcu + \ num2big(device_id_len, 1) + device_id + num2big(communicate_id_len, 1) + communicate_id msg_body = msg_type + '01' + peripheral_id + num2big( len(msg_info) // 2, 1) + msg_info body = '0900' + calc_length_su_ter( msg_body) + communication_id + num2big(get_serial_no()) + msg_body data = '7E' + body + calc_check_code(body) + '7E' send_queue.put(data) log_data.debug('—————— END ——————')
'00000013': '主服务器地址', '00000017': '备份服务器地址', '00000018': 'TCP端口', '00000019': 'UDP端口', '00000055': '最高速度', '00000056': '超速持续时间', '00000057': '连续驾驶时间门限', '00000084': '车牌颜色', '0000F400': '告警灵敏度级别', '0000F401': '告警产生时间间隔', '0000F402': '告警上传平台时间间隔', } # 解析8106不同的参数ID类型有不同的解析方法 para_data_deal_method = { '00000001': lambda x: big2num(byte2str(x)), '00000002': lambda x: big2num(byte2str(x)), '00000003': lambda x: big2num(byte2str(x)), '00000013': lambda x: x.decode('gbk'), '00000017': lambda x: x.decode('gbk'), '00000018': lambda x: big2num(byte2str(x)), '00000019': lambda x: big2num(byte2str(x)), '00000055': lambda x: big2num(byte2str(x)), '00000056': lambda x: big2num(byte2str(x)), '00000057': lambda x: big2num(byte2str(x)), '00000084': lambda x: big2num(byte2str(x)), } # 回复0104应答参数查询时的内容 para_data_content = { '00000001': num2big(10, 4),
client = socket.socket() client.connect(('172.16.100.66', 8888)) first = True while True: # time.sleep(3) # msg_body = '033D' + '65' + 'E4' + '0003' + '05' + str2hex("abcde", 5) + '06' + str2hex('123456', 6) + '0A' + str2hex('sxdsz12345', 10) # data = '7E' + calc_check_code(msg_body) + num2big(10, 2) + msg_body + '7E' # data = send_translate(bytes.fromhex(data)) # client.sendall(data) # time.sleep(60) buf = client.recv(10240) if buf[4:8] == b'\x03\x3d\x65\xb0': print(byte2str(buf)) serial_num = buf[2:4] msg_data = buf[8:-1] person_id = byte2str(msg_data[0:4]) name = msg_data[4:68].decode('utf-8') card = msg_data[68:88].decode('utf-8') regdt = byte2str(msg_data[88:94]) print(person_id, name, card, regdt) msg_body = '033D' + '65' + 'B0' + '00' + '00000001' data = '7E' + calc_check_code(msg_body) + byte2str( serial_num) + msg_body + '7E' data = send_translate(bytes.fromhex(data)) client.sendall(data) if buf[4:8] == b'\x03\x3d\x65\xb1':
from Util.CommonMethod import * import socket from Util.CommonMethod import byte2str, big2num from Util.GlobalVar import get_serial_no client = socket.socket() client.connect(('172.16.100.65', 8888)) first = True while True: buf = client.recv(10240) if buf[1:3] == b'\x8e\x11': print(byte2str(buf)) if buf[1:3] == b'\x8e\x12': print(byte2str(buf))