4 changed files with 109 additions and 4 deletions
-
10src/main/java/com/iflytop/gd/app/cmd/debug/ColdTrapStartHeatingCommandHandler.java
-
96src/main/java/com/iflytop/gd/app/service/DeviceCommandService.java
-
2src/main/java/com/iflytop/gd/common/cmd/CommandHandler.java
-
5src/main/java/com/iflytop/gd/hardware/service/HardwareService.java
@ -0,0 +1,96 @@ |
|||
package com.iflytop.gd.app.service; |
|||
|
|||
|
|||
import cn.hutool.json.JSONObject; |
|||
import com.iflytop.gd.common.cmd.CommandFuture; |
|||
import com.iflytop.gd.common.cmd.CyclicNumberGenerator; |
|||
import com.iflytop.gd.common.cmd.DeviceCommand; |
|||
import com.iflytop.gd.hardware.service.HardwareService; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
import java.util.concurrent.ConcurrentMap; |
|||
|
|||
@Slf4j |
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class DeviceCommandService { |
|||
|
|||
private final ConcurrentMap<Integer, CommandFuture> commandFutureMap = new ConcurrentHashMap<>(); |
|||
|
|||
private final WebSocketService webSocketService; |
|||
private final HardwareService hardwareService; |
|||
|
|||
|
|||
|
|||
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 (!hardwareService.sendCommand(cmdToDevice)) { |
|||
commandFutureMap.remove(cmdToDevice.getCmdId()); |
|||
throw new RuntimeException("向设备发送指令失败"); |
|||
} |
|||
|
|||
cmdFuture.getResponseFuture().whenComplete((result, ex) -> { |
|||
commandFutureMap.remove(cmdToDevice.getCmdId()); |
|||
}); |
|||
|
|||
return cmdFuture; |
|||
} |
|||
|
|||
|
|||
public CommandFuture sendCommandNoFront(DeviceCommand deviceCommand) { |
|||
return executeCommand(deviceCommand); |
|||
} |
|||
|
|||
public CommandFuture sendCommand(String cmdId, String cmdCode, DeviceCommand deviceCommand){ |
|||
CommandFuture commandFuture = executeCommand(deviceCommand); |
|||
commandFuture.setCmdId(cmdId); |
|||
commandFuture.setCmdCode(cmdCode); |
|||
commandFuture.setDeviceCommand(deviceCommand); |
|||
// webSocketService.push(FrontResponseGenerator.generateJson(cmdId, cmdCode, CommandStatus.DEVICE_SEND, deviceCommand.getCmdName() + "指令,已发给设备", deviceCommand)); |
|||
return commandFuture; |
|||
} |
|||
|
|||
public void completeCommandResponse(JSONObject deviceResult) { |
|||
Integer cmdId = deviceResult.getInt("cmdId"); |
|||
if (cmdId != null) { |
|||
CommandFuture future = commandFutureMap.get(cmdId); |
|||
if (future != null) { |
|||
future.setEndSendTime(System.currentTimeMillis()); |
|||
Boolean success = deviceResult.getBool("success"); //数据验证 |
|||
if (success == null || !success) { //response失败 |
|||
if (future.getCmdId() != null) { |
|||
// webSocketService.push(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 { |
|||
if (future.getCmdId() != null) { |
|||
// webSocketService.push(FrontResponseGenerator.generateJson(future.getCmdId(), future.getCmdCode(), CommandStatus.DEVICE_RESULT, |
|||
// future.getDeviceCommand().getCmdName() + "指令,设备response正常,耗时:" + (future.getEndSendTime() - future.getStartSendTime()), deviceResult)); |
|||
} |
|||
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); |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue