DebugConfigManager.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.mes.core;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.alibaba.fastjson2.JSONWriter;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import java.io.*;
  8. import java.nio.charset.StandardCharsets;
  9. import java.nio.file.*;
  10. import java.util.*;
  11. /**
  12. * 调试配置管理器 - 持久化调试规则到文件
  13. */
  14. public class DebugConfigManager {
  15. private static final Logger log = LoggerFactory.getLogger(DebugConfigManager.class);
  16. private static final String CONFIG_DIR = "config";
  17. private static final String CONFIG_FILE = "debug_rules.json";
  18. private final Path configPath;
  19. private final Map<String, StationDebugConfig> stationConfigs = new HashMap<>();
  20. /**
  21. * 单个工位的调试配置
  22. */
  23. public static class StationDebugConfig {
  24. private String stationCode;
  25. private Map<String, Object> persistentRules = new HashMap<>();
  26. private boolean enabled = false;
  27. public StationDebugConfig() {}
  28. public StationDebugConfig(String stationCode) {
  29. this.stationCode = stationCode;
  30. }
  31. public String getStationCode() { return stationCode; }
  32. public void setStationCode(String stationCode) { this.stationCode = stationCode; }
  33. public Map<String, Object> getPersistentRules() { return persistentRules; }
  34. public void setPersistentRules(Map<String, Object> rules) { this.persistentRules = rules; }
  35. public boolean isEnabled() { return enabled; }
  36. public void setEnabled(boolean enabled) { this.enabled = enabled; }
  37. public void addRule(String fieldName, Object value) {
  38. persistentRules.put(fieldName, value);
  39. }
  40. public void removeRule(String fieldName) {
  41. persistentRules.remove(fieldName);
  42. }
  43. }
  44. public DebugConfigManager() {
  45. // 配置文件路径:当前目录/config/debug_rules.json
  46. this.configPath = Paths.get(CONFIG_DIR, CONFIG_FILE);
  47. load();
  48. }
  49. public DebugConfigManager(String configDir) {
  50. this.configPath = Paths.get(configDir, CONFIG_FILE);
  51. load();
  52. }
  53. /**
  54. * 加载配置
  55. */
  56. public void load() {
  57. if (!Files.exists(configPath)) {
  58. log.info("[DebugConfig] 配置文件不存在,使用默认配置: {}", configPath);
  59. return;
  60. }
  61. try {
  62. String content = new String(Files.readAllBytes(configPath), StandardCharsets.UTF_8);
  63. JSONObject json = JSON.parseObject(content);
  64. if (json != null && json.containsKey("stations")) {
  65. JSONObject stations = json.getJSONObject("stations");
  66. for (String stationCode : stations.keySet()) {
  67. JSONObject stationJson = stations.getJSONObject(stationCode);
  68. StationDebugConfig config = new StationDebugConfig(stationCode);
  69. config.setEnabled(stationJson.getBooleanValue("enabled"));
  70. JSONObject rules = stationJson.getJSONObject("rules");
  71. if (rules != null) {
  72. for (String fieldName : rules.keySet()) {
  73. config.addRule(fieldName, rules.get(fieldName));
  74. }
  75. }
  76. stationConfigs.put(stationCode, config);
  77. }
  78. }
  79. log.info("[DebugConfig] 加载配置成功,共 {} 个工位配置", stationConfigs.size());
  80. } catch (Exception e) {
  81. log.error("[DebugConfig] 加载配置失败: {}", e.getMessage(), e);
  82. }
  83. }
  84. /**
  85. * 保存配置
  86. */
  87. public void save() {
  88. try {
  89. // 确保目录存在
  90. Files.createDirectories(configPath.getParent());
  91. JSONObject root = new JSONObject();
  92. JSONObject stations = new JSONObject();
  93. for (Map.Entry<String, StationDebugConfig> entry : stationConfigs.entrySet()) {
  94. StationDebugConfig config = entry.getValue();
  95. JSONObject stationJson = new JSONObject();
  96. stationJson.put("enabled", config.isEnabled());
  97. stationJson.put("rules", config.getPersistentRules());
  98. stations.put(entry.getKey(), stationJson);
  99. }
  100. root.put("stations", stations);
  101. root.put("lastModified", System.currentTimeMillis());
  102. String content = JSON.toJSONString(root, JSONWriter.Feature.PrettyFormat);
  103. Files.write(configPath, content.getBytes(StandardCharsets.UTF_8));
  104. log.info("[DebugConfig] 保存配置成功: {}", configPath);
  105. } catch (Exception e) {
  106. log.error("[DebugConfig] 保存配置失败: {}", e.getMessage(), e);
  107. }
  108. }
  109. /**
  110. * 获取工位配置
  111. */
  112. public StationDebugConfig getStationConfig(String stationCode) {
  113. return stationConfigs.computeIfAbsent(stationCode, StationDebugConfig::new);
  114. }
  115. /**
  116. * 添加规则
  117. */
  118. public void addRule(String stationCode, String fieldName, Object value) {
  119. getStationConfig(stationCode).addRule(fieldName, value);
  120. save();
  121. }
  122. /**
  123. * 移除规则
  124. */
  125. public void removeRule(String stationCode, String fieldName) {
  126. getStationConfig(stationCode).removeRule(fieldName);
  127. save();
  128. }
  129. /**
  130. * 设置工位调试启用状态
  131. */
  132. public void setEnabled(String stationCode, boolean enabled) {
  133. getStationConfig(stationCode).setEnabled(enabled);
  134. save();
  135. }
  136. /**
  137. * 应用配置到拦截器
  138. */
  139. public void applyToInterceptor(String stationCode, ConditionInterceptor interceptor) {
  140. StationDebugConfig config = stationConfigs.get(stationCode);
  141. if (config != null && config.isEnabled()) {
  142. interceptor.clearRules();
  143. for (Map.Entry<String, Object> rule : config.getPersistentRules().entrySet()) {
  144. interceptor.addRule(rule.getKey(), rule.getValue());
  145. }
  146. log.info("[DebugConfig] 应用 {} 条规则到工位 {}",
  147. config.getPersistentRules().size(), stationCode);
  148. }
  149. }
  150. }