commit ff64c4eab0b41b6c326ab0bfc63f3387e0be63fa Author: sunyugang Date: Thu May 15 14:59:45 2025 +0800 初始化 diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..bee405a --- /dev/null +++ b/config.ini @@ -0,0 +1,12 @@ +[mqtt] +send_client_id = linuoruite_mqtt_send_xkrs +send_address = 192.168.8.77 +send_port = 1883 +send_username = emqx +send_password = !QAZ0okm + +receive_client_id = linuoruite_mqtt_receive_xkrs +receive_address = 192.168.129.10 +receive_port = 1883 +receive_username = +receive_password = \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..e51d08e --- /dev/null +++ b/main.py @@ -0,0 +1,461 @@ +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/220202020240301008/XT/up/system') + # 边框上料-机器人 + client.subscribe('linuoruite/220202020240301008/BKSLroboot/up/system') + # 边框上料-边框上料工站 + client.subscribe('linuoruite/220202020240301008/BKSLstation/up/system') + # 边框上料-标签粘贴打印机 + client.subscribe('linuoruite/220202020240301008/BKSLprinter/up/system') + # 板芯安装-板芯安装机器人 + client.subscribe('linuoruite/220202020240301008/BXAZroboot/up/system') + # 板芯安装-板芯红环检测,此板块不进行任何对接 + # 板芯安装-撕膜机 + client.subscribe('linuoruite/220202020240301008/BXAZsmj/up/system') + # 板芯安装-板芯安装工站 + client.subscribe('linuoruite/220202020240301008/BXAZstation/up/system') + # 护丝帽安装-护丝帽工站 + client.subscribe('linuoruite/220202020240301008/MFQAZstation/up/system') + # 护丝帽安装-护丝帽安装机器人 + client.subscribe('linuoruite/220202020240301008/MFQAZroboot/up/system') + # 卡子安装-卡子工站 + client.subscribe('linuoruite/220202020240301008/KZAZstation/up/system') + # 卡子安装-卡子安装机器人 + client.subscribe('linuoruite/220202020240301008/KZAZroboot/up/system') + # 自动涂胶-涂胶机 + client.subscribe('linuoruite/220202020240301008/TJtjj/up/system') + # 自动涂胶-自动涂胶机器人 + client.subscribe('linuoruite/220202020240301008/TJrobot/up/system') + # 玻璃安装-玻璃安装机器人 + client.subscribe('linuoruite/220202020240301008/BLAZrobot/up/system') + # 玻璃安装-玻璃安装工站 + client.subscribe('linuoruite/220202020240301008/BLAZstation/up/system') + # 压条安装-压条专机 + client.subscribe('linuoruite/220202020240301008/YTAZstation/up/system') + # LOGO喷印工站-LOGO喷印机 + client.subscribe('linuoruite/220202020240301008/PYTMstation/up/system') + # AI外观检测-相机 + client.subscribe('linuoruite/220202020240301008/WGJCstation/up/system') + # 自动贴膜-自动贴膜工站 + client.subscribe('linuoruite/220202020240301008/ZDTMstation/up/system') + # 自动包装-包装机 + client.subscribe('linuoruite/220202020240301008/ZDBZpacking/up/system') + # 自动包装-打包机 + client.subscribe('linuoruite/220202020240301008/ZDBZbundler/up/system') + # 码垛入库-机器人 + client.subscribe('linuoruite/220202020240301008/MDRKrobot/up/system') + # 码垛入库-码垛入库工站 + client.subscribe('linuoruite/220202020240301008/MDRKstation/up/system') + # 码垛入库-打印机 + client.subscribe('linuoruite/220202020240301008/MDRKprinter/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/220202020240301008/XT/up/system': + if receive_data[0]['XT_breakdown'] == 1: + p_status = '4' + elif receive_data[1]['XT_pause'] == 1: + p_status = '5' + elif receive_data[2]['XT_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + XT_upph = receive_data[3]['XT_upph'] + send_data['state']['reported']['XT_upph'] = XT_upph + XT_xiaohao = receive_data[4]['XT_xiaohao'] + send_data['state']['reported']['XT_xiaohao'] = XT_xiaohao + elif topic == 'linuoruite/220202020240301008/BKSLroboot/up/system': + if receive_data[0]['BKSL_robot_breakdown'] == 1: + p_status = '4' + elif receive_data[1]['BKSL_robot_pause'] == 1: + p_status = '5' + elif receive_data[2]['BKSL_robot_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + elif topic == 'linuoruite/220202020240301008/BKSLstation/up/system': + if receive_data[3]['BKSL_station_breakdown'] == 1: + p_status = '4' + elif receive_data[4]['BKSL_station_pause'] == 1: + p_status = '5' + elif receive_data[5]['BKSL_station_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + BKSL_beat = receive_data[0]['BKSL_beat'] + send_data['state']['reported']['BKSL_beat'] = BKSL_beat + BKSL_count = receive_data[1]['BKSL_count'] + send_data['state']['reported']['BKSL_count'] = BKSL_count + BKSL_encoding = receive_data[2]['BKSL_encoding'] + send_data['state']['reported']['BKSL_encoding'] = BKSL_encoding + elif topic == 'linuoruite/220202020240301008/BKSLprinter/up/system': + if receive_data[0]['BKSL_printer_breakdown'] == 1: + p_status = '4' + elif receive_data[1]['BKSL_printer_pause'] == 1: + p_status = '5' + elif receive_data[3]['BKSL_printer_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + BKSL_printer_qlyj = receive_data[2]['BKSL_printer_qlyj'] + send_data['state']['reported']['BKSL_printer_qlyj'] = BKSL_printer_qlyj + elif topic == 'linuoruite/220202020240301008/BXAZroboot/up/system': + if receive_data[1]['BXAZ_robot_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['BXAZ_robot_pause'] == 1: + p_status = '5' + elif receive_data[3]['BXAZ_robot_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + BXAZ_robot_beat = receive_data[0]['BXAZ_robot_beat'] + send_data['state']['reported']['BXAZ_robot_beat'] = BXAZ_robot_beat + BXAZ_time = receive_data[4]['BXAZ_time'] + send_data['state']['reported']['BXAZ_time'] = BXAZ_time + elif topic == 'linuoruite/220202020240301008/BXAZsmj/up/system': + if receive_data[1]['BXAZ_smj_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['BXAZ_smj_pause'] == 1: + p_status = '5' + elif receive_data[4]['BXAZ_smj_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + BXAZ_smj_beat = receive_data[0]['BXAZ_smj_beat'] + send_data['state']['reported']['BXAZ_smj_beat'] = BXAZ_smj_beat + BXAZ_smj_result = receive_data[3]['BXAZ_smj_result'] + send_data['state']['reported']['BXAZ_smj_result'] = BXAZ_smj_result + elif topic == 'linuoruite/220202020240301008/BXAZstation/up/system': + if receive_data[1]['BXAZ_station_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['BXAZ_station_pause'] == 1: + p_status = '5' + elif receive_data[3]['BXAZ_station_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + BXAZ_beat = receive_data[0]['BXAZ_beat'] + send_data['state']['reported']['BXAZ_beat'] = BXAZ_beat + elif topic == 'linuoruite/220202020240301008/MFQAZstation/up/system': + if receive_data[1]['MFQAN_station_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['MFQAN_station_pause'] == 1: + p_status = '5' + elif receive_data[3]['MFQAN_station_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + MFQAN_beat = receive_data[0]['MFQAN_beat'] + send_data['state']['reported']['MFQAN_beat'] = MFQAN_beat + MFQAN_time = receive_data[4]['MFQAN_time'] + send_data['state']['reported']['MFQAN_time'] = MFQAN_time + elif topic == 'linuoruite/220202020240301008/MFQAZroboot/up/system': + if receive_data[0]['MFQAN_robot_breakdown'] == 1: + p_status = '4' + elif receive_data[1]['MFQAN_robot_pause'] == 1: + p_status = '5' + elif receive_data[2]['MFQAZ_robot_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + elif topic == 'linuoruite/220202020240301008/KZAZstation/up/system': + if receive_data[1]['KZAZ_station_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['KZAZ_station_pause'] == 1: + p_status = '5' + elif receive_data[3]['KZAZ_station_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + KZAZ_station_beat = receive_data[0]['KZAZ_station_beat'] + send_data['state']['reported']['KZAZ_station_beat'] = KZAZ_station_beat + KZAZ_station_time = receive_data[4]['KZAZ_station_time'] + send_data['state']['reported']['KZAZ_station_time'] = KZAZ_station_time + elif topic == 'linuoruite/220202020240301008/KZAZroboot/up/system': + if receive_data[0]['KZAZ_robot_breakdown'] == 1: + p_status = '4' + elif receive_data[1]['KZAZ_robot_pause'] == 1: + p_status = '5' + elif receive_data[2]['KZAZ_robot_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + elif topic == 'linuoruite/220202020240301008/TJtjj/up/system': + if receive_data[1]['TJ_tjj_breakdown'] == 1: + p_status = '4' + elif receive_data[3]['TJ_tjj_pause'] == 1: + p_status = '5' + elif receive_data[7]['TJ_tjj_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + TJ_tjj_beat = receive_data[0]['TJ_tjj_beat'] + send_data['state']['reported']['TJ_tjj_beat'] = TJ_tjj_beat + TJ_tjj_glue = receive_data[2]['TJ_tjj_glue'] + send_data['state']['reported']['TJ_tjj_glue'] = TJ_tjj_glue + TJ_tjj_product = receive_data[4]['TJ_tjj_product'] + send_data['state']['reported']['TJ_tjj_product'] = TJ_tjj_product + TJ_tjj_refill = receive_data[5]['TJ_tjj_refill'] + send_data['state']['reported']['TJ_tjj_refill'] = TJ_tjj_refill + TJ_tjj_remain = receive_data[6]['TJ_tjj_remain'] + send_data['state']['reported']['TJ_tjj_remain'] = TJ_tjj_remain + TJ_tjj_stress = receive_data[8]['TJ_tjj_stress'] + send_data['state']['reported']['TJ_tjj_stress'] = TJ_tjj_stress + elif topic == 'linuoruite/220202020240301008/TJrobot/up/system': + if receive_data[1]['TJ_robot_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['TJ_robot_pause'] == 1: + p_status = '5' + elif receive_data[3]['TJ_robot_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + TJ_beat = receive_data[0]['TJ_beat'] + send_data['state']['reported']['TJ_beat'] = TJ_beat + TJ_time = receive_data[4]['TJ_time'] + send_data['state']['reported']['TJ_time'] = TJ_time + elif topic == 'linuoruite/220202020240301008/BLAZrobot/up/system': + if receive_data[0]['BLAZ_robot_breakdown'] == 1: + p_status = '4' + elif receive_data[1]['BLAZ_robot_pause'] == 1: + p_status = '5' + elif receive_data[2]['BLAZ_robot_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + elif topic == 'linuoruite/220202020240301008/BLAZstation/up/system': + if receive_data[2]['BLAZ_station_breakdown'] == 1: + p_status = '4' + elif receive_data[3]['BLAZ_station_pause'] == 1: + p_status = '5' + elif receive_data[4]['BLAZ_station_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + BLAZ_quantity = receive_data[0]['BLAZ_quantity'] + send_data['state']['reported']['BLAZ_quantity'] = BLAZ_quantity + BLAZ_station_beat = receive_data[1]['BLAZ_station_beat'] + send_data['state']['reported']['BLAZ_station_beat'] = BLAZ_station_beat + BLAZ_time = receive_data[5]['BLAZ_time'] + send_data['state']['reported']['BLAZ_time'] = BLAZ_time + elif topic == 'linuoruite/220202020240301008/YTAZstation/up/system': + if receive_data[1]['YTAZ_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['YTAZ_pause'] == 1: + p_status = '5' + elif receive_data[4]['YTAZ_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + YTAZ_beat = receive_data[0]['YTAZ_beat'] + send_data['state']['reported']['YTAZ_beat'] = YTAZ_beat + YTAZ_quantity = receive_data[3]['YTAZ_quantity'] + send_data['state']['reported']['YTAZ_quantity'] = YTAZ_quantity + YTAZ_time = receive_data[5]['YTAZ_time'] + send_data['state']['reported']['YTAZ_time'] = YTAZ_time + YTAZ_yj = receive_data[6]['YTAZ_yj'] + send_data['state']['reported']['YTAZ_yj'] = YTAZ_yj + elif topic == 'linuoruite/220202020240301008/PYTMstation/up/system': + if receive_data[0]['PYTM_inkjet_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['PYTM_inkjet_pause'] == 1: + p_status = '5' + elif receive_data[3]['PYTM_inkjet_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + PYTM_inkjet_qlyj = receive_data[1]['PYTM_inkjet_qlyj'] + send_data['state']['reported']['PYTM_inkjet_qlyj'] = PYTM_inkjet_qlyj + PYTM_beat = receive_data[4]['PYTM_beat'] + send_data['state']['reported']['PYTM_beat'] = PYTM_beat + elif topic == 'linuoruite/220202020240301008/WGJCstation/up/system': + if receive_data[4]['WGJC_breakdown'] == 1: + p_status = '4' + elif receive_data[8]['WGJC_pause'] == 1: + p_status = '5' + elif receive_data[11]['WGJC_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + WGJC_beat = receive_data[0]['WGJC_beat'] + send_data['state']['reported']['WGJC_beat'] = WGJC_beat + WGJC_bkhh = receive_data[1]['WGJC_bkhh'] + send_data['state']['reported']['WGJC_bkhh'] = WGJC_bkhh + WGJC_blhh = receive_data[2]['WGJC_blhh'] + send_data['state']['reported']['WGJC_blhh'] = WGJC_blhh + WGJC_bq = receive_data[3]['WGJC_bq'] + send_data['state']['reported']['WGJC_bq'] = WGJC_bq + WGJC_cpm = receive_data[5]['WGJC_cpm'] + send_data['state']['reported']['WGJC_cpm'] = WGJC_cpm + WGJC_jcjg = receive_data[6]['WGJC_jcjg'] + send_data['state']['reported']['WGJC_jcjg'] = WGJC_jcjg + WGJC_jcqx = receive_data[7]['WGJC_jcqx'] + send_data['state']['reported']['WGJC_jcqx'] = WGJC_jcqx + WGJC_peijian = receive_data[9]['WGJC_peijian'] + send_data['state']['reported']['WGJC_peijian'] = WGJC_peijian + WGJC_pycs = receive_data[10]['WGJC_pycs'] + send_data['state']['reported']['WGJC_pycs'] = WGJC_pycs + WGJC_yj = receive_data[12]['WGJC_yj'] + send_data['state']['reported']['WGJC_yj'] = WGJC_yj + WGJC_ytfx = receive_data[13]['WGJC_ytfx'] + send_data['state']['reported']['WGJC_ytfx'] = WGJC_ytfx + elif topic == 'linuoruite/220202020240301008/ZDTMstation/up/system': + if receive_data[1]['ZDTM_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['ZDTM_pause'] == 1: + p_status = '5' + elif receive_data[3]['ZDTM_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + ZDTM_beat = receive_data[0]['ZDTM_beat'] + send_data['state']['reported']['ZDTM_beat'] = ZDTM_beat + elif topic == 'linuoruite/220202020240301008/ZDBZpacking/up/system': + if receive_data[1]['ZDBZ_packing_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['ZDBZ_packing_pause'] == 1: + p_status = '5' + elif receive_data[4]['ZDBZ_packing_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + ZDBZ_packing_beat = receive_data[0]['ZDBZ_packing_beat'] + send_data['state']['reported']['ZDBZ_packing_beat'] = ZDBZ_packing_beat + ZDBZ_packing_qlyj = receive_data[3]['ZDBZ_packing_qlyj'] + send_data['state']['reported']['ZDBZ_packing_qlyj'] = ZDBZ_packing_qlyj + elif topic == 'linuoruite/220202020240301008/ZDBZbundler/up/system': + if receive_data[1]['ZDBZ_bundler_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['ZDBZ_bundler_pause'] == 1: + p_status = '5' + elif receive_data[4]['ZDBZ_bundler_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + ZDBZ_bundler_beat = receive_data[0]['ZDBZ_bundler_beat'] + send_data['state']['reported']['ZDBZ_bundler_beat'] = ZDBZ_bundler_beat + ZDBZ_bundler_qlyj = receive_data[3]['ZDBZ_bundler_qlyj'] + send_data['state']['reported']['ZDBZ_bundler_qlyj'] = ZDBZ_bundler_qlyj + elif topic == 'linuoruite/220202020240301008/MDRKrobot/up/system': + if receive_data[0]['MDRK_robot_breakdown'] == 1: + p_status = '4' + elif receive_data[1]['MDRK_robot_puase'] == 1: + p_status = '5' + elif receive_data[2]['MDRK_robot_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + elif topic == 'linuoruite/220202020240301008/MDRKstation/up/system': + if receive_data[4]['MDRK_station_breatdown'] == 1: + p_status = '4' + elif receive_data[5]['MDRK_station_puase'] == 1: + p_status = '5' + elif receive_data[6]['MDRK_station_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + MDRK_beat = receive_data[0]['MDRK_beat'] + send_data['state']['reported']['MDRK_beat'] = MDRK_beat + MDRK_cpm = receive_data[1]['MDRK_cpm'] + send_data['state']['reported']['MDRK_cpm'] = MDRK_cpm + MDRK_quantity = receive_data[2]['MDRK_quantity'] + send_data['state']['reported']['MDRK_quantity'] = MDRK_quantity + MDRK_signal = receive_data[3]['MDRK_signal'] + send_data['state']['reported']['MDRK_signal'] = MDRK_signal + MDRK_tpm = receive_data[7]['MDRK_tpm'] + send_data['state']['reported']['MDRK_tpm'] = MDRK_tpm + elif topic == 'linuoruite/220202020240301008/MDRKprinter/up/system': + if receive_data[1]['ZDBZ_printer_breakdown'] == 1: + p_status = '4' + elif receive_data[2]['ZDBZ_printer_pause'] == 1: + p_status = '5' + elif receive_data[4]['ZDBZ_printer_run'] == 1: + p_status = '3' + send_data['state']['reported']['pStatus'] = p_status + ZDBZ_printer_beat = receive_data[0]['ZDBZ_printer_beat'] + send_data['state']['reported']['ZDBZ_printer_beat'] = ZDBZ_printer_beat + ZDBZ_printer_qlyj = receive_data[3]['ZDBZ_printer_qlyj'] + send_data['state']['reported']['ZDBZ_printer_qlyj'] = ZDBZ_printer_qlyj + 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.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.py --add-data config.ini:. + start_loop()