Files

543 lines
20 KiB
Python

# The `CpyMqttClient` class is a Python class that handles MQTT requests, sends commands to a server,
# processes server responses, and calculates various results based on received data.
from PySide6.QtCore import QObject, Signal, Slot, QTimer, QUrl, QIODevice
import os
import gzip
from io import BytesIO
from Manager.history_manager import historyManager
from Manager.product_manager import productManager
import json
from config import *
import logging
import struct
import base64
import threading
import uuid
import copy
import time
import socket
import paho.mqtt.client as mqtt
import ssl
import pandas as pd
# 配置日志记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('CpyMqttClient')
class CpyMqttClient(QObject):
reciveData = Signal(dict)
def __init__(self, address, port, dev_id, psk=None):
super().__init__()
self.dev_id = dev_id
self.init = 1
self.file_name = ""
self.scanner_name = ""
self.address = address
self.port = port
self.tempIndex = 0
self.last_server_ack_time = 0
self.cal_queue = {}
# 初始化MQTT客户端,通过SSL连接
# 使用V2.0 API版本
self.client = mqtt.Client(client_id=dev_id, callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
# 设置连接回调
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.on_disconnect = self.on_disconnect
# 设置用户名密码
self.client.username_pw_set('cpy_tgx-08_controller', 'tgzz2026.')
# 配置SSL
logger.info("Configuring SSL for MQTT connection...")
# 使用默认系统CA证书
self.client.tls_set(cert_reqs=ssl.CERT_NONE)
# 禁用证书验证,解决自签名证书问题
self.client.tls_insecure_set(True)
# 开始循环
#logger.info("Starting MQTT client loop...")
#self.client.loop_start()
self.check_timer = QTimer(self)
self.check_timer.timeout.connect(self.check_mqtt_server)
self.check_timer.start(1000)
thread = threading.Thread(target=self.main_loop)
thread.start()
def cal_queue_lock(self):
if not hasattr(self, "_cal_queue_lock"):
self._cal_queue_lock = threading.Lock()
self._cal_queue_lock.acquire()
def cal_queue_unlock(self):
try:
self._cal_queue_lock.release()
except:
pass
def check_cal_queue(self):
"""检查计算队列"""
if len(self.cal_queue) == 0:
return
logger.info(f"check cal queue: {len(self.cal_queue)} items in queue")
self.cal_queue_lock()
# 使用列表复制进行遍历,避免字典大小变化导致的错误
for uid, data in list(self.cal_queue.items()):
if time.time() - data.get("time", 0) > 5:
logger.error(f"calculate response timeout: {uid}")
del self.cal_queue[uid]
self.cal_queue_unlock()
def check_network_ready(self, host="cpycal.tgoim.com", port=8883):
try:
ip = socket.gethostbyname(host)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.5)
result = sock.connect_ex((ip, port))
sock.close()
print(result)
if result == 0:
return ip
return False
except Exception as e:
print(e)
return False
def check_mqtt_server(self):
if self.status == "init":
ip = self.check_network_ready(host='localhost')
if ip:
logger.info("Connecting to MQTT broker at cpycal.tgoim.com:8883...")
self.check_timer.stop()
self.client.connect(ip, 8883, 10)
self.client.loop_start()
def main_loop(self):
"""主循环"""
self.status = "init"
while True:
print(self.status)
self.check_cal_queue()
time.sleep(1)
if self.status == "init__":
if self.check_network_ready():
# 连接到MQTT服务器
logger.info("Connecting to MQTT broker at cpycal.tgoim.com:8883...")
self.client.connect('cpycal.tgoim.com', 8883, 10)
time.sleep(2)
else:
logger.info('network not ready...')
if self.status == "connected":
self.status = "checking"
self.check_server_status()
elif self.status == "available":
self.status = "registering"
self.register()
elif self.status == "checking":
if time.time() > self.check_server_status_time + 5:
self.status = "checking"
self.check_server_status()
elif self.status == "unavailable":
self.status = "checking"
self.check_server_status()
elif self.status == "registering":
if time.time() > self.register_time + 5:
self.status = "checking"
self.check_server_status()
elif self.status == "registered":
if time.time() > self.last_server_ack_time + 10:
self.status = "checking"
self.check_server_status()
def on_disconnect(self, client, userdata, flags, rc, properties=None):
"""断开连接回调 (V2.0 风格)"""
self.status = "disconnected"
logger.info(f"Disconnected from MQTT broker, return code: {rc}")
def on_connect(self, client, userdata, flags, rc, properties=None):
"""连接成功回调 (V2.0 风格)"""
if rc == 0:
logger.info("Connected to MQTT broker successfully")
self.status = "connected"
self.client.subscribe(f"v1/cpy/{self.dev_id}", qos=1)
else:
logger.error(f"Failed to connect to MQTT broker, return code: {rc}")
def handle_calculate_response(self, result, uid):
"""处理计算响应"""
if not result:
logger.error(f"Received calculate response with empty result: {result}")
return
try:
if not uid:
logger.error(f"Received calculate response with no 'uuid' field: {uid}")
return
self.cal_queue_lock()
_data = self.cal_queue.get(uid, None)
self.cal_queue_unlock()
if not _data:
logger.error(f"Received calculate response with no 'uuid' field: {uid}")
return
cal_data = copy.deepcopy(_data) #使用深拷贝
self.cal_queue_lock()
del self.cal_queue[uid] #删除队列中的数据
self.cal_queue_unlock()
if cal_data.get("time", None):
logger.info(f"calculation costs time: {time.time() - cal_data.get('time', None)}")
data_path = cal_data.get("data_path", None)
if not data_path:
logger.error(f"Received calculate response with no 'data_path' field: {data_path}")
return
info = cal_data.get("info", None)
if not info:
logger.error(f"Received calculate response with no 'info' field: {result}")
return
# 保存设备名称和文件名
scanner_id = info.get("scanner_id", None)
if not scanner_id:
logger.error(f"Received calculate response with no 'scanner_id' field: {result}")
return
result['device'] = f"scanner{scanner_id}"
# 计算额外的结果数据
processed_result = self.calResult(json.dumps(result))
# 保存结果到文件
self.save_result(data_path, processed_result)
# 发射结果信号
self.emit_recive_data(processed_result)
except json.JSONDecodeError:
logger.error("Failed to parse server response as JSON")
def handle_server_response(self, payload):
"""处理服务器响应"""
cmd = payload.get("ack", None)
error = payload.get("error", None)
result = payload.get("result", None)
if not cmd or error is None or result is None:
logger.error(f"Received message with no 'ack' field: {payload}")
return
if cmd == "status":
logger.info(f"Server status: {result}")
if error == 200:
self.status = "available"
else:
self.status = "unavailable"
elif cmd == 'register':
if error == 200:
self.status = "registered"
self.last_server_ack_time = time.time()
else:
self.status = "unavailable"
logger.info(f"Register status: {'success' if self.status == 'registered' else 'failed'}")
elif cmd == 'calculate':
if error == 200:
self.last_server_ack_time = time.time()
self.handle_calculate_response(result, payload.get("uuid", None))
else:
logger.error(f"Received calculate response with error: {error}")
def on_message(self, client, userdata, msg):
"""接收消息回调 (V2.0 风格)"""
try:
payload = json.loads(msg.payload.decode())
logger.info(f"Received message on topic {msg.topic}: {payload}")
self.handle_server_response(payload)
except json.JSONDecodeError:
logger.error(f"Failed to decode JSON from message on topic {msg.topic}")
def send_command(self, payload):
"""发送MQTT命令"""
topic = f"v1/cpy/server/1"
self.client.publish(topic, json.dumps(payload), qos=0)
def register(self):
"""注册设备到服务器"""
self.register_time = time.time()
payload = {
"cmd":"register",
"ver":1,
"dev_id": self.dev_id
}
self.send_command(payload)
def check_server_status(self):
"""检查服务器状态"""
self.check_server_status_time = time.time()
try:
payload = {
"cmd":"status",
"ver":1,
"dev_id": self.dev_id
}
self.send_command(payload)
logger.info(f"Sent status check command: {payload}")
except Exception as e:
logger.error(f"Error checking server status: {e}")
return None
def calculate_data(self, info, data, data_path):
"""计算数据"""
try:
info['uuid'] = str(uuid.uuid4())
info['cmd'] = 'calculate'
info['ver'] = 1
info['data'] = base64.b64encode(data).decode('utf-8')
self.cal_queue_lock()
self.cal_queue[info['uuid']] = {
"data_path" : data_path,
"info" : info,
"time" : time.time(),
}
self.cal_queue_unlock()
self.send_command(info)
except Exception as e:
logger.error(f"Error in calculate_data: {e}")
def compress_file_with_name(self, csv_path: str):
compressed_data = None
try:
#使用pandas读取csv文件
df = pd.read_csv(csv_path)
file_content = bytearray()
# 遍历DataFrame中的每一行
for index, row in df.iterrows():
channel_A = row['CH1']
channel_B = row['CH2']
file_content.extend(struct.pack('<ff', channel_A, channel_B))
# 使用BytesIO来创建一个可写的文件对象
compressed_file = BytesIO()
with gzip.GzipFile(fileobj=compressed_file, mode='wb') as gzf:
gzf.write(file_content)
# 读取压缩后的文件内容
compressed_data = compressed_file.getvalue()
return compressed_data
except Exception as e:
logger.error(f'Error compressing file: {e}')
return None
def compress_file_with_params(self, file_content: bytes):
compressed_data = None
try:
# 使用BytesIO来创建一个可写的文件对象
compressed_file = BytesIO()
with gzip.GzipFile(fileobj=compressed_file, mode='wb') as gzf:
gzf.write(file_content)
# 读取压缩后的文件内容
compressed_data = compressed_file.getvalue()
return compressed_data
except Exception as e:
logger.error(f'Error compressing file: {e}')
return None
@Slot(dict)
def emit_recive_data(self, data):
"""在主线程中发射信号"""
self.reciveData.emit(data)
def save_result(self, data_path: str, result: dict):
"""保存计算结果到文件"""
try:
result_path = data_path.replace('.csv', '_result.json')
if '_corrected' in result_path:
# 修正后的结果文件名
result_path = result_path.replace('_corrected', '')
# 保存结果到文件
with open(result_path, 'w') as f:
json.dump(result, f, indent=4)
logger.info(f"Result saved to {result_path}")
os.remove(data_path)
except Exception as e:
logger.error(f"Error saving result: {e}")
@Slot(int, str, str, int)
def start_cal(self, motor_index, data_path, direction, row_count):
"""开始计算,通过HTTP发送文件数据(异步实现)"""
if not self.status == "registered":
return
logger.info(f"Starting calculation with motor_index: {motor_index}, direction: {direction}")
if direction == "":
return
if not os.path.exists(data_path):
logger.error(f"File not found: {data_path}")
return
compress_file_content = self.compress_file_with_name(data_path)
if not compress_file_content:
logger.error("Failed to compress file content")
return
# 准备表单数据
info = {
"init": str(self.init),
"scanner_id": str(motor_index),
"dev_id": self.dev_id,
"version": "2",
"offset": "0",
"scale": "1",
"file_type": "raw",
"file_name": os.path.basename(data_path),
"compress": "gz",
"row_count": str(row_count),
"direction": direction
}
try:
product_info = json.loads(productManager.getCurrentProduct().get("info", "{}"))
if product_info:
pinfo = {}
pinfo["OSC"] = float(product_info["WtOSC"]["bcz"])
pinfo["INS"] = float(product_info["WtINS"]["bcz"])
pinfo["ISC"] = float(product_info["WtISC"]["bcz"])
if pinfo["OSC"] > 0.1 or pinfo["INS"] > 0.1 or pinfo["ISC"] > 0.1:
info["product_info"] = pinfo
except Exception as e:
logger.error(f"Error in getCurrentProduct: {e}")
logger.info(f"info: {info}")
# 重置init标志
self.init = 0
self.calculate_data(info, compress_file_content, data_path)
@Slot()
def set_init(self):
self.init = 1
logger.info(f"=====set init==== {self.init}")
def calResult(self, r_str):
"""处理服务器返回的计算结果"""
try:
robj = json.loads(r_str)
# 安全地获取结果中的数据,如果不存在则使用默认空列表
DmALL = robj.get("DmALL", [])
DmCORE = robj.get("DmCORE", [])
WtOSC = robj.get("WtOSC", [])
WtINS = robj.get("WtINS", [])
WtISC = robj.get("WtISC", [])
WtALL = robj.get("WtALL", [])
# 计算椭圆度
if len(DmALL) >= 3:
robj["EllipticityDmALL"] = 2*abs(DmALL[1]-DmALL[2])/(DmALL[1] + DmALL[2]) if (DmALL[1] + DmALL[2]) != 0 else 0
else:
robj["EllipticityDmALL"] = 0
if len(DmCORE) >= 3:
robj["EllipticityDmCORE"] = 2*abs(DmCORE[1]-DmCORE[2])/(DmCORE[1] + DmCORE[2]) if (DmCORE[1] + DmCORE[2]) != 0 else 0
else:
robj["EllipticityDmCORE"] = 0
# 计算平均值
robj["AvgALL"] = self.avg(WtALL)
robj["AvgISC"] = self.avg(WtISC)
robj["AvgOSC"] = self.avg(WtOSC)
robj["AvgINS"] = self.avg(WtINS)
# 计算最小值
if WtALL:
robj["MinALL"] = min(WtALL)
robj["ConcentricityALL"] = 200*min(WtALL)/(min(WtALL)+max(WtALL)) if (min(WtALL)+max(WtALL)) != 0 else 0
robj["EccentricALL"] = (max(WtALL) - min(WtALL))/2
robj["EccentricityALL"] = (max(WtALL) - min(WtALL))*100/max(WtALL) if max(WtALL) != 0 else 0
else:
robj["MinALL"] = 0
robj["ConcentricityALL"] = 0
robj["EccentricALL"] = 0
robj["EccentricityALL"] = 0
if WtISC:
robj["MinISC"] = min(WtISC)
robj["ConcentricityISC"] = 200*min(WtISC)/(min(WtISC)+max(WtISC)) if (min(WtISC)+max(WtISC)) != 0 else 0
robj["EccentricISC"] = (max(WtISC) - min(WtISC))/2
robj["EccentricityISC"] = (max(WtISC) - min(WtISC))*100/max(WtISC) if max(WtISC) != 0 else 0
else:
robj["MinISC"] = 0
robj["ConcentricityISC"] = 0
robj["EccentricISC"] = 0
robj["EccentricityISC"] = 0
if WtOSC:
robj["MinOSC"] = min(WtOSC)
robj["ConcentricityOSC"] = 200*min(WtOSC)/(min(WtOSC)+max(WtOSC)) if (min(WtOSC)+max(WtOSC)) != 0 else 0
robj["EccentricOSC"] = (max(WtOSC) - min(WtOSC))/2
robj["EccentricityOSC"] = (max(WtOSC) - min(WtOSC))*100/max(WtOSC) if max(WtOSC) != 0 else 0
else:
robj["MinOSC"] = 0
robj["ConcentricityOSC"] = 0
robj["EccentricOSC"] = 0
robj["EccentricityOSC"] = 0
if WtINS:
robj["MinINS"] = min(WtINS)
robj["ConcentricityINS"] = 200*min(WtINS)/(min(WtINS)+max(WtINS)) if (min(WtINS)+max(WtINS)) != 0 else 0
robj["EccentricINS"] = (max(WtINS) - min(WtINS))/2
robj["EccentricityINS"] = (max(WtINS) - min(WtINS))*100/max(WtINS) if max(WtINS) != 0 else 0
else:
robj["MinINS"] = 0
robj["ConcentricityINS"] = 0
robj["EccentricINS"] = 0
robj["EccentricityINS"] = 0
# 保存到历史记录
try:
historyManager.create(robj)
except Exception as hist_error:
logger.error(f"Error saving to history: {hist_error}")
return robj
except Exception as e:
logger.error(f"Error processing calculation result: {e}")
return {}
def avg(self, data):
"""计算平均值"""
if len(data) == 0:
return 0
if len(data) == 1:
return data[0]
try:
mean_value = sum(data) / len(data)
return mean_value
except TypeError:
logger.error("Error calculating average: data contains non-numeric values")
return 0
@Slot(object)
def psk_required(self, auth):
# HTTP客户端不需要PSK认证
pass