Browse Source

add: 更新

master
HSZ_HeSongZhen 3 months ago
parent
commit
eb5f0bdcca
  1. 6
      src/main/java/com/iflytop/gd/hardware/command/CommandHandler.java
  2. 70
      src/main/java/com/iflytop/gd/hardware/command/SupportDevice.java
  3. 43
      src/main/java/com/iflytop/gd/hardware/command/command/handler/FanHandler.java
  4. 36
      src/main/java/com/iflytop/gd/hardware/command/command_handler/DoorHandler.java
  5. 52
      src/main/java/com/iflytop/gd/hardware/command/command_handler/FanHandler.java
  6. 34
      src/main/java/com/iflytop/gd/hardware/service/HardwareService.java

6
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;
}

70
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;
// }
}

43
src/main/java/com/iflytop/gd/hardware/command/command/handler/FanHandler.java

@ -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;
}
}

36
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'");
}
}
}

52
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");
}
}
}
}

34
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<String, CommandHandler> cmdHandlers = new HashMap<>();
private final Map<CmdDevice, CommandHandler> 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)
{
}
// 这里或者搞成自定义结构体

Loading…
Cancel
Save