5 changed files with 164 additions and 0 deletions
-
8src/main/java/com/iflytop/gd/hardware/command/CommandHandler.java
-
41src/main/java/com/iflytop/gd/hardware/command/SupportDevice.java
-
30src/main/java/com/iflytop/gd/hardware/command/SupportMethod.java
-
44src/main/java/com/iflytop/gd/hardware/command/command_handler/FanHandler.java
-
41src/main/java/com/iflytop/gd/hardware/service/HardwareService.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); |
|||
} |
@ -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; |
|||
} |
|||
|
|||
} |
@ -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); |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
@ -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) |
|||
{ |
|||
// 发送命令成功 |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue