Files
2026-07-06 17:11:25 +08:00

468 lines
18 KiB
Python

import socket
import base64
import json
import time
import os
from unittest import result
import psutil
import struct
import csv
import threading
from collections import defaultdict
from typing import Dict, List, Tuple, Optional
import logging
from config import *
from x_ray_data_processor import processor
from PySide6.QtCore import QObject, Signal, Slot, QThread, QTimer
from scipy.ndimage import gaussian_filter1d
import numpy as np
import binascii
def crc16_modbus(data: bytes) -> int:
"""CRC-16/MODBUS算法实现,与客户端C代码完全一致"""
crc = 0xFFFF
for byte in data:
crc ^= byte
for _ in range(8):
if crc & 0x0001:
crc >>= 1
crc ^= 0xA001
else:
crc >>= 1
return crc
from http.server import BaseHTTPRequestHandler, HTTPServer
from scipy.ndimage import gaussian_filter1d
from fastapi import FastAPI, File, Form, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
# 配置日志
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
__udp_port = 50009
http_thread = None
def udp_send(data):
_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
_socket.sendto(data, ("127.0.0.1", __udp_port))
_socket.sendto(data, ("192.168.8.150", 50009)) #调试用
# 创建FastAPI应用
app = FastAPI(
title="CPY Controller API",
description="HTTP and UDP server for CPY Controller",
version="1.0.0"
)
# 配置CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# HTTP路由
@app.get("/")
async def root():
"""根路径"""
return {
"message": "Welcome to CPY Controller API!",
"endpoints": {
"post_data": "/api/data",
"multipart_endpoint": "/api/endpoint"
}
}
@app.post("/api/endpoint")
async def receive_multipart_data(
request: Request,
metadata: str = Form(...), # 接收JSON字符串
data: bytes = File(...), # 接收二进制数据,使用File但接收bytes
):
"""接收multipart/form-data格式数据"""
try:
# 解析metadata JSON
metadata_dict = json.loads(metadata)
logger.info(f"Received metadata: {metadata_dict}")
binary_data = data
print('binary_data:', binary_data[:10].hex(' '))
# 构造与原系统兼容的数据结构
data_json = {
'type': metadata_dict.get('type', 'data_points'),
'name': metadata_dict.get('name', 'daq'),
'id': metadata_dict.get('id', ''),
'version': metadata_dict.get('version', 'V8'),
'direction': metadata_dict.get('direction', ''),
'data': base64.b64encode(binary_data).decode('utf-8'),
'data_len': metadata_dict.get('data_len', len(binary_data))
}
# 发送到UDP
udp_send(json.dumps(data_json).encode())
return {
"status": "success"
}
except json.JSONDecodeError as e:
logger.error(f"Error parsing metadata: {e}")
raise HTTPException(status_code=400, detail="Invalid metadata JSON format")
except Exception as e:
logger.error(f"Error processing multipart data: {e}")
raise HTTPException(status_code=400, detail=f"Error processing data: {str(e)}")
class HttpServerEnhanced(QThread):
start_cal = Signal(int, str, str, int) # 信号:接收到消息,包含数据文件路径、发送者地址和端口
send_collect_data = Signal(dict) #
update_acquisition_info = Signal(int, dict)
def __init__(self, ip="0.0.0.0", port=50009, parent=None):
super(HttpServerEnhanced, self).__init__(parent)
global __udp_port
self.ip = ip
self.port = port
__udp_port = port
self.running = True
self.motor1_direction = ""
self.motor2_direction = ""
self.collect_object1 = {
"method" : "scanner1",
"channelA_data" :"base64",
"channelB_data" :"base64",
"intensityA" : 98,
"intensityB" : 98
}
self.collect_object2 = {
"method" : "scanner2",
"channelA_data" :"base64",
"channelB_data" :"base64",
"intensityA" : 98,
"intensityB" : 98
}
self.debug_timer = QTimer(self)
self.debug_timer.timeout.connect(self.update_debug_timer)
if YSMODE:
self.debug_timer.start(1000)
global http_thread
http_thread = threading.Thread(target=self.http_loop, args=(10008,))
http_thread.start()
def update_debug_timer(self):
import random
channelA = [random.randint(1, 1000) for _ in range(3000)]
channelB = [random.randint(1, 1000) for _ in range(3000)]
self.collect_object1['channelA_data'] = base64.b64encode(json.dumps(channelA).encode()).decode()
self.collect_object1['channelB_data'] = base64.b64encode(json.dumps(channelB).encode()).decode()
self.collect_object2['channelA_data'] = base64.b64encode(json.dumps(channelA).encode()).decode()
self.collect_object2['channelB_data'] = base64.b64encode(json.dumps(channelB).encode()).decode()
self.send_collect_data.emit(self.collect_object1)
self.send_collect_data.emit(self.collect_object2)
def parse_V8_packet(self, base64_data, data_len):
binary_data = base64.b64decode(base64_data)
channel_A = []
channel_B = []
offset = 0
print("data_len:", data_len, "len(binary_data):", len(binary_data))
bytes_len = struct.unpack('<H', binary_data[offset:offset+2])[0]
data_crc = struct.unpack('<H', binary_data[offset+2:offset+4])[0]
if bytes_len + 4 != len(binary_data):
print("bytes_len error", bytes_len, len(binary_data))
return None, None
# 计算binary_data[4:]的crc16校验和(使用与客户端相同的MODBUS算法)
crc = crc16_modbus(binary_data[4:])
if crc != data_crc:
print("crc error", hex(data_crc), hex(crc))
return None, None, None, None
offset = 4
while offset + 4 <= len(binary_data):
channel_A.append(struct.unpack('<H', binary_data[offset:offset+2])[0])
channel_B.append(struct.unpack('<H', binary_data[offset+2:offset+4])[0])
offset += 4
# file_content = bytearray()
# for i in range(data_len):
# file_content.extend(struct.pack('<ff', channel_A[i], channel_B[i]))
# 解析数据单元
# return channel_A, channel_B,file_content, data_len
return channel_A, channel_B
def parse_packet(self, base64_data):
binary_data = base64.b64decode(base64_data)
channel_A = []
channel_B = []
header_a = struct.unpack('<H', binary_data[0:2])[0]
header_b = struct.unpack('<H', binary_data[2:4])[0]
queue_remain = struct.unpack('<H', binary_data[4:6])[0]
data_len = struct.unpack('<H', binary_data[6:8])[0]
if header_a != 0xAA55 or header_b != 0x55CC:
print("unknown packet header",hex(header_a), hex(header_b))
return None
data_points = []
offset = 8
for _ in range(data_len):
if offset + 4 > len(binary_data):
print("offset:", offset, "data_len:", len(data_points))
break
channel_A.append(struct.unpack('<H', binary_data[offset:offset+2])[0])
channel_B.append(struct.unpack('<H', binary_data[offset+2:offset+4])[0])
offset += 4
# file_content = bytearray()
# for i in range(data_len):
# file_content.extend(struct.pack('<ff', channel_A[i], channel_B[i]))
# 解析数据单元
return channel_A, channel_B
def http_loop(self, port):
# 启动FastAPI服务器
uvicorn.run(
app,
host="0.0.0.0",
port=port, # 与原HTTP服务器端口一致
log_level="debug"
)
def handle_daq_info(self, msg):
"""处理 DAQ 信息"""
try:
daq_id = msg['id']
daq_info = msg['status']
print(f"Received DAQ info: {daq_id}, {daq_info}")
if daq_id in [1, 2] and 'temp' in daq_info:
self.update_acquisition_info.emit(daq_id, daq_info)
except KeyError:
print("Error: 'daq_id' key not found in message")
def run(self):
"""启动 UDP 服务器"""
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.server_socket.bind((self.ip, self.port))
self.running = True
# 增加接收缓冲区大小
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 65536) # 设置为 64KB
print(f"UDP server is running on {self.ip}:{self.port}")
while self.running:
try:
while True: # 循环接收数据
try:
data, addr = self.server_socket.recvfrom(65536) # 接收最多 64KB 数据
jsstr = data.decode().rstrip('\0')
msg = json.loads(jsstr)
msg_type = msg.get('type')
if msg_type == "dev_discovery":
self.handle_dev_discovery(msg, addr[0])
if msg_type == "daq_info":
self.handle_daq_info(msg)
if msg_type == "data_points":
self.handle_data_point(msg, addr[0])
except json.JSONDecodeError:
print(f"Error decoding JSON data: {data.decode()}")
except OSError as e:
print(f"Error receiving data: {e}") # 记录错误信息
finally:
self.stop()
def correct_data(self, filename):
# 加载数据
processor.load_data(filename)
# 自动选择圆弧区间
if processor.auto_select_arcs():
# 拟合数据
if processor.fit_data():
# 校正数据
result = processor.correct_data()
# 保存校正后的数据,去掉头尾各200个点
# filename = filename[0:-4] +"_corrected.csv"
# channel_A,channel_B,result = processor.save_corrected_data(
# result,
# filename,
# trim_edges=True,
# trim_amount=0
# )
channel_A = result['corrected_data']['CH1'].tolist()
channel_B = result['corrected_data']['CH2'].tolist()
return channel_A, channel_B
return None, None
def handle_dev_discovery(self, msg, ip_address):
if msg.get("name") == "daq":
status = msg.get("status")
if status:
# temp = status.get("temp", 0) / 10
# speed = status.get("speed", 0)
if ip_address == "192.168.100.236":
self.update_acquisition_info.emit(1, status)
elif ip_address == "192.168.100.237":
self.update_acquisition_info.emit(2, status)
def save_data_to_csv(self, filename, channel_A, channel_B):
if channel_A is None or channel_B is None:
print("invalid data packet")
return
if os.path.exists('/mnt/nvme'):
path = '/mnt/nvme/CPY_DATA/'
else:
path = '/home/admin/CPY/data/'
try:
if not os.path.exists(path):
os.makedirs(path)
except:
return None
#按年、月、日、时分出目录层级
year = time.strftime("%Y", time.localtime())
month = time.strftime("%m", time.localtime())
day = time.strftime("%d", time.localtime())
hour = time.strftime("%H", time.localtime())
path = os.path.join(path, year, month, day, hour)
if not os.path.exists(path):
os.makedirs(path)
abs_path = os.path.join(path, filename)
with open(abs_path, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['INDEX', 'CH1', 'CH2'])
for i in range(len(channel_A)):
writer.writerow([i, int(channel_A[i]), int(channel_B[i])])
return abs_path
def handle_data_point(self, msg, ip_address):
try:
print("handle data from", ip_address)
direction = ""
if msg.get("direction").upper() == "UP":
direction = "UP"
elif msg.get("direction").upper() == "DOWN":
direction = "DOWN"
else:
direction = "UNKNOWN"
return
if msg.get("name") == "daq":
if msg.get("version") == "V8":
channelA, channelB =self.parse_V8_packet(msg.get("data","") , msg.get("data_len", 0))
else:
channelA, channelB =self.parse_packet(msg.get("data","") )
if channelA is None or channelB is None:
print("invalid data packet")
return
if len(channelA) > 100:
#gause filter
channelA_filtered = gaussian_filter1d(channelA, 0.1)
channelA = np.array(channelA_filtered).tolist()
channelB_filtered = gaussian_filter1d(channelB, 0.1)
channelB = np.array(channelB_filtered).tolist()
#save data to csv file
chop_num = 300
if ip_address == "192.168.100.236" or msg.get("id","") == "1#SCANNER":
dev_name = "Scanner1"
chop_num = 1
elif ip_address == "192.168.100.237" or msg.get("id","") == "2#SCANNER":
dev_name = "Scanner2"
chop_num = 1
if direction == "UP":
channelA = channelA[chop_num:-1]
channelB = channelB[chop_num:-1]
elif direction == "DOWN":
channelA = channelA[1:-chop_num]
channelB = channelB[1:-chop_num]
save_filename = f'{time.strftime("%Y%m%d_%H%M%S")}_{dev_name}_{direction}.csv'
data_path = self.save_data_to_csv(save_filename, channelA, channelB)
channelA_average = np.mean(channelA)
channelB_average = np.mean(channelB)
# COREECTMODE = False
corrected_name = None
if COREECTMODE and channelA_average > 5000 and channelB_average > 5000:
try:
new_A, new_B = self.correct_data(data_path)
#save corrected data to csv file
if new_A is not None and new_B is not None:
corrected_name = save_filename[0:-4] +"_corrected.csv"
new_path = self.save_data_to_csv(corrected_name, new_A, new_B)
if new_path and os.path.exists(new_path):
os.remove(data_path) #remove old file
data_path = new_path
channelA = new_A
channelB = new_B
except Exception as e:
print(f"Error correcting data: {e}")
channelA_json = json.dumps(channelA)
channelB_json = json.dumps(channelB)
# print("================channelA_json",channelA_json)
# print("================channelB_json",channelB_json)
channelA_bytes = channelA_json.encode('utf-8')
channelB_bytes = channelB_json.encode('utf-8')
if ip_address == "192.168.100.236" or msg.get("id","") == "1#SCANNER":
self.collect_object1["method"] = "scanner1"
if channelA_average < 5000 or channelB_average < 5000:
print("xray low idensity")
else:
if data_path:
self.start_cal.emit(1, data_path, direction, len(channelA))
print("====emit direction[1]:", direction)
self.collect_object1['channelA_data'] = base64.b64encode(channelA_bytes).decode('utf-8')
self.collect_object1['channelB_data'] = base64.b64encode(channelB_bytes).decode('utf-8')
self.send_collect_data.emit(self.collect_object1)
elif ip_address == "192.168.100.237" or msg.get("id","") == "2#SCANNER":
self.collect_object2["method"] = "scanner2"
if channelA_average < 5000 or channelB_average < 5000:
print("xray low idensity")
else:
self.start_cal.emit(2, data_path, direction, len(channelA))
print("====emit direction[2]:", direction)
self.collect_object2['channelA_data'] = base64.b64encode(channelA_bytes).decode('utf-8')
self.collect_object2['channelB_data'] = base64.b64encode(channelB_bytes).decode('utf-8')
self.send_collect_data.emit(self.collect_object2)
except OSError as e:
print(f"Error handle_data_point: {e}") # 记录错误信息
def stop(self):
"""停止 UDP 服务器"""
self.running = False
http_thread.join()
self.wait() # 等待线程结束
def __del__(self):
self.stop() # 确保在对象销毁时停止服务器