|
|
@ -4,6 +4,7 @@ import cn.hutool.json.JSONObject; |
|
|
|
import com.qyft.ms.app.device.status.DeviceStatus; |
|
|
|
import com.qyft.ms.system.common.constant.CommandStatus; |
|
|
|
import com.qyft.ms.system.common.device.command.CommandFuture; |
|
|
|
import com.qyft.ms.system.common.device.command.CyclicNumberGenerator; |
|
|
|
import com.qyft.ms.system.common.device.command.FrontResponseGenerator; |
|
|
|
import com.qyft.ms.system.core.client.DeviceTcpClient; |
|
|
|
import com.qyft.ms.system.model.bo.DeviceCommand; |
|
|
@ -29,29 +30,35 @@ public class DeviceCommandService { |
|
|
|
|
|
|
|
|
|
|
|
public CommandFuture executeCommand(DeviceCommand cmdToDevice) { |
|
|
|
int cmdId = CyclicNumberGenerator.getInstance().generateNumber(); |
|
|
|
cmdToDevice.setCmdId(cmdId); |
|
|
|
CommandFuture cmdFuture = new CommandFuture(); |
|
|
|
commandFutureMap.put(cmdToDevice.getCmdId(), cmdFuture); |
|
|
|
cmdFuture.setStartSendTime(System.currentTimeMillis()); |
|
|
|
if (!deviceTcpClient.sendToJSON(cmdToDevice)) { |
|
|
|
commandFutureMap.remove(cmdToDevice.getCmdId()); |
|
|
|
throw new RuntimeException("向设备发送指令失败"); |
|
|
|
} |
|
|
|
|
|
|
|
cmdFuture.getResponseFuture().whenComplete((result, ex) -> |
|
|
|
commandFutureMap.remove(cmdToDevice.getCmdId())); |
|
|
|
cmdFuture.getResponseFuture().whenComplete((result, ex) -> { |
|
|
|
log.info("commandFutureMap 移除id:{} 剩余:{}", cmdToDevice.getCmdId(),commandFutureMap.size()); |
|
|
|
commandFutureMap.remove(cmdToDevice.getCmdId()); |
|
|
|
}); |
|
|
|
|
|
|
|
return cmdFuture; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public CommandFuture sendCommandNoFront(DeviceCommand deviceCommand) { |
|
|
|
CommandFuture commandFuture = executeCommand(deviceCommand); |
|
|
|
commandFuture.getAckFuture().thenApply(result -> { |
|
|
|
Boolean status = result.getBool("status"); |
|
|
|
if (!status) { //ack失败 |
|
|
|
String message = deviceCommand.getCmdName() + "指令,设备ack错误"; |
|
|
|
throw new RuntimeException(message); |
|
|
|
} |
|
|
|
return result; |
|
|
|
}); |
|
|
|
// commandFuture.getAckFuture().thenApply(result -> { |
|
|
|
// Boolean status = result.getBool("status"); |
|
|
|
// if (!status) { //ack失败 |
|
|
|
// String message = deviceCommand.getCmdName() + "指令,设备ack错误"; |
|
|
|
// throw new RuntimeException(message); |
|
|
|
// } |
|
|
|
// return result; |
|
|
|
// }); |
|
|
|
|
|
|
|
commandFuture.getResponseFuture().thenApply(result -> { |
|
|
|
Boolean status = result.getBool("status"); |
|
|
@ -67,17 +74,17 @@ public class DeviceCommandService { |
|
|
|
|
|
|
|
public CommandFuture sendCommand(String cmdId, String cmdCode, DeviceCommand deviceCommand) throws IOException { |
|
|
|
CommandFuture commandFuture = executeCommand(deviceCommand); |
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, "已向设备发送了" + deviceCommand.getCmdName() + "指令", deviceCommand)); |
|
|
|
commandFuture.getAckFuture().thenApply(result -> { |
|
|
|
Boolean status = result.getBool("status"); |
|
|
|
if (!status) { //ack失败 |
|
|
|
String message = deviceCommand.getCmdName() + "指令,设备ack错误"; |
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, message, result)); |
|
|
|
throw new RuntimeException(message); |
|
|
|
} |
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.RESULT, deviceCommand.getCmdName() + "指令,设备ack正常", result)); |
|
|
|
return result; |
|
|
|
}); |
|
|
|
// webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, "已向设备发送了" + deviceCommand.getCmdName() + "指令", deviceCommand)); |
|
|
|
// commandFuture.getAckFuture().thenApply(result -> { |
|
|
|
// Boolean status = result.getBool("status"); |
|
|
|
// if (!status) { //ack失败 |
|
|
|
// String message = deviceCommand.getCmdName() + "指令,设备ack错误"; |
|
|
|
// webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, message, result)); |
|
|
|
// throw new RuntimeException(message); |
|
|
|
// } |
|
|
|
// webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.RESULT, deviceCommand.getCmdName() + "指令,设备ack正常", result)); |
|
|
|
// return result; |
|
|
|
// }); |
|
|
|
|
|
|
|
commandFuture.getResponseFuture().thenApply(result -> { |
|
|
|
Boolean status = result.getBool("status"); |
|
|
@ -86,7 +93,7 @@ public class DeviceCommandService { |
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.SEND, message, result)); |
|
|
|
throw new RuntimeException(message); |
|
|
|
} |
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.RESULT, deviceCommand.getCmdName() + "指令,设备response正常", result)); |
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.RESULT, deviceCommand.getCmdName() + "指令,设备response正常,耗时:" + (commandFuture.getEndSendTime() - commandFuture.getStartSendTime()), result)); |
|
|
|
return result.get("data"); |
|
|
|
}); |
|
|
|
|
|
|
@ -113,13 +120,13 @@ public class DeviceCommandService { |
|
|
|
} |
|
|
|
|
|
|
|
public void completeCommandAck(JSONObject deviceResult) { |
|
|
|
Integer cmdId = deviceResult.getInt("cmdId"); |
|
|
|
if (cmdId != null) { |
|
|
|
CommandFuture future = commandFutureMap.get(cmdId); |
|
|
|
if (future != null) { |
|
|
|
future.completeAck(deviceResult); |
|
|
|
} |
|
|
|
} |
|
|
|
// Integer cmdId = deviceResult.getInt("cmdId"); |
|
|
|
// if (cmdId != null) { |
|
|
|
// CommandFuture future = commandFutureMap.get(cmdId); |
|
|
|
// if (future != null) { |
|
|
|
// future.completeAck(deviceResult); |
|
|
|
// } |
|
|
|
// } |
|
|
|
} |
|
|
|
|
|
|
|
public void completeCommandResponse(JSONObject deviceResult) { |
|
|
@ -127,6 +134,7 @@ public class DeviceCommandService { |
|
|
|
if (cmdId != null) { |
|
|
|
CommandFuture future = commandFutureMap.get(cmdId); |
|
|
|
if (future != null) { |
|
|
|
future.setEndSendTime(System.currentTimeMillis()); |
|
|
|
future.completeResponse(deviceResult); |
|
|
|
} |
|
|
|
} |
|
|
|