def mlx_init(): ret = wpi.wiringPiSetup() # wPi if ret: log(logging.ERROR, "设备初始化失败") raise WiringPiSetupError("主设备初始化失败!") bus_fd = wpi.wiringPiI2CSetup(settings.SMBus) # I2C初始化 return bus_fd
def get_ta_to(bus_fd): # 数据采集函数 ta = wpi.wiringPiI2CReadReg16(bus_fd, settings.TA) # 读取环境温度 ta = int(ta) * 0.02 - 273.15 log(logging.DEBUG, "环境温度==>%s", str(ta)) to_1 = wpi.wiringPiI2CReadReg16(bus_fd, settings.TOBJ1) # 读取物体温度to1 to_2 = wpi.wiringPiI2CReadReg16(bus_fd, settings.TOBJ2) # 这里到底是什么?好像读出来是0 log(logging.DEBUG, "To1+To2==> %d %d", int(to_1), int(to_2)) return ta, to_1, to_2
def handle_custom_cmd(self): while True: try: data = self.request.recv(1024).strip() if not data and data == b'max_906': continue menu, kind, action = data.decode('utf-8').strip().split() if menu and kind and action: func = menu_mapping[menu][kind][action] func() log(DEBUG, "[INFO]%s" % data.decode("utf-8")) except Exception as e: log(ERROR, "[ERROR]%s" % str(e)) break
def _handle_data(bus_fd): """ 数据处理, 并负责吧数据放入队列中 :param bus_fd: 从设备句柄 :return: """ ret = get_ta_to(bus_fd) if ret[2] == 0: to = ret[1]*0.02 - 273.15 to = ret[1] * 0.02 - 273.15 msg = "TA: {:.2f} TO: {:.2f} ".format(ret[0], to) log(DEBUG, msg) # 本地保存数据 try: q.put(msg, block=False) # 这样做的目的是为了客户端在连接时看到的是实时数据 except Full: log(DEBUG, "队列已满")
def handle(self): t = Thread(target=self.handle_custom_cmd) t.setDaemon(True) # 开启守护线程,主线程结束子线程也同时结束 t.start() while True: try: time.sleep(1) try: send_data = q.get(timeout=q_timeout) except Empty: log(ERROR, "队列get超时,连接断开") raise self.request.send(bytes(send_data, encoding="utf-8")) except Exception as e: gpi_write(warn_pin, 0) # 异常退出将警报和恒温关闭 gpi_write(warm_pin, 0) log(ERROR, str(e)) break
def run(): try: bus_fd = mlx_init() # init mlx_90614 gpi_init(warn_pin) # 初始化温度控制系统 gpi_init(warm_pin) # 初始化恒温控制系统 except WiringPiSetupError as e: log(ERROR, str(e)) sys.exit(1) server_t = Process(target=init) # 开启socket服务端子进程 server_t.daemon = True # 设置为守护进程 server_t.start() # 主进程处理数据 while True: try: time.sleep(1) # 采样时间间隔 _handle_data(bus_fd) except Exception as e: log(ERROR, str(e)) sys.exit(1)