diff --git a/src/main/java/com/qyft/ms/app/handler/debugimpl/SyringePumpStart.java b/src/main/java/com/qyft/ms/app/handler/debugimpl/SyringePumpStart.java new file mode 100644 index 0000000..32309b8 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/handler/debugimpl/SyringePumpStart.java @@ -0,0 +1,111 @@ +package com.qyft.ms.app.handler.debugimpl; + +import cn.hutool.json.JSONObject; +import com.qyft.ms.app.common.annotation.CommandMapping; +import com.qyft.ms.app.common.command.CommandFuture; +import com.qyft.ms.app.common.command.CurrentSendCmdMapInstance; +import com.qyft.ms.app.common.command.DeviceCommandGenerator; +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.handler.CommandHandler; +import com.qyft.ms.app.model.bo.CMDToDevice; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.client.TcpClient; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.MediaType; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.util.Map; + +/** + * z轴回原点 + */ +@Slf4j +@Component +@RequiredArgsConstructor +@Async("asyncExecutor") +@CommandMapping("syringe_pump_start")//业务指令注解 +public class SyringePumpStart implements CommandHandler { + private final TcpClient deviceClient; + + /** + * { + * cmdName:'motor_x_move', + * cmdId:'', + * device:'x', + * action:'move', + * param:{ + * current:'',//这四个参数可以不同时存在 + * direction:'forward|backward', + * position:'', + * speed:'' + * } + * } + * @param cmdForm + * @param emitter + * @throws Exception + */ + @Override + public void handle(CMDFormV2 cmdForm, ResponseBodyEmitter emitter) throws Exception { + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + /** + * 1.向前端发送接收到指令 + */ + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + Double speed = (Double) param.get("speed"); + String direction = (String) param.get("direction"); + + + + + + /** + * 5.生成电机移动指令,并发送指令 + */ + + CMDToDevice cmdToDevice = DeviceCommandGenerator.syringe_pump_start(direction,speed);//关闭喷嘴阀 + CommandFuture cmdToDeviceFuture = new CommandFuture(); + cmdToDeviceFuture.setCmdToDevice(cmdToDevice); + Integer nozzleValveCloseCMDToDeviceCmdId = cmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(nozzleValveCloseCMDToDeviceCmdId, cmdToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(cmdToDevice); // 发送指令给设备 + + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了移动注射泵指令", cmdToDevice), MediaType.APPLICATION_JSON); + + /** + * 6.等待 + */ + cmdToDeviceFuture.waitForContinue(); // 等待设备的反馈 + + + /** + * 7.处理结果 + */ + JSONObject cmdToDeviceResult = cmdToDeviceFuture.getCallbackResult(); // 拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(nozzleValveCloseCMDToDeviceCmdId); // 从map中删除该指令 + + if (!cmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动注射泵指令响应超时", cmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + } + if (cmdToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动注射泵返回错误", cmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + } + Boolean nozzleValveCloseCMDToDeviceResultStatus = cmdToDeviceResult.getBool("result"); + if (!nozzleValveCloseCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动注射泵执行失败", cmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + } + + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动注射泵反馈", cmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + } +}