Browse Source

fix:设备指令改为cmd

tags/1.0
白凤吉 5 months ago
parent
commit
fbc14439aa
  1. 8
      src/main/java/com/qyft/ms/system/common/device/command/DeviceCommandGenerator.java
  2. 8
      src/main/java/com/qyft/ms/system/model/bo/DeviceCommand.java
  3. 20
      src/main/java/com/qyft/ms/system/service/device/DeviceCommandService.java

8
src/main/java/com/qyft/ms/system/common/device/command/DeviceCommandGenerator.java

@ -502,12 +502,12 @@ public class DeviceCommandGenerator {
private static DeviceCommand deviceCmd(String code, String device, String action, Map<String, Object> params, String commandName) { private static DeviceCommand deviceCmd(String code, String device, String action, Map<String, Object> params, String commandName) {
int cmdId = CyclicNumberGenerator.getInstance().generateNumber(); int cmdId = CyclicNumberGenerator.getInstance().generateNumber();
DeviceCommand cmdToDevice = new DeviceCommand(); DeviceCommand cmdToDevice = new DeviceCommand();
cmdToDevice.setCommandId(cmdId);
cmdToDevice.setCommandCode(code);
cmdToDevice.setTargetDevice(device);
cmdToDevice.setCmdId(cmdId);
cmdToDevice.setCmdCode(code);
cmdToDevice.setDevice(device);
cmdToDevice.setAction(action); cmdToDevice.setAction(action);
cmdToDevice.setParams(params); cmdToDevice.setParams(params);
cmdToDevice.setCommandName(commandName);
cmdToDevice.setCmdName(commandName);
return cmdToDevice; return cmdToDevice;
} }
} }

8
src/main/java/com/qyft/ms/system/model/bo/DeviceCommand.java

@ -9,22 +9,22 @@ public class DeviceCommand {
/** /**
* 指令ID * 指令ID
*/ */
private Integer commandId;
private Integer cmdId;
/** /**
* 指令名称 * 指令名称
*/ */
private String commandName;
private String cmdName;
/** /**
* 指令代码 例如 "device_status_get" * 指令代码 例如 "device_status_get"
*/ */
private String commandCode;
private String cmdCode;
/** /**
* 目标设备 * 目标设备
*/ */
private String targetDevice;
private String device;
/** /**
* 执行动作 * 执行动作

20
src/main/java/com/qyft/ms/system/service/device/DeviceCommandService.java

@ -30,14 +30,14 @@ public class DeviceCommandService {
public CommandFuture executeCommand(DeviceCommand cmdToDevice) { public CommandFuture executeCommand(DeviceCommand cmdToDevice) {
CommandFuture cmdFuture = new CommandFuture(); CommandFuture cmdFuture = new CommandFuture();
commandFutureMap.put(cmdToDevice.getCommandId(), cmdFuture);
commandFutureMap.put(cmdToDevice.getCmdId(), cmdFuture);
if (!deviceTcpClient.sendToJSON(cmdToDevice)) { if (!deviceTcpClient.sendToJSON(cmdToDevice)) {
commandFutureMap.remove(cmdToDevice.getCommandId());
commandFutureMap.remove(cmdToDevice.getCmdId());
throw new RuntimeException("向设备发送指令失败"); throw new RuntimeException("向设备发送指令失败");
} }
cmdFuture.getResponseFuture().whenComplete((result, ex) -> cmdFuture.getResponseFuture().whenComplete((result, ex) ->
commandFutureMap.remove(cmdToDevice.getCommandId()));
commandFutureMap.remove(cmdToDevice.getCmdId()));
return cmdFuture; return cmdFuture;
} }
@ -47,7 +47,7 @@ public class DeviceCommandService {
commandFuture.getAckFuture().thenApply(result -> { commandFuture.getAckFuture().thenApply(result -> {
Boolean status = result.getBool("status"); Boolean status = result.getBool("status");
if (!status) { //ack失败 if (!status) { //ack失败
String message = deviceCommand.getCommandName() + "指令,设备ack错误";
String message = deviceCommand.getCmdName() + "指令,设备ack错误";
throw new RuntimeException(message); throw new RuntimeException(message);
} }
return result; return result;
@ -56,7 +56,7 @@ public class DeviceCommandService {
commandFuture.getResponseFuture().thenApply(result -> { commandFuture.getResponseFuture().thenApply(result -> {
Boolean status = result.getBool("status"); Boolean status = result.getBool("status");
if (!status) { //response失败 if (!status) { //response失败
String message = deviceCommand.getCommandName() + "指令,设备response错误";
String message = deviceCommand.getCmdName() + "指令,设备response错误";
throw new RuntimeException(message); throw new RuntimeException(message);
} }
return result.get("data"); return result.get("data");
@ -67,26 +67,26 @@ public class DeviceCommandService {
public CommandFuture sendCommand(String cmdId, String cmdCode, DeviceCommand deviceCommand) throws IOException { public CommandFuture sendCommand(String cmdId, String cmdCode, DeviceCommand deviceCommand) throws IOException {
CommandFuture commandFuture = executeCommand(deviceCommand); CommandFuture commandFuture = executeCommand(deviceCommand);
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, "已向设备发送了" + deviceCommand.getCommandName() + "指令", deviceCommand));
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, "已向设备发送了" + deviceCommand.getCmdName() + "指令", deviceCommand));
commandFuture.getAckFuture().thenApply(result -> { commandFuture.getAckFuture().thenApply(result -> {
Boolean status = result.getBool("status"); Boolean status = result.getBool("status");
if (!status) { //ack失败 if (!status) { //ack失败
String message = deviceCommand.getCommandName() + "指令,设备ack错误";
String message = deviceCommand.getCmdName() + "指令,设备ack错误";
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, message, result)); webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, message, result));
throw new RuntimeException(message); throw new RuntimeException(message);
} }
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.RESULT, deviceCommand.getCommandName() + "指令,设备ack正常", result));
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.RESULT, deviceCommand.getCmdName() + "指令,设备ack正常", result));
return result; return result;
}); });
commandFuture.getResponseFuture().thenApply(result -> { commandFuture.getResponseFuture().thenApply(result -> {
Boolean status = result.getBool("status"); Boolean status = result.getBool("status");
if (!status) { //response失败 if (!status) { //response失败
String message = deviceCommand.getCommandName() + "指令,设备response错误";
String message = deviceCommand.getCmdName() + "指令,设备response错误";
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, message, result)); webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, message, result));
throw new RuntimeException(message); throw new RuntimeException(message);
} }
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.RESULT, deviceCommand.getCommandName() + "指令,设备response正常", result));
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.RESULT, deviceCommand.getCmdName() + "指令,设备response正常", result));
return result.get("data"); return result.get("data");
}); });

Loading…
Cancel
Save