|
@ -1,7 +1,6 @@ |
|
|
package com.qyft.ms.system.service.device; |
|
|
package com.qyft.ms.system.service.device; |
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONObject; |
|
|
import cn.hutool.json.JSONObject; |
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
|
|
import com.qyft.ms.system.common.constant.CommandStatus; |
|
|
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.CommandFuture; |
|
|
import com.qyft.ms.system.common.device.command.CyclicNumberGenerator; |
|
|
import com.qyft.ms.system.common.device.command.CyclicNumberGenerator; |
|
@ -49,22 +48,12 @@ public class DeviceCommandService { |
|
|
|
|
|
|
|
|
public CommandFuture sendCommandNoFront(DeviceCommand deviceCommand) { |
|
|
public CommandFuture sendCommandNoFront(DeviceCommand deviceCommand) { |
|
|
CommandFuture commandFuture = executeCommand(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.getResponseFuture().thenApply(result -> { |
|
|
commandFuture.getResponseFuture().thenApply(result -> { |
|
|
Boolean success = result.getBool("success"); |
|
|
Boolean success = result.getBool("success"); |
|
|
if (!success) { //response失败 |
|
|
if (!success) { //response失败 |
|
|
String message = deviceCommand.getCmdName() + "指令,设备response错误"; |
|
|
String message = deviceCommand.getCmdName() + "指令,设备response错误"; |
|
|
throw new RuntimeException(message); |
|
|
|
|
|
} |
|
|
} |
|
|
return result.get("data"); |
|
|
|
|
|
|
|
|
return result; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
return commandFuture; |
|
|
return commandFuture; |
|
@ -72,49 +61,44 @@ 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.getCmdName() + "指令", deviceCommand)); |
|
|
|
|
|
// commandFuture.getAckFuture().thenApply(result -> { |
|
|
|
|
|
// Boolean success = result.getBool("success"); |
|
|
|
|
|
// if (success == null || !success) { //ack失败 |
|
|
|
|
|
// String message = deviceCommand.getCmdName() + "指令,设备ack错误"; |
|
|
|
|
|
// webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.DEVICE_ERROR, 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.DEVICE_SEND, deviceCommand.getCmdName() + "指令,已发给设备", deviceCommand)); |
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.DEVICE_SEND, deviceCommand.getCmdName() + "指令,已发给设备", deviceCommand)); |
|
|
commandFuture.getResponseFuture().thenApply(result -> { |
|
|
commandFuture.getResponseFuture().thenApply(result -> { |
|
|
Boolean success = result.getBool("success"); |
|
|
Boolean success = result.getBool("success"); |
|
|
if (success == null || !success) { //response失败 |
|
|
if (success == null || !success) { //response失败 |
|
|
String message = deviceCommand.getCmdName() + "指令,设备response错误"; |
|
|
String message = deviceCommand.getCmdName() + "指令,设备response错误"; |
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.DEVICE_ERROR, message, result)); |
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.DEVICE_ERROR, message, result)); |
|
|
throw new RuntimeException(message); |
|
|
|
|
|
} |
|
|
} |
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.DEVICE_RESULT, deviceCommand.getCmdName() + "指令,设备response正常,耗时:" + (commandFuture.getEndSendTime() - commandFuture.getStartSendTime()), result)); |
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.DEVICE_RESULT, deviceCommand.getCmdName() + "指令,设备response正常,耗时:" + (commandFuture.getEndSendTime() - commandFuture.getStartSendTime()), result)); |
|
|
return result.get("data"); |
|
|
|
|
|
|
|
|
return result; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
return commandFuture; |
|
|
return commandFuture; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void completeCommandAck(JSONObject 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) { |
|
|
public void completeCommandResponse(JSONObject deviceResult) { |
|
|
Integer cmdId = deviceResult.getInt("cmdId"); |
|
|
Integer cmdId = deviceResult.getInt("cmdId"); |
|
|
if (cmdId != null) { |
|
|
if (cmdId != null) { |
|
|
CommandFuture future = commandFutureMap.get(cmdId); |
|
|
CommandFuture future = commandFutureMap.get(cmdId); |
|
|
if (future != null) { |
|
|
if (future != null) { |
|
|
future.setEndSendTime(System.currentTimeMillis()); |
|
|
future.setEndSendTime(System.currentTimeMillis()); |
|
|
future.completeResponse(deviceResult); |
|
|
|
|
|
|
|
|
Boolean success = deviceResult.getBool("success"); //数据验证 |
|
|
|
|
|
if (success == null || !success) { //response失败 |
|
|
|
|
|
future.completeResponseExceptionally(new RuntimeException("response失败")); |
|
|
|
|
|
} else { |
|
|
|
|
|
future.completeResponse(deviceResult); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 取消等待中的future并从map中移除 |
|
|
|
|
|
*/ |
|
|
|
|
|
public synchronized void releaseAllCommandFutures() { |
|
|
|
|
|
for (Integer key : commandFutureMap.keySet()) { |
|
|
|
|
|
CommandFuture future = commandFutureMap.remove(key); |
|
|
|
|
|
if (future != null) { |
|
|
|
|
|
future.getResponseFuture().cancel(true); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|