def parse_media_upload_finish_su_ter(data, rec_obj): loss_pkg_list = [] name_len = data[13:14] media_name = data[14:14 + big2num(byte2str(name_len))].split( b'\x00')[0].decode('utf-8') media_type = byte2str(data[-7:-6]) media_size = big2num(byte2str(data[-6:-2])) quotient = media_size // 65536 remainder = media_size % 65536 log_event.debug('{} —————— 告警结束 ——————'.format(rec_obj.client_address)) log_event.debug('{} 文件名 {}'.format(rec_obj.client_address, media_name)) log_event.debug('{} 文件类型 {} '.format(rec_obj.client_address, media_type)) log_event.debug('{} 文件大小 {}'.format(rec_obj.client_address, media_size)) log_event.debug('{} —————— END ——————'.format(rec_obj.client_address)) filename_length = data[13:14] file_name = data[14:14 + big2num(byte2str(filename_length))] file_type = data[-7:-6] offset_data = name_offset_data.get(media_name) if offset_data: # if len(offset_data) > 1: # offset_data[0] = None # offset_data[65536] = None for x in offset_data.keys(): if not offset_data.get(x): loss_pkg_list.append(x) if not loss_pkg_list: msg_body = byte2str(filename_length) + byte2str( file_name) + byte2str(file_type) + '00' + '00' for x in sorted(offset_data.keys()): media_queue.put(offset_data.get(x)) name_offset_data.pop(media_name) else: loss_pkg = '' loss_len = len(loss_pkg_list) log_event.info("{} {} 多媒体数据丢包,丢包偏移量为 {}".format( rec_obj.client_address, media_name, loss_pkg_list)) if quotient in loss_pkg_list: loss_pkg_list.pop(quotient) for x in loss_pkg_list: loss_pkg += num2big(x, 4) + num2big(65536, 4) loss_pkg += num2big(quotient, 4) + num2big(remainder, 4) else: for x in loss_pkg_list: loss_pkg += num2big(x, 4) + num2big(65536, 4) msg_body = byte2str(filename_length) + byte2str( file_name) + byte2str(file_type) + '01' + num2big(loss_len, 1) + loss_pkg else: msg_body = byte2str(filename_length) + byte2str(file_name) + byte2str( file_type) + '00' + '00' body = '%s%s%s%s%s' % ('9212', num2big(int( len(msg_body) / 2)), GlobalVar.DEVICEID, num2big(GlobalVar.get_serial_no()), msg_body) data = '%s%s%s%s' % ('7E', body, calc_check_code(body), '7E') return data
def handle(self): address, port = self.client_address logger.debug('【 Video Server 】 Connected by {} {} ...'.format(address, port)) TCPRequestHandler.isAlive = True logger.debug('【 Video Server 】 Producer Thread Start ...') # send_thread = SendData('【 File Server 】 Send Thread Start ...', self) # send_thread.setDaemon(True) # send_thread.start() while True: try: buf = b'' if self.remain: self.remain = ParseData.produce_for_video(buf, self.remain, self) try: buf = self.request.recv(1024) except TimeoutError: log_event.debug('{} 【 Video Server 】 Receiving ack timeout,connection is interrupted.'.format(self.client_address)) except ConnectionResetError: log_event.debug('{} 【 Video Server 】 ConnectionResetError,connection is interrupted.'.format(self.client_address)) except ConnectionAbortedError: log_event.debug('{} 【 Video Server 】 ConnectionAbortedError,connection is interrupted.'.format(self.client_address)) except Exception as e: log_event.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') log_event.error(e) except socket.timeout: break if not buf: self.isAlive = False time.sleep(0.3) log_event.debug('{} 【 Video Server 】 Receive empty data,connection is interrupted.'.format(self.client_address)) break self.remain = ParseData.produce_for_video(buf, self.remain, self) time.sleep(0.001)
def parse_media_msg_upload_su_ter(data, rec_obj): name_len = data[13:14] media_name = data[14:14 + big2num(byte2str(name_len))].split( b'\x00')[0].decode('utf-8') media_type = byte2str(data[-7:-6]) media_size = big2num(byte2str(data[-6:-2])) name_size[media_name] = media_size quotient = media_size // 65536 name_offset_data[media_name] = dict( zip([x * 65536 for x in range(quotient + 1)], [None] * quotient)) log_event.debug('{} —————— 文件信息上传 ——————'.format(rec_obj.client_address)) log_event.debug('{} 文件名 {}'.format(rec_obj.client_address, media_name)) log_event.debug('{} 文件类型 {}'.format(rec_obj.client_address, media_type)) log_event.debug('{} 文件大小 {}'.format(rec_obj.client_address, media_size)) log_event.debug('{} —————— END ——————'.format(rec_obj.client_address)) reply_data = comm_reply_su_ter(data, '00') return reply_data
def parse_media_msg_upload_su_ter(data, rec_obj): # 2019修改项 if conf.jt808_version == 2011: msg_body = data[12:-1] elif conf.jt808_version == 2019: msg_body = data[17:-1] name_len = msg_body[0:1] media_name = msg_body[1:1 + big2num(byte2str(name_len))].split( b'\x00')[0].decode('utf-8') media_type = byte2str(msg_body[-5:-4]) media_size = big2num(byte2str(msg_body[-4:])) name_size[media_name] = media_size quotient = media_size // 65536 name_offset_data[media_name] = dict( zip([x * 65536 for x in range(quotient + 1)], [None] * quotient)) log_event.debug('{} —————— 文件信息上传 ——————'.format(rec_obj.client_address)) log_event.debug('{} 文件名 {}'.format(rec_obj.client_address, media_name)) log_event.debug('{} 文件类型 {}'.format(rec_obj.client_address, media_type)) log_event.debug('{} 文件大小 {}'.format(rec_obj.client_address, media_size)) log_event.debug('{} —————— END ——————'.format(rec_obj.client_address)) reply_data = comm_reply_su_ter(data, '00') return reply_data
def produce_for_file(buf, remain, rec_obj): data = remain + buf if data[0:1] == b'\x7e': st_list = [m.start() for m in re.finditer(b"\x7e", data)] if len(st_list) >= 2: data_piece = data[:st_list[1] + 1] data_piece = rec_translate(data_piece) if byte2str(data_piece[-2:-1]) == calc_check_code( byte2str(data_piece[1:-2])): text = byte2str(data_piece) text_hex = ' '.join(text[i:i + 2] for i in range(0, len(text), 2)) log_event.debug( '%s%s%s%s%s%s' % (rec_obj.client_address, " RECV DATA: ", 'lens: ', str(len(data_piece)).ljust( 5, ' '), ' data: || ', text_hex)) command = data_piece[1:3] func = parse_type_su_ter_for_file.get(command) if func: reply = func(data_piece[1:-1], rec_obj) data_bak = reply text_hex = ' '.join(data_bak[i:i + 2] for i in range(0, len(data_bak), 2)) log_event.debug( '%s%s%s%s%s%s' % (rec_obj.client_address, " SEND DATA: ", 'lens: ', str(int(len(data_bak) / 2)).ljust( 5, ' '), ' data: || ', text_hex)) rec_obj.request.sendall( send_translate(bytes.fromhex(reply))) remain = data[st_list[1] + 1:] else: logger.error('校验码错误。') logger.error(byte2str(data_piece)) else: remain = data elif data[0:4] == b'\x30\x31\x63\x64': data_length = data[58:62] if data_length: if big2num(byte2str(data_length)) + 62 > len(data): remain = data else: data_piece = data[:62 + big2num(byte2str(data_length))] media_name = data_piece[4:54].split(b'\x00')[0].decode('utf-8') offset = big2num(byte2str(data_piece[54:58])) # logger.error(media_name) name_offset_data.get(media_name)[offset] = data_piece text = byte2str(data)[:100] text_hex = ' '.join(text[i:i + 2] for i in range(0, len(text), 2)) log_event.debug( '%s%s%s%s%s%s' % (rec_obj.client_address, " RECV DATA: ", 'lens: ', str(len(data_piece)).ljust( 5, ' '), ' data: || ', text_hex)) remain = data[62 + big2num(byte2str(data_length)):] else: remain = data else: if len(data) >= 4: logger.error('收到错误开头的报文。') logger.error(byte2str(data)) else: remain = data return remain
def parse_alarm_attachment_msg_su_ter(data, rec_obj): terminal_id = byte2str(data[13:20]) alarm_flag = byte2str(data[20:36]) alarm_num = data[36:68].decode('utf-8') msg_type = byte2str(data[68:69]) attachment_num = big2num(byte2str(data[69:70])) log_event.debug('{} —————— 报警附件信息 ——————'.format(rec_obj.client_address)) log_event.debug('{} 终端ID {}'.format(rec_obj.client_address, terminal_id)) log_event.debug('{} 报警标识号 {}'.format(rec_obj.client_address, alarm_flag)) log_event.debug('{} 报警编号 {}'.format(rec_obj.client_address, alarm_num)) log_event.debug('{} 信息类型 {}'.format(rec_obj.client_address, msg_type)) log_event.debug('{} 附件数量 {}'.format(rec_obj.client_address, attachment_num)) attachment_data = data[70:-2] while len(attachment_data): name_len = big2num(byte2str(attachment_data[0:1])) log_event.debug('{} 文件名称长度 {}'.format(rec_obj.client_address, name_len)) name = attachment_data[1:1 + name_len].decode('utf-8') log_event.debug('{} 文件名称 {}'.format(rec_obj.client_address, name)) file_size = big2num( byte2str(attachment_data[1 + name_len:1 + name_len + 4])) log_event.debug('{} 文件大小 {}'.format(rec_obj.client_address, file_size)) attachment_data = attachment_data[1 + name_len + 4:] log_event.debug('{} —————— END ——————'.format(rec_obj.client_address)) reply_data = comm_reply_su_ter(data, '00') return reply_data
def finish(self): address, port = self.client_address log_event.debug('{} 【 Video Server 】 Connection {} {} is disconnected.'.format(self.client_address, address, port)) log_event.debug('-'*100)
def parse_alarm_attachment_msg_su_ter(data, rec_obj): # 2019修改项 if conf.jt808_version == 2011: msg_body = data[12:-1] elif conf.jt808_version == 2019: msg_body = data[17:-1] terminal_id = byte2str(msg_body[0:7]) alarm_flag = byte2str(msg_body[7:23]) alarm_num = msg_body[23:55].decode('utf-8') msg_type = byte2str(msg_body[55:56]) attachment_num = big2num(byte2str(msg_body[56:57])) log_event.debug('{} —————— 报警附件信息 ——————'.format(rec_obj.client_address)) log_event.debug('{} 终端ID {}'.format(rec_obj.client_address, terminal_id)) log_event.debug('{} 报警标识号 {}'.format(rec_obj.client_address, alarm_flag)) log_event.debug('{} 报警编号 {}'.format(rec_obj.client_address, alarm_num)) log_event.debug('{} 信息类型 {}'.format(rec_obj.client_address, msg_type)) log_event.debug('{} 附件数量 {}'.format(rec_obj.client_address, attachment_num)) attachment_data = msg_body[57:] while len(attachment_data): name_len = big2num(byte2str(attachment_data[0:1])) log_event.debug('{} 文件名称长度 {}'.format(rec_obj.client_address, name_len)) name = attachment_data[1:1 + name_len].decode('utf-8') log_event.debug('{} 文件名称 {}'.format(rec_obj.client_address, name)) file_size = big2num( byte2str(attachment_data[1 + name_len:1 + name_len + 4])) log_event.debug('{} 文件大小 {}'.format(rec_obj.client_address, file_size)) attachment_data = attachment_data[1 + name_len + 4:] log_event.debug('{} —————— END ——————'.format(rec_obj.client_address)) reply_data = comm_reply_su_ter(data, '00') return reply_data