import os import json import threading import paho.mqtt.enums import paho.mqtt.client as mqtt from configparser import ConfigParser def on_send_connect(client, userdata, flags, rc, p): if rc == 'Success': print("mqtt:发送客户端连接成功") # 当客户端连接到 MQTT Broker 时调用的回调函数 def on_receive_connect(client, userdata, flags, rc, p): if rc == 'Success': print("mqtt:接收客户端连接成功") # 总控-主控制柜、主线体-plc client.subscribe('linuoruite/20250709/XT/up/system') # 打口踏平工站-专机 client.subscribe('linuoruite/20250709/CY04/up/system') # 上端盖安装工站-工站 client.subscribe('linuoruite/20250709/CY06Station/up/system') # 上端盖安装工站-上端盖安装机器人 client.subscribe('linuoruite/20250709/CY06Robot/up/system') # 外桶转序工站-工站 client.subscribe('linuoruite/20250709/CY07Station/up/system') # 外桶转序工站-放外桶机器人 client.subscribe('linuoruite/20250709/CY07Robot/up/system') # 热泵水箱装配工站-工站 client.subscribe('linuoruite/20250709/CY08Station/up/system') # 热泵水箱装配工站-热泵装配机器人 client.subscribe('linuoruite/20250709/CY08Robot/up/system') # 承压水箱装配工站-工站 client.subscribe('linuoruite/20250709/CY09Station/up/system') # 承压水箱装配工站-取内胆机器人 client.subscribe('linuoruite/20250709/CY09ndRobot/up/system') # 承压水箱装配工站-装配内胆机器人 client.subscribe('linuoruite/20250709/CY09zpRobot/up/system') # 挂架安装靠块安装-自动打钉机器人 client.subscribe('linuoruite/20250709/CY10nailRobot/up/system') # 挂架安装靠块安装-靠块上料机器人 client.subscribe('linuoruite/20250709/CY10rapidRobot/up/system') # 挂架安装靠块安装-挂架上料机器人 client.subscribe('linuoruite/20250709/CY10rackRobot/up/system') # 挂架安装靠块安装-工站 client.subscribe('linuoruite/20250709/CY10Station/up/system') # 内胆泡沫装配工站-工站 client.subscribe('linuoruite/20250709/CY11Station/up/system') # 内胆泡沫装配工站-内胆泡沫装配机器人 client.subscribe('linuoruite/20250709/CY11Robot/up/system') # 下端盖安装工站-工站 client.subscribe('linuoruite/20250709/CY13Station/up/system') # 下端盖安装工站-下端盖装配机器人 client.subscribe('linuoruite/20250709/CY13Robot/up/system') # 发泡注料工站-工站 client.subscribe('linuoruite/20250709/CY14Station/up/system') # 发泡注料工站-注胶机器人 client.subscribe('linuoruite/20250709/CY14Robot/up/system') # 发泡注料工站-发泡机 client.subscribe('linuoruite/20250709/CY14FM/up/system') # 注料口扣盖工站-工站 client.subscribe('linuoruite/20250709/CY15Station/up/system') # 注料口扣盖工站-扣盖机器人 client.subscribe('linuoruite/20250709/CY15Robot/up/system') # 水箱贴标工站-工站 client.subscribe('linuoruite/20250709/CY16Station/up/system') # 水箱贴标工站-贴标机器人 client.subscribe('linuoruite/20250709/CY16Robot/up/system') # 水箱贴标工站-铭牌打印机 client.subscribe('linuoruite/20250709/CY16plaquePrinter/up/system') # 水箱贴标工站-能效打印机 client.subscribe('linuoruite/20250709/CY16efficientPrint/up/system') # 维修盖安装-工站 client.subscribe('linuoruite/20250709/CY17Station/up/system') # 维修盖安装-维修盖排列专机 client.subscribe('linuoruite/20250709/CY17special/up/system') # 维修盖安装-维修盖标签打印机 client.subscribe('linuoruite/20250709/CY17print/up/system') # 维修盖安装-维修盖装配机器人 client.subscribe('linuoruite/20250709/CY17robot/up/system') # 安全性能检测-安检仪 client.subscribe('linuoruite/20250709/CY18Station/up/system') # 外观视觉检测-工站 client.subscribe('linuoruite/20250709/CY19Station/up/system') # 自动套袋工站-专机 client.subscribe('linuoruite/20250709/CY20Station/up/system') # 自动套箱工站-工站 client.subscribe('linuoruite/20250709/CY21Station/up/system') # 自动套箱工站-套箱机器人 client.subscribe('linuoruite/20250709/CY21robot/up/system') # 预遮盖+泡沫装配工站-工站 client.subscribe('linuoruite/20250709/CY22Station/up/system') # 预遮盖+泡沫装配工站-泡沫装配机器人 client.subscribe('linuoruite/20250709/CY22robot/up/system') # 自动封箱工站-专机 client.subscribe('linuoruite/20250709/CY23Station/up/system') # 包装箱贴标工站-工站 client.subscribe('linuoruite/20250709/CY24Station/up/system') # 包装箱贴标工站-外包装打印机 client.subscribe('linuoruite/20250709/CY24print/up/system') # 自动打包工站-专机 client.subscribe('linuoruite/20250709/CY25Station/up/system') # 码垛入库工站-工站 client.subscribe('linuoruite/20250709/CY26Station/up/system') # 码垛入库工站-套箱机器人 client.subscribe('linuoruite/20250709/CY26robot/up/system') print("mqtt:接收客户端订阅成功") # 当收到消息时调用的回调函数 def on_receive_message(client, userdata, msg): topic = msg.topic receive_payload = json.loads(msg.payload.decode()) print('receive_topic:', topic, 'receive_payload:', receive_payload) receive_data = receive_payload['RTValue'] timestamp = receive_data[0]['timestamp'] # 对接的json格式 data_str = ''' { "state": { "reported": { "code": 0, "pStatus": "3" } }, "clientToken": "324A7798", "timeStamp": 21692590 } ''' send_data = json.loads(data_str) send_data['timeStamp'] = timestamp p_status = '3' if topic == 'linuoruite/20250709/XT/up/system': if receive_data[3]['XT_breakdown'] == 1: p_status = '4' elif receive_data[2]['XT_pause'] == 1: p_status = '5' elif receive_data[1]['XT_run'] == 1: p_status = '3' send_data['state']['reported']['pStatus'] = p_status XT_xiaohao = receive_data[0]['XT_xiaohao'] send_data['state']['reported']['XT_xiaohao'] = XT_xiaohao XT_upph = receive_data[4]['XT_upph'] send_data['state']['reported']['XT_upph'] = XT_upph XT_sl = receive_data[5]['XT_sl'] send_data['state']['reported']['XT_sl'] = XT_sl XT_xiaxian = receive_data[6]['XT_xiaxian'] send_data['state']['reported']['XT_xiaxian'] = XT_xiaxian XT_buliang = receive_data[7]['XT_buliang'] send_data['state']['reported']['XT_buliang'] = XT_buliang elif topic == 'linuoruite/20250709/CY04/up/system': if receive_data[0]['CY04_run'] == 1: p_status = '3' elif receive_data[1]['CY04_pause'] == 1: p_status = '5' elif receive_data[2]['CY04_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY04_beat = receive_data[3]['CY04_beat'] send_data['state']['reported']['CY04_beat'] = CY04_beat elif topic == 'linuoruite/20250709/CY06Station/up/system': if receive_data[0]['CY06_run'] == 1: p_status = '3' elif receive_data[1]['CY06_pause'] == 1: p_status = '5' elif receive_data[2]['CY06_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY06_beat = receive_data[3]['CY06_beat'] send_data['state']['reported']['CY06_beat'] = CY06_beat elif topic == 'linuoruite/20250709/CY06Robot/up/system': if receive_data[0]['CY06_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY06_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY06_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY07Station/up/system': if receive_data[0]['CY07_run'] == 1: p_status = '3' elif receive_data[1]['CY07_pause'] == 1: p_status = '5' elif receive_data[2]['CY07_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY07Robot/up/system': if receive_data[0]['CY07_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY07_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY07_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY07_beat = receive_data[3]['CY07_beat'] send_data['state']['reported']['CY07_beat'] = CY07_beat elif topic == 'linuoruite/20250709/CY08Station/up/system': if receive_data[0]['CY08_run'] == 1: p_status = '3' elif receive_data[1]['CY08_pause'] == 1: p_status = '5' elif receive_data[2]['CY08_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY08_beat = receive_data[3]['CY08_beat'] send_data['state']['reported']['CY08_beat'] = CY08_beat elif topic == 'linuoruite/20250709/CY08Robot/up/system': if receive_data[0]['CY08_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY08_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY08_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY09Station/up/system': if receive_data[0]['CY09_run'] == 1: p_status = '3' elif receive_data[1]['CY09_pause'] == 1: p_status = '5' elif receive_data[2]['CY09_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY09_beat = receive_data[3]['CY09_beat'] send_data['state']['reported']['CY09_beat'] = CY09_beat elif topic == 'linuoruite/20250709/CY09ndRobot/up/system': if receive_data[0]['CY09_ndRobot_run'] == 1: p_status = '3' elif receive_data[1]['CY09_ndRobot_pause'] == 1: p_status = '5' elif receive_data[2]['CY09_ndRobot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY09zpRobot/up/system': if receive_data[0]['CY09_zpRobot_run'] == 1: p_status = '3' elif receive_data[1]['CY09_zpRobot_pause'] == 1: p_status = '5' elif receive_data[2]['CY09_zpRobot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY10nailRobot/up/system': if receive_data[2]['CY10_nailRobot_run'] == 1: p_status = '3' elif receive_data[3]['CY10_nailRobot_pause'] == 1: p_status = '5' elif receive_data[4]['CY10_nailRobot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY10_nailRobot_angle = receive_data[0]['CY10_nailRobot_angle'] send_data['state']['reported']['CY10_nailRobot_angle'] = CY10_nailRobot_angle CY10_nailRobot_torque = receive_data[1]['CY10_nailRobot_torque'] send_data['state']['reported']['CY10_nailRobot_torque'] = CY10_nailRobot_torque elif topic == 'linuoruite/20250709/CY10rapidRobot/up/system': if receive_data[0]['CY10_rapidRobot_run'] == 1: p_status = '3' elif receive_data[1]['CY10_rapidRobot_pause'] == 1: p_status = '5' elif receive_data[2]['CY10_rapidRobot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY10rackRobot/up/system': if receive_data[0]['CY10_rackRobot_run'] == 1: p_status = '3' elif receive_data[1]['CY10_rackRobot_pause'] == 1: p_status = '5' elif receive_data[2]['CY10_rackRobot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY10Station/up/system': if receive_data[0]['CY10_run'] == 1: p_status = '3' elif receive_data[1]['CY10_pause'] == 1: p_status = '5' elif receive_data[2]['CY10_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY10_beat = receive_data[3]['CY10_beat'] send_data['state']['reported']['CY10_beat'] = CY10_beat elif topic == 'linuoruite/20250709/CY11Station/up/system': if receive_data[0]['CY11_run'] == 1: p_status = '3' elif receive_data[1]['CY11_pause'] == 1: p_status = '5' elif receive_data[2]['CY11_berakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY11_beat = receive_data[3]['CY11_beat'] send_data['state']['reported']['CY11_beat'] = CY11_beat elif topic == 'linuoruite/20250709/CY11Robot/up/system': if receive_data[0]['CY11_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY11_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY11_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY13Station/up/system': if receive_data[0]['CY13_run'] == 1: p_status = '3' elif receive_data[1]['CY13_pause'] == 1: p_status = '5' elif receive_data[2]['CY06_breakdown'] == 1: p_status = '4' send_data['state']['CY13_breakdown']['pStatus'] = p_status CY13_beat = receive_data[3]['CY13_beat'] send_data['state']['reported']['CY13_beat'] = CY13_beat elif topic == 'linuoruite/20250709/CY13Robot/up/system': if receive_data[0]['CY13_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY13_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY13_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY14Station/up/system': if receive_data[0]['CY14_run'] == 1: p_status = '3' elif receive_data[1]['CY14_pause'] == 1: p_status = '5' elif receive_data[2]['CY14_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY14_beat = receive_data[3]['CY14_beat'] send_data['state']['reported']['CY14_beat'] = CY14_beat elif topic == 'linuoruite/20250709/CY14Robot/up/system': if receive_data[0]['CY14_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY14_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY14_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY14FM/up/system': if receive_data[0]['CY14_FM_run'] == 1: p_status = '3' elif receive_data[1]['CY14_FM_pause'] == 1: p_status = '5' elif receive_data[2]['CY14_FM_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY14_FM_beat = receive_data[3]['CY14_FM_beat'] send_data['state']['reported']['CY14_FM_beat'] = CY14_FM_beat CY14_FM_MMQ = receive_data[4]['CY14_FM_MMQ'] send_data['state']['reported']['CY14_FM_MMQ'] = CY14_FM_MMQ CY14_FM_IT = receive_data[5]['CY14_FM_IT'] send_data['state']['reported']['CY14_FM_IT'] = CY14_FM_IT CY14_FM_MT = receive_data[6]['CY14_FM_MT'] send_data['state']['reported']['CY14_FM_MT'] = CY14_FM_MT CY14_FM_HP = receive_data[7]['CY14_FM_HP'] send_data['state']['reported']['CY14_FM_HP'] = CY14_FM_HP CY14_FM_LP = receive_data[8]['CY14_FM_LP'] send_data['state']['reported']['CY14_FM_LP'] = CY14_FM_LP CY14_FM_FR = receive_data[9]['CY14_FM_FR'] send_data['state']['reported']['CY14_FM_FR'] = CY14_FM_FR elif topic == 'linuoruite/20250709/CY15Station/up/system': if receive_data[0]['CY15_run'] == 1: p_status = '3' elif receive_data[1]['CY15_pause'] == 1: p_status = '5' elif receive_data[2]['CY15_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY15_beat = receive_data[3]['CY15_beat'] send_data['state']['reported']['CY15_beat'] = CY15_beat elif topic == 'linuoruite/20250709/CY15Robot/up/system': if receive_data[0]['CY15_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY15_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY15_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY16Station/up/system': if receive_data[0]['CY16_run'] == 1: p_status = '3' elif receive_data[1]['CY16_pause'] == 1: p_status = '5' elif receive_data[2]['CY16_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY16_beat = receive_data[3]['CY16_beat'] send_data['state']['reported']['CY16_beat'] = CY16_beat elif topic == 'linuoruite/20250709/CY16Robot/up/system': if receive_data[0]['CY16_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY16_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY16_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY16plaquePrinter/up/system': if receive_data[0]['CY16_plaquePrinter_run'] == 1: p_status = '3' elif receive_data[1]['CY16_plaquePrinter_pause'] == 1: p_status = '5' elif receive_data[2]['CY16_plaquePrinter_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY16efficientPrint/up/system': if receive_data[0]['CY16_efficientPrint_run'] == 1: p_status = '3' elif receive_data[1]['CY16_efficientPrint_pause'] == 1: p_status = '5' elif receive_data[2]['CY16_efficientPrint_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY17Station/up/system': if receive_data[0]['CY17_run'] == 1: p_status = '3' elif receive_data[1]['CY17_pause'] == 1: p_status = '5' elif receive_data[2]['CY17_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY17_beat = receive_data[3]['CY17_beat'] send_data['state']['reported']['CY17_beat'] = CY17_beat elif topic == 'linuoruite/20250709/CY17special/up/system': if receive_data[0]['CY17_special_run'] == 1: p_status = '3' elif receive_data[1]['CY17_special_pause'] == 1: p_status = '5' elif receive_data[2]['CY17_special_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY17print/up/system': if receive_data[0]['CY17_print_run'] == 1: p_status = '3' elif receive_data[1]['CY17_print_pause'] == 1: p_status = '5' elif receive_data[2]['CY17_print_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY17robot/up/system': if receive_data[2]['CY17_Robot_run'] == 1: p_status = '3' elif receive_data[3]['CY17_Robot_pause'] == 1: p_status = '5' elif receive_data[4]['CY17_Robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY17_Robot_angle = receive_data[0]['CY17_Robot_angle'] send_data['state']['reported']['CY17_Robot_angle'] = CY17_Robot_angle CY17_Robot_beat = receive_data[1]['CY17_Robot_beat'] send_data['state']['reported']['CY17_Robot_beat'] = CY17_Robot_beat CY17_Robot_torque1 = receive_data[5]['CY17_Robot_torque1'] send_data['state']['reported']['CY17_Robot_torque1'] = CY17_Robot_torque1 CY17_Robot_torque2 = receive_data[6]['CY17_Robot_torque2'] send_data['state']['reported']['CY17_Robot_torque2'] = CY17_Robot_torque2 CY17_Robot_torque3 = receive_data[7]['CY17_Robot_torque3'] send_data['state']['reported']['CY17_Robot_torque3'] = CY17_Robot_torque3 elif topic == 'linuoruite/20250709/CY18Station/up/system': if receive_data[0]['CY18_run'] == 1: p_status = '3' elif receive_data[1]['CY18_pause'] == 1: p_status = '5' elif receive_data[2]['CY18_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY18_beat = receive_data[3]['CY18_beat'] send_data['state']['reported']['CY18_beat'] = CY18_beat CY18_IR = receive_data[4]['CY18_IR'] send_data['state']['reported']['CY18_IR'] = CY18_IR CY18_VW = receive_data[5]['CY18_VW'] send_data['state']['reported']['CY18_VW'] = CY18_VW CY18_GR = receive_data[6]['CY18_GR'] send_data['state']['reported']['CY18_GR'] = CY18_GR CY18_EHP = receive_data[7]['CY18_EHP'] send_data['state']['reported']['CY18_EHP'] = CY18_EHP CY18_ncCount = receive_data[8]['CY18_ncCount'] send_data['state']['reported']['CY18_ncCount'] = CY18_ncCount elif topic == 'linuoruite/20250709/CY19Station/up/system': if receive_data[0]['CY19_run'] == 1: p_status = '3' elif receive_data[1]['CY19_pause'] == 1: p_status = '5' elif receive_data[2]['CY19_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY19_beat = receive_data[3]['CY19_beat'] send_data['state']['reported']['CY19_beat'] = CY19_beat elif topic == 'linuoruite/20250709/CY20Station/up/system': if receive_data[0]['CY20_run'] == 1: p_status = '3' elif receive_data[1]['CY20_pause'] == 1: p_status = '5' elif receive_data[2]['CY20_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY20_beat = receive_data[3]['CY20_beat'] send_data['state']['reported']['CY20_beat'] = CY20_beat elif topic == 'linuoruite/20250709/CY21Station/up/system': if receive_data[0]['CY21_run'] == 1: p_status = '3' elif receive_data[1]['CY21_pause'] == 1: p_status = '5' elif receive_data[2]['CY21_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY21_beat = receive_data[3]['CY21_beat'] send_data['state']['reported']['CY21_beat'] = CY21_beat elif topic == 'linuoruite/20250709/CY21robot/up/system': if receive_data[0]['CY21_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY21_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY21_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY22Station/up/system': if receive_data[0]['CY22_run'] == 1: p_status = '3' elif receive_data[1]['CY22_pause'] == 1: p_status = '5' elif receive_data[2]['CY22_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY22_beat = receive_data[3]['CY22_beat'] send_data['state']['reported']['CY22_beat'] = CY22_beat elif topic == 'linuoruite/20250709/CY22robot/up/system': if receive_data[0]['CY22_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY22_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY22_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY23Station/up/system': if receive_data[0]['CY23_run'] == 1: p_status = '3' elif receive_data[1]['CY23_pause'] == 1: p_status = '5' elif receive_data[2]['CY23_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY23_beat = receive_data[3]['CY23_beat'] send_data['state']['reported']['CY23_beat'] = CY23_beat elif topic == 'linuoruite/20250709/CY24Station/up/system': if receive_data[0]['CY24_run'] == 1: p_status = '3' elif receive_data[1]['CY24_pause'] == 1: p_status = '5' elif receive_data[2]['CY24_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY24_beat = receive_data[3]['CY24_beat'] send_data['state']['reported']['CY24_beat'] = CY24_beat elif topic == 'linuoruite/20250709/CY24print/up/system': if receive_data[0]['CY24_print_run'] == 1: p_status = '3' elif receive_data[1]['CY24_print_pause'] == 1: p_status = '5' elif receive_data[2]['CY24_print_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status elif topic == 'linuoruite/20250709/CY25Station/up/system': if receive_data[0]['CY25_run'] == 1: p_status = '3' elif receive_data[1]['CY25_pause'] == 1: p_status = '5' elif receive_data[2]['CY25_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY25_beat = receive_data[3]['CY25_beat'] send_data['state']['reported']['CY25_beat'] = CY25_beat elif topic == 'linuoruite/20250709/CY26Station/up/system': if receive_data[0]['CY26_run'] == 1: p_status = '3' elif receive_data[1]['CY26_pause'] == 1: p_status = '5' elif receive_data[2]['CY26_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status CY26_beat = receive_data[3]['CY26_beat'] send_data['state']['reported']['CY26_beat'] = CY26_beat elif topic == 'linuoruite/20250709/CY26robot/up/system': if receive_data[0]['CY26_robot_run'] == 1: p_status = '3' elif receive_data[1]['CY26_robot_pause'] == 1: p_status = '5' elif receive_data[2]['CY26_robot_breakdown'] == 1: p_status = '4' send_data['state']['reported']['pStatus'] = p_status send_client.publish(topic=topic, payload=json.dumps(send_data)) def run_client_loop(client): client.loop_forever() # 获取mqtt的配置连接 current_dir = os.path.dirname(os.path.abspath(__file__)) config_path = os.path.join(current_dir, 'config_cy.ini') config = ConfigParser() config.read(config_path) # 读取mom端的mqtt配置信息 send_address = config.get('mqtt', 'send_address') send_port = int(config.get('mqtt', 'send_port')) send_client_id = config.get('mqtt', 'send_client_id') send_username = config.get('mqtt', 'send_username') send_password = config.get('mqtt', 'send_password') # 读取plc端的mqtt配置信息 receive_address = config.get('mqtt', 'receive_address') receive_port = int(config.get('mqtt', 'receive_port')) receive_client_id = config.get('mqtt', 'receive_client_id') receive_username = config.get('mqtt', 'receive_username') receive_password = config.get('mqtt', 'receive_password') # 创建一个发送的MQTT和接收的MQTT客户端实例 receive_client = mqtt.Client(paho.mqtt.enums.CallbackAPIVersion.VERSION2, receive_client_id) send_client = mqtt.Client(paho.mqtt.enums.CallbackAPIVersion.VERSION2, send_client_id) def start_loop(): # 设置回调函数 receive_client.on_connect = on_receive_connect receive_client.on_message = on_receive_message send_client.on_connect = on_send_connect # 设置用户名和密码 if send_username: send_client.username_pw_set(send_username, send_password) if receive_username: receive_client.username_pw_set(receive_username, receive_password) # 开始连接 receive_client.connect(receive_address, receive_port) send_client.connect(send_address, send_port) # 启动线程分别运行两个客户端 threading.Thread(target=run_client_loop, args=(receive_client,), daemon=True).start() threading.Thread(target=run_client_loop, args=(send_client,), daemon=True).start() # 主线程保持运行 try: while True: pass except KeyboardInterrupt: print("正在关闭...") receive_client.disconnect() send_client.disconnect() if __name__ == '__main__': # pyinstaller main_cy.py --add-data config_cy.ini:. start_loop()