6 changed files with 151 additions and 90 deletions
-
6src/main/java/com/iflytop/gd/hardware/command/CommandHandler.java
-
70src/main/java/com/iflytop/gd/hardware/command/SupportDevice.java
-
43src/main/java/com/iflytop/gd/hardware/command/command/handler/FanHandler.java
-
36src/main/java/com/iflytop/gd/hardware/command/command_handler/DoorHandler.java
-
52src/main/java/com/iflytop/gd/hardware/command/command_handler/FanHandler.java
-
24src/main/java/com/iflytop/gd/hardware/service/HardwareService.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; |
|||
} |
@ -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; |
|||
// } |
|||
|
|||
} |
@ -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; |
|||
} |
|||
} |
@ -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'"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
|
|||
|
@ -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"); |
|||
} |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue