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_file_upload(data): global recv_pkg_dict global global_pkg_sum # 文件名称长度 file_length = big2num(byte2str(data[8:9])) # 文件名称 face_name = data[9:9 + file_length].decode('utf-8') # 分包总数 global_pkg_sum = big2num(byte2str(data[9 + file_length:11 + file_length])) # 当前分包序号 pkg_no = big2num(byte2str(data[11 + file_length:13 + file_length])) # 数据偏移量 offset = big2num(byte2str(data[13 + file_length:17 + file_length])) # 数据长度 length = big2num(byte2str(data[17 + file_length:21 + file_length])) data = data[21 + file_length:-1] recv_pkg_dict[pkg_no] = (face_name, offset, length, data) logger.debug( "———————————————— 收到 {} 的上传分片 包总数 {} 包序号 {} 偏移量 {} 分片长度 {} ————————————————" .format(face_name, global_pkg_sum, pkg_no, offset, length))
def get_log_su(start_date, end_date): logger.debug('—————— 获取 {} 到 {} 的日志 ——————'.format(start_date, end_date)) start_date = num2big(int(start_date), 4) end_date = num2big(int(end_date), 4) body = '%s%s%s%s%s' % (COMPANY_NO, PERIPHERAL, 'FA', start_date, end_date) data = '%s%s%s%s%s' % ('7E', calc_check_code(body), num2big(get_serial_no()), body, '7E') send_queue.put(data)
def parse_set_device_id(data): state = data[8:9] if state == b'\x00': txt = '成功' elif state == b'\x01': txt = '失败' else: txt = '状态出错' logger.debug('—————— 写设备ID结果 {} ——————'.format(txt))
def parse_set_para_reply(data): state = data[8:9] if state == b'\x00': txt = '成功' elif state == b'\x01': txt = '失败' else: txt = '状态出错' logger.debug('—————— 查询外设指令应答结果 {} ——————'.format(txt))
def parse_smoke_sensor_status(data): state = data[8:9] if state == b'\x00': txt = '初始化正常' elif state == b'\x01': txt = '初始化失败' else: txt = '状态出错' logger.debug('—————— 烟感状态 {} ——————'.format(txt))
def run_http_server(): addr = conf.get_file_address_sf_local() port = conf.get_file_port_sf_local() server_address = (addr, port) logger.debug('【 File Server 】 Starting HTTP Server ... Local IP {} Port {}'.format(addr, port)) httpd = HTTPServer(server_address, WebServer) httpd.serve_forever()
def parse_turn_signal_status(data): status = data[8:9] if status == b'\x00': txt = '未打转向灯' elif status == b'\x01': txt = '左转向' elif status == b'\x10': txt = '右转向' logger.debug('—————— 转向灯状态 {} ——————'.format(txt))
def run(self): logger.debug(threading.current_thread().getName()) self.send_driver_face_info() event.wait() if self.comm_type == 0 or self.modify_info_mask & 0b00000001: for file in self.file_list: self.send_driver_face_data(file) event.clear() event.wait()
def parse_tf_status(data): state = data[8:9] if state == b'\x00': txt = '读写正常' elif state == b'\x01': txt = '读写异常' else: txt = '状态出错' logger.debug('—————— TF卡读写状态 {} ——————'.format(txt))
def parse_delete_face_youwei(data): msg_body = data[8:-1] result = byte2str(msg_body[0:1]) person_id = big2num(byte2str(msg_body[1:5])) image_id = big2num(byte2str(msg_body[5:9])) logger.debug('———————————————— 删除人员(B5) ————————————————') logger.debug("结果码: {}".format(result)) logger.debug("人员ID: {}".format(person_id)) logger.debug("人员ID: {}".format(image_id)) logger.debug('———————————————— END ————————————————')
def parse_state_report(data): peripheral = data[6:7] function_no = data[7:8] serial_num = data[2:4] state_return_body = '%s%s%s' % (COMPANY_NO, byte2str(peripheral), byte2str(function_no)) state_return = '%s%s%s%s%s' % (SU_FLAG, calc_check_code(state_return_body), byte2str(serial_num), state_return_body, SU_FLAG) send_queue.put(state_return) work_state = data[8:9] logger.debug('—————— 当前的工作状态是 {} ——————'.format(work_state_dict.get(big2num(byte2str(work_state)))))
def parse_adas_status(data): state = data[8:12] device_id_len = big2num(byte2str(data[12:13])) device_id = data[13:13 + device_id_len].decode('utf-8') if state == b'\x00\x00\x00\x01': txt = '授权成功' else: txt = '授权失败 返回码 {}'.format(byte2str(state)) logger.debug('—————— ADAS授权状态 {} ——————'.format(txt)) logger.debug('—————— 设备ID {} ——————'.format(device_id))
def create_sheet1(self, sheet_name, position=None): try: if position: self.wb.create_sheet(sheet_name, position) else: self.wb.create_sheet(sheet_name) return True except Exception as e: #print(e) logger.debug(e) return False
def run(self): logger.debug(threading.current_thread().getName()) while self.rec_obj.isAlive: if GlobalVar.send_address_time_out == 0: send_address_data_list = GlobalVar.send_address_dict.values() for data in list(send_address_data_list)[:3]: logger.debug('—————— 重传服务器地址 ——————') send_queue.put(data) GlobalVar.send_address_time_out = 10 else: GlobalVar.send_address_time_out -= 1 time.sleep(1)
def save_log_su(): logger.debug(threading.current_thread().getName()) t = time.strftime(r'%Y%m%d%H%M%S', time.localtime()) log_name = 'log_{}.zip'.format(t) while True: while not log_queue.empty(): data = log_queue.get(block=False) if conf.get_procotol_type_flag() == 1: log_data = data[12:-1] with open(log_name, 'ab') as f: f.write(log_data) time.sleep(0.1) time.sleep(0.1)
def parse_get_log_result_su(data): result = data[8:9] if result == b'\x00': txt = '成功' elif result == b'\x01': txt = '未找到文件' elif result == b'\x02': txt = '超时' elif result == b'\x03': txt = '失败' else: txt = '返回状态错误' logger.debug('—————— 获取日志状态 {} ——————'.format(txt))
def power_on(self): self.isSaved = 0 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('192.168.1.190', 6000)) sock.listen(5) conn, addr = sock.accept() conn.sendall(self.PWR_ON_CMD) data = conn.recv(1024) if data == b'OK\r\n': self.poweron_time = datetime.now() sock.close() logger.debug("设备上电。")
def power_off(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('192.168.1.190', 6000)) sock.listen(5) conn, addr = sock.accept() conn.sendall(self.PWR_OFF_CMD) data = conn.recv(1024) if data == b'OK\r\n': self.poweroff_time = datetime.now() sock.close() logger.debug("设备断电。") logger.debug("超时计数器关闭。") self.break_time_out_flag = True logger.debug("关机时间重置为0。") logger.debug("超时时间重置为0。") self.reset_power_timer() self.reset_timer() if self.connect_time == datetime.strptime("000101000000", '%y%m%d%H%M%S'): self.connect_time = "TimeOut" if self.located_time == datetime.strptime("000101000000", '%y%m%d%H%M%S'): self.located_time = "TimeOut" self.write_record() reset_thread = threading.Thread(target=self.reset) reset_thread.start() self.poweron_time = datetime.strptime("000101000000", '%y%m%d%H%M%S') self.connect_time = datetime.strptime("000101000000", '%y%m%d%H%M%S') self.located_time = datetime.strptime("000101000000", '%y%m%d%H%M%S')
def send_mail(attach_file_path): mail_host = ProjConfigVar.mail_host mail_user = ProjConfigVar.mail_user mail_pass = ProjConfigVar.mail_pass sender = ProjConfigVar.sender receivers = ProjConfigVar.receivers #创建一个带附件类型的MIMEMultipart()实例 message = MIMEMultipart() #对邮件属性赋值 message['From'] = formataddr(['李国芳', '*****@*****.**']) message['To'] = ''.join(receivers) subject = '接口自动化执行测试报告' message['Subject'] = Header(subject, 'utf-8') #邮件正文内容 message.attach(MIMEText('最新执行的接口自动化测试报告,请参阅附件内容!', 'plain', 'utf-8')) #构造附件1,传送测试结果的exce文件 #print("os.path.exists(attach_file_path):%s" % os.path.exists(attach_file_path)) logger.info("os.path.exists(attach_file_path):%s" % os.path.exists(attach_file_path)) logger.info(os.path.exists(attach_file_path)) #构建附件对象,添加html文件 att = MIMEBase('application', 'octet-stream') att.set_payload(open(attach_file_path, 'rb').read()) att.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', '接口自动化测试报告.html')) encoders.encode_base64(att) message.attach(att) try: #创建一个SMTP对象 smtpObj = smtplib.SMTP(mail_host) #传入用户名和密码登录邮箱 smtpObj.login(mail_user, mail_pass) #发送传参数包括发件人地址,收件人地址和邮件内容 smtpObj.sendmail(sender, receivers, message.as_string()) #print("邮件发送成功") logger.info("邮件发送成功") except smtplib.SMTPException as e: #print("Error:无法发送邮件",e) logger.debug(e)
def run(self): if conf.get_protocol_type() == 1: logger.debug(threading.current_thread().getName()) conf_path = os.path.join('TestData', '苏标外设实时同步数据.xls') while self.rec_obj.isAlive: table = GetTestData(conf_path) table.open() test_point, data = table.get_excel_data() if ' ' in data: data = ''.join(data.split()) lock.acquire() send_queue.put(data) lock.release() time.sleep(0.5)
def send_request(interface_name, data): #data_handler主要用于数据处理,因为excel读取的数据都是字符串,需要转换为对应的格式,比如字典,列表,元组等格式, # 因为授权信息为元组格式,body是json格式,在转换格式之前先进行变量处理,因此会调用data_handler函数处理 data = data_handler(data) try: responseObj = api_request( eval(interface_name)[1], eval(interface_name)[0], eval(data)) #返回请求对象和请求数据 return responseObj, data except Exception as e: #print("调用接口的函数参数出错,调用的参数为%s"%interface_name,"\n错误信息是:",e) logger.debug("调用接口的函数参数出错,调用的参数为%s" % interface_name, "\n错误信息是:", e) return None, data
def write_record(self): file = 'TestData/上下电测试数据.xlsx' newline = pd.DataFrame() newline['上电时间'] = [self.poweron_time] newline['连接平台时间'] = [self.connect_time] newline['定位成功时间'] = [self.located_time] logger.debug(newline) if os.path.exists(file): df = pd.read_excel(file) else: df = pd.DataFrame() new_df = df.append(newline) new_df.to_excel(file, index=None) self.isSaved = True
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_add_or_modify_person_youwei(data): if data[7:8] == b'\xb0': txt = '添加人员(B0)' elif data[7:8] == b'\xb1': txt = '修改人员(B1)' msg_body = data[8:-1] result = byte2str(msg_body[0:1]) person_id = big2num(byte2str(msg_body[1:5])) logger.debug('———————————————— {} ————————————————'.format(txt)) logger.debug("结果码: {}".format(result)) logger.debug("人员ID: {}".format(person_id)) logger.debug('———————————————— END ————————————————')
def run(self): logger.debug(threading.current_thread().getName()) while True: try: data, command = rec_alarm_queue.get_nowait() except queue.Empty: data = None if data: if conf.get_protocol_type() == 1: func = parse_type_su.get(command) if func: func(data) elif conf.get_protocol_type() == 2: func = parse_type.get(command) if func: func(data) elif conf.get_protocol_type() == 3 or conf.get_protocol_type( ) == 5: GetMediaThread.parse_media_upload(data) time.sleep(0.001)
def get_unique_number_value(unique_number): global global_vars try: with open(data_file, "rb") as fp: var = pickle.load( fp ) # 读取pickle序列化字节流文件中内容,反序列化成python的字典对象:{"unique_num1":100,"unique_num2":1000} data = var[unique_number] # 获取字典对象中key为unique_number的值 #print("全局唯一数当前生成的值是:%s" %data) logger.info("全局唯一数当前生成的值是:%s" % data) global_vars[unique_number] = str(data) var[unique_number] += 1 # 把字典对象中key为unique_number的值进行加一操作,以便下提取时保持唯一 with open(data_file, "wb") as fp: pickle.dump(var, fp) # 修改后的字典对象,序列化到字节流文件中 except Exception as e: #print("获取测试框架的全局唯一数变量值失败,请求的全局唯一数变量是%s,异常原因如下:%s" %(unique_number,e)) logger.debug(e) data = None finally: return data
def parse_upgrade_result_youwei(data): msg_body = data[8:-1] command = byte2str(msg_body[0:1]) result = byte2str(msg_body[1:2]) if command == '09': logger.debug('———————————————— 有为升级结果 ————————————————') logger.debug("升级结果: {}".format(result)) logger.debug('———————————————— END ————————————————')
def parse_face_data_check(data): file_length = big2num(byte2str(data[8:9])) face_name = data[9:9 + file_length].decode('utf-8') face_id_length = big2num(byte2str(data[9 + file_length:10 + file_length])) face_id = data[10 + file_length:10 + file_length + face_id_length].decode('utf-8') result = data[10 + file_length + face_id_length:11 + file_length + face_id_length] logger.debug('———————————————— 人脸图片有效性检查结果通知 ————————————————') logger.debug("文件名称: {}".format(face_name)) logger.debug("人脸ID: {}".format(face_id)) logger.debug("检查结果: {}".format(byte2str(result))) logger.debug('———————————————— END ————————————————') msg_body = '033D' + '65' + 'E5' data = '7E' + calc_check_code(msg_body) + num2big( get_serial_no()) + msg_body + '7E' send_queue.put(data)
def parse_add_face_youwei(data): msg_body = data[8:-1] result = byte2str(msg_body[0:1]) image_id = big2num(byte2str(msg_body[1:5])) person_id = big2num(byte2str(msg_body[5:9])) if result == '00': event_youwei.set() else: logger.debug('人脸数据下发返回失败') logger.debug("人脸ID: {}".format(image_id)) logger.debug("人员ID: {}".format(person_id))