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))