From 976673a838d2cdbf6c40925f7c1e2398f3a6c01a Mon Sep 17 00:00:00 2001 From: "WIN-20230613SRA\\Administrator" <871066422@qq.com> Date: Thu, 2 Jul 2026 10:45:47 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=AE=A1=E9=81=93?= =?UTF-8?q?=E7=9B=B4=E5=BE=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 8 ++++++++ x_ray_data_processor.py | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index b6edc9e..8fb9dc1 100644 --- a/main.py +++ b/main.py @@ -13,6 +13,9 @@ import time import sqlite3 import sys import json + +from x_ray_data_processor import processor + def import_sql_to_database(db_path, table_bame): conn = sqlite3.connect(db_path) @@ -91,6 +94,11 @@ if __name__ == "__main__": config.update(sn = device_id) save_config(config) + if 'mid_pipe_diameter' in config: + val = float(config['mid_pipe_diameter']) + if val >= 100 and val <= 200: + processor.set_pipe_diameter(val) + ip = "101.200.29.204" port = 18003 try: diff --git a/x_ray_data_processor.py b/x_ray_data_processor.py index bb2ef13..775aedc 100644 --- a/x_ray_data_processor.py +++ b/x_ray_data_processor.py @@ -23,6 +23,11 @@ class XRayDataProcessor: self.derivative_data = None self.results = None self.selected_spans = {'CH1': [], 'CH2': []} + self.pipe_diameter = 130 + + def set_pipe_diameter(self, diameter): + """设置管道直径""" + self.pipe_diameter = diameter def load_data(self, filepath): """加载CSV数据文件""" @@ -256,7 +261,7 @@ class XRayDataProcessor: def _model(self, index, Q0, mu, offset): """X光束衰减模型""" - R = 65.0 + R = self.pipe_diameter / 2 t = 0.8 factor = 0.04 X = factor * (index - offset) From d23afda60ff928d8d2c22f6dd0f588eb5a21cc23 Mon Sep 17 00:00:00 2001 From: "WIN-20230613SRA\\Administrator" <871066422@qq.com> Date: Fri, 3 Jul 2026 11:32:51 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BC=98=E5=8C=96X=E5=85=89=E8=A1=B0?= =?UTF-8?q?=E5=87=8F=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- x_ray_data_processor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x_ray_data_processor.py b/x_ray_data_processor.py index 775aedc..8fbd6b1 100644 --- a/x_ray_data_processor.py +++ b/x_ray_data_processor.py @@ -265,7 +265,7 @@ class XRayDataProcessor: t = 0.8 factor = 0.04 X = factor * (index - offset) - X_clip = np.clip(X, -64.0, 64.0) + X_clip = np.clip(X, -(R-1), R-1) term1 = np.sqrt((R + t)**2 - X_clip**2) term2 = np.sqrt(R**2 - X_clip**2) L = 2 * (term1 - term2) From f0693cc3d4d846f2371516b5b9781ca1747178d1 Mon Sep 17 00:00:00 2001 From: "WIN-20230613SRA\\Administrator" <871066422@qq.com> Date: Mon, 6 Jul 2026 08:36:24 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=9B=B2=E7=BA=BF?= =?UTF-8?q?=E4=B8=AD=E7=94=B5=E7=BC=86=E8=BE=B9=E7=95=8C=E7=9A=84=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=20=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- x_ray_data_processor.py | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/x_ray_data_processor.py b/x_ray_data_processor.py index 8fbd6b1..e4144a6 100644 --- a/x_ray_data_processor.py +++ b/x_ray_data_processor.py @@ -75,6 +75,28 @@ class XRayDataProcessor: print(f"计算平滑数据和导数出错: {str(e)}") return False + def find_outer_peak(self, data, sigma=2.0, peak_threshold=0.15): + """ + 找到一阶导数绝对值的主要极值点,作为圆弧的外侧 + """ + # 1. 平滑数据,消除噪声 + smoothed = gaussian_filter1d(data, sigma=sigma) + + # 2. 从头开始,若第data[k] - data[k + 50] > data[k] * 0.2,说明k是外侧极值点 + outer_peaks = [100, len(data) - 100] + for k in range(len(data) - 50): + if smoothed[k] - smoothed[k + 50] > smoothed[k] * peak_threshold: + outer_peaks[0] = k + break + + # 3. 从尾开始,若第data[k] - data[k - 50] > data[k] * 0.2,说明k是外侧极值点 + for k in range(len(data) - 50, 0, -1): + if smoothed[k] - smoothed[k - 50] > smoothed[k] * peak_threshold: + outer_peaks[1] = k + break + + return outer_peaks + def find_derivative_peaks(self, deriv_data, sigma=5, peak_threshold=0.3): """ 精准找出一阶导数绝对值的主要极值点 @@ -155,23 +177,21 @@ class XRayDataProcessor: def _find_spans_for_channel(self, channel): """为指定通道寻找合适的区间""" - derivative = self.derivative_data[channel] + data = self.channel_data[channel] n = len(self.index_data) - abs_derivative = np.abs(derivative) - - potential_extremes = self.find_derivative_peaks(abs_derivative) + potential_extremes = self.find_outer_peak(data) # 至少有两个极值点,选择第一个和最后一个 p1 = potential_extremes[0] p2 = potential_extremes[-1] # 计算区间边界: [p1*0.2, p1*0.9], [(n-p2)*0.1+p2, (n-p2)*0.7+p2] - left_start_idx = max(0, int(p1 * 0.1)) - left_end_idx = min(n-1, int(p1 * 0.99)) + left_start_idx = max(0, int(p1 * 0.01)) + left_end_idx = min(n-1, int(p1)) - right_start_idx = min(n-1, int((n - p2) * 0.01 + p2)) - right_end_idx = min(n-1, int(n - (n - p2) * 0.1)) + right_start_idx = min(n-1, p2) + right_end_idx = min(n-1, int(n - (n - p2) * 0.01)) # 转换为INDEX值 left_span = (self.index_data[left_start_idx], self.index_data[left_end_idx]) @@ -274,10 +294,10 @@ class XRayDataProcessor: def _analyze_channel(self, channel_name, Q_data, index_data): """分析单个通道的数据""" # 参数设置 - initial_guess = [np.max(Q_data)*1.5, 0.4, np.max(index_data) * 0.5] + initial_guess = [np.max(Q_data)*2, 0.4, np.max(index_data) * 0.5] bounds = ( [np.max(Q_data), 0.3, np.max(index_data) * 0.2], - [2*np.max(Q_data), 0.5, np.max(index_data) * 0.8] + [3*np.max(Q_data), 0.5, np.max(index_data) * 0.8] ) # 执行拟合 From 98f47be9ff0f917e541696808d83944b9220a153 Mon Sep 17 00:00:00 2001 From: leon Date: Mon, 6 Jul 2026 17:11:25 +0800 Subject: [PATCH 4/7] .. --- http_server.py | 20 ++++++++++---------- x_ray_data_processor.py | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/http_server.py b/http_server.py index ce467f0..a0ea085 100644 --- a/http_server.py +++ b/http_server.py @@ -378,7 +378,7 @@ class HttpServerEnhanced(QThread): 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) @@ -388,20 +388,20 @@ class HttpServerEnhanced(QThread): #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 = 200 + chop_num = 1 elif ip_address == "192.168.100.237" or msg.get("id","") == "2#SCANNER": dev_name = "Scanner2" - chop_num = 200 + chop_num = 1 if direction == "UP": - channelA = channelA[chop_num:-200] - channelB = channelB[chop_num:-200] + channelA = channelA[chop_num:-1] + channelB = channelB[chop_num:-1] elif direction == "DOWN": - channelA = channelA[200:-chop_num] - channelB = channelB[200:-chop_num] + 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) @@ -409,7 +409,7 @@ class HttpServerEnhanced(QThread): channelA_average = np.mean(channelA) channelB_average = np.mean(channelB) - #COREECTMODE = False + # COREECTMODE = False corrected_name = None if COREECTMODE and channelA_average > 5000 and channelB_average > 5000: try: @@ -453,7 +453,7 @@ class HttpServerEnhanced(QThread): 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}") # 记录错误信息 diff --git a/x_ray_data_processor.py b/x_ray_data_processor.py index e4144a6..affa484 100644 --- a/x_ray_data_processor.py +++ b/x_ray_data_processor.py @@ -321,7 +321,7 @@ class XRayDataProcessor: for ch in ['CH1', 'CH2']: p = self.results[ch]['params_opt'] e = np.sqrt(np.diag(self.results[ch]['params_cov'])) - print(f"\n{ch}通道:") + print(f"\n{ch}通道 {self.pipe_diameter}:") print(f"Q0 = {p[0]:.1f} ± {e[0]:.1f}") print(f"μ = {p[1]:.6f} ± {e[1]:.6f} mm⁻¹") print(f"offset = {p[2]:.1f} ± {e[2]:.1f}") From 7e2b80aa3a90251ee6202d84d22f2fdf28f0141f Mon Sep 17 00:00:00 2001 From: "WIN-20230613SRA\\Administrator" <871066422@qq.com> Date: Mon, 6 Jul 2026 18:06:55 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E8=AE=A1=E7=AE=97=E6=97=B6=E9=99=84?= =?UTF-8?q?=E5=8A=A0=E7=94=B5=E7=BC=86=E4=BA=A7=E5=93=81=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Manager/product_manager.py | 28 ++++++++++++++++++++++++++++ config.py | 13 +++++++++++++ cpy_mqtt_client.py | 17 ++++++++++++++++- main.py | 14 ++------------ mqtt_client.py | 19 ++++++++++--------- x_ray_data_processor.py | 11 ++++++++++- 6 files changed, 79 insertions(+), 23 deletions(-) diff --git a/Manager/product_manager.py b/Manager/product_manager.py index 142b69c..224d5b4 100644 --- a/Manager/product_manager.py +++ b/Manager/product_manager.py @@ -1,8 +1,36 @@ from models import Product, Session import uuid +from config import * class ProductManager: def __init__(self, parent=None): super().__init__() + self.cur_product_id = "" + + config = read_config() + if config.get('product_id', '') == "": + try: + products = self.getInfo("all") + if products == []: + print("no product found in database") + else: + config.update(product_id = products[0]["id"]) + save_config(config) + except: + pass + + def setCurProductId(self, id): + self.cur_product_id = id + config = read_config() + if config.get('product_id', '') == id: + return + config.update(product_id = id) + save_config(config) + + def getCurProductId(self): + return self.cur_product_id + + def getCurrentProduct(self): + return self.getInfo(self.cur_product_id) def create(self, info): data = { diff --git a/config.py b/config.py index ffc7dc1..8ec955a 100644 --- a/config.py +++ b/config.py @@ -11,3 +11,16 @@ YSMODE = False COREECTMODE = True + +def read_config(): + config = {} + path = "/home/admin/.config/cpy/config.json" + with open(path, "r") as f: + config = json.load(f) + return config + return None + +def save_config(config): + path = "/home/admin/.config/cpy/config.json" + with open(path, "w", encoding='utf-8') as f: + json.dump(config, f, indent=4) \ No newline at end of file diff --git a/cpy_mqtt_client.py b/cpy_mqtt_client.py index 1832f4a..10bf1b8 100644 --- a/cpy_mqtt_client.py +++ b/cpy_mqtt_client.py @@ -6,6 +6,7 @@ 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 @@ -406,7 +407,21 @@ class CpyMqttClient(QObject): "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 diff --git a/main.py b/main.py index 8fb9dc1..47a1d2c 100644 --- a/main.py +++ b/main.py @@ -14,6 +14,8 @@ import sqlite3 import sys import json +from Manager.product_manager import productManager + from x_ray_data_processor import processor def import_sql_to_database(db_path, table_bame): @@ -44,18 +46,6 @@ def get_cpu_serial(): return serial -def read_config(): - config = {} - path = "/home/admin/.config/cpy/config.json" - with open(path, "r") as f: - config = json.load(f) - return config - return None - -def save_config(config): - path = "/home/admin/.config/cpy/config.json" - with open(path, "w", encoding='utf-8') as f: - json.dump(config, f, indent=4) if __name__ == "__main__": app = QCoreApplication(sys.argv) diff --git a/mqtt_client.py b/mqtt_client.py index 8948b71..4dc717e 100644 --- a/mqtt_client.py +++ b/mqtt_client.py @@ -4,7 +4,7 @@ import time import base64 import socket import httpx -from config import * + from PySide6.QtCore import Signal, Slot,QObject,QMetaObject,Qt,QTimer from Manager.product_manager import productManager from Manager.history_manager import historyManager @@ -42,7 +42,7 @@ class MQTTClient(QObject): self.remote_client = mqtt.Client(client_id=device_id) # 创建远程客户端 self.device_client = mqtt.Client(client_id = 'controller_dev_' + self.random_string(6)) # 创建设备客户端 self.m_clienttimestamp = 0 - self.cur_product_id = "C8111D73-F387-981D-18D5-849816E1793C" + # 设置回调函数 self.local_client.on_connect = self.on_connect self.local_client.on_message = self.on_message @@ -246,7 +246,7 @@ class MQTTClient(QObject): if json_obj.get("value") == "true": cid = json_obj.get("cid") print("Follow,",cid) - self.send_current_product(self.cur_product_id) + self.send_current_product(productManager.cur_product_id) elif method == "input": key = json_obj.get("key") value = json_obj.get("value") @@ -303,10 +303,11 @@ class MQTTClient(QObject): self.send_products() elif method == "currentProduct": - self.cur_product_id = json_obj.get("id") - print(f"Current product id: {self.cur_product_id}") - self.send_current_product(self.cur_product_id) - self.send_current_product_id(self.cur_product_id) + cur_product_id = json_obj.get("id") + print(f"Current product id: {cur_product_id}") + self.send_current_product(cur_product_id) + self.send_current_product_id(cur_product_id) + productManager.setCurProductId(cur_product_id) elif method == "loadHistoryList": cid = json_obj.get("cid") @@ -571,10 +572,10 @@ class MQTTClient(QObject): def send_products(self): """发送产品信息""" data = dict() - data["current_id"] = self.cur_product_id + data["current_id"] = productManager.cur_product_id data["method"] = "productList" data["infos"] = productManager.getInfo() - print(f"Current product id: {self.cur_product_id}") + print(f"Current product id: {productManager.cur_product_id}") self.publish_cpy(TOPICPRODUCT, data, 1) # 发布设备信息 def start_ys(self, flag): diff --git a/x_ray_data_processor.py b/x_ray_data_processor.py index affa484..7da091c 100644 --- a/x_ray_data_processor.py +++ b/x_ray_data_processor.py @@ -83,7 +83,10 @@ class XRayDataProcessor: smoothed = gaussian_filter1d(data, sigma=sigma) # 2. 从头开始,若第data[k] - data[k + 50] > data[k] * 0.2,说明k是外侧极值点 - outer_peaks = [100, len(data) - 100] + if len(data) < 1000: + return [0, 0] + + outer_peaks = [0, 0] for k in range(len(data) - 50): if smoothed[k] - smoothed[k + 50] > smoothed[k] * peak_threshold: outer_peaks[0] = k @@ -150,6 +153,9 @@ class XRayDataProcessor: # 为两个通道分别寻找区间 ch1_spans = self._find_spans_for_channel('CH1') ch2_spans = self._find_spans_for_channel('CH2') + + if ch1_spans == [] or ch2_spans == []: + return False # 为每个通道存储各自的区间 success_count = 0 @@ -185,6 +191,9 @@ class XRayDataProcessor: # 至少有两个极值点,选择第一个和最后一个 p1 = potential_extremes[0] p2 = potential_extremes[-1] + + if p1 == 0 and p2 == 0: + return [] # 计算区间边界: [p1*0.2, p1*0.9], [(n-p2)*0.1+p2, (n-p2)*0.7+p2] left_start_idx = max(0, int(p1 * 0.01)) From 1f13cde0a7c31b0380772cf30f51d8c10edf2d19 Mon Sep 17 00:00:00 2001 From: "WIN-20230613SRA\\Administrator" <871066422@qq.com> Date: Mon, 6 Jul 2026 18:17:23 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9import=E7=BC=BA=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 2 ++ mqtt_client.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/config.py b/config.py index 8ec955a..4456ce3 100644 --- a/config.py +++ b/config.py @@ -1,3 +1,5 @@ +import json + LOCALBORKER = "127.0.0.1" LOCALPORT = 1883 DEVICEBORKER = "127.0.0.1" diff --git a/mqtt_client.py b/mqtt_client.py index 4dc717e..0fd4e25 100644 --- a/mqtt_client.py +++ b/mqtt_client.py @@ -5,6 +5,8 @@ import base64 import socket import httpx +from config import * + from PySide6.QtCore import Signal, Slot,QObject,QMetaObject,Qt,QTimer from Manager.product_manager import productManager from Manager.history_manager import historyManager From 720a42b46521556bef7ccf69aa3d7d293a701fad Mon Sep 17 00:00:00 2001 From: "WIN-20230613SRA\\Administrator" <871066422@qq.com> Date: Tue, 7 Jul 2026 08:18:29 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=8E=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E8=AF=BB=E5=8F=96=E4=BA=A7=E5=93=81?= =?UTF-8?q?ID=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Manager/product_manager.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Manager/product_manager.py b/Manager/product_manager.py index 224d5b4..660f611 100644 --- a/Manager/product_manager.py +++ b/Manager/product_manager.py @@ -17,6 +17,8 @@ class ProductManager: save_config(config) except: pass + else: + self.cur_product_id = config.get('product_id', '') def setCurProductId(self, id): self.cur_product_id = id