| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- package com.mes.core;
- import com.alibaba.fastjson2.JSON;
- import com.alibaba.fastjson2.JSONObject;
- import com.alibaba.fastjson2.JSONWriter;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.*;
- import java.util.*;
- /**
- * 调试配置管理器 - 持久化调试规则到文件
- */
- public class DebugConfigManager {
- private static final Logger log = LoggerFactory.getLogger(DebugConfigManager.class);
-
- private static final String CONFIG_DIR = "config";
- private static final String CONFIG_FILE = "debug_rules.json";
-
- private final Path configPath;
- private final Map<String, StationDebugConfig> stationConfigs = new HashMap<>();
-
- /**
- * 单个工位的调试配置
- */
- public static class StationDebugConfig {
- private String stationCode;
- private Map<String, Object> persistentRules = new HashMap<>();
- private boolean enabled = false;
-
- public StationDebugConfig() {}
-
- public StationDebugConfig(String stationCode) {
- this.stationCode = stationCode;
- }
-
- public String getStationCode() { return stationCode; }
- public void setStationCode(String stationCode) { this.stationCode = stationCode; }
-
- public Map<String, Object> getPersistentRules() { return persistentRules; }
- public void setPersistentRules(Map<String, Object> rules) { this.persistentRules = rules; }
-
- public boolean isEnabled() { return enabled; }
- public void setEnabled(boolean enabled) { this.enabled = enabled; }
-
- public void addRule(String fieldName, Object value) {
- persistentRules.put(fieldName, value);
- }
-
- public void removeRule(String fieldName) {
- persistentRules.remove(fieldName);
- }
- }
-
- public DebugConfigManager() {
- // 配置文件路径:当前目录/config/debug_rules.json
- this.configPath = Paths.get(CONFIG_DIR, CONFIG_FILE);
- load();
- }
-
- public DebugConfigManager(String configDir) {
- this.configPath = Paths.get(configDir, CONFIG_FILE);
- load();
- }
-
- /**
- * 加载配置
- */
- public void load() {
- if (!Files.exists(configPath)) {
- log.info("[DebugConfig] 配置文件不存在,使用默认配置: {}", configPath);
- return;
- }
-
- try {
- String content = new String(Files.readAllBytes(configPath), StandardCharsets.UTF_8);
- JSONObject json = JSON.parseObject(content);
-
- if (json != null && json.containsKey("stations")) {
- JSONObject stations = json.getJSONObject("stations");
- for (String stationCode : stations.keySet()) {
- JSONObject stationJson = stations.getJSONObject(stationCode);
- StationDebugConfig config = new StationDebugConfig(stationCode);
- config.setEnabled(stationJson.getBooleanValue("enabled"));
-
- JSONObject rules = stationJson.getJSONObject("rules");
- if (rules != null) {
- for (String fieldName : rules.keySet()) {
- config.addRule(fieldName, rules.get(fieldName));
- }
- }
-
- stationConfigs.put(stationCode, config);
- }
- }
-
- log.info("[DebugConfig] 加载配置成功,共 {} 个工位配置", stationConfigs.size());
-
- } catch (Exception e) {
- log.error("[DebugConfig] 加载配置失败: {}", e.getMessage(), e);
- }
- }
-
- /**
- * 保存配置
- */
- public void save() {
- try {
- // 确保目录存在
- Files.createDirectories(configPath.getParent());
-
- JSONObject root = new JSONObject();
- JSONObject stations = new JSONObject();
-
- for (Map.Entry<String, StationDebugConfig> entry : stationConfigs.entrySet()) {
- StationDebugConfig config = entry.getValue();
- JSONObject stationJson = new JSONObject();
- stationJson.put("enabled", config.isEnabled());
- stationJson.put("rules", config.getPersistentRules());
- stations.put(entry.getKey(), stationJson);
- }
-
- root.put("stations", stations);
- root.put("lastModified", System.currentTimeMillis());
-
- String content = JSON.toJSONString(root, JSONWriter.Feature.PrettyFormat);
- Files.write(configPath, content.getBytes(StandardCharsets.UTF_8));
-
- log.info("[DebugConfig] 保存配置成功: {}", configPath);
-
- } catch (Exception e) {
- log.error("[DebugConfig] 保存配置失败: {}", e.getMessage(), e);
- }
- }
-
- /**
- * 获取工位配置
- */
- public StationDebugConfig getStationConfig(String stationCode) {
- return stationConfigs.computeIfAbsent(stationCode, StationDebugConfig::new);
- }
-
- /**
- * 添加规则
- */
- public void addRule(String stationCode, String fieldName, Object value) {
- getStationConfig(stationCode).addRule(fieldName, value);
- save();
- }
-
- /**
- * 移除规则
- */
- public void removeRule(String stationCode, String fieldName) {
- getStationConfig(stationCode).removeRule(fieldName);
- save();
- }
-
- /**
- * 设置工位调试启用状态
- */
- public void setEnabled(String stationCode, boolean enabled) {
- getStationConfig(stationCode).setEnabled(enabled);
- save();
- }
-
- /**
- * 应用配置到拦截器
- */
- public void applyToInterceptor(String stationCode, ConditionInterceptor interceptor) {
- StationDebugConfig config = stationConfigs.get(stationCode);
- if (config != null && config.isEnabled()) {
- interceptor.clearRules();
- for (Map.Entry<String, Object> rule : config.getPersistentRules().entrySet()) {
- interceptor.addRule(rule.getKey(), rule.getValue());
- }
- log.info("[DebugConfig] 应用 {} 条规则到工位 {}",
- config.getPersistentRules().size(), stationCode);
- }
- }
- }
|