You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.2 KiB

import json
import os
from dataclasses import (
dataclass,
field
)
from typing import Dict
import numpy as np
from dataclasses_json import dataclass_json
import models.target
import tools.macInfo
@dataclass_json
@dataclass
class Server:
port: int = 0
@dataclass_json
@dataclass
class Fps:
data: int = 0
video: int = 0
@dataclass_json
@dataclass
class Alert:
enable: bool = False
intervalSec: int = 0
@dataclass_json
@dataclass
class Win:
enable: bool = False
method: str = "median"
size: int = 1
threshold: float = 1.0
imgThreshold: float = 20.0
@dataclass_json
@dataclass
class Mqtt:
broker: str = ""
port: int = 1883
topic: str = "wybb/mqtt"
username: str = ""
password: str = ""
client_id: str = ""
@dataclass_json
@dataclass
class Upload:
mqtt: Mqtt
enable: bool = False
@dataclass_json
@dataclass
class ConfigInfo:
mac: str
server:Server
fps:Fps
alert:Alert
win:Win
upload:Upload
capture: str = "0"
# 标靶配置
targets: Dict[int, models.target.CircleTarget] = field(default_factory=dict)
class ConfigOperate:
_file_path: str
config_info: ConfigInfo
def __init__(self,path:str):
self._file_path = path
self.load2obj_sample()
def load2dict(self):
""""读取配置"""
if not os.path.exists(self._file_path):
raise FileNotFoundError(f"配置文件 {self._file_path} 不存在")
with open(self._file_path) as json_file:
config = json.load(json_file)
return config
def load2obj_sample(self):
dic=self.load2dict()
dict_str = json.dumps(dic)
self.config_info=ConfigInfo.from_json(dict_str)
# 初次加载 重置标靶初始状态
self.reset_targets_init_state()
if self.config_info.mac=="":
self.config_info.mac =tools.macInfo.get_mac_address()
def save2json_file(self):
json_str = self.config_info.to_json(indent=4)
""""更新配置"""
with open(self._file_path, 'w') as json_file:
json_file.write(json_str)
# json.dump(self, json_file, indent=4)
return None
def save_dict_config(self, dict_data:Dict):
""""更新配置"""
with open(self._file_path, 'w') as json_file:
json.dump(dict_data, json_file, indent=4)
return None
def update_dict_config(self, updates):
"""
更新配置文件中的特定字段。
:param file_path: 配置文件路径
:param updates: 包含更新内容的字典
"""
config_dict = self.load2dict()
config_dict.update(updates)
self.save_dict_config(config_dict)
def reset_targets_init_state(self):
"""重置所有标靶的初始化状态"""
for target_id, target in self.config_info.targets.items():
if target.handler_info is not None:
target.handler_info.is_init = False
def convert_to_ndarray(matrix_data):
"""
将 JSON 中的矩阵数据转换为 numpy ndarray。
:param matrix_data: JSON 中的矩阵数据(列表形式)
:return: numpy ndarray
"""
return np.array(matrix_data, dtype=np.float64)