From 95b296b62bb5e65263666b31742e3f57da26ecd3 Mon Sep 17 00:00:00 2001 From: HSZ_HeSongZhen <210202959@qq.com> Date: Sun, 13 Jul 2025 15:19:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E6=B8=A9=E6=8E=A7?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/mgc/server/SSCommandHandler.java | 3 + .../java/org/mgc/server/command/SupportDevice.java | 2 + .../command/command_handler/TempControlDevice.java | 243 +++++++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 src/main/java/org/mgc/server/command/command_handler/TempControlDevice.java diff --git a/src/main/java/org/mgc/server/SSCommandHandler.java b/src/main/java/org/mgc/server/SSCommandHandler.java index 349f837..6b1cc75 100644 --- a/src/main/java/org/mgc/server/SSCommandHandler.java +++ b/src/main/java/org/mgc/server/SSCommandHandler.java @@ -45,6 +45,9 @@ public class SSCommandHandler { cmdHandlers.put(CmdDevice.HUMIDITY.getStrValue(), new HumidityDevice()); cmdHandlers.put(CmdDevice.TEMPERATURE.getStrValue(), new TemperatureDevice()); cmdHandlers.put(CmdDevice.BOARD.getStrValue(), new BoardDevice()); + + cmdHandlers.put(CmdDevice.NOZZLE_TEMP.getStrValue(), new TempControlDevice()); + cmdHandlers.put(CmdDevice.SLIDE_TEMP.getStrValue(), new TempControlDevice()); } public JsonResponse executeCommand(Command command) { diff --git a/src/main/java/org/mgc/server/command/SupportDevice.java b/src/main/java/org/mgc/server/command/SupportDevice.java index eeb6374..940cae8 100644 --- a/src/main/java/org/mgc/server/command/SupportDevice.java +++ b/src/main/java/org/mgc/server/command/SupportDevice.java @@ -17,6 +17,8 @@ public class SupportDevice { MOTOR_XYX("xyz"), HUMIDITY("humidity"), TEMPERATURE("temperature"), + NOZZLE_TEMP("nozzle_temperature"), + SLIDE_TEMP("slide_temperature"), BOARD("device"); diff --git a/src/main/java/org/mgc/server/command/command_handler/TempControlDevice.java b/src/main/java/org/mgc/server/command/command_handler/TempControlDevice.java new file mode 100644 index 0000000..92de35a --- /dev/null +++ b/src/main/java/org/mgc/server/command/command_handler/TempControlDevice.java @@ -0,0 +1,243 @@ +package org.mgc.server.command.command_handler; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import lombok.extern.slf4j.Slf4j; +import org.mgc.app.SubSprayAppSingleton; +import org.mgc.server.CommandFuture; +import org.mgc.server.DeviceCommandGenerator; +import org.mgc.server.command.BaseCommandHandler; +import org.mgc.server.command.Command; +import org.mgc.server.command.SupportDevice; +import org.mgc.server.response.JsonErrorResponse; +import org.mgc.server.response.JsonResponse; +import org.mgc.server.response.JsonSucResponse; +import org.mgc.service.DeviceCommandManager; +import org.mgc.service.SerialCommand; +import org.mgc.service.SerialResult; +import org.mgc.utils.StringHelper; + +import java.io.IOException; +import java.security.InvalidParameterException; +import java.util.concurrent.ExecutionException; + +@Slf4j +public class TempControlDevice extends BaseCommandHandler { + public static final int TMEP_CONTROL_NOZZLE_SET_TEMP_ADDRESS = 0x24; // 设置温度 + public static final int TMEP_CONTROL_NOZZLE_CLOSE_TEMP_ADDRESS = 0x25; // 关闭温度 + public static final int TMEP_CONTROL_NOZZLE_READ_TEMP_ADDRESS = 0x26; // 读取温度 + public static final int TMEP_CONTROL_SLIDE_SET_TEMP_ADDRESS = 0x27; // 设置温度 + public static final int TMEP_CONTROL_SLIDE_CLOSE_TEMP_ADDRESS = 0x28; // 关闭温度 + public static final int TMEP_CONTROL_SLIDE_READ_TEMP_ADDRESS = 0x29; // 读取温度; + void checkAction(String action) { + if(TempAction.fromString(action) != null) + { + return ; + } + throw new InvalidParameterException(StringHelper.format("Action E:{}", action)); + } + + @Override + public JsonResponse execute(Command command) { + int cmdId = (int) command.getCmdId(); + int res = 0; + + checkAction(command.getAction()); + + TempAction action = TempAction.fromString(command.getAction()); + + if(action == TempAction.ActionOpen) + { + if(!command.getParam().has("temp")) + { + throw new InvalidParameterException("temp"); + } + double temp = command.getParam().get("temp").asDouble(); + log.info("TempControlDevice open temp: {}", temp); + res = openTemp(getCmdDevice(command.getDevice()), cmdId, temp); + } + else if(action == TempAction.ActionClose) + { + log.info("TempControlDevice close"); + res = closeTemp(getCmdDevice(command.getDevice()), cmdId); + } + else if(action == TempAction.ActionGet) + { + double temperature = getTemp(getCmdDevice(command.getDevice()), cmdId); + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode jsonNode = objectMapper.createObjectNode(); + jsonNode.put("temp", temperature); + JsonSucResponse jsonSucResponse = new JsonSucResponse(command.getCmdId(), jsonNode); + jsonSucResponse.setDevice(command.getDevice()); + return jsonSucResponse; + } + else + { + res = -1; + } + + boolean isSuc = res == 0; + if (isSuc) { + return new JsonSucResponse(command.getCmdId(), null); + } else { + return new JsonErrorResponse(command.getCmdId(), res, "command execution failed"); + } + } + + int openTemp(SupportDevice.CmdDevice cmdDevice, int cmdId, double temp) + { + int address; + if(cmdDevice == SupportDevice.CmdDevice.NOZZLE_TEMP) + { + address = TMEP_CONTROL_NOZZLE_SET_TEMP_ADDRESS; + } + else if(cmdDevice == SupportDevice.CmdDevice.SLIDE_TEMP) + { + address = TMEP_CONTROL_SLIDE_SET_TEMP_ADDRESS; + } + else + { + return -1; // 不支持的设备 + } + + short i_temp = (short) (temp * 10); // 转换为整数 + + // 拼 485 指令 + SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlSingleRegCmdControl(cmdId, address, + i_temp); + try { + // 发送 485 指令 + DeviceCommandManager deviceCommandManager = SubSprayAppSingleton.getInstance().getDeviceCommandManager(); + CommandFuture ctrlCommandFuture = deviceCommandManager.sendCommand(ctrlCommand); + // 等待 + commandWait(1000, ctrlCommandFuture); + + // 485 指令 转 data + SerialResult result = ctrlCommandFuture.getAckFuture().get(); + return result.getCode(); + } catch (IOException | ExecutionException | InterruptedException e) { + return -1; + } + } + + int closeTemp(SupportDevice.CmdDevice cmdDevice, int cmdId) + { + int address; + if(cmdDevice == SupportDevice.CmdDevice.NOZZLE_TEMP) + { + address = TMEP_CONTROL_NOZZLE_CLOSE_TEMP_ADDRESS; + } + else if(cmdDevice == SupportDevice.CmdDevice.SLIDE_TEMP) + { + address = TMEP_CONTROL_SLIDE_CLOSE_TEMP_ADDRESS; + } + else + { + return -1; // 不支持的设备 + } + // 拼 485 指令 + SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlSingleRegCmdControl(cmdId, address, + (short)(0)); + try { + // 发送 485 指令 + DeviceCommandManager deviceCommandManager = SubSprayAppSingleton.getInstance().getDeviceCommandManager(); + CommandFuture ctrlCommandFuture = deviceCommandManager.sendCommand(ctrlCommand); + // 等待 + commandWait(1000, ctrlCommandFuture); + + // 485 指令 转 data + SerialResult result = ctrlCommandFuture.getAckFuture().get(); + return result.getCode(); + } catch (IOException | ExecutionException | InterruptedException e) { + return -1; + } + } + + double getTemp(SupportDevice.CmdDevice cmdDevice, int cmdId) + { + int address; + if(cmdDevice == SupportDevice.CmdDevice.NOZZLE_TEMP) + { + address = TMEP_CONTROL_NOZZLE_READ_TEMP_ADDRESS; + } + else if(cmdDevice == SupportDevice.CmdDevice.SLIDE_TEMP) + { + address = TMEP_CONTROL_SLIDE_READ_TEMP_ADDRESS; + } + else + { + return -1; // 不支持的设备 + } + // 拼 485 指令 + SerialCommand ctrlCommand = DeviceCommandGenerator.getMultiRegInfoCmd(cmdId, address, + 1); + try { + // 发送 485 指令 + DeviceCommandManager deviceCommandManager = SubSprayAppSingleton.getInstance().getDeviceCommandManager(); + CommandFuture ctrlCommandFuture = deviceCommandManager.sendCommand(ctrlCommand); + // 等待 + commandWait(1000, ctrlCommandFuture); + + // 485 指令 转 data + SerialResult result = ctrlCommandFuture.getAckFuture().get(); + if(result.getDataType() == SerialResult.DataType.REGISTER) + { + short []array = result.getRegisterData(); + if(array == null || array.length != 1) + { + return -1; + } + double temperature = array[0] / 10.0; + log.info("Temperature: {}", temperature); + return temperature; // 成功获取温湿度 + } + else + { + return 0.0; + } + } catch (IOException | ExecutionException | InterruptedException e) { + return 0.0; // 异常处理 + } + } + + public SupportDevice.CmdDevice getCmdDevice(String strDevice) { + if(strDevice.equals(SupportDevice.CmdDevice.NOZZLE_TEMP.getStrValue()) ) + { + return SupportDevice.CmdDevice.NOZZLE_TEMP; + } + else if(strDevice.equals(SupportDevice.CmdDevice.SLIDE_TEMP.getStrValue())) + { + return SupportDevice.CmdDevice.SLIDE_TEMP; + } + else { + return SupportDevice.CmdDevice.NOZZLE_TEMP; + } + } + + public enum TempAction { + ActionOpen("open"), + ActionClose("close"), + ActionGet("get"); + + private final String strAction; + + TempAction(String strAction) { + this.strAction = strAction; + } + + // 获取关联的字符串值 + public String getStrAction() { + return strAction; + } + + // 根据字符串查找枚举值 + public static TempAction fromString(String value) { + for (TempAction action : TempAction.values()) { + if (action.getStrAction().equals(value)) { + return action; + } + } + return null; + } + } +} \ No newline at end of file