def parse_data(self): log_data.debug("【 Data Server 】 Parse Thread start...") while not getattr(self.connection, '_closed', False): try: data = rec_queue.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_data.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 start_work(self): ip_bind = (self.address, self.port) self.connection = socket.socket() self.connection.connect(ip_bind) log_data.debug('=' * 100) if self.conn_type == 1: log_data.debug('【 Data Server 】 {} {} Connected ...'.format( self.connection.getpeername(), self.connection.getsockname())) th1 = threading.Thread(target=self.recv_data, args=()) th1.start() th2 = threading.Thread(target=self.parse_data, args=()) th2.start() th3 = threading.Thread(target=self.send_data, args=()) th3.start() elif self.conn_type == 2: log_file.debug('【 File Server 】 {} {} Connected ...'.format( self.connection.getpeername(), self.connection.getsockname())) th1 = threading.Thread(target=self.recv_data_file, args=()) th1.start() th2 = threading.Thread(target=self.parse_data_file, args=()) th2.start() th3 = threading.Thread(target=self.send_data_file, args=()) th3.start()
def run(self): log_data.debug(threading.current_thread().getName()) while True: while not location_queue.empty(): location_data = location_queue.get_nowait() status, latitude, longitude, speed, report_time, mileage = location_data alarm_flag = '00000000' height = '0000' direction = '0000' # status = '00000003' # latitude = '0157CB96' # longitude = '06CA9628' # speed = num2big(880, 2) # report_time = '200206221122' msg_body = alarm_flag + status + latitude + longitude + height + speed + direction + report_time + '0104' + mileage body = '0200' + calc_length_su_ter( msg_body) + self.device_id + num2big( get_serial_no()) + msg_body data = '7E' + body + calc_check_code(body) + '7E' send_queue.put(data) msg_body = '' body = '0002' + calc_length_su_ter( msg_body) + self.device_id + num2big( get_serial_no()) + msg_body data = '7E' + body + calc_check_code(body) + '7E' send_queue.put(data) time.sleep(0.1)
def close_conn(self): if self.conn_type == 1: log_data.debug('【 Data Server 】 {} {} Disconnected ...'.format( self.connection.getpeername(), self.connection.getsockname())) elif self.conn_type == 2: log_file.debug('【 File Server 】 {} {} Disconnected ...'.format( self.connection.getpeername(), self.connection.getsockname())) self.connection.close() time.sleep(0.5) log_data.debug('=' * 100)
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 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 recv_data_file(self): log_file.debug("【 File Server 】 Recv Thread start...") while not getattr(self.connection, '_closed', False): buf = b'' try: if self.remain: self.remain = parse_data.produce_for_file(buf, self.remain) buf = self.connection.recv(1024) except socket.timeout: time.sleep(0.1) log_file.debug( '【 File Server 】 [Recv Thread] Receiving data timeout,connection is interrupted.' ) break except OSError: time.sleep(0.1) log_file.debug( '【 File Server 】 [Recv Thread] OSError,connection is interrupted.' ) break except ConnectionResetError: time.sleep(0.1) log_data.debug( '【 Data Server 】 [Recv Thread] ConnectionResetError,connection is interrupted.' ) break except ConnectionAbortedError: time.sleep(0.1) log_data.debug( '【 Data Server 】 [Recv Thread] ConnectionAbortedError,connection is interrupted.' ) break except Exception as e: log_data.error('[Recv Thread] Unknown Error.') log_data.error(e) break if not buf: time.sleep(0.1) log_file.debug( '【 File Server 】 [Recv Thread] Receive empty data,connection is interrupted.' ) break self.remain = parse_data.produce_for_file(buf, self.remain) time.sleep(0.1)
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 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 send_queue_data(data): if data: if ' ' in data: data = ''.join(data.split(' ')) data_bak = data if data.startswith('7E'): data = send_translate(bytes.fromhex(data)) else: data = bytes.fromhex(data) send_data = data text_hex = ' '.join(data_bak[i:i + 2] for i in range(0, len(data_bak), 2)) if len(text_hex) > 500: text_hex = text_hex[:500] log_data.debug( '%s%s%s%s%s' % ("SEND DATA: ", 'lens: ', str(int(len(data_bak) / 2)).ljust( 5, ' '), ' data: || ', text_hex)) return send_data
def parse_query_terminal_property(): log_data.debug('—————— 查询终端属性 ——————') terminal_type = '0000' maker_id = str2hex('recon', 5) terminal_model = str2hex('Reconova_P500', 20) terminal_id = str2hex('12_0107', 7) sim_iccid = str2hex('1351101122', 10) hw_len = 32 hw = str2hex('This is HardWare.0107', hw_len) fw_len = 32 fw = str2hex('This is FirmWare.', fw_len) gnss = '00' comm_pro = '00' msg_body = terminal_type + maker_id + terminal_model + terminal_id + sim_iccid + num2big(hw_len, 1) + hw + \ num2big(fw_len, 1) + fw + gnss + comm_pro body = '0107' + 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 run(self): log_data.debug(threading.current_thread().getName()) self.rs = xlrd.open_workbook(self.filename) self.table = self.rs.sheets()[0] rows = self.table.nrows with open("num.txt", 'r') as f: num = f.read() if not num: num = 1 else: num = int(num) for case in range(num, rows): # 获取字段长度列和字段值列 data_row = self.table.row_values(case) # 获取excel行的数据 status, latitude, longitude, speed, report_time, mileage = data_row if not status: status = "00000003" latitude = num2big(int(latitude * 1000000), 4) longitude = num2big(int(longitude * 1000000), 4) else: status = "00000001" latitude = num2big(0, 4) longitude = num2big(0, 4) speed = num2big(int(speed * 10), 2) if not report_time: report_time = datetime.datetime.now().strftime('%y%m%d%H%M%S') if not mileage: mileage = num2big(int(num), 4) location_queue.put( (status, latitude, longitude, speed, report_time, mileage)) with open("num.txt", 'w') as f: num = int(num) + 1 f.write(str(num)) time.sleep(10)
def parse_phone_recall(data): reply_data = comm_reply_su_ter(data, '00') send_queue.put(reply_data) msg_body = data[12:-1] flag = msg_body[0:1] phone_number = msg_body[1:] if flag == b'\x00': flag_content = '普通通话' elif flag == b'\x01': flag_content = '监听' else: flag_content = '未知类型标志!!!' log_data.debug('—————— 电话回拨 ——————') log_data.debug('标志: {}'.format(flag_content)) log_data.debug('文本信息: {}'.format(phone_number.decode('gbk'))) log_data.debug('—————— END ——————')
def send_data(self): log_data.debug("【 Data Server 】 Send Thread start...") while not getattr(self.connection, '_closed', False): try: data = send_queue.get_nowait() except queue.Empty: data = None text = parse_data.send_queue_data(data) if text: try: self.connection.sendall(text) except socket.timeout: time.sleep(0.1) log_file.debug( '【 Data Server 】 [Send Thread] Receiving data timeout,connection is interrupted.' ) break except OSError: time.sleep(0.1) log_file.debug( '【 Data Server 】 [Send Thread] OSError,connection is interrupted.' ) break except ConnectionResetError: time.sleep(0.1) log_data.debug( '【 Data Server 】 [Send Thread] ConnectionResetError,connection is interrupted.' ) break except ConnectionAbortedError: time.sleep(0.1) log_data.debug( '【 Data Server 】 [Send Thread] ConnectionAbortedError,connection is interrupted.' ) break except Exception as e: log_data.error('[Send Thread] Unknown Error.') log_data.error(e) break time.sleep(0.1)
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 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 ——————')
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 run_time(*args, **kw): start = time.time() result = func(*args, **kw) stop = time.time() log_data.debug(test + '-----' + str(stop - start)) return result