From eb5f0bdcca8eddd87a2f4313ca1f5d26b3ce88bc Mon Sep 17 00:00:00 2001 From: HSZ_HeSongZhen <210202959@qq.com> Date: Wed, 30 Apr 2025 16:12:38 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gd/hardware/command/CommandHandler.java | 6 +- .../iflytop/gd/hardware/command/SupportDevice.java | 70 +++++++++++----------- .../command/command/handler/FanHandler.java | 43 ------------- .../command/command_handler/DoorHandler.java | 36 +++++++++++ .../command/command_handler/FanHandler.java | 52 ++++++++++++++++ .../gd/hardware/service/HardwareService.java | 34 +++++++---- 6 files changed, 151 insertions(+), 90 deletions(-) delete mode 100644 src/main/java/com/iflytop/gd/hardware/command/command/handler/FanHandler.java create mode 100644 src/main/java/com/iflytop/gd/hardware/command/command_handler/DoorHandler.java create mode 100644 src/main/java/com/iflytop/gd/hardware/command/command_handler/FanHandler.java diff --git a/src/main/java/com/iflytop/gd/hardware/command/CommandHandler.java b/src/main/java/com/iflytop/gd/hardware/command/CommandHandler.java index 135ab77..da69877 100644 --- a/src/main/java/com/iflytop/gd/hardware/command/CommandHandler.java +++ b/src/main/java/com/iflytop/gd/hardware/command/CommandHandler.java @@ -1,8 +1,10 @@ package com.iflytop.gd.hardware.command; import com.iflytop.gd.common.cmd.DeviceCommand; +import com.iflytop.gd.common.cmd.DeviceCommandParams; +import com.iflytop.gd.common.enums.cmd.CmdAction; public abstract class CommandHandler { - public abstract void checkAction(String action) throws Exception; - public abstract boolean sendCommand(DeviceCommand command); + protected abstract void checkAction(CmdAction action) throws Exception; + public abstract boolean sendCommand(DeviceCommand command) throws Exception; } diff --git a/src/main/java/com/iflytop/gd/hardware/command/SupportDevice.java b/src/main/java/com/iflytop/gd/hardware/command/SupportDevice.java index 0bb2b3c..85905ea 100644 --- a/src/main/java/com/iflytop/gd/hardware/command/SupportDevice.java +++ b/src/main/java/com/iflytop/gd/hardware/command/SupportDevice.java @@ -1,41 +1,41 @@ package com.iflytop.gd.hardware.command; public class SupportDevice { - public enum CmdDevice { - DOOR("door"), - ShakeMotor("shake_motor"), - FAN1("fan1"), - FAN2("fan1"), - ; +// public enum CmdDevice { +// DOOR("door"), +// ShakeMotor("shake_motor"), +// FAN1("fan1"), +// FAN2("fan1"), +// ; +// +// private final String strValue; +// +// CmdDevice(String strValue) { +// this.strValue = strValue; +// } +// +// public String getStrValue() { +// return strValue; +// } +// +// public static CmdDevice fromString(String value) { +// for (CmdDevice device : CmdDevice.values()) { +// if (device.getStrValue().equals(value)) { +// return device; +// } +// } +// return null; +// } +// } - private final String strValue; - - CmdDevice(String strValue) { - this.strValue = strValue; - } - - public String getStrValue() { - return strValue; - } - - public static CmdDevice fromString(String value) { - for (CmdDevice device : CmdDevice.values()) { - if (device.getStrValue().equals(value)) { - return device; - } - } - return null; - } - } - - static public boolean check(String strDevice) - { - for (CmdDevice device : CmdDevice.values()) { - if (device.getStrValue().equals(strDevice)) { - return true; - } - } - return false; - } +// static public boolean check(String strDevice) +// { +// for (CmdDevice device : CmdDevice.values()) { +// if (device.getStrValue().equals(strDevice)) { +// return true; +// } +// } +// return false; +// } } diff --git a/src/main/java/com/iflytop/gd/hardware/command/command/handler/FanHandler.java b/src/main/java/com/iflytop/gd/hardware/command/command/handler/FanHandler.java deleted file mode 100644 index 8cd48c3..0000000 --- a/src/main/java/com/iflytop/gd/hardware/command/command/handler/FanHandler.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.iflytop.gd.hardware.command.command.handler; - -import com.iflytop.gd.common.cmd.DeviceCommand; -import com.iflytop.gd.common.enums.cmd.CmdAction; -import com.iflytop.gd.common.enums.cmd.CmdDirection; -import com.iflytop.gd.hardware.command.CommandHandler; - -public class FanHandler extends CommandHandler { - - private int fanId = 0; - - public FanHandler(int fanId) { - super(); - } - - @Override - public boolean sendCommand(DeviceCommand command) { - // 校验动作 -// checkAction(command.getAction()); - - - // 发送命令 - if (command.getAction() == CmdAction.open) { - - // 校验参数 -// checkParams(command.getParams()); - // get 参数值 - Double speed = command.getParam().getSpeed(); - - // 打开风扇 - CmdDirection direction = command.getParam().getDirection(); - - // 组包 - // 发送命令 - HardwareService.sendPacket(); - return true; - } else if (command.getAction() == CmdAction.close) { - // 关闭风扇 - return true; - } - return false; - } -} diff --git a/src/main/java/com/iflytop/gd/hardware/command/command_handler/DoorHandler.java b/src/main/java/com/iflytop/gd/hardware/command/command_handler/DoorHandler.java new file mode 100644 index 0000000..b3f3168 --- /dev/null +++ b/src/main/java/com/iflytop/gd/hardware/command/command_handler/DoorHandler.java @@ -0,0 +1,36 @@ +package com.iflytop.gd.hardware.command.command_handler; + +import com.iflytop.gd.common.cmd.DeviceCommand; +import com.iflytop.gd.common.enums.cmd.CmdAction; +import com.iflytop.gd.hardware.command.CommandHandler; + +public class DoorHandler extends CommandHandler { + @Override + public boolean sendCommand(DeviceCommand command) throws Exception { + // 校验动作 + checkAction(command.getAction()); + // 发送命令 + if (command.getAction() == CmdAction.open) { + // 组包 + // 发送命令 + return true; + } else if (command.getAction() == CmdAction.close) { + return true; + } + else if (command.getAction() == CmdAction.stop) { + return true; + } + + return false; + } + @Override + public void checkAction(CmdAction action) throws Exception { + if (!action.equals(CmdAction.open) && !action.equals(CmdAction.close)) { + throw new IllegalArgumentException("action must be 'open' or 'close'"); + } + } +} + + + + diff --git a/src/main/java/com/iflytop/gd/hardware/command/command_handler/FanHandler.java b/src/main/java/com/iflytop/gd/hardware/command/command_handler/FanHandler.java new file mode 100644 index 0000000..61a72b9 --- /dev/null +++ b/src/main/java/com/iflytop/gd/hardware/command/command_handler/FanHandler.java @@ -0,0 +1,52 @@ +package com.iflytop.gd.hardware.command.command_handler; + +import com.iflytop.gd.common.cmd.DeviceCommand; +import com.iflytop.gd.common.cmd.DeviceCommandParams; +import com.iflytop.gd.common.enums.cmd.CmdAction; +import com.iflytop.gd.hardware.command.CommandHandler; + +public class FanHandler extends CommandHandler { + + private int fandId_; + + public FanHandler(int fanId) { + fandId_ = fanId; + } + + @Override + public boolean sendCommand(DeviceCommand command) throws Exception { + // 校验动作 + checkAction(command.getAction()); + + // 发送命令 + if (command.getAction() == CmdAction.open) { + // 校验参数 + checkParams(command.getParam()); + // get 参数值 + Double speed = command.getParam().getSpeed(); + + // 组包 + // 发送命令 + return true; + } else if (command.getAction() == CmdAction.close) { + return true; + } + return false; + } + + @Override + protected void checkAction(CmdAction action) throws Exception { + if (!action.equals(CmdAction.open) && !action.equals(CmdAction.close)) { + throw new IllegalArgumentException("action must be 'open' or 'close'"); + } + } + + private void checkParams(DeviceCommandParams params) throws Exception { + Double speed = params.getSpeed(); + if (speed < 0 || speed > 100) { + { + throw new IllegalArgumentException("speed must be between 0 and 100"); + } + } + } +} diff --git a/src/main/java/com/iflytop/gd/hardware/service/HardwareService.java b/src/main/java/com/iflytop/gd/hardware/service/HardwareService.java index 6c34940..e27d0b0 100644 --- a/src/main/java/com/iflytop/gd/hardware/service/HardwareService.java +++ b/src/main/java/com/iflytop/gd/hardware/service/HardwareService.java @@ -1,36 +1,50 @@ package com.iflytop.gd.hardware.service; +import cn.hutool.core.util.StrUtil; import com.iflytop.gd.common.cmd.DeviceCommand; +import com.iflytop.gd.common.enums.cmd.CmdDevice; import com.iflytop.gd.hardware.command.CommandHandler; -import com.iflytop.gd.hardware.command.SupportDevice.CmdDevice; import com.iflytop.gd.hardware.command.SupportMethod; -import com.iflytop.gd.hardware.command.command.handler.FanHandler; import java.security.InvalidParameterException; import java.util.HashMap; import java.util.Map; + + public class HardwareService { - private final Map cmdHandlers = new HashMap<>(); + private final Map cmdHandlers = new HashMap<>(); public void post_initialize() { - cmdHandlers.put(CmdDevice.FAN1.getStrValue(), new FanHandler(0)); +// cmdHandlers.put(CmdDevice.door, new FanHandler(0)); } boolean sendCommand(DeviceCommand cmd) { - String strMethod = cmd.getCmdCode(); + try { + String strMethod = cmd.getCmdCode(); - SupportMethod.checkMethod(strMethod); + SupportMethod.checkMethod(strMethod); - if(cmdHandlers.containsKey(cmd.getDevice())) { - return cmdHandlers.get(cmd.getDevice()).sendCommand(cmd); + if(cmdHandlers.containsKey(cmd.getDevice())) { + return cmdHandlers.get(cmd.getDevice()).sendCommand(cmd); + } + else { + throw new InvalidParameterException(StrUtil.format("[Device]: {}", cmd.getDevice())); + } } - else { - throw new InvalidParameterException("[Device]: {}", cmd.getDevice()); + catch (Exception e) { + handleException(e); + return false; } + + } + + void handleException(Exception e) + { + } // 这里或者搞成自定义结构体