Browse Source

add: 新增 HardwareService

master
HSZ_HeSongZhen 3 months ago
parent
commit
9149f6169a
  1. 8
      src/main/java/com/iflytop/gd/hardware/command/CommandHandler.java
  2. 41
      src/main/java/com/iflytop/gd/hardware/command/SupportDevice.java
  3. 30
      src/main/java/com/iflytop/gd/hardware/command/SupportMethod.java
  4. 44
      src/main/java/com/iflytop/gd/hardware/command/command_handler/FanHandler.java
  5. 41
      src/main/java/com/iflytop/gd/hardware/service/HardwareService.java

8
src/main/java/com/iflytop/gd/hardware/command/CommandHandler.java

@ -0,0 +1,8 @@
package com.iflytop.gd.hardware.command;
import com.iflytop.gd.common.cmd.DeviceCommand;
public abstract class CommandHandler {
public abstract void checkAction(String action) throws Exception;
public abstract boolean sendCommand(DeviceCommand command);
}

41
src/main/java/com/iflytop/gd/hardware/command/SupportDevice.java

@ -0,0 +1,41 @@
package com.iflytop.gd.hardware.command;
public class SupportDevice {
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;
}
}
static public boolean check(String strDevice)
{
for (CmdDevice device : CmdDevice.values()) {
if (device.getStrValue().equals(strDevice)) {
return true;
}
}
return false;
}
}

30
src/main/java/com/iflytop/gd/hardware/command/SupportMethod.java

@ -0,0 +1,30 @@
package com.iflytop.gd.hardware.command;
public class SupportMethod {
public enum CommandMethod {
CMD_CONTROL("controlCmd"),
CMD_CONTROL_MOTOR("controlMotorCmd"),
CMD_GET_INFO("getInfoCmd");
private final String cmdName;
CommandMethod(String commandName) {
this.cmdName = commandName;
}
public String getCmdName() {
return cmdName;
}
}
public static void checkMethod(String strMethod) {
if (strMethod == null || strMethod.isEmpty()) {
throw new NoSuchMethodError("* Empty Method *");
}
for (CommandMethod type : CommandMethod.values()) {
if (type.getCmdName().equals(strMethod)) {
return;
}
}
throw new NoSuchMethodError(strMethod);
}
}

44
src/main/java/com/iflytop/gd/hardware/command/command_handler/FanHandler.java

@ -0,0 +1,44 @@
package com.iflytop.gd.hardware.command.command_handler;
import com.iflytop.gd.common.cmd.DeviceCommand;
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().equals("open")) {
// 校验参数
checkParams(command.getParams());
// get 参数值
String speed = command.getParams().get("speed");
// 打开风扇
String direction = command.getParams().get("direction");
// 组包
// 发送命令
HardwareService.sendPacket();
return true;
}
else if (command.getAction().equals("close")) {
// 关闭风扇
return true;
}
return false;
}
}

41
src/main/java/com/iflytop/gd/hardware/service/HardwareService.java

@ -0,0 +1,41 @@
package com.iflytop.gd.hardware.service;
import com.iflytop.gd.common.cmd.DeviceCommand;
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<String, CommandHandler> cmdHandlers = new HashMap<>();
public void post_initialize()
{
cmdHandlers.put(CmdDevice.FAN1.getStrValue(), new FanHandler(0));
}
boolean sendCommand(DeviceCommand cmd)
{
String strMethod = cmd.getCmdCode();
SupportMethod.checkMethod(strMethod);
if(cmdHandlers.containsKey(cmd.getDevice())) {
return cmdHandlers.get(cmd.getDevice()).execute(command);
}
else {
throw new InvalidParameterException(StringHelper.format("[Device]: {}", command.getDevice()));
}
}
// 这里或者搞成自定义结构体
void sendCmdResponse(int cmdId, int resultCode, String resultMsg)
{
// 发送命令成功
}
}
Loading…
Cancel
Save