| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- package com.water.iot.adapter;
-
- import com.water.iot.adapter.impl.CoapAdapter;
- import com.water.iot.adapter.impl.HttpAdapter;
- import com.water.iot.adapter.impl.ModbusTcpAdapter;
- import com.water.iot.model.DeviceCommand;
- import com.water.iot.model.DeviceInfo;
- import org.springframework.stereotype.Component;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * 适配器工厂
- * 根据协议类型自动创建和管理设备适配器实例
- */
- @Component
- public class AdapterFactory {
-
- private final Map<String, DeviceAdapter> adapters = new HashMap<>();
- private final Map<String, AdapterConfig> adapterConfigs = new HashMap<>();
-
- /**
- * 注册适配器配置
- */
- public void registerAdapter(String protocol, AdapterConfig config) {
- adapterConfigs.put(protocol, config);
- }
-
- /**
- * 获取或创建适配器
- */
- public DeviceAdapter getAdapter(String protocol) {
- if (!adapters.containsKey(protocol)) {
- DeviceAdapter adapter = createAdapter(protocol);
- if (adapter != null) {
- adapters.put(protocol, adapter);
- }
- }
- return adapters.get(protocol);
- }
-
- /**
- * 获取指定协议的适配器(带配置)
- */
- public DeviceAdapter getAdapter(String protocol, String adapterName, Map<String, Object> config) {
- String key = protocol + ":" + adapterName;
- if (!adapters.containsKey(key)) {
- DeviceAdapter adapter = createAdapter(protocol, config);
- if (adapter != null) {
- adapters.put(key, adapter);
- }
- }
- return adapters.get(key);
- }
-
- /**
- * 创建适配器实例
- */
- private DeviceAdapter createAdapter(String protocol) {
- return createAdapter(protocol, null);
- }
-
- /**
- * 创建适配器实例(带配置)
- */
- private DeviceAdapter createAdapter(String protocol, Map<String, Object> config) {
- try {
- switch (protocol.toLowerCase()) {
- case "modbus_tcp":
- return createModbusTcpAdapter(config);
- case "coap":
- return createCoapAdapter(config);
- case "http":
- return createHttpAdapter(config);
- default:
- throw new IllegalArgumentException("不支持的协议类型: " + protocol);
- }
- } catch (Exception e) {
- System.err.println("创建适配器失败,协议=" + protocol + ", 错误=" + e.getMessage());
- return null;
- }
- }
-
- /**
- * 创建Modbus TCP适配器
- */
- private DeviceAdapter createModbusTcpAdapter(Map<String, Object> config) {
- if (config == null) {
- config = new HashMap<>();
- }
-
- String host = (String) config.getOrDefault("host", "localhost");
- int port = (int) config.getOrDefault("port", 502);
-
- ModbusTcpAdapter adapter = new ModbusTcpAdapter(host, port);
-
- // 连接适配器
- if (adapter.connect()) {
- System.out.println("Modbus TCP适配器创建成功: " + host + ":" + port);
- return adapter;
- } else {
- System.err.println("Modbus TCP适配器连接失败: " + host + ":" + port);
- return null;
- }
- }
-
- /**
- * 创建CoAP适配器
- */
- private DeviceAdapter createCoapAdapter(Map<String, Object> config) {
- if (config == null) {
- config = new HashMap<>();
- }
-
- String host = (String) config.getOrDefault("host", "localhost");
- int port = (int) config.getOrDefault("port", 5683);
-
- CoapAdapter adapter = new CoapAdapter(host, port);
-
- // 连接适配器
- if (adapter.connect()) {
- System.out.println("CoAP适配器创建成功: " + host + ":" + port);
- return adapter;
- } else {
- System.err.println("CoAP适配器连接失败: " + host + ":" + port);
- return null;
- }
- }
-
- /**
- * 创建HTTP适配器
- */
- private DeviceAdapter createHttpAdapter(Map<String, Object> config) {
- if (config == null) {
- config = new HashMap<>();
- }
-
- String baseUrl = (String) config.getOrDefault("base_url", "http://localhost");
-
- HttpAdapter adapter = new HttpAdapter(baseUrl);
-
- // 连接适配器
- if (adapter.connect()) {
- System.out.println("HTTP适配器创建成功: " + baseUrl);
- return adapter;
- } else {
- System.err.println("HTTP适配器连接失败: " + baseUrl);
- return null;
- }
- }
-
- /**
- * 列出所有可用的协议
- */
- public List<String> listAvailableProtocols() {
- return List.of("modbus_tcp", "coap", "http");
- }
-
- /**
- * 获取适配器信息
- */
- public AdapterInfo getAdapterInfo(String protocol) {
- DeviceAdapter adapter = getAdapter(protocol);
- if (adapter != null) {
- return adapter.getAdapterInfo();
- }
- return null;
- }
-
- /**
- * 移除适配器
- */
- public void removeAdapter(String protocol) {
- DeviceAdapter adapter = adapters.remove(protocol);
- if (adapter != null) {
- adapter.disconnect();
- }
- }
-
- /**
- * 移除所有适配器
- */
- public void removeAllAdapters() {
- for (DeviceAdapter adapter : adapters.values()) {
- adapter.disconnect();
- }
- adapters.clear();
- }
-
- /**
- * 根据设备配置自动创建适配器
- */
- public DeviceAdapter getOrCreateAdapter(Map<String, Object> deviceConfig) {
- String deviceSn = (String) deviceConfig.get("device_id");
- String protocol = (String) deviceConfig.get("protocol");
- String adapterName = (String) deviceConfig.get("adapter_name");
- Map<String, Object> adapterConfig = (Map<String, Object>) deviceConfig.get("adapter_config");
-
- System.out.println("为设备 " + deviceSn + " 创建适配器,协议=" + protocol);
-
- return getAdapter(protocol, adapterName, adapterConfig);
- }
-
- /**
- * 处理设备数据
- */
- public void processData(String protocol, byte[] payload) {
- DeviceAdapter adapter = getAdapter(protocol);
- if (adapter != null) {
- adapter.onMessage(payload);
- } else {
- System.err.println("协议 " + protocol + " 对应的适配器不存在");
- }
- }
-
- /**
- * 发送设备指令
- */
- public void sendCommand(String protocol, String deviceSn, DeviceCommand command) {
- DeviceAdapter adapter = getAdapter(protocol);
- if (adapter != null) {
- adapter.sendCommand(deviceSn, command);
- } else {
- System.err.println("协议 " + protocol + " 对应的适配器不存在");
- }
- }
-
- /**
- * 适配器配置类
- */
- public static class AdapterConfig {
- private String name;
- private String description;
- private Map<String, Object> properties;
-
- public AdapterConfig(String name, String description) {
- this.name = name;
- this.description = description;
- this.properties = new HashMap<>();
- }
-
- // Getters and Setters
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
- public Map<String, Object> getProperties() {
- return properties;
- }
-
- public void setProperties(Map<String, Object> properties) {
- this.properties = properties;
- }
- }
- }
|