智慧水务管理系统 - 精河县供水工程综合管理平台

AdapterFactory.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package com.water.iot.adapter;
  2. import com.water.iot.adapter.impl.CoapAdapter;
  3. import com.water.iot.adapter.impl.HttpAdapter;
  4. import com.water.iot.adapter.impl.ModbusTcpAdapter;
  5. import com.water.iot.model.DeviceCommand;
  6. import com.water.iot.model.DeviceInfo;
  7. import org.springframework.stereotype.Component;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. /**
  12. * 适配器工厂
  13. * 根据协议类型自动创建和管理设备适配器实例
  14. */
  15. @Component
  16. public class AdapterFactory {
  17. private final Map<String, DeviceAdapter> adapters = new HashMap<>();
  18. private final Map<String, AdapterConfig> adapterConfigs = new HashMap<>();
  19. /**
  20. * 注册适配器配置
  21. */
  22. public void registerAdapter(String protocol, AdapterConfig config) {
  23. adapterConfigs.put(protocol, config);
  24. }
  25. /**
  26. * 获取或创建适配器
  27. */
  28. public DeviceAdapter getAdapter(String protocol) {
  29. if (!adapters.containsKey(protocol)) {
  30. DeviceAdapter adapter = createAdapter(protocol);
  31. if (adapter != null) {
  32. adapters.put(protocol, adapter);
  33. }
  34. }
  35. return adapters.get(protocol);
  36. }
  37. /**
  38. * 获取指定协议的适配器(带配置)
  39. */
  40. public DeviceAdapter getAdapter(String protocol, String adapterName, Map<String, Object> config) {
  41. String key = protocol + ":" + adapterName;
  42. if (!adapters.containsKey(key)) {
  43. DeviceAdapter adapter = createAdapter(protocol, config);
  44. if (adapter != null) {
  45. adapters.put(key, adapter);
  46. }
  47. }
  48. return adapters.get(key);
  49. }
  50. /**
  51. * 创建适配器实例
  52. */
  53. private DeviceAdapter createAdapter(String protocol) {
  54. return createAdapter(protocol, null);
  55. }
  56. /**
  57. * 创建适配器实例(带配置)
  58. */
  59. private DeviceAdapter createAdapter(String protocol, Map<String, Object> config) {
  60. try {
  61. switch (protocol.toLowerCase()) {
  62. case "modbus_tcp":
  63. return createModbusTcpAdapter(config);
  64. case "coap":
  65. return createCoapAdapter(config);
  66. case "http":
  67. return createHttpAdapter(config);
  68. default:
  69. throw new IllegalArgumentException("不支持的协议类型: " + protocol);
  70. }
  71. } catch (Exception e) {
  72. System.err.println("创建适配器失败,协议=" + protocol + ", 错误=" + e.getMessage());
  73. return null;
  74. }
  75. }
  76. /**
  77. * 创建Modbus TCP适配器
  78. */
  79. private DeviceAdapter createModbusTcpAdapter(Map<String, Object> config) {
  80. if (config == null) {
  81. config = new HashMap<>();
  82. }
  83. String host = (String) config.getOrDefault("host", "localhost");
  84. int port = (int) config.getOrDefault("port", 502);
  85. ModbusTcpAdapter adapter = new ModbusTcpAdapter(host, port);
  86. // 连接适配器
  87. if (adapter.connect()) {
  88. System.out.println("Modbus TCP适配器创建成功: " + host + ":" + port);
  89. return adapter;
  90. } else {
  91. System.err.println("Modbus TCP适配器连接失败: " + host + ":" + port);
  92. return null;
  93. }
  94. }
  95. /**
  96. * 创建CoAP适配器
  97. */
  98. private DeviceAdapter createCoapAdapter(Map<String, Object> config) {
  99. if (config == null) {
  100. config = new HashMap<>();
  101. }
  102. String host = (String) config.getOrDefault("host", "localhost");
  103. int port = (int) config.getOrDefault("port", 5683);
  104. CoapAdapter adapter = new CoapAdapter(host, port);
  105. // 连接适配器
  106. if (adapter.connect()) {
  107. System.out.println("CoAP适配器创建成功: " + host + ":" + port);
  108. return adapter;
  109. } else {
  110. System.err.println("CoAP适配器连接失败: " + host + ":" + port);
  111. return null;
  112. }
  113. }
  114. /**
  115. * 创建HTTP适配器
  116. */
  117. private DeviceAdapter createHttpAdapter(Map<String, Object> config) {
  118. if (config == null) {
  119. config = new HashMap<>();
  120. }
  121. String baseUrl = (String) config.getOrDefault("base_url", "http://localhost");
  122. HttpAdapter adapter = new HttpAdapter(baseUrl);
  123. // 连接适配器
  124. if (adapter.connect()) {
  125. System.out.println("HTTP适配器创建成功: " + baseUrl);
  126. return adapter;
  127. } else {
  128. System.err.println("HTTP适配器连接失败: " + baseUrl);
  129. return null;
  130. }
  131. }
  132. /**
  133. * 列出所有可用的协议
  134. */
  135. public List<String> listAvailableProtocols() {
  136. return List.of("modbus_tcp", "coap", "http");
  137. }
  138. /**
  139. * 获取适配器信息
  140. */
  141. public AdapterInfo getAdapterInfo(String protocol) {
  142. DeviceAdapter adapter = getAdapter(protocol);
  143. if (adapter != null) {
  144. return adapter.getAdapterInfo();
  145. }
  146. return null;
  147. }
  148. /**
  149. * 移除适配器
  150. */
  151. public void removeAdapter(String protocol) {
  152. DeviceAdapter adapter = adapters.remove(protocol);
  153. if (adapter != null) {
  154. adapter.disconnect();
  155. }
  156. }
  157. /**
  158. * 移除所有适配器
  159. */
  160. public void removeAllAdapters() {
  161. for (DeviceAdapter adapter : adapters.values()) {
  162. adapter.disconnect();
  163. }
  164. adapters.clear();
  165. }
  166. /**
  167. * 根据设备配置自动创建适配器
  168. */
  169. public DeviceAdapter getOrCreateAdapter(Map<String, Object> deviceConfig) {
  170. String deviceSn = (String) deviceConfig.get("device_id");
  171. String protocol = (String) deviceConfig.get("protocol");
  172. String adapterName = (String) deviceConfig.get("adapter_name");
  173. Map<String, Object> adapterConfig = (Map<String, Object>) deviceConfig.get("adapter_config");
  174. System.out.println("为设备 " + deviceSn + " 创建适配器,协议=" + protocol);
  175. return getAdapter(protocol, adapterName, adapterConfig);
  176. }
  177. /**
  178. * 处理设备数据
  179. */
  180. public void processData(String protocol, byte[] payload) {
  181. DeviceAdapter adapter = getAdapter(protocol);
  182. if (adapter != null) {
  183. adapter.onMessage(payload);
  184. } else {
  185. System.err.println("协议 " + protocol + " 对应的适配器不存在");
  186. }
  187. }
  188. /**
  189. * 发送设备指令
  190. */
  191. public void sendCommand(String protocol, String deviceSn, DeviceCommand command) {
  192. DeviceAdapter adapter = getAdapter(protocol);
  193. if (adapter != null) {
  194. adapter.sendCommand(deviceSn, command);
  195. } else {
  196. System.err.println("协议 " + protocol + " 对应的适配器不存在");
  197. }
  198. }
  199. /**
  200. * 适配器配置类
  201. */
  202. public static class AdapterConfig {
  203. private String name;
  204. private String description;
  205. private Map<String, Object> properties;
  206. public AdapterConfig(String name, String description) {
  207. this.name = name;
  208. this.description = description;
  209. this.properties = new HashMap<>();
  210. }
  211. // Getters and Setters
  212. public String getName() {
  213. return name;
  214. }
  215. public void setName(String name) {
  216. this.name = name;
  217. }
  218. public String getDescription() {
  219. return description;
  220. }
  221. public void setDescription(String description) {
  222. this.description = description;
  223. }
  224. public Map<String, Object> getProperties() {
  225. return properties;
  226. }
  227. public void setProperties(Map<String, Object> properties) {
  228. this.properties = properties;
  229. }
  230. }
  231. }