|
@ -1,6 +1,7 @@ |
|
|
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 com.qyft.ms.app.device.status.DeviceStatus; |
|
|
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; |
|
@ -12,7 +13,6 @@ import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
import java.util.concurrent.ConcurrentMap; |
|
|
import java.util.concurrent.ConcurrentMap; |
|
|
|
|
|
|
|
@ -25,6 +25,7 @@ public class DeviceCommandService { |
|
|
|
|
|
|
|
|
private final DeviceTcpClient deviceTcpClient; |
|
|
private final DeviceTcpClient deviceTcpClient; |
|
|
private final WebSocketService webSocketService; |
|
|
private final WebSocketService webSocketService; |
|
|
|
|
|
private final DeviceStatus deviceStatus; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CommandFuture executeCommand(DeviceCommand cmdToDevice) { |
|
|
public CommandFuture executeCommand(DeviceCommand cmdToDevice) { |
|
@ -47,31 +48,18 @@ public class DeviceCommandService { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CommandFuture sendCommandNoFront(DeviceCommand deviceCommand) { |
|
|
public CommandFuture sendCommandNoFront(DeviceCommand deviceCommand) { |
|
|
CommandFuture commandFuture = executeCommand(deviceCommand); |
|
|
|
|
|
commandFuture.getResponseFuture().thenApply(result -> { |
|
|
|
|
|
Boolean success = result.getBool("success"); |
|
|
|
|
|
if (!success) { //response失败 |
|
|
|
|
|
String message = deviceCommand.getCmdName() + "指令,设备response错误"; |
|
|
|
|
|
} |
|
|
|
|
|
return result; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return commandFuture; |
|
|
|
|
|
|
|
|
return executeCommand(deviceCommand); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public CommandFuture sendCommand(String cmdId, String cmdCode, DeviceCommand deviceCommand) throws IOException { |
|
|
|
|
|
|
|
|
public CommandFuture sendCommand(String cmdId, String cmdCode, DeviceCommand deviceCommand) throws Exception { |
|
|
|
|
|
if (deviceStatus.isStopPressed()) { |
|
|
|
|
|
throw new RuntimeException("设备急停中"); |
|
|
|
|
|
} |
|
|
CommandFuture commandFuture = executeCommand(deviceCommand); |
|
|
CommandFuture commandFuture = executeCommand(deviceCommand); |
|
|
|
|
|
commandFuture.setCmdId(cmdId); |
|
|
|
|
|
commandFuture.setCmdCode(cmdCode); |
|
|
|
|
|
commandFuture.setDeviceCommand(deviceCommand); |
|
|
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 -> { |
|
|
|
|
|
Boolean success = result.getBool("success"); |
|
|
|
|
|
if (success == null || !success) { //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_RESULT, deviceCommand.getCmdName() + "指令,设备response正常,耗时:" + (commandFuture.getEndSendTime() - commandFuture.getStartSendTime()), result)); |
|
|
|
|
|
return result; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return commandFuture; |
|
|
return commandFuture; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -83,8 +71,16 @@ public class DeviceCommandService { |
|
|
future.setEndSendTime(System.currentTimeMillis()); |
|
|
future.setEndSendTime(System.currentTimeMillis()); |
|
|
Boolean success = deviceResult.getBool("success"); //数据验证 |
|
|
Boolean success = deviceResult.getBool("success"); //数据验证 |
|
|
if (success == null || !success) { //response失败 |
|
|
if (success == null || !success) { //response失败 |
|
|
future.completeResponseExceptionally(new RuntimeException("response失败")); |
|
|
|
|
|
|
|
|
if(future.getCmdId() != null) { |
|
|
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(future.getCmdId(), future.getCmdCode(), CommandStatus.DEVICE_ERROR, |
|
|
|
|
|
future.getDeviceCommand().getCmdName() + "指令,设备response错误,耗时:" + (future.getEndSendTime() - future.getStartSendTime()), deviceResult)); |
|
|
|
|
|
} |
|
|
|
|
|
future.completeResponseExceptionally(new RuntimeException("response失败:" + deviceResult)); |
|
|
} else { |
|
|
} else { |
|
|
|
|
|
if(future.getCmdId() != null) { |
|
|
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(future.getCmdId(), future.getCmdCode(), CommandStatus.DEVICE_RESULT, |
|
|
|
|
|
future.getDeviceCommand().getCmdName() + "指令,设备response正常,耗时:" + (future.getEndSendTime() - future.getStartSendTime()), deviceResult)); |
|
|
|
|
|
} |
|
|
future.completeResponse(deviceResult); |
|
|
future.completeResponse(deviceResult); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|