diff --git a/src/main/java/com/qyft/ms/app/common/command/DeviceCommandGenerator.java b/src/main/java/com/qyft/ms/app/common/command/DeviceCommandGenerator.java index f203df3..048e421 100644 --- a/src/main/java/com/qyft/ms/app/common/command/DeviceCommandGenerator.java +++ b/src/main/java/com/qyft/ms/app/common/command/DeviceCommandGenerator.java @@ -224,15 +224,15 @@ public class DeviceCommandGenerator { /** * 推入玻片托盘 */ - public static CMDToDevice slide_tray_in(int position, int speed) { - return motor_y_distance_set(position, speed); + public static CMDToDevice slide_tray_in(Double position, Integer speed) { + return motor_y_position_set(position, speed); } /** * 推出玻片托盘 */ - public static CMDToDevice slide_tray_out(Integer position, Integer speed) { - return motor_y_distance_set(position, speed); + public static CMDToDevice slide_tray_out(Double position, Integer speed) { + return motor_y_position_set(position, speed); } /** @@ -285,21 +285,21 @@ public class DeviceCommandGenerator { /** * 移动x轴到指定位置 */ - public static CMDToDevice motor_x_distance_set(Integer position, Integer speed) { + public static CMDToDevice motor_x_position_set(Double position, Integer speed) { return motor_x("move", null, "forward", position, speed); } /** * 移动y轴到指定位置 */ - public static CMDToDevice motor_y_distance_set(Integer position, Integer speed) { + public static CMDToDevice motor_y_position_set(Double position, Integer speed) { return motor_y("move", null, "forward", position, speed); } /** * 移动z轴到指定位置 */ - public static CMDToDevice motor_z_distance_set(Integer position, Integer speed) { + public static CMDToDevice motor_z_position_set(Double position, Integer speed) { return motor_z("move", null, "forward", position, speed); } @@ -348,28 +348,28 @@ public class DeviceCommandGenerator { /** * x轴电机控制 */ - public static CMDToDevice motor_x(String action, Integer current, String direction, Integer position, Integer speed) { + public static CMDToDevice motor_x(String action, Integer current, String direction, Double position, Integer speed) { return controlMotorCmd("x", action, current, direction, position, speed); } /** * 轴电机控制 */ - public static CMDToDevice motor_y(String action, Integer current, String direction, Integer position, Integer speed) { + public static CMDToDevice motor_y(String action, Integer current, String direction, Double position, Integer speed) { return controlMotorCmd("y", action, current, direction, position, speed); } /** * z轴电机控制 */ - public static CMDToDevice motor_z(String action, Integer current, String direction, Integer position, Integer speed) { + public static CMDToDevice motor_z(String action, Integer current, String direction, Double position, Integer speed) { return controlMotorCmd("z", action, current, direction, position, speed); } /** * 控制电机指令 */ - public static CMDToDevice controlMotorCmd(String device, String action, Integer current, String direction, Integer position, Integer speed) { + public static CMDToDevice controlMotorCmd(String device, String action, Integer current, String direction, Double position, Integer speed) { Map params = new HashMap<>(); params.put("current", current); params.put("direction", direction); diff --git a/src/main/java/com/qyft/ms/app/controller/FrontCmdDebugController.java b/src/main/java/com/qyft/ms/app/controller/FrontCmdDebugController.java new file mode 100644 index 0000000..2a10185 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/FrontCmdDebugController.java @@ -0,0 +1,58 @@ +package com.qyft.ms.app.controller; + +import cn.hutool.json.JSONUtil; +import com.qyft.ms.app.common.command.CurrentSendCmdMapInstance; +import com.qyft.ms.app.core.registry.CommandHandlerRegistry; +import com.qyft.ms.app.handler.CommandHandler; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +@Tag(name = "前端调用指令") +@RestController +@RequestMapping("/api/device/debug/front") +@RequiredArgsConstructor +@Slf4j +public class FrontCmdDebugController { + private final DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2; + private final CommandHandlerRegistry registry; + + @Operation(summary = "前端统一调用一个接口") + @PostMapping("/control") + public ResponseBodyEmitter controlMethod(@RequestBody CMDFormV2 cmdForm) { + ResponseBodyEmitter emitter = new ResponseBodyEmitter(); + String frontCmdName = cmdForm.getCmdName(); // 获取前端传入的命令字符串 + + CommandHandler handler = registry.getHandler(frontCmdName); + if (handler != null) { + try { + handler.handle(cmdForm, emitter); + } catch (Exception e) { + log.error("指令执行失败:{}", JSONUtil.toJsonStr(cmdForm), e); + emitter.completeWithError(e); + } + } else { + try { + emitter.send("未找到对应的业务指令: " + frontCmdName); + emitter.complete(); + } catch (Exception e) { + emitter.completeWithError(e); + } + } + return emitter; + } + + @RequestMapping("/controlTest") + public void controlMethodTest() { + CurrentSendCmdMapInstance.getInstance().getCommand(1).commandContinue(); + } + +} diff --git a/src/main/java/com/qyft/ms/app/handler/impl/DehumidifierStart.java b/src/main/java/com/qyft/ms/app/handler/impl/DehumidifierStart.java index 01d476d..2c4b1aa 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/DehumidifierStart.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/DehumidifierStart.java @@ -54,7 +54,7 @@ public class DehumidifierStart implements CommandHandler { deviceClient.sendToJSON(humidityCmdToDevice); //发送指令给设备 emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了获取湿度指令", humidityCmdToDevice), MediaType.APPLICATION_JSON); humidityGetFuture.waitForContinue();//等待设备的反馈 - //3.处理返回结果或者没有响应 + //3.处理返回结果或者响应超时 JSONObject deviceResult = humidityGetFuture.getCallbackResult();//拿到设备返回结果 CurrentSendCmdMapInstance.getInstance().removeCommand(toDeviceCmdId);//将指令从map中删除 if (humidityGetFuture.isReceived() || humidityGetFuture.isGetResult()) { @@ -80,11 +80,11 @@ public class DehumidifierStart implements CommandHandler { if (dehumidifier_valve_control_Future.isReceived() || dehumidifier_valve_control_Future.isGetResult()) { emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "开启除湿阀指令指令设备反馈", dehumidifier_valve_control_result), MediaType.APPLICATION_JSON);//设备已经收到指令并且执行成功 } else { - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启除湿阀指令没有响应", deviceResult), MediaType.APPLICATION_JSON); //设备未收到指令或者执行失败 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启除湿阀指令响应超时", deviceResult), MediaType.APPLICATION_JSON); //设备未收到指令或者执行失败 } } } else { - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "设备没有响应", deviceResult), MediaType.APPLICATION_JSON);//设备未收到指令或者执行失败 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "设备响应超时", deviceResult), MediaType.APPLICATION_JSON);//设备未收到指令或者执行失败 } emitter.complete(); } diff --git a/src/main/java/com/qyft/ms/app/handler/impl/MotorXToHome.java b/src/main/java/com/qyft/ms/app/handler/impl/MotorXToHome.java index 8ffaeb5..57c350b 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/MotorXToHome.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/MotorXToHome.java @@ -52,7 +52,7 @@ public class MotorXToHome implements CommandHandler { if (motorXToHomeCmdToDeviceFuture.isReceived()) { emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴回原点指令设备反馈", deviceResult), MediaType.APPLICATION_JSON); } else { - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令没有响应", deviceResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令响应超时", deviceResult), MediaType.APPLICATION_JSON); } } } diff --git a/src/main/java/com/qyft/ms/app/handler/impl/MotorYToHome.java b/src/main/java/com/qyft/ms/app/handler/impl/MotorYToHome.java index 94ae307..e25ac56 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/MotorYToHome.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/MotorYToHome.java @@ -52,7 +52,7 @@ public class MotorYToHome implements CommandHandler { if (motorYToHomeCmdToDeviceFuture.isReceived()) { emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "y轴回原点指令设备反馈", deviceResult), MediaType.APPLICATION_JSON); } else { - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令没有响应", deviceResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令响应超时", deviceResult), MediaType.APPLICATION_JSON); } } } diff --git a/src/main/java/com/qyft/ms/app/handler/impl/MotorZToHome.java b/src/main/java/com/qyft/ms/app/handler/impl/MotorZToHome.java index dc7396f..ae48b00 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/MotorZToHome.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/MotorZToHome.java @@ -52,7 +52,7 @@ public class MotorZToHome implements CommandHandler { if (motorZToHomeCmdToDeviceFuture.isReceived()) { emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "z轴回原点指令设备反馈", deviceResult), MediaType.APPLICATION_JSON); } else { - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令没有响应", deviceResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令响应超时", deviceResult), MediaType.APPLICATION_JSON); } } } diff --git a/src/main/java/com/qyft/ms/app/handler/impl/NozzlePipelinePreFill.java b/src/main/java/com/qyft/ms/app/handler/impl/NozzlePipelinePreFill.java index bfe9942..62ac4de 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/NozzlePipelinePreFill.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/NozzlePipelinePreFill.java @@ -1,10 +1,16 @@ package com.qyft.ms.app.handler.impl; +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 org.springframework.http.MediaType; import org.springframework.scheduling.annotation.Async; @@ -21,6 +27,10 @@ import java.util.Map; @Async("asyncExecutor") @CommandMapping("matrix_prefill")//业务指令注解 public class NozzlePipelinePreFill implements CommandHandler { + /** + * 设备通信client + */ + private final TcpClient deviceClient; @Override public void handle(CMDFormV2 cmdForm, ResponseBodyEmitter emitter) throws Exception { @@ -28,5 +38,167 @@ public class NozzlePipelinePreFill implements CommandHandler { String frontCmdName = cmdForm.getCmdName(); Map param = cmdForm.getParam(); emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON);//向前端发送接收到指令 + //1.传参校验 + Integer speed = (Integer) param.get("speed"); + if (speed == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 speed 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + //2.判断z轴是否在安全距离,如果不在安全距离可以不抬升z轴 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "判断z轴是否在安全距离,如果不在安全距离可以不抬升z轴"), MediaType.APPLICATION_JSON); + CMDToDevice motorXYZPositionGetCmdToDevice = DeviceCommandGenerator.motor_xyz_position_get();//生成指令 获取xyz当前位置 + CommandFuture motorXYZPositionGetCmdToDeviceFuture = new CommandFuture(); + motorXYZPositionGetCmdToDeviceFuture.setCmdToDevice(motorXYZPositionGetCmdToDevice); + Integer motorXYZPositionGetCmdToDeviceCmdId = motorXYZPositionGetCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorXYZPositionGetCmdToDeviceCmdId, motorXYZPositionGetCmdToDeviceFuture);//将指令放入map + deviceClient.sendToJSON(motorXYZPositionGetCmdToDevice); //发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了获得电机XYZ相对原点坐标指令", motorXYZPositionGetCmdToDevice), MediaType.APPLICATION_JSON); + motorXYZPositionGetCmdToDeviceFuture.waitForContinue();//等待设备的反馈 + JSONObject motorXYZPositionGetCmdToDeviceResult = motorXYZPositionGetCmdToDeviceFuture.getCallbackResult();//拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorXYZPositionGetCmdToDeviceCmdId);//将指令从map中删除 + if (!motorXYZPositionGetCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "获得电机XYZ相对原点坐标指令响应超时", motorXYZPositionGetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorXYZPositionGetCmdToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "获得电机XYZ相对原点坐标指令返回错误", motorXYZPositionGetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Double zAxisPosition = motorXYZPositionGetCmdToDeviceResult.getJSONObject("result").getDouble("zAxisPosition");//当前z轴相对于原点的位置 + if(zAxisPosition == null || zAxisPosition < 0.0) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "获得电机XYZ相对原点坐标指令执行失败", motorXYZPositionGetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "电机XYZ相对原点坐标指令反馈", motorXYZPositionGetCmdToDeviceResult), MediaType.APPLICATION_JSON); + if (zAxisPosition > 80) { //z轴超出安全距离,抬升z轴 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "z轴超出安全距离,抬升z轴至安全距离"), MediaType.APPLICATION_JSON); + CMDToDevice motorZPositionSetCmdToDevice = DeviceCommandGenerator.motor_z_position_set(80.0, 2);//生成指令 移动z轴到指定位置 + CommandFuture motorZPositionSetCmdToDeviceFuture = new CommandFuture(); + motorZPositionSetCmdToDeviceFuture.setCmdToDevice(motorZPositionSetCmdToDevice); + Integer motorZPositionSetCmdToDeviceCmdId = motorXYZPositionGetCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorZPositionSetCmdToDeviceCmdId, motorZPositionSetCmdToDeviceFuture);//将指令放入map + deviceClient.sendToJSON(motorZPositionSetCmdToDevice); //发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了移动z轴到指定位置指令", motorXYZPositionGetCmdToDevice), MediaType.APPLICATION_JSON); + motorZPositionSetCmdToDeviceFuture.waitForContinue();//等待设备的反馈 + JSONObject motorZPositionSetCmdToDeviceResult = motorXYZPositionGetCmdToDeviceFuture.getCallbackResult();//拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorZPositionSetCmdToDeviceCmdId);//将指令从map中删除 + if (!motorZPositionSetCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动z轴到指定位指令置响应超时", motorZPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorZPositionSetCmdToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动z轴到指定位指令返回错误", motorZPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + + Boolean motorZPositionSetCmdToDeviceResultStatus = motorZPositionSetCmdToDeviceResult.getBool("result"); + if(!motorZPositionSetCmdToDeviceResultStatus){ + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动z轴到指定位指令执行失败", motorZPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动z轴到指定位指令反馈", motorZPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + } + //3.轴移动到废液位置 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "轴移动到废液位置"), MediaType.APPLICATION_JSON); + CMDToDevice motorXPositionSetCmdToDevice = DeviceCommandGenerator.motor_x_position_set(173.08, 0);//生成指令 移动x轴到指定位置 + CommandFuture motorXPositionSetCmdToDeviceFuture = new CommandFuture(); + motorXPositionSetCmdToDeviceFuture.setCmdToDevice(motorXPositionSetCmdToDevice); + Integer motorXPositionSetCmdToDeviceCmdId = motorXPositionSetCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorXPositionSetCmdToDeviceCmdId, motorXPositionSetCmdToDeviceFuture);//将指令放入map + deviceClient.sendToJSON(motorXPositionSetCmdToDevice); //发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了移动x轴到指定位置指令", motorXPositionSetCmdToDevice), MediaType.APPLICATION_JSON); + motorXPositionSetCmdToDeviceFuture.waitForContinue();//等待设备的反馈 + JSONObject motorXPositionSetCmdToDeviceResult = motorXPositionSetCmdToDeviceFuture.getCallbackResult();//拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorXPositionSetCmdToDeviceCmdId);//将指令从map中删除 + if (!motorXPositionSetCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动x轴到指定位置指令响应超时", motorXPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorXPositionSetCmdToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动x轴到指定位置指令返回错误", motorXPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean motorXYZPositionGetCmdToDeviceResultStatus = motorXPositionSetCmdToDeviceResult.getBool("result"); + if (!motorXYZPositionGetCmdToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动x轴到指定位置指令执行失败", motorXPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动x轴到指定位置指令反馈", motorXPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + + + CMDToDevice motorYPositionSetCMDToDevice = DeviceCommandGenerator.motor_y_position_set(173.08, 0); + CommandFuture motorYPositionSetCMDToDeviceFuture = new CommandFuture(); + motorYPositionSetCMDToDeviceFuture.setCmdToDevice(motorYPositionSetCMDToDevice); + Integer motorYPositionSetCMDToDeviceCmdId = motorYPositionSetCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorYPositionSetCMDToDeviceCmdId, motorYPositionSetCMDToDeviceFuture);//将指令放入map + deviceClient.sendToJSON(motorYPositionSetCMDToDevice); //发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了移动y轴到指定位置指令", motorYPositionSetCMDToDevice), MediaType.APPLICATION_JSON); + motorYPositionSetCMDToDeviceFuture.waitForContinue();//等待设备的反馈 + JSONObject motorYPositionSetCMDToDeviceResult = motorYPositionSetCMDToDeviceFuture.getCallbackResult();//拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorYPositionSetCMDToDeviceCmdId);//将指令从map中删除 + if (!motorYPositionSetCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动x轴到指定位置指令响应超时", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorYPositionSetCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动x轴到指定位置指令返回错误", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean motorYPositionSetCMDToDeviceResultStatus = motorYPositionSetCMDToDeviceResult.getBool("result"); + if (!motorYPositionSetCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动x轴到指定位置指令执行失败", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动x轴到指定位置指令反馈", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + //4.下降z轴高度,防止飞溅 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "下降z轴高度,防止飞溅"), MediaType.APPLICATION_JSON); + CMDToDevice motorZPositionSetDownCmdToDevice = DeviceCommandGenerator.motor_z_position_set(85.0, 2);//生成指令 移动z轴到指定位置 + CommandFuture motorZPositionSetDownCmdToDeviceFuture = new CommandFuture(); + motorZPositionSetDownCmdToDeviceFuture.setCmdToDevice(motorZPositionSetDownCmdToDevice); + Integer motorZPositionSetDownCmdToDeviceCmdId = motorZPositionSetDownCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorZPositionSetDownCmdToDeviceCmdId, motorZPositionSetDownCmdToDeviceFuture);//将指令放入map + deviceClient.sendToJSON(motorZPositionSetDownCmdToDevice); //发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了移动z轴到指定位置指令", motorZPositionSetDownCmdToDevice), MediaType.APPLICATION_JSON); + motorZPositionSetDownCmdToDeviceFuture.waitForContinue();//等待设备的反馈 + JSONObject motorZPositionSetDownCmdToDeviceResult = motorZPositionSetDownCmdToDeviceFuture.getCallbackResult();//拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorZPositionSetDownCmdToDeviceCmdId);//将指令从map中删除 + if (!motorZPositionSetDownCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动z轴到指定位指令置响应超时", motorZPositionSetDownCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorZPositionSetDownCmdToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动z轴到指定位指令返回错误", motorZPositionSetDownCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + + Boolean motorZPositionSetDownCmdToDeviceResultStatus = motorZPositionSetDownCmdToDeviceResult.getBool("result"); + if(!motorZPositionSetDownCmdToDeviceResultStatus){ + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动z轴到指定位指令执行失败", motorZPositionSetDownCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动z轴到指定位指令反馈", motorZPositionSetDownCmdToDeviceResult), MediaType.APPLICATION_JSON); + //5.三通阀切换到注射器管路 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "三通阀切换到注射器管路"), MediaType.APPLICATION_JSON); + CMDToDevice a = DeviceCommandGenerator.motor_z_position_set(85.0, 2);//生成指令 移动z轴到指定位置 + //6.打开喷嘴阀 + //7.设置注射泵速度 + //8.推注射泵" + emitter.complete(); } } diff --git a/src/main/java/com/qyft/ms/app/handler/impl/SlideTrayIn.java b/src/main/java/com/qyft/ms/app/handler/impl/SlideTrayIn.java index 62a58dc..92d0943 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/SlideTrayIn.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/SlideTrayIn.java @@ -37,7 +37,7 @@ public class SlideTrayIn implements CommandHandler { String frontCmdName = cmdForm.getCmdName(); //向前端发送接收到指令 emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - CMDToDevice slideTrayInCmdToDevice = DeviceCommandGenerator.slide_tray_in(0, 2);//生成指令 推入玻片托盘 + CMDToDevice slideTrayInCmdToDevice = DeviceCommandGenerator.slide_tray_in(0.0, 2);//生成指令 推入玻片托盘 CommandFuture slideTrayInCmdToDeviceFuture = new CommandFuture(); slideTrayInCmdToDeviceFuture.setCmdToDevice(slideTrayInCmdToDevice); @@ -54,7 +54,7 @@ public class SlideTrayIn implements CommandHandler { if (slideTrayInCmdToDeviceFuture.isReceived()) { emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "推入玻片托盘指令设备反馈:", deviceResult), MediaType.APPLICATION_JSON); } else { - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "推入玻片托盘指令没有响应:", deviceResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "推入玻片托盘指令响应超时:", deviceResult), MediaType.APPLICATION_JSON); } } } diff --git a/src/main/java/com/qyft/ms/app/handler/impl/SlideTrayOut.java b/src/main/java/com/qyft/ms/app/handler/impl/SlideTrayOut.java index 6156800..76d3084 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/SlideTrayOut.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/SlideTrayOut.java @@ -37,7 +37,7 @@ public class SlideTrayOut implements CommandHandler { //向前端发送接收到指令 emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); //TODO 数值应该从数据库中获取 - CMDToDevice slideTrayOutCmdToDevice = DeviceCommandGenerator.slide_tray_out(20, 2);//生成指令 推出玻片托盘 + CMDToDevice slideTrayOutCmdToDevice = DeviceCommandGenerator.slide_tray_out(20.0, 2);//生成指令 推出玻片托盘 CommandFuture slideTrayOutCmdToDeviceFuture = new CommandFuture(); slideTrayOutCmdToDeviceFuture.setCmdToDevice(slideTrayOutCmdToDevice); @@ -54,7 +54,7 @@ public class SlideTrayOut implements CommandHandler { if (slideTrayOutCmdToDeviceFuture.isReceived()) { emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "推出玻片托盘指令设备反馈:", deviceResult), MediaType.APPLICATION_JSON); } else { - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "推出玻片托盘指令没有响应:", deviceResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "推出玻片托盘指令响应超时:", deviceResult), MediaType.APPLICATION_JSON); } } } \ No newline at end of file diff --git a/src/main/java/com/qyft/ms/device/handler/DeviceMessageHandler.java b/src/main/java/com/qyft/ms/device/handler/DeviceMessageHandler.java index 1b5a3a5..6c0a7e0 100644 --- a/src/main/java/com/qyft/ms/device/handler/DeviceMessageHandler.java +++ b/src/main/java/com/qyft/ms/device/handler/DeviceMessageHandler.java @@ -52,44 +52,15 @@ public class DeviceMessageHandler extends ChannelInboundHandlerAdapter { String tag = deviceResult.get("tag").toString(); if ("event".equals(tag)) { //设备上报事件 - - - - } else if ("ack".equals(tag)) { - - - //设备指令反馈 Integer cmdId = payload.getInt("cmdId"); CommandFuture commandFuture = CurrentSendCmdMapInstance.getInstance().getCommand(cmdId); - if(commandFuture!=null){ commandFuture.setReceived(true); commandFuture.setCallbackResult(payload); commandFuture.commandContinue(); } -// -// if (error != null) { -// //指令执行错误,将错误放到CallbackResult中 -// commandFuture.setCallbackResult(error); -// } -// Object result = payload.get("result"); -// if (result instanceof Boolean) { -// //ack 没携带数据 -// } else if (result instanceof JSONObject) { -// //ack 携带了数据,将数据放到了CallbackResult中 -// commandFuture.setCallbackResult((JSONObject) result); -// } else { -// //TODO 没有定义的类型 -// } -// commandFuture.setReceived(true);//已收到设备反馈 -// commandFuture.commandContinue(); - - - - - } else if ("status".equals(tag)) { //设备上报状态 }