| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package com.water.iot.adapter.impl;
-
- import com.water.iot.adapter.AdapterInfo;
- import com.water.iot.adapter.AdapterStatus;
- import com.water.iot.adapter.DeviceAdapter;
- import com.water.iot.model.DeviceCommand;
- import com.water.iot.model.DeviceInfo;
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * HTTP 协议适配器
- */
- public class HttpAdapter implements DeviceAdapter {
-
- private String baseUrl;
- private AdapterStatus status = AdapterStatus.DISCONNECTED;
- private long connectionTime;
- private Map<String, DeviceInfo> connectedDevices = new HashMap<>();
-
- public HttpAdapter(String baseUrl) {
- this.baseUrl = baseUrl;
- }
-
- @Override
- public String getProtocol() {
- return "http";
- }
-
- @Override
- public void onMessage(byte[] payload) {
- System.out.println("HTTP 接收到设备数据: " + new String(payload));
-
- try {
- // 解析JSON格式数据
- String message = new String(payload);
- System.out.println("HTTP消息内容: " + message);
-
- // 这里应该解析JSON并提取设备信息
- if (message.contains("deviceSn")) {
- processDeviceMessage(message);
- }
- } catch (Exception e) {
- System.err.println("处理HTTP消息时出错: " + e.getMessage());
- }
- }
-
- private void processDeviceMessage(String message) {
- System.out.println("处理HTTP设备消息: " + message);
-
- // 解析设备消息并更新设备状态
- // 在实际实现中,这里应该解析JSON并提取字段
- DeviceInfo device = new DeviceInfo("HTTP_001", "HTTP控制器", "controller");
- device.setLastSeen(System.currentTimeMillis());
-
- Map<String, Object> metrics = new HashMap<>();
- metrics.put("status", "running");
- metrics.put("temperature", 35.0);
- device.setMetrics(metrics);
-
- connectedDevices.put(device.getDeviceSn(), device);
- System.out.println("更新HTTP设备状态: " + device.getDeviceSn());
- }
-
- @Override
- public void sendCommand(String deviceSn, DeviceCommand cmd) {
- System.out.println("发送HTTP命令到设备 " + deviceSn + ": " + cmd.getCommandType());
-
- try {
- // 构建HTTP请求
- String url = baseUrl + "/api/command";
-
- // 构建请求体
- Map<String, Object> requestBody = new HashMap<>();
- requestBody.put("deviceSn", deviceSn);
- requestBody.put("commandType", cmd.getCommandType());
- requestBody.put("parameterKey", cmd.getParameterKey());
- requestBody.put("parameterValue", cmd.getParameterValue());
- requestBody.put("timeout", cmd.getTimeout());
-
- System.out.println("HTTP请求URL: " + url);
- System.out.println("HTTP请求体: " + requestBody.toString());
-
- } catch (Exception e) {
- System.err.println("发送HTTP命令失败: " + e.getMessage());
- }
- }
-
- @Override
- public DeviceInfo parseDeviceInfo(byte[] payload) {
- System.out.println("解析HTTP设备信息: " + new String(payload));
-
- // 解析HTTP设备信息
- DeviceInfo deviceInfo = new DeviceInfo("HTTP_CONTROLLER_001", "HTTP控制器", "valve_controller");
- deviceInfo.setManufacturer("ValveTech");
- deviceInfo.setProtocolVersion("HTTP/1.1");
-
- // 解析设备属性
- Map<String, Object> properties = new HashMap<>();
- properties.put("endpoint", baseUrl + "/api/devices");
- properties.put("method", "POST");
- deviceInfo.setProperties(properties);
-
- return deviceInfo;
- }
-
- @Override
- public AdapterStatus getStatus(String deviceSn) {
- return status;
- }
-
- @Override
- public boolean connect() {
- try {
- // 测试HTTP连接
- String healthUrl = baseUrl + "/api/health";
-
- System.out.println("测试HTTP连接: " + healthUrl);
-
- // 在实际实现中,这里应该发送HTTP GET请求测试连接
- // 这里模拟成功连接
- status = AdapterStatus.CONNECTED;
- connectionTime = System.currentTimeMillis();
- System.out.println("HTTP 适配器连接成功: " + baseUrl);
- return true;
- } catch (Exception e) {
- status = AdapterStatus.ERROR;
- System.err.println("HTTP适配器连接失败: " + e.getMessage());
- return false;
- }
- }
-
- @Override
- public void disconnect() {
- status = AdapterStatus.DISCONNECTED;
- connectedDevices.clear();
- System.out.println("HTTP 适配器已断开连接");
- }
-
- @Override
- public AdapterInfo getAdapterInfo() {
- return new AdapterInfo("HTTP适配器", "http", "1.0", "支持HTTP协议的设备适配");
- }
- }
|