From 8fa11ef2f311fa7e3fabe871a1e7e0ff079bbcf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Fri, 14 Mar 2025 00:50:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AE=8C=E5=96=84=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qyft/ms/app/common/command/CommandFuture.java | 13 +- .../ms/app/common/command/CommandWaitControl.java | 9 +- .../app/common/command/DeviceCommandGenerator.java | 56 +- .../app/handler/impl/MatrixSprayChangeParam.java | 328 ++++++++ .../ms/app/handler/impl/MatrixSprayContinue.java | 108 ++- .../qyft/ms/app/handler/impl/MatrixSprayPause.java | 145 ++++ .../qyft/ms/app/handler/impl/MatrixSprayStart.java | 844 +++++++++++++++++++-- .../qyft/ms/app/handler/impl/MatrixSprayStop.java | 270 +++++++ .../com/qyft/ms/app/handler/impl/MotorXToHome.java | 43 +- .../com/qyft/ms/app/handler/impl/MotorYToHome.java | 47 +- .../com/qyft/ms/app/handler/impl/MotorZToHome.java | 46 +- .../ms/app/handler/impl/NozzlePipelinePreFill.java | 2 +- .../java/com/qyft/ms/app/service/CMDService.java | 8 +- .../ms/device/handler/DeviceMessageHandler.java | 53 +- src/main/resources/application.yml | 3 +- 15 files changed, 1828 insertions(+), 147 deletions(-) diff --git a/src/main/java/com/qyft/ms/app/common/command/CommandFuture.java b/src/main/java/com/qyft/ms/app/common/command/CommandFuture.java index d856e4f..2ced5e7 100644 --- a/src/main/java/com/qyft/ms/app/common/command/CommandFuture.java +++ b/src/main/java/com/qyft/ms/app/common/command/CommandFuture.java @@ -4,6 +4,8 @@ import cn.hutool.json.JSONObject; import com.qyft.ms.app.model.bo.CMDToDevice; import lombok.Data; +import java.util.concurrent.CompletableFuture; + @Data public class CommandFuture { @@ -27,6 +29,16 @@ public class CommandFuture { */ private boolean isGetResult = false; + /** + * 指令执行完毕 + */ + private CompletableFuture finishedFuture = new CompletableFuture<>(); + + + public void waitForContinue(int timeoutMillis) { + commandWaitController.commandWait(timeoutMillis); + } + public void waitForContinue() { commandWaitController.commandWait(); } @@ -35,5 +47,4 @@ public class CommandFuture { commandWaitController.commandContinue(); } - } diff --git a/src/main/java/com/qyft/ms/app/common/command/CommandWaitControl.java b/src/main/java/com/qyft/ms/app/common/command/CommandWaitControl.java index 93917c7..97d44c3 100644 --- a/src/main/java/com/qyft/ms/app/common/command/CommandWaitControl.java +++ b/src/main/java/com/qyft/ms/app/common/command/CommandWaitControl.java @@ -2,17 +2,18 @@ package com.qyft.ms.app.common.command; public class CommandWaitControl { - public synchronized void commandWait() { + public synchronized void commandWait(int timeoutMillis) { try { - wait(3000); + wait(timeoutMillis); } catch (InterruptedException e) { throw new RuntimeException(e); } } + public synchronized void commandWait() { + commandWait(3000); + } public synchronized void commandContinue() { notify(); } - - } 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 2d3dc9c..c455eb1 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 @@ -192,12 +192,21 @@ public class DeviceCommandGenerator { */ public static CMDToDevice syringe_pump_injection_volume_set(Integer volume) { Map params = new HashMap<>(); - params.put("current", "forward"); + params.put("direction", "forward"); params.put("volume", volume); return controlCmd("syringe_pump", "move", params); } /** + * 注射泵流速设置 + */ + public static CMDToDevice syringe_pump_volume_set(Integer volume) { + Map params = new HashMap<>(); + params.put("volume", volume); + return controlCmd("syringe_pump", "set", params); + } + + /** * 控制注射泵 */ public static CMDToDevice syringe_pump_control(String action, String forward, Integer volume) { @@ -249,12 +258,14 @@ public class DeviceCommandGenerator { public static CMDToDevice motor_x_stop() { return motor_x("stop", null, null, null, null); } + /** * y轴停止移动 */ public static CMDToDevice motor_y_stop() { return motor_x("stop", null, null, null, null); } + /** * z轴停止移动 */ @@ -286,6 +297,27 @@ public class DeviceCommandGenerator { /** * 移动x轴到指定位置 */ + public static CMDToDevice motor_x_position_set(Double position) { + return motor_x("move", null, "forward", position, null); + } + + /** + * 移动y轴到指定位置 + */ + public static CMDToDevice motor_y_position_set(Double position) { + return motor_y("move", null, "forward", position, null); + } + + /** + * 移动z轴到指定位置 + */ + public static CMDToDevice motor_z_position_set(Double position) { + return motor_z("move", null, "forward", position, null); + } + + /** + * 移动x轴到指定位置 + */ public static CMDToDevice motor_x_position_set(Double position, Integer speed) { return motor_x("move", null, "forward", position, speed); } @@ -347,6 +379,27 @@ public class DeviceCommandGenerator { } /** + * x轴电机速度设置 + */ + public static CMDToDevice motor_x_speed_set(Integer speed) { + return controlMotorCmd("x", "set", null, null, null, speed); + } + + /** + * y轴电机速度设置 + */ + public static CMDToDevice motor_y_speed_set(Integer speed) { + return controlMotorCmd("y", "set", null, null, null, speed); + } + + /** + * z轴电机速度设置 + */ + public static CMDToDevice motor_z_speed_set(Integer speed) { + return controlMotorCmd("z", "set", null, null, null, speed); + } + + /** * x轴电机控制 */ public static CMDToDevice motor_x(String action, Integer current, String direction, Double position, Integer speed) { @@ -416,6 +469,7 @@ public class DeviceCommandGenerator { /** * 电机移动 + * * @return */ public static CMDToDevice motor_to_position(MotorNameEnum motorName, int position) { diff --git a/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayChangeParam.java b/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayChangeParam.java index d268da7..b9a0a45 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayChangeParam.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayChangeParam.java @@ -1,14 +1,27 @@ 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 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.io.IOException; +import java.util.List; +import java.util.Map; + /** * 喷涂_喷涂过程中参数实时调整 */ @@ -18,9 +31,324 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter @Async("asyncExecutor") @CommandMapping("matrix_spray_change_param")//业务指令注解 public class MatrixSprayChangeParam implements CommandHandler { + /** + * 设备通信client + */ + private final TcpClient deviceClient; + /** + * { + * cmdName:'matrix_spray_change_param' + * cmdId:'', + * param:{ + * motor_z_height:;//高度 + * gas_pressure://Mpa兆帕 + * volume:20;//单位uL微升 + * high_voltage:true/false;//是否打开高压 + * high_voltage_value:4000;//高压值 + * spacing:''//毫米 + * moving_speed:8mm/s;//移动速度 + * } + * } + */ @Override public void handle(CMDFormV2 cmdForm, ResponseBodyEmitter emitter) throws Exception { + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON);//向前端发送接收到指令 + // 1. 参数校验 + Integer motorZHeight = (Integer) param.get("motor_z_height");//Z轴距离玻片的高度(距玻片的距离) + Integer gasPressure = (Integer) param.get("gas_pressure");//Mpa兆帕 不处理 + Integer volume = (Integer) param.get("volume");//单位uL微升 基质流速(控制注射泵速度) + Boolean highVoltage = (Boolean) param.get("high_voltage");//是否打开高压 + Integer highVoltageValue = (Integer) param.get("high_voltage_value");//高压值 + Integer movingSpeed = (Integer) param.get("moving_speed");//轴移动速度 + //noinspection unchecked + List> positionList = (List>) param.get("position"); + if (validate(emitter, frontCmdId, frontCmdName, motorZHeight, gasPressure, volume, highVoltage, highVoltageValue, movingSpeed, positionList)) + return; + + //1.速度 + if (motorSpeedSet(emitter, movingSpeed, frontCmdId, frontCmdName)) return; + //2.流速 + if (syringePumpVolumeSet(emitter, volume, frontCmdId, frontCmdName)) return; + //3.z轴高度 + Double height = 101.2 - motorZHeight;//TODO 101.2是玻片高度,这个应该从数据库中获取 + if (moveZ(emitter, height, movingSpeed, frontCmdId, frontCmdName)) return; + //4.是否打开高压 + if (highVoltage) {//加电 + highVoltageOpen(emitter, highVoltageValue, frontCmdId, frontCmdName); + }else{//关闭高压 + highVoltageClose(emitter, frontCmdId, frontCmdName); + } + } + + private boolean motorSpeedSet(ResponseBodyEmitter emitter, Integer movingSpeed, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorXSpeedSetCmdToDevice = DeviceCommandGenerator.motor_x_speed_set(movingSpeed); // x轴电机速度设置 + CommandFuture motorXSpeedSetCmdToDeviceFuture = new CommandFuture(); + motorXSpeedSetCmdToDeviceFuture.setCmdToDevice(motorXSpeedSetCmdToDevice); + Integer motorXSpeedSetCmdId = motorXSpeedSetCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorXSpeedSetCmdId, motorXSpeedSetCmdToDeviceFuture); + deviceClient.sendToJSON(motorXSpeedSetCmdToDevice); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了x轴电机速度设置指令", motorXSpeedSetCmdToDevice), MediaType.APPLICATION_JSON); + + motorXSpeedSetCmdToDeviceFuture.waitForContinue(); + JSONObject motorXSpeedSetResult = motorXSpeedSetCmdToDeviceFuture.getCallbackResult(); + CurrentSendCmdMapInstance.getInstance().removeCommand(motorXSpeedSetCmdId); + + if (!motorXSpeedSetCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴电机速度设置指令响应超时", motorXSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorXSpeedSetResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴电机速度设置指令返回错误", motorXSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorXSpeedSetStatus = motorXSpeedSetResult.getBool("result"); + if (!motorXSpeedSetStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴电机速度设置指令执行失败", motorXSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴电机速度设置指令反馈", motorXSpeedSetResult), MediaType.APPLICATION_JSON); + + + CMDToDevice motorYSpeedSetCmdToDevice = DeviceCommandGenerator.motor_x_speed_set(movingSpeed); // y轴电机速度设置 + CommandFuture motorYSpeedSetCmdToDeviceFuture = new CommandFuture(); + motorYSpeedSetCmdToDeviceFuture.setCmdToDevice(motorYSpeedSetCmdToDevice); + Integer motorYSpeedSetCmdId = motorYSpeedSetCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorYSpeedSetCmdId, motorYSpeedSetCmdToDeviceFuture); + deviceClient.sendToJSON(motorYSpeedSetCmdToDevice); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了y轴电机速度设置指令", motorYSpeedSetCmdToDevice), MediaType.APPLICATION_JSON); + + motorYSpeedSetCmdToDeviceFuture.waitForContinue(); + JSONObject motorYSpeedSetResult = motorYSpeedSetCmdToDeviceFuture.getCallbackResult(); + CurrentSendCmdMapInstance.getInstance().removeCommand(motorYSpeedSetCmdId); + + if (!motorYSpeedSetCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴电机速度设置指令响应超时", motorYSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorYSpeedSetResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴电机速度设置指令返回错误", motorYSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorYSpeedSetStatus = motorYSpeedSetResult.getBool("result"); + if (!motorYSpeedSetStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴电机速度设置指令执行失败", motorYSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "y轴电机速度设置指令反馈", motorYSpeedSetResult), MediaType.APPLICATION_JSON); + + + CMDToDevice motorZSpeedSetCmdToDevice = DeviceCommandGenerator.motor_x_speed_set(movingSpeed);// z轴电机速度设置 + CommandFuture motorZSpeedSetCmdToDeviceFuture = new CommandFuture(); + motorZSpeedSetCmdToDeviceFuture.setCmdToDevice(motorZSpeedSetCmdToDevice); + Integer motorZSpeedSetCmdId = motorZSpeedSetCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorZSpeedSetCmdId, motorZSpeedSetCmdToDeviceFuture); + deviceClient.sendToJSON(motorZSpeedSetCmdToDevice); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了z轴电机速度设置指令", motorZSpeedSetCmdToDevice), MediaType.APPLICATION_JSON); + + motorZSpeedSetCmdToDeviceFuture.waitForContinue(); + JSONObject motorZSpeedSetResult = motorZSpeedSetCmdToDeviceFuture.getCallbackResult(); + CurrentSendCmdMapInstance.getInstance().removeCommand(motorZSpeedSetCmdId); + + if (!motorZSpeedSetCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴电机速度设置指令响应超时", motorZSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorZSpeedSetResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴电机速度设置指令返回错误", motorZSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorZSpeedSetStatus = motorZSpeedSetResult.getBool("result"); + if (!motorZSpeedSetStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴电机速度设置指令执行失败", motorZSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "z轴电机速度设置指令反馈", motorZSpeedSetResult), MediaType.APPLICATION_JSON); + return false; + } + + private boolean syringePumpVolumeSet(ResponseBodyEmitter emitter, Integer volume, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice syringePumpVolumeSetCmdToDevice = DeviceCommandGenerator.syringe_pump_volume_set(volume);//注射泵流速设置 + CommandFuture syringePumpVolumeSetCmdToDeviceFuture = new CommandFuture(); + syringePumpVolumeSetCmdToDeviceFuture.setCmdToDevice(syringePumpVolumeSetCmdToDevice); + Integer syringePumpVolumeSetCmdId = syringePumpVolumeSetCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(syringePumpVolumeSetCmdId, syringePumpVolumeSetCmdToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(syringePumpVolumeSetCmdToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了注射泵流速设置指令", syringePumpVolumeSetCmdToDevice), MediaType.APPLICATION_JSON); + + syringePumpVolumeSetCmdToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject syringePumpVolumeSetResult = syringePumpVolumeSetCmdToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(syringePumpVolumeSetCmdId); // 从map中删除该指令 + + if (!syringePumpVolumeSetCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "注射泵流速设置指令响应超时", syringePumpVolumeSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + if (syringePumpVolumeSetResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "注射泵流速设置指令返回错误", syringePumpVolumeSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + Boolean syringePumpVolumeSetStatus = syringePumpVolumeSetResult.getBool("result"); + if (!syringePumpVolumeSetStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "注射泵流速设置指令执行失败", syringePumpVolumeSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "注射泵流速设置指令反馈", syringePumpVolumeSetResult), MediaType.APPLICATION_JSON); + return false; + } + + private void highVoltageClose(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice highVoltageCloseCmdToDevice = DeviceCommandGenerator.high_voltage_close();//关闭高压 + CommandFuture highVoltageCloseCmdToDeviceFuture = new CommandFuture(); + highVoltageCloseCmdToDeviceFuture.setCmdToDevice(highVoltageCloseCmdToDevice); + Integer highVoltageCloseCmdId = highVoltageCloseCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(highVoltageCloseCmdId, highVoltageCloseCmdToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(highVoltageCloseCmdToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了关闭高压指令", highVoltageCloseCmdToDevice), MediaType.APPLICATION_JSON); + + highVoltageCloseCmdToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject highVoltageCloseResult = highVoltageCloseCmdToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(highVoltageCloseCmdId); // 从map中删除该指令 + + if (!highVoltageCloseCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭高压指令响应超时", highVoltageCloseResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + + if (highVoltageCloseResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭高压指令返回错误", highVoltageCloseResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + + Boolean highVoltageCloseStatus = highVoltageCloseResult.getBool("result"); + if (!highVoltageCloseStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭高压指令执行失败", highVoltageCloseResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "关闭高压指令反馈", highVoltageCloseResult), MediaType.APPLICATION_JSON); + } + + private static boolean validate(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName, Integer motorZHeight, Integer gasPressure, Integer volume, Boolean highVoltage, Integer highVoltageValue, Integer movingSpeed, List> positionList) throws IOException { + if (motorZHeight == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 motor_z_height 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (gasPressure == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 gas_pressure 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (volume == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 volume 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (highVoltage == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 high_voltage 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (highVoltageValue == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 high_voltage_value 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (movingSpeed == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 moving_speed 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (positionList == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 position 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + return false; + } + + + private boolean moveZ(ResponseBodyEmitter emitter, Double height, Integer movingSpeed, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorZPositionSetDownCmdToDevice = DeviceCommandGenerator.motor_z_position_set(height, movingSpeed);//移动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 true; + } + if (motorZPositionSetDownCmdToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动z轴到指定位指令返回错误", motorZPositionSetDownCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + Boolean motorZPositionSetDownCmdToDeviceResultStatus = motorZPositionSetDownCmdToDeviceResult.getBool("result"); + if (!motorZPositionSetDownCmdToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动z轴到指定位指令执行失败", motorZPositionSetDownCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动z轴到指定位指令反馈", motorZPositionSetDownCmdToDeviceResult), MediaType.APPLICATION_JSON); + return false; + } + private boolean highVoltageOpen(ResponseBodyEmitter emitter, Integer highVoltageValue, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice highVoltageOpenCmdToDevice = DeviceCommandGenerator.high_voltage_open(highVoltageValue);//开启高压 + CommandFuture highVoltageOpenCmdToDeviceFuture = new CommandFuture(); + highVoltageOpenCmdToDeviceFuture.setCmdToDevice(highVoltageOpenCmdToDevice); + Integer highVoltageOpenCmdId = highVoltageOpenCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(highVoltageOpenCmdId, highVoltageOpenCmdToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(highVoltageOpenCmdToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了开启高压指令", highVoltageOpenCmdToDevice), MediaType.APPLICATION_JSON); + highVoltageOpenCmdToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject highVoltageOpenResult = highVoltageOpenCmdToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(highVoltageOpenCmdId); // 从map中删除该指令 + if (!highVoltageOpenCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启高压指令响应超时", highVoltageOpenResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (highVoltageOpenResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启高压指令返回错误", highVoltageOpenResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean highVoltageOpenStatus = highVoltageOpenResult.getBool("result"); + if (!highVoltageOpenStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启高压指令执行失败", highVoltageOpenResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "开启高压指令反馈", highVoltageOpenResult), MediaType.APPLICATION_JSON); + return false; } } diff --git a/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayContinue.java b/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayContinue.java index f59821d..0fb80b1 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayContinue.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayContinue.java @@ -1,10 +1,19 @@ 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 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; @@ -18,9 +27,106 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter @Async("asyncExecutor") @CommandMapping("matrix_spray_continue")//业务指令注解 public class MatrixSprayContinue implements CommandHandler { + /** + * 设备通信client + */ + private final TcpClient deviceClient; + + private final MatrixSprayStart matrixSprayStart; @Override public void handle(CMDFormV2 cmdForm, ResponseBodyEmitter emitter) throws Exception { - + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON);//向前端发送接收到指令 + //开启喷嘴阀 + CMDToDevice nozzleValveOpenCMDToDevice = DeviceCommandGenerator.nozzle_valve_open();//生成指令 开启喷嘴阀 + CommandFuture nozzleValveOpenCMDToDeviceFuture = new CommandFuture(); + nozzleValveOpenCMDToDeviceFuture.setCmdToDevice(nozzleValveOpenCMDToDevice); + Integer nozzleValveOpenCMDToDeviceCmdId = nozzleValveOpenCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(nozzleValveOpenCMDToDeviceCmdId, nozzleValveOpenCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(nozzleValveOpenCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了开启喷嘴阀指令", nozzleValveOpenCMDToDevice), MediaType.APPLICATION_JSON); + nozzleValveOpenCMDToDeviceFuture.waitForContinue(); // 等待设备的反馈 + JSONObject nozzleValveOpenCMDToDeviceResult = nozzleValveOpenCMDToDeviceFuture.getCallbackResult(); // 拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(nozzleValveOpenCMDToDeviceCmdId); // 将指令从map中删除 + if (!nozzleValveOpenCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启喷嘴阀指令响应超时", nozzleValveOpenCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (nozzleValveOpenCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启喷嘴阀指令返回错误", nozzleValveOpenCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean nozzleValveOpenCMDToDeviceResultStatus = nozzleValveOpenCMDToDeviceResult.getBool("result"); + if (!nozzleValveOpenCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启喷嘴阀指令执行失败", nozzleValveOpenCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "开启喷嘴阀指令反馈", nozzleValveOpenCMDToDeviceResult), MediaType.APPLICATION_JSON); + //轴继续移动 + Double targetX = matrixSprayStart.getTargetX(); + Double targetY = matrixSprayStart.getTargetY(); + matrixSprayStart.setTargetX(null); + matrixSprayStart.setTargetY(null); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "轴移动到废液位置"), MediaType.APPLICATION_JSON); + CMDToDevice motorXPositionSetCmdToDevice = DeviceCommandGenerator.motor_x_position_set(targetX);//生成指令 移动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 motorXYZPositionSetCmdToDeviceResultStatus = motorXPositionSetCmdToDeviceResult.getBool("result"); + if (!motorXYZPositionSetCmdToDeviceResultStatus) { + 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(targetY);//移动y轴到指定位置 + 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, "移动y轴到指定位置指令响应超时", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorYPositionSetCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动y轴到指定位置指令返回错误", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean motorYPositionSetCMDToDeviceResultStatus = motorYPositionSetCMDToDeviceResult.getBool("result"); + if (!motorYPositionSetCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动y轴到指定位置指令执行失败", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动y轴到指定位置指令反馈", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); } } diff --git a/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayPause.java b/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayPause.java index 50eba11..b859007 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayPause.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayPause.java @@ -1,10 +1,19 @@ 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 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; @@ -18,9 +27,145 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter @Async("asyncExecutor") @CommandMapping("matrix_spray_pause")//业务指令注解 public class MatrixSprayPause implements CommandHandler { + /** + * 设备通信client + */ + private final TcpClient deviceClient; + + private final MatrixSprayStart matrixSprayStart; @Override public void handle(CMDFormV2 cmdForm, ResponseBodyEmitter emitter) throws Exception { + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON);//向前端发送接收到指令 + + //1.xy轴停止移动 + CMDToDevice motorXStopCMDToDevice = DeviceCommandGenerator.motor_x_stop(); //x轴停止移动指令 + CommandFuture motorXStopCMDToDeviceFuture = new CommandFuture(); + motorXStopCMDToDeviceFuture.setCmdToDevice(motorXStopCMDToDevice); + Integer motorXStopCmdId = motorXStopCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorXStopCmdId, motorXStopCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorXStopCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了x轴停止移动指令", motorXStopCMDToDevice), MediaType.APPLICATION_JSON); + + motorXStopCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorXStopResult = motorXStopCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorXStopCmdId); // 从map中删除该指令 + + if (!motorXStopCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴停止移动指令响应超时", motorXStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorXStopResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴停止移动指令返回错误", motorXStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean motorXStopStatus = motorXStopResult.getBool("result"); + if (!motorXStopStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴停止移动指令执行失败", motorXStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴停止移动指令反馈", motorXStopResult), MediaType.APPLICATION_JSON); + + CMDToDevice motorYStopCMDToDevice = DeviceCommandGenerator.motor_y_stop(); //y轴停止移动 + CommandFuture motorYStopCMDToDeviceFuture = new CommandFuture(); + motorYStopCMDToDeviceFuture.setCmdToDevice(motorYStopCMDToDevice); + Integer motorYStopCmdId = motorYStopCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorYStopCmdId, motorYStopCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorYStopCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了y轴停止移动指令", motorYStopCMDToDevice), MediaType.APPLICATION_JSON); + + motorYStopCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorYStopResult = motorYStopCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorYStopCmdId); // 从map中删除该指令 + if (!motorYStopCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴停止移动指令响应超时", motorYStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorYStopResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴停止移动指令返回错误", motorYStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean motorYStopStatus = motorYStopResult.getBool("result"); + if (!motorYStopStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴停止移动指令执行失败", motorYStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "y轴停止移动指令反馈", motorYStopResult), MediaType.APPLICATION_JSON); + //获取xy位置 + 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; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "电机XYZ相对原点坐标指令反馈", motorXYZPositionGetCmdToDeviceResult), MediaType.APPLICATION_JSON); + Double xAxisPosition = motorXYZPositionGetCmdToDeviceResult.getJSONObject("result").getDouble("xAxisPosition");//当前x轴相对于原点的位置 + if (xAxisPosition == null || xAxisPosition < 0.0) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "获得电机XYZ相对原点坐标指令执行失败", motorXYZPositionGetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Double yAxisPosition = motorXYZPositionGetCmdToDeviceResult.getJSONObject("result").getDouble("yAxisPosition");//当前y轴相对于原点的位置 + if (yAxisPosition == null || yAxisPosition < 0.0) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "获得电机XYZ相对原点坐标指令执行失败", motorXYZPositionGetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + matrixSprayStart.setTargetX(xAxisPosition); + matrixSprayStart.setTargetY(yAxisPosition); + //2.关闭喷嘴阀 + CMDToDevice nozzleValveCloseCMDToDevice = DeviceCommandGenerator.nozzle_valve_close();//关闭喷嘴阀 + CommandFuture nozzleValveCloseCMDToDeviceFuture = new CommandFuture(); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "关闭喷嘴阀"), MediaType.APPLICATION_JSON); + nozzleValveCloseCMDToDeviceFuture.setCmdToDevice(nozzleValveCloseCMDToDevice); + Integer nozzleValveCloseCMDToDeviceCmdId = nozzleValveCloseCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(nozzleValveCloseCMDToDeviceCmdId, nozzleValveCloseCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(nozzleValveCloseCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了关闭喷嘴阀指令", nozzleValveCloseCMDToDevice), MediaType.APPLICATION_JSON); + nozzleValveCloseCMDToDeviceFuture.waitForContinue(); // 等待设备的反馈 + JSONObject nozzleValveCloseCMDToDeviceResult = nozzleValveCloseCMDToDeviceFuture.getCallbackResult(); // 拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(nozzleValveCloseCMDToDeviceCmdId); // 从map中删除该指令 + if (!nozzleValveCloseCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭喷嘴阀指令响应超时", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (nozzleValveCloseCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭喷嘴阀指令返回错误", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean nozzleValveCloseCMDToDeviceResultStatus = nozzleValveCloseCMDToDeviceResult.getBool("result"); + if (!nozzleValveCloseCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭喷嘴阀指令执行失败", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "关闭喷嘴阀指令反馈", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); } } diff --git a/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayStart.java b/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayStart.java index d9775c6..36c985d 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayStart.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayStart.java @@ -1,19 +1,34 @@ 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.core.registry.CommandHandlerRegistry; +import com.qyft.ms.app.common.generator.PathGenerator; 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.device.*; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import com.qyft.ms.device.client.TcpClient; +import com.qyft.ms.device.device.DeviceInstance; +import com.qyft.ms.device.device.MatrixSprayStatusEnum; +import com.qyft.ms.device.handler.DeviceMessageHandler; +import lombok.Getter; import lombok.RequiredArgsConstructor; +import lombok.Setter; 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.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; + /** * 喷涂_基质喷涂开始 */ @@ -24,82 +39,807 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter @CommandMapping("matrix_spray_start")//业务指令注解 public class MatrixSprayStart implements CommandHandler { - private final CommandHandlerRegistry registry; + private final TcpClient deviceClient; + +// private final CommandHandlerRegistry registry; + + private final DeviceMessageHandler deviceMessageHandler; + + /** + * 目标坐标 + */ + @Getter + @Setter + public Double targetX = null; + @Getter + @Setter + public Double targetY = null; + /** * { - * cmdName:'matrix_spray_start' - * cmdId:'', - * param:{ - * matrix_path_type:;//喷涂路径类型 - * motor_z_height:;//高度 - * gas_pressure://Mpa兆帕 - * volume:20;//单位uL微升 - * high_voltage:true/false;//是否打开高压 - * high_voltage_value:4000;//高压值 - * spacing:''//毫米 - * moving_speed:8mm/s;//移动速度 - * times:;//喷涂遍数 - * position:[{x1,y1,x2,y2,index}] - * } + * cmdName:'matrix_spray_start' + * cmdId:'', + * param:{ + * matrix_path_type:horizontal | vertical | grid;//喷涂路径类型 + * motor_z_height:;//高度 Z轴距离玻片的高度 + * gas_pressure://Mpa兆帕 不处理 + * volume:20;//单位uL微升 基质流速(控制注射泵速度) + * high_voltage:true/false;//是否打开高压 + * high_voltage_value:4000;//高压值 + * spacing:''//毫米 间距 + * moving_speed:8mm/s;//移动速度 轴速度 + * times:;//喷涂遍数 + * position:[{x1,y1,x2,y2,index}] + * } * } - * @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(); - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON);//向前端发送接收到指令 + // 1. 参数校验 + String matrixPathType = (String) param.get("matrix_path_type");//喷涂路径类型 horizontal | vertical | grid + Integer motorZHeight = (Integer) param.get("motor_z_height");//Z轴距离玻片的高度(距玻片的距离) + Integer gasPressure = (Integer) param.get("gas_pressure");//Mpa兆帕 不处理 + Integer volume = (Integer) param.get("volume");//单位uL微升 基质流速(控制注射泵速度) + Boolean highVoltage = (Boolean) param.get("high_voltage");//是否打开高压 + Integer highVoltageValue = (Integer) param.get("high_voltage_value");//高压值 + Integer spacing = (Integer) param.get("spacing");//毫米 间距 + Integer movingSpeed = (Integer) param.get("moving_speed");//轴移动速度 + Integer times = (Integer) param.get("times");//喷涂遍数 + //noinspection unchecked + List> positionList = (List>) param.get("position"); + + if (validate(emitter, matrixPathType, frontCmdId, frontCmdName, motorZHeight, gasPressure, volume, highVoltage, highVoltageValue, spacing, movingSpeed, times, positionList)) + return; DeviceInstance deviceInstance = DeviceInstance.getInstance(); MatrixSprayStatusEnum matrixSprayStatus = DeviceInstance.getInstance().getMatrixSprayStatusEnum(); - - /** - * 1.判断设备是否正在喷涂中 - */ - if("yes".equals(matrixSprayStatus.getCode())){ + //2.判断设备是否正在喷涂中 + if ("yes".equals(matrixSprayStatus.getCode())) { emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "设备正在喷涂中,请勿重复操作"), MediaType.APPLICATION_JSON); return; } - /** - * 2.判断喷涂状态 - */ - deviceInstance.setMatrixSprayStatusEnum(MatrixSprayStatusEnum.YES); + deviceInstance.setMatrixSprayStatusEnum(MatrixSprayStatusEnum.YES);//如果没有在喷涂,设定当前状态为正在喷涂 + //三轴回到原点 先 z + CountDownLatch latch = new CountDownLatch(1); + deviceMessageHandler.setLatch(latch); + // z轴回原点 + if (originZ(emitter, frontCmdId, frontCmdName)) return; + latch.await();//等待z轴回到原点 + latch = new CountDownLatch(2); + deviceMessageHandler.setLatch(latch); + // x轴回原点 + if (originX(emitter, frontCmdId, frontCmdName)) return; + + // y轴回原点 + if (originY(emitter, frontCmdId, frontCmdName)) return; + latch.await();//等待x与y轴回到原点 + + //设定轴移动速度 + if (motorSpeedSet(emitter, movingSpeed, frontCmdId, frontCmdName)) return; + + //2. 插入日志 + //TODO写日志 + //3.将三通阀转至喷涂管路 + //4.开启三通阀到喷嘴管路 + if (threeWayValveOpenSyringePipeLine(emitter, frontCmdId, frontCmdName)) return; + //4.设置指定轴的电机的运行速度 + //5.注射泵速度 + //6.判断高度安全值 + //7.移动到指定高度(位置) (z - height) + Double height = 101.2 - motorZHeight;//TODO 101.2是玻片高度,这个应该从数据库中获取 + if (moveZ(emitter, height, frontCmdId, frontCmdName)) return; + //8.是否加电 电压 + if (highVoltage) {//加电 + if (highVoltageOpen(emitter, highVoltageValue, frontCmdId, frontCmdName)) return; + } + //9 循环position进 position:[{x1,y1,x2,y2,index}] + Double[][] slideArr = { + {18.08, 101.2}, + {45.08, 101.2}, + {72.08, 101.2}, + {99.08, 101.2} + }; + for (Map postion : positionList) { + //范围左上角 x1,y1 + Double[] upperLeft = {(Double) postion.get("x1"), (Double) postion.get("y1")}; + //范围右下角 x2,y2 + Double[] lowerRight = {(Double) postion.get("x2"), (Double) postion.get("y2")}; + //index 第几个玻片 + int index = (int) postion.get("index"); + //1.获取玻片的位置 + Double[] slide = slideArr[index]; + //2.玻片范围的实际位置 + Double slideCompleteUCSX = slide[0] + upperLeft[0]; + Double slideCompleteUCSY = slide[1] + upperLeft[1]; + //3.算出路径坐标list + //定义边界 + // 托盘点位 x y z + double left = slideCompleteUCSX + upperLeft[0]; + double right = slideCompleteUCSX + lowerRight[0]; + double top = slideCompleteUCSY + upperLeft[1]; + double bottom = slideCompleteUCSY + lowerRight[1]; + //移动xy到slide(玻片)的位置 + if (moveX(emitter, slide[0], frontCmdId, frontCmdName)) return; + if (moveY(emitter, slide[1], frontCmdId, frontCmdName)) return; + + if ("horizontal".equals(matrixPathType)) { //生成路径(从“左上角”往下的水平之字形) String matrixPathType = (String) param.get("matrix_path_type");//喷涂路径类型 horizontal | vertical | grid + for (int i = 0; i < times; i++) { + double topReal = top; + double bottomReal = bottom; + if (i % 2 == 0) {//双数喷涂 + double halfSpacing = (double) spacing / 2; + topReal = top - halfSpacing; + bottomReal = bottom + halfSpacing; + } + List pathList = PathGenerator.generatePathPoints( + left, right, topReal, bottomReal, + spacing, + PathGenerator.MoveMode.HORIZONTAL_ZIGZAG_TOP_DOWN + ); + if (pathSpray(emitter, frontCmdId, frontCmdName, volume, pathList, movingSpeed)) return;//路径喷涂 + } + } else if ("vertical".equals(matrixPathType)) { + for (int i = 0; i < times; i++) { + double leftReal = left; + double rightReal = right; + if (i % 2 == 0) {//双数喷涂 + double halfSpacing = (double) spacing / 2; + leftReal = top - halfSpacing; + rightReal = bottom + halfSpacing; + } + List pathList = PathGenerator.generatePathPoints( + leftReal, rightReal, top, bottom, + spacing, + PathGenerator.MoveMode.VERTICAL_ZIGZAG_LEFT_RIGHT + ); + if (pathSpray(emitter, frontCmdId, frontCmdName, volume, pathList, movingSpeed)) return;//路径喷涂 + } + } else if ("grid".equals(matrixPathType)) {//横竖喷2次 + for (int i = 0; i < times; i++) { + double leftReal = left; + double rightReal = right; + double topReal = top; + double bottomReal = bottom; + if (i % 2 == 0) {//双数喷涂 + double halfSpacing = (double) spacing / 2; + leftReal = top - halfSpacing; + rightReal = bottom + halfSpacing; + topReal = top - halfSpacing; + bottomReal = bottom + halfSpacing; + } + List pathList = PathGenerator.generatePathPoints( + leftReal, rightReal, topReal, bottomReal, + spacing, + PathGenerator.MoveMode.VERTICAL_ZIGZAG_LEFT_RIGHT + ); + if (pathSpray(emitter, frontCmdId, frontCmdName, volume, pathList, movingSpeed)) return;//路径喷涂 + pathList = PathGenerator.generatePathPoints( + leftReal, rightReal, topReal, bottomReal, + spacing, + PathGenerator.MoveMode.HORIZONTAL_ZIGZAG_TOP_DOWN + ); + if (pathSpray(emitter, frontCmdId, frontCmdName, volume, pathList, movingSpeed)) return;//路径喷涂 + } + } + } + //10.关高压电 + if (highVoltageClose(emitter, frontCmdId, frontCmdName)) return; + //11.回到原点 motorMoveToHome 先 z + latch = new CountDownLatch(1); + deviceMessageHandler.setLatch(latch); + // z轴回原点 + if (originZ(emitter, frontCmdId, frontCmdName)) return; + latch.await();//等待z轴回到原点 + latch = new CountDownLatch(2); + deviceMessageHandler.setLatch(latch); + // x轴回原点 + if (originX(emitter, frontCmdId, frontCmdName)) return; + + // y轴回原点 + if (originY(emitter, frontCmdId, frontCmdName)) return; + latch.await();//等待x与y轴回到原点 + //12.结束日志 + //TODO写日志 + emitter.complete(); + } + + + private boolean motorSpeedSet(ResponseBodyEmitter emitter, Integer movingSpeed, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorXSpeedSetCmdToDevice = DeviceCommandGenerator.motor_x_speed_set(movingSpeed); // x轴电机速度设置 + CommandFuture motorXSpeedSetCmdToDeviceFuture = new CommandFuture(); + motorXSpeedSetCmdToDeviceFuture.setCmdToDevice(motorXSpeedSetCmdToDevice); + Integer motorXSpeedSetCmdId = motorXSpeedSetCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorXSpeedSetCmdId, motorXSpeedSetCmdToDeviceFuture); + deviceClient.sendToJSON(motorXSpeedSetCmdToDevice); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了x轴电机速度设置指令", motorXSpeedSetCmdToDevice), MediaType.APPLICATION_JSON); + + motorXSpeedSetCmdToDeviceFuture.waitForContinue(); + JSONObject motorXSpeedSetResult = motorXSpeedSetCmdToDeviceFuture.getCallbackResult(); + CurrentSendCmdMapInstance.getInstance().removeCommand(motorXSpeedSetCmdId); + + if (!motorXSpeedSetCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴电机速度设置指令响应超时", motorXSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorXSpeedSetResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴电机速度设置指令返回错误", motorXSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorXSpeedSetStatus = motorXSpeedSetResult.getBool("result"); + if (!motorXSpeedSetStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴电机速度设置指令执行失败", motorXSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴电机速度设置指令反馈", motorXSpeedSetResult), MediaType.APPLICATION_JSON); + + + CMDToDevice motorYSpeedSetCmdToDevice = DeviceCommandGenerator.motor_x_speed_set(movingSpeed); // y轴电机速度设置 + CommandFuture motorYSpeedSetCmdToDeviceFuture = new CommandFuture(); + motorYSpeedSetCmdToDeviceFuture.setCmdToDevice(motorYSpeedSetCmdToDevice); + Integer motorYSpeedSetCmdId = motorYSpeedSetCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorYSpeedSetCmdId, motorYSpeedSetCmdToDeviceFuture); + deviceClient.sendToJSON(motorYSpeedSetCmdToDevice); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了y轴电机速度设置指令", motorYSpeedSetCmdToDevice), MediaType.APPLICATION_JSON); + + motorYSpeedSetCmdToDeviceFuture.waitForContinue(); + JSONObject motorYSpeedSetResult = motorYSpeedSetCmdToDeviceFuture.getCallbackResult(); + CurrentSendCmdMapInstance.getInstance().removeCommand(motorYSpeedSetCmdId); + + if (!motorYSpeedSetCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴电机速度设置指令响应超时", motorYSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorYSpeedSetResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴电机速度设置指令返回错误", motorYSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorYSpeedSetStatus = motorYSpeedSetResult.getBool("result"); + if (!motorYSpeedSetStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴电机速度设置指令执行失败", motorYSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "y轴电机速度设置指令反馈", motorYSpeedSetResult), MediaType.APPLICATION_JSON); + + + CMDToDevice motorZSpeedSetCmdToDevice = DeviceCommandGenerator.motor_x_speed_set(movingSpeed);// z轴电机速度设置 + CommandFuture motorZSpeedSetCmdToDeviceFuture = new CommandFuture(); + motorZSpeedSetCmdToDeviceFuture.setCmdToDevice(motorZSpeedSetCmdToDevice); + Integer motorZSpeedSetCmdId = motorZSpeedSetCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorZSpeedSetCmdId, motorZSpeedSetCmdToDeviceFuture); + deviceClient.sendToJSON(motorZSpeedSetCmdToDevice); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了z轴电机速度设置指令", motorZSpeedSetCmdToDevice), MediaType.APPLICATION_JSON); + + motorZSpeedSetCmdToDeviceFuture.waitForContinue(); + JSONObject motorZSpeedSetResult = motorZSpeedSetCmdToDeviceFuture.getCallbackResult(); + CurrentSendCmdMapInstance.getInstance().removeCommand(motorZSpeedSetCmdId); + + if (!motorZSpeedSetCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴电机速度设置指令响应超时", motorZSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorZSpeedSetResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴电机速度设置指令返回错误", motorZSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorZSpeedSetStatus = motorZSpeedSetResult.getBool("result"); + if (!motorZSpeedSetStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴电机速度设置指令执行失败", motorZSpeedSetResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "z轴电机速度设置指令反馈", motorZSpeedSetResult), MediaType.APPLICATION_JSON); + return false; + } + + private boolean highVoltageClose(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice highVoltageCloseCmdToDevice = DeviceCommandGenerator.high_voltage_close();//关闭高压电 + CommandFuture highVoltageCloseCmdToDeviceFuture = new CommandFuture(); + highVoltageCloseCmdToDeviceFuture.setCmdToDevice(highVoltageCloseCmdToDevice); + Integer highVoltageCloseCmdId = highVoltageCloseCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(highVoltageCloseCmdId, highVoltageCloseCmdToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(highVoltageCloseCmdToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了关闭高压电指令", highVoltageCloseCmdToDevice), MediaType.APPLICATION_JSON); + + highVoltageCloseCmdToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject highVoltageCloseResult = highVoltageCloseCmdToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(highVoltageCloseCmdId); // 从map中删除该指令 + + if (!highVoltageCloseCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭高压电指令响应超时", highVoltageCloseResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + if (highVoltageCloseResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭高压电指令返回错误", highVoltageCloseResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + Boolean highVoltageCloseStatus = highVoltageCloseResult.getBool("result"); + if (!highVoltageCloseStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭高压电指令执行失败", highVoltageCloseResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "关闭高压电指令反馈", highVoltageCloseResult), MediaType.APPLICATION_JSON); + return false; + } + + private boolean pathSpray(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName, Integer volume, List pathList, Integer movingSpeed) throws IOException, InterruptedException { + CountDownLatch latch; + //1.开启喷嘴阀 + if (nozzleValveOpen(emitter, frontCmdId, frontCmdName)) return true; + //2.推注射泵 + if (syringePumpInjectionVolumeSet(emitter, frontCmdId, frontCmdName, volume)) return true; + //3 喷针移动目标位置 + for (PathGenerator.Points p : pathList) { + latch = new CountDownLatch(2); + deviceMessageHandler.setLatch(latch); + //移动x与y到点位 + if (moveX(emitter, p.getX(), frontCmdId, frontCmdName)) return true; + if (moveY(emitter, p.getY(), frontCmdId, frontCmdName)) return true; + latch.await();//等待移动完毕 + } + //5. 停止注射泵 + if (syringePumpStop(emitter, frontCmdId, frontCmdName)) return true; + //6.关闭喷嘴阀 + if (nozzleValveClose(emitter, frontCmdId, frontCmdName)) return true; + return false; + } + + private boolean nozzleValveClose(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice nozzleValveCloseCMDToDevice = DeviceCommandGenerator.nozzle_valve_close();//关闭喷嘴阀 + CommandFuture nozzleValveCloseCMDToDeviceFuture = new CommandFuture(); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "关闭喷嘴阀"), MediaType.APPLICATION_JSON); + nozzleValveCloseCMDToDeviceFuture.setCmdToDevice(nozzleValveCloseCMDToDevice); + Integer nozzleValveCloseCMDToDeviceCmdId = nozzleValveCloseCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(nozzleValveCloseCMDToDeviceCmdId, nozzleValveCloseCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(nozzleValveCloseCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了关闭喷嘴阀指令", nozzleValveCloseCMDToDevice), MediaType.APPLICATION_JSON); + nozzleValveCloseCMDToDeviceFuture.waitForContinue(); // 等待设备的反馈 + JSONObject nozzleValveCloseCMDToDeviceResult = nozzleValveCloseCMDToDeviceFuture.getCallbackResult(); // 拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(nozzleValveCloseCMDToDeviceCmdId); // 从map中删除该指令 + if (!nozzleValveCloseCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭喷嘴阀指令响应超时", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (nozzleValveCloseCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭喷嘴阀指令返回错误", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean nozzleValveCloseCMDToDeviceResultStatus = nozzleValveCloseCMDToDeviceResult.getBool("result"); + if (!nozzleValveCloseCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭喷嘴阀指令执行失败", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "关闭喷嘴阀指令反馈", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + return false; + } + + private boolean syringePumpStop(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice syringePumpStopCMDToDevice = DeviceCommandGenerator.syringe_pump_stop();//停止推动注射泵 + CommandFuture syringePumpStopCMDToDeviceCMDToDeviceFuture = new CommandFuture(); + syringePumpStopCMDToDeviceCMDToDeviceFuture.setCmdToDevice(syringePumpStopCMDToDevice); + Integer syringePumpStopCMDToDeviceCmdId = syringePumpStopCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(syringePumpStopCMDToDeviceCmdId, syringePumpStopCMDToDeviceCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(syringePumpStopCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了停止推动注射泵指令", syringePumpStopCMDToDevice), MediaType.APPLICATION_JSON); + syringePumpStopCMDToDeviceCMDToDeviceFuture.waitForContinue(); // 等待设备的反馈 + JSONObject syringePumpStopCMDToDeviceResult = syringePumpStopCMDToDeviceCMDToDeviceFuture.getCallbackResult(); // 拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(syringePumpStopCMDToDeviceCmdId); // 将指令从map中删除 + if (!syringePumpStopCMDToDeviceCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "停止推动注射泵指令响应超时", syringePumpStopCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (syringePumpStopCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "停止推动注射泵指令返回错误", syringePumpStopCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean syringePumpStopCMDToDeviceResultStatus = syringePumpStopCMDToDeviceResult.getBool("result"); + if (!syringePumpStopCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "停止推动注射泵指令执行失败", syringePumpStopCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "停止推动注射泵指令反馈", syringePumpStopCMDToDeviceResult), MediaType.APPLICATION_JSON); + return false; + } + + private static boolean validate(ResponseBodyEmitter emitter, String matrixPathType, String frontCmdId, String frontCmdName, Integer motorZHeight, Integer gasPressure, Integer volume, Boolean highVoltage, Integer highVoltageValue, Integer spacing, Integer movingSpeed, Integer times, List> positionList) throws IOException { + if (matrixPathType == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 matrix_path_type 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorZHeight == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 motor_z_height 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (gasPressure == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 gas_pressure 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (volume == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 volume 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (highVoltage == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 high_voltage 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (highVoltageValue == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 high_voltage_value 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (spacing == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 spacing 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (movingSpeed == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 moving_speed 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (times == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 times 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (positionList == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 position 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + return false; + } + + private boolean syringePumpInjectionVolumeSet(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName, Integer volume) throws IOException { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "设置注射泵速度,推注射泵"), MediaType.APPLICATION_JSON); + CMDToDevice syringePumpInjectionVolumeSetCMDToDevice = DeviceCommandGenerator.syringe_pump_injection_volume_set(volume);//推动移动注射泵 + CommandFuture syringePumpInjectionVolumeSetCMDToDeviceFuture = new CommandFuture(); + syringePumpInjectionVolumeSetCMDToDeviceFuture.setCmdToDevice(syringePumpInjectionVolumeSetCMDToDevice); + Integer syringePumpInjectionVolumeSetCMDToDeviceCmdId = syringePumpInjectionVolumeSetCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(syringePumpInjectionVolumeSetCMDToDeviceCmdId, syringePumpInjectionVolumeSetCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(syringePumpInjectionVolumeSetCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了推动移动注射泵指令", syringePumpInjectionVolumeSetCMDToDevice), MediaType.APPLICATION_JSON); + syringePumpInjectionVolumeSetCMDToDeviceFuture.waitForContinue(); // 等待设备的反馈 + JSONObject syringePumpInjectionVolumeSetCMDToDeviceResult = syringePumpInjectionVolumeSetCMDToDeviceFuture.getCallbackResult(); // 拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(syringePumpInjectionVolumeSetCMDToDeviceCmdId); // 将指令从map中删除 + if (!syringePumpInjectionVolumeSetCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "推动移动注射泵指令响应超时", syringePumpInjectionVolumeSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (syringePumpInjectionVolumeSetCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "推动移动注射泵指令返回错误", syringePumpInjectionVolumeSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean syringePumpInjectionVolumeSetCMDToDeviceResultStatus = syringePumpInjectionVolumeSetCMDToDeviceResult.getBool("result"); + if (!syringePumpInjectionVolumeSetCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "推动移动注射泵指令执行失败", syringePumpInjectionVolumeSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "推动移动注射泵指令反馈", syringePumpInjectionVolumeSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + return false; + } + + private boolean nozzleValveOpen(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice nozzleValveOpenCMDToDevice = DeviceCommandGenerator.nozzle_valve_open();//生成指令 开启喷嘴阀 + CommandFuture nozzleValveOpenCMDToDeviceFuture = new CommandFuture(); + nozzleValveOpenCMDToDeviceFuture.setCmdToDevice(nozzleValveOpenCMDToDevice); + Integer nozzleValveOpenCMDToDeviceCmdId = nozzleValveOpenCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(nozzleValveOpenCMDToDeviceCmdId, nozzleValveOpenCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(nozzleValveOpenCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了开启喷嘴阀指令", nozzleValveOpenCMDToDevice), MediaType.APPLICATION_JSON); + nozzleValveOpenCMDToDeviceFuture.waitForContinue(); // 等待设备的反馈 + JSONObject nozzleValveOpenCMDToDeviceResult = nozzleValveOpenCMDToDeviceFuture.getCallbackResult(); // 拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(nozzleValveOpenCMDToDeviceCmdId); // 将指令从map中删除 + if (!nozzleValveOpenCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启喷嘴阀指令响应超时", nozzleValveOpenCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (nozzleValveOpenCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启喷嘴阀指令返回错误", nozzleValveOpenCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean nozzleValveOpenCMDToDeviceResultStatus = nozzleValveOpenCMDToDeviceResult.getBool("result"); + if (!nozzleValveOpenCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启喷嘴阀指令执行失败", nozzleValveOpenCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "开启喷嘴阀指令反馈", nozzleValveOpenCMDToDeviceResult), MediaType.APPLICATION_JSON); + return false; + } + + private boolean originZ(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorZOriginCMDToDevice = DeviceCommandGenerator.motor_z_origin(); // 生成 z轴回原点指令 + CommandFuture motorZOriginCMDToDeviceFuture = new CommandFuture(); + motorZOriginCMDToDeviceFuture.setCmdToDevice(motorZOriginCMDToDevice); + Integer motorZOriginCmdId = motorZOriginCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorZOriginCmdId, motorZOriginCMDToDeviceFuture); // 将指令放入map + + deviceClient.sendToJSON(motorZOriginCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了z轴回原点指令", motorZOriginCMDToDevice), MediaType.APPLICATION_JSON); + + motorZOriginCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorZOriginResult = motorZOriginCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorZOriginCmdId); // 从map中删除该指令 + + if (!motorZOriginCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令响应超时", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + if (motorZOriginResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令返回错误", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } - /** - * 3.获取XYZ轴的当前位置 - */ - MotorX motorX = deviceInstance.getMotorX(); - MotorY motorY = deviceInstance.getMotorY(); - MotorZ motorZ = deviceInstance.getMotorZ(); + Boolean motorZOriginStatus = motorZOriginResult.getBool("result"); + if (!motorZOriginStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令执行失败", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "z轴回原点指令反馈", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "z轴已回到原点", motorZOriginCMDToDevice), MediaType.APPLICATION_JSON); + return false; + } + + private boolean originX(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorXOriginCMDToDevice = DeviceCommandGenerator.motor_x_origin(); //x轴回原点指令 + CommandFuture motorXOriginCMDToDeviceFuture = new CommandFuture(); + motorXOriginCMDToDeviceFuture.setCmdToDevice(motorXOriginCMDToDevice); + Integer motorXOriginCmdId = motorXOriginCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorXOriginCmdId, motorXOriginCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorXOriginCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了x轴回原点指令", motorXOriginCMDToDevice), MediaType.APPLICATION_JSON); + motorXOriginCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorXOriginResult = motorXOriginCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorXOriginCmdId); // 从map中删除该指令 + + if (!motorXOriginCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令响应超时", motorXOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorXOriginResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令返回错误", motorXOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorXOriginResultStatus = motorXOriginResult.getBool("result"); + if (!motorXOriginResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令执行失败", motorXOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴回原点指令反馈", motorXOriginResult), MediaType.APPLICATION_JSON); + return false; + } - param.get("position"); + private boolean originY(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorYOriginCMDToDevice = DeviceCommandGenerator.motor_y_origin(); //y轴回原点指令 + CommandFuture motorYOriginCMDToDeviceFuture = new CommandFuture(); + motorYOriginCMDToDeviceFuture.setCmdToDevice(motorYOriginCMDToDevice); + Integer motorYOriginCmdId = motorYOriginCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorYOriginCmdId, motorYOriginCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorYOriginCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了y轴回原点指令", motorYOriginCMDToDevice), MediaType.APPLICATION_JSON); - /** - * 4.XYZ回原点 - */ + motorYOriginCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorYOriginResult = motorYOriginCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorYOriginCmdId); // 从map中删除该指令 + if (!motorYOriginCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令响应超时", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorYOriginResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令返回错误", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorYOriginResultStatus = motorYOriginResult.getBool("result"); + if (!motorYOriginResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令执行失败", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "y轴回原点指令反馈", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴与y轴已回到原点"), MediaType.APPLICATION_JSON); + return false; + } - /** - * 5. - */ + private boolean threeWayValveOpenSyringePipeLine(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice threeWayValveOpenSyringePipelineCMDToDevice = DeviceCommandGenerator.three_way_valve_open_syringe_pipeline();//打开三通阀喷嘴管路 + CommandFuture threeWayValveOpenSyringePipelineCMDToDeviceFuture = new CommandFuture(); + threeWayValveOpenSyringePipelineCMDToDeviceFuture.setCmdToDevice(threeWayValveOpenSyringePipelineCMDToDevice); + Integer threeWayValveOpenSyringePipelineCMDToDeviceCmdId = threeWayValveOpenSyringePipelineCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(threeWayValveOpenSyringePipelineCMDToDeviceCmdId, threeWayValveOpenSyringePipelineCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(threeWayValveOpenSyringePipelineCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了打开三通阀喷嘴管路指令", threeWayValveOpenSyringePipelineCMDToDevice), MediaType.APPLICATION_JSON); + threeWayValveOpenSyringePipelineCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject threeWayValveOpenSyringePipelineCMDToDeviceResult = threeWayValveOpenSyringePipelineCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(threeWayValveOpenSyringePipelineCMDToDeviceCmdId); // 从map中删除该指令 + if (!threeWayValveOpenSyringePipelineCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "打开三通阀喷嘴管路指令响应超时", threeWayValveOpenSyringePipelineCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (threeWayValveOpenSyringePipelineCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "打开三通阀喷嘴管路指令返回错误", threeWayValveOpenSyringePipelineCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean threeWayValveOpenSyringePipelineCMDToDeviceResultStatus = threeWayValveOpenSyringePipelineCMDToDeviceResult.getBool("result"); + if (!threeWayValveOpenSyringePipelineCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "打开三通阀喷嘴管路指令执行失败", threeWayValveOpenSyringePipelineCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "打开三通阀喷嘴管路指令反馈", threeWayValveOpenSyringePipelineCMDToDeviceResult), MediaType.APPLICATION_JSON); + return false; + } - for(int i=0;i<5;i++){ + private boolean highVoltageOpen(ResponseBodyEmitter emitter, Integer highVoltageValue, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice highVoltageOpenCmdToDevice = DeviceCommandGenerator.high_voltage_open(highVoltageValue);//开启高压 + CommandFuture highVoltageOpenCmdToDeviceFuture = new CommandFuture(); + highVoltageOpenCmdToDeviceFuture.setCmdToDevice(highVoltageOpenCmdToDevice); + Integer highVoltageOpenCmdId = highVoltageOpenCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(highVoltageOpenCmdId, highVoltageOpenCmdToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(highVoltageOpenCmdToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了开启高压指令", highVoltageOpenCmdToDevice), MediaType.APPLICATION_JSON); + highVoltageOpenCmdToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject highVoltageOpenResult = highVoltageOpenCmdToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(highVoltageOpenCmdId); // 从map中删除该指令 + if (!highVoltageOpenCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启高压指令响应超时", highVoltageOpenResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (highVoltageOpenResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启高压指令返回错误", highVoltageOpenResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean highVoltageOpenStatus = highVoltageOpenResult.getBool("result"); + if (!highVoltageOpenStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启高压指令执行失败", highVoltageOpenResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "开启高压指令反馈", highVoltageOpenResult), MediaType.APPLICATION_JSON); + return false; + } + private boolean moveZ(ResponseBodyEmitter emitter, Double height, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorZPositionSetDownCmdToDevice = DeviceCommandGenerator.motor_z_position_set(height);//移动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 true; + } + if (motorZPositionSetDownCmdToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动z轴到指定位指令返回错误", motorZPositionSetDownCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; } - //TODO 业务逻辑 - //1.生成指令 - //2.发送指令 - //3.等待设备反馈 - //4.返回结果 + Boolean motorZPositionSetDownCmdToDeviceResultStatus = motorZPositionSetDownCmdToDeviceResult.getBool("result"); + if (!motorZPositionSetDownCmdToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动z轴到指定位指令执行失败", motorZPositionSetDownCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动z轴到指定位指令反馈", motorZPositionSetDownCmdToDeviceResult), MediaType.APPLICATION_JSON); + return false; + } + private boolean moveY(ResponseBodyEmitter emitter, Double position, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorYPositionSetCMDToDevice = DeviceCommandGenerator.motor_y_position_set(position);//移动y轴到指定位置 + 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, "移动y轴到指定位置指令响应超时", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorYPositionSetCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动y轴到指定位置指令返回错误", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorYPositionSetCMDToDeviceResultStatus = motorYPositionSetCMDToDeviceResult.getBool("result"); + if (!motorYPositionSetCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动y轴到指定位置指令执行失败", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动y轴到指定位置指令反馈", motorYPositionSetCMDToDeviceResult), MediaType.APPLICATION_JSON); + return false; + } + private boolean moveX(ResponseBodyEmitter emitter, Double position, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorXPositionSetCmdToDevice = DeviceCommandGenerator.motor_x_position_set(position);//移动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 true; + } + if (motorXPositionSetCmdToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动x轴到指定位置指令返回错误", motorXPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorXYZPositionSetCmdToDeviceResultStatus = motorXPositionSetCmdToDeviceResult.getBool("result"); + if (!motorXYZPositionSetCmdToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "移动x轴到指定位置指令执行失败", motorXPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动x轴到指定位置指令反馈", motorXPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); + return false; } + } diff --git a/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayStop.java b/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayStop.java index 60aa127..7ec4989 100644 --- a/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayStop.java +++ b/src/main/java/com/qyft/ms/app/handler/impl/MatrixSprayStop.java @@ -1,14 +1,27 @@ 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 com.qyft.ms.device.handler.DeviceMessageHandler; 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.io.IOException; +import java.util.concurrent.CountDownLatch; + /** * 喷涂_基质喷涂结束 * (还有自动结束喷涂) @@ -19,9 +32,266 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter @Async("asyncExecutor") @CommandMapping("matrix_spray_stop")//业务指令注解 public class MatrixSprayStop implements CommandHandler { + /** + * 设备通信client + */ + private final TcpClient deviceClient; + + private final MatrixSprayStart matrixSprayStart; + + private final DeviceMessageHandler deviceMessageHandler; @Override public void handle(CMDFormV2 cmdForm, ResponseBodyEmitter emitter) throws Exception { + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON);//向前端发送接收到指令 + //1.轴停止移动 + CMDToDevice motorXStopCMDToDevice = DeviceCommandGenerator.motor_x_stop(); //x轴停止移动指令 + CommandFuture motorXStopCMDToDeviceFuture = new CommandFuture(); + motorXStopCMDToDeviceFuture.setCmdToDevice(motorXStopCMDToDevice); + Integer motorXStopCmdId = motorXStopCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorXStopCmdId, motorXStopCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorXStopCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了x轴停止移动指令", motorXStopCMDToDevice), MediaType.APPLICATION_JSON); + + motorXStopCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorXStopResult = motorXStopCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorXStopCmdId); // 从map中删除该指令 + + if (!motorXStopCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴停止移动指令响应超时", motorXStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorXStopResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴停止移动指令返回错误", motorXStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean motorXStopStatus = motorXStopResult.getBool("result"); + if (!motorXStopStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴停止移动指令执行失败", motorXStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴停止移动指令反馈", motorXStopResult), MediaType.APPLICATION_JSON); + + CMDToDevice motorYStopCMDToDevice = DeviceCommandGenerator.motor_y_stop(); //y轴停止移动 + CommandFuture motorYStopCMDToDeviceFuture = new CommandFuture(); + motorYStopCMDToDeviceFuture.setCmdToDevice(motorYStopCMDToDevice); + Integer motorYStopCmdId = motorYStopCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorYStopCmdId, motorYStopCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorYStopCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了y轴停止移动指令", motorYStopCMDToDevice), MediaType.APPLICATION_JSON); + + motorYStopCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorYStopResult = motorYStopCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorYStopCmdId); // 从map中删除该指令 + + if (!motorYStopCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴停止移动指令响应超时", motorYStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorYStopResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴停止移动指令返回错误", motorYStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean motorYStopStatus = motorYStopResult.getBool("result"); + if (!motorYStopStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴停止移动指令执行失败", motorYStopResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "y轴停止移动指令反馈", motorYStopResult), MediaType.APPLICATION_JSON); + //2.关闭喷嘴阀 + CMDToDevice nozzleValveCloseCMDToDevice = DeviceCommandGenerator.nozzle_valve_close();//关闭喷嘴阀 + CommandFuture nozzleValveCloseCMDToDeviceFuture = new CommandFuture(); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "关闭喷嘴阀"), MediaType.APPLICATION_JSON); + nozzleValveCloseCMDToDeviceFuture.setCmdToDevice(nozzleValveCloseCMDToDevice); + Integer nozzleValveCloseCMDToDeviceCmdId = nozzleValveCloseCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(nozzleValveCloseCMDToDeviceCmdId, nozzleValveCloseCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(nozzleValveCloseCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了关闭喷嘴阀指令", nozzleValveCloseCMDToDevice), MediaType.APPLICATION_JSON); + nozzleValveCloseCMDToDeviceFuture.waitForContinue(); // 等待设备的反馈 + JSONObject nozzleValveCloseCMDToDeviceResult = nozzleValveCloseCMDToDeviceFuture.getCallbackResult(); // 拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(nozzleValveCloseCMDToDeviceCmdId); // 从map中删除该指令 + if (!nozzleValveCloseCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭喷嘴阀指令响应超时", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (nozzleValveCloseCMDToDeviceResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭喷嘴阀指令返回错误", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean nozzleValveCloseCMDToDeviceResultStatus = nozzleValveCloseCMDToDeviceResult.getBool("result"); + if (!nozzleValveCloseCMDToDeviceResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭喷嘴阀指令执行失败", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "关闭喷嘴阀指令反馈", nozzleValveCloseCMDToDeviceResult), MediaType.APPLICATION_JSON); + //3.关闭高压电 + CMDToDevice highVoltageCloseCmdToDevice = DeviceCommandGenerator.high_voltage_close();//关闭高压电 + CommandFuture highVoltageCloseCmdToDeviceFuture = new CommandFuture(); + highVoltageCloseCmdToDeviceFuture.setCmdToDevice(highVoltageCloseCmdToDevice); + Integer highVoltageCloseCmdId = highVoltageCloseCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(highVoltageCloseCmdId, highVoltageCloseCmdToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(highVoltageCloseCmdToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了关闭高压电指令", highVoltageCloseCmdToDevice), MediaType.APPLICATION_JSON); + + highVoltageCloseCmdToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject highVoltageCloseResult = highVoltageCloseCmdToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(highVoltageCloseCmdId); // 从map中删除该指令 + if (!highVoltageCloseCmdToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭高压电指令响应超时", highVoltageCloseResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (highVoltageCloseResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭高压电指令返回错误", highVoltageCloseResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean highVoltageCloseStatus = highVoltageCloseResult.getBool("result"); + if (!highVoltageCloseStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "关闭高压电指令执行失败", highVoltageCloseResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "关闭高压电指令反馈", highVoltageCloseResult), MediaType.APPLICATION_JSON); + //4.回到原点 + CountDownLatch latch = new CountDownLatch(1); + deviceMessageHandler.setLatch(latch); + // z轴回原点 + if (originZ(emitter, frontCmdId, frontCmdName)) return; + latch.await();//等待z轴回到原点 + latch = new CountDownLatch(2); + deviceMessageHandler.setLatch(latch); + // x轴回原点 + if (originX(emitter, frontCmdId, frontCmdName)) return; + + // y轴回原点 + if (originY(emitter, frontCmdId, frontCmdName)) return; + latch.await();//等待x与y轴回到原点 + + //5.结束日志 + //TODO记录日志 + + matrixSprayStart.setTargetX(null); + matrixSprayStart.setTargetY(null); + + emitter.complete(); + } + + private boolean originZ(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorZOriginCMDToDevice = DeviceCommandGenerator.motor_z_origin(); // 生成 z轴回原点指令 + CommandFuture motorZOriginCMDToDeviceFuture = new CommandFuture(); + motorZOriginCMDToDeviceFuture.setCmdToDevice(motorZOriginCMDToDevice); + Integer motorZOriginCmdId = motorZOriginCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorZOriginCmdId, motorZOriginCMDToDeviceFuture); // 将指令放入map + + deviceClient.sendToJSON(motorZOriginCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了z轴回原点指令", motorZOriginCMDToDevice), MediaType.APPLICATION_JSON); + + motorZOriginCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorZOriginResult = motorZOriginCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorZOriginCmdId); // 从map中删除该指令 + + if (!motorZOriginCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令响应超时", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + if (motorZOriginResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令返回错误", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + Boolean motorZOriginStatus = motorZOriginResult.getBool("result"); + if (!motorZOriginStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令执行失败", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "z轴回原点指令反馈", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "z轴已回到原点", motorZOriginCMDToDevice), MediaType.APPLICATION_JSON); + return false; } + + private boolean originX(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorXOriginCMDToDevice = DeviceCommandGenerator.motor_x_origin(); //x轴回原点指令 + CommandFuture motorXOriginCMDToDeviceFuture = new CommandFuture(); + motorXOriginCMDToDeviceFuture.setCmdToDevice(motorXOriginCMDToDevice); + Integer motorXOriginCmdId = motorXOriginCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorXOriginCmdId, motorXOriginCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorXOriginCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了x轴回原点指令", motorXOriginCMDToDevice), MediaType.APPLICATION_JSON); + + motorXOriginCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorXOriginResult = motorXOriginCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorXOriginCmdId); // 从map中删除该指令 + + if (!motorXOriginCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令响应超时", motorXOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorXOriginResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令返回错误", motorXOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorXOriginResultStatus = motorXOriginResult.getBool("result"); + if (!motorXOriginResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令执行失败", motorXOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴回原点指令反馈", motorXOriginResult), MediaType.APPLICATION_JSON); + return false; + } + + private boolean originY(ResponseBodyEmitter emitter, String frontCmdId, String frontCmdName) throws IOException { + CMDToDevice motorYOriginCMDToDevice = DeviceCommandGenerator.motor_y_origin(); //y轴回原点指令 + CommandFuture motorYOriginCMDToDeviceFuture = new CommandFuture(); + motorYOriginCMDToDeviceFuture.setCmdToDevice(motorYOriginCMDToDevice); + Integer motorYOriginCmdId = motorYOriginCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorYOriginCmdId, motorYOriginCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorYOriginCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了y轴回原点指令", motorYOriginCMDToDevice), MediaType.APPLICATION_JSON); + + motorYOriginCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorYOriginResult = motorYOriginCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorYOriginCmdId); // 从map中删除该指令 + + if (!motorYOriginCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令响应超时", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + if (motorYOriginResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令返回错误", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + Boolean motorYOriginResultStatus = motorYOriginResult.getBool("result"); + if (!motorYOriginResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令执行失败", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return true; + } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "y轴回原点指令反馈", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴与y轴已回到原点"), MediaType.APPLICATION_JSON); + return false; + } + } \ No newline at end of file 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 57c350b..a18417e 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 @@ -35,24 +35,35 @@ public class MotorXToHome implements CommandHandler { String frontCmdName = cmdForm.getCmdName(); //向前端发送接收到指令 emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - CMDToDevice motorXToHomeCmdToDevice = DeviceCommandGenerator.motor_x_origin();//生成指令 回原点 + CMDToDevice motorXOriginCMDToDevice = DeviceCommandGenerator.motor_x_origin(); //x轴回原点指令 + CommandFuture motorXOriginCMDToDeviceFuture = new CommandFuture(); + motorXOriginCMDToDeviceFuture.setCmdToDevice(motorXOriginCMDToDevice); + Integer motorXOriginCmdId = motorXOriginCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorXOriginCmdId, motorXOriginCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorXOriginCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了x轴回原点指令", motorXOriginCMDToDevice), MediaType.APPLICATION_JSON); - CommandFuture motorXToHomeCmdToDeviceFuture = new CommandFuture(); - motorXToHomeCmdToDeviceFuture.setCmdToDevice(motorXToHomeCmdToDevice); - Integer toDeviceCmdId = motorXToHomeCmdToDevice.getCmdId(); - CurrentSendCmdMapInstance.getInstance().putCommand(toDeviceCmdId, motorXToHomeCmdToDeviceFuture);//将指令放入map - deviceClient.sendToJSON(motorXToHomeCmdToDevice); //发送指令给设备 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了x轴回原点指令", motorXToHomeCmdToDevice), MediaType.APPLICATION_JSON); + motorXOriginCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorXOriginResult = motorXOriginCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorXOriginCmdId); // 从map中删除该指令 - motorXToHomeCmdToDeviceFuture.waitForContinue();//等待设备的反馈 - - JSONObject deviceResult = motorXToHomeCmdToDeviceFuture.getCallbackResult();//拿到设备返回结果 - CurrentSendCmdMapInstance.getInstance().removeCommand(toDeviceCmdId);//将指令从map中删除 - - 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); + if (!motorXOriginCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令响应超时", motorXOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorXOriginResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令返回错误", motorXOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean motorXOriginResultStatus = motorXOriginResult.getBool("result"); + if (!motorXOriginResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "x轴回原点指令执行失败", motorXOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴回原点指令反馈", motorXOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); } } 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 e25ac56..504f892 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 @@ -27,6 +27,9 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter @Async("asyncExecutor") @CommandMapping("motor_y_to_home")//业务指令注解 public class MotorYToHome implements CommandHandler { + /** + * 设备通信client + */ private final TcpClient deviceClient; @Override @@ -35,24 +38,36 @@ public class MotorYToHome implements CommandHandler { String frontCmdName = cmdForm.getCmdName(); //向前端发送接收到指令 emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - CMDToDevice motorYToHomeCmdToDevice = DeviceCommandGenerator.motor_y_origin();//生成指令 回原点 + CMDToDevice motorYOriginCMDToDevice = DeviceCommandGenerator.motor_y_origin(); //y轴回原点指令 + CommandFuture motorYOriginCMDToDeviceFuture = new CommandFuture(); + motorYOriginCMDToDeviceFuture.setCmdToDevice(motorYOriginCMDToDevice); + Integer motorYOriginCmdId = motorYOriginCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorYOriginCmdId, motorYOriginCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorYOriginCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了y轴回原点指令", motorYOriginCMDToDevice), MediaType.APPLICATION_JSON); - CommandFuture motorYToHomeCmdToDeviceFuture = new CommandFuture(); - motorYToHomeCmdToDeviceFuture.setCmdToDevice(motorYToHomeCmdToDevice); - Integer toDeviceCmdId = motorYToHomeCmdToDevice.getCmdId(); - CurrentSendCmdMapInstance.getInstance().putCommand(toDeviceCmdId, motorYToHomeCmdToDeviceFuture);//将指令放入map - deviceClient.sendToJSON(motorYToHomeCmdToDevice); //发送指令给设备 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了y轴回原点指令", motorYToHomeCmdToDevice), MediaType.APPLICATION_JSON); + motorYOriginCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorYOriginResult = motorYOriginCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorYOriginCmdId); // 从map中删除该指令 - motorYToHomeCmdToDeviceFuture.waitForContinue();//等待设备的反馈 - - JSONObject deviceResult = motorYToHomeCmdToDeviceFuture.getCallbackResult();//拿到设备返回结果 - CurrentSendCmdMapInstance.getInstance().removeCommand(toDeviceCmdId);//将指令从map中删除 - - 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); + if (!motorYOriginCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令响应超时", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorYOriginResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令返回错误", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean motorYOriginResultStatus = motorYOriginResult.getBool("result"); + if (!motorYOriginResultStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "y轴回原点指令执行失败", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "y轴回原点指令反馈", motorYOriginResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "x轴与y轴已回到原点"), MediaType.APPLICATION_JSON); + emitter.complete(); } } 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 ae48b00..52fb827 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 @@ -35,24 +35,34 @@ public class MotorZToHome implements CommandHandler { String frontCmdName = cmdForm.getCmdName(); //向前端发送接收到指令 emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - CMDToDevice motorZToHomeCmdToDevice = DeviceCommandGenerator.motor_z_origin();//生成指令 回原点 - - CommandFuture motorZToHomeCmdToDeviceFuture = new CommandFuture(); - motorZToHomeCmdToDeviceFuture.setCmdToDevice(motorZToHomeCmdToDevice); - Integer toDeviceCmdId = motorZToHomeCmdToDevice.getCmdId(); - CurrentSendCmdMapInstance.getInstance().putCommand(toDeviceCmdId, motorZToHomeCmdToDeviceFuture);//将指令放入map - deviceClient.sendToJSON(motorZToHomeCmdToDevice); //发送指令给设备 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了z轴回原点指令", motorZToHomeCmdToDevice), MediaType.APPLICATION_JSON); - - motorZToHomeCmdToDeviceFuture.waitForContinue();//等待设备的反馈 - - JSONObject deviceResult = motorZToHomeCmdToDeviceFuture.getCallbackResult();//拿到设备返回结果 - CurrentSendCmdMapInstance.getInstance().removeCommand(toDeviceCmdId);//将指令从map中删除 - - 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); + CMDToDevice motorZOriginCMDToDevice = DeviceCommandGenerator.motor_z_origin(); //轴回原点指令 + CommandFuture motorZOriginCMDToDeviceFuture = new CommandFuture(); + motorZOriginCMDToDeviceFuture.setCmdToDevice(motorZOriginCMDToDevice); + Integer motorZOriginCmdId = motorZOriginCMDToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(motorZOriginCmdId, motorZOriginCMDToDeviceFuture); // 将指令放入map + deviceClient.sendToJSON(motorZOriginCMDToDevice); // 发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了z轴回原点指令", motorZOriginCMDToDevice), MediaType.APPLICATION_JSON); + motorZOriginCMDToDeviceFuture.waitForContinue(); // 等待设备反馈 + JSONObject motorZOriginResult = motorZOriginCMDToDeviceFuture.getCallbackResult(); // 获取设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(motorZOriginCmdId); // 从map中删除该指令 + if (!motorZOriginCMDToDeviceFuture.isReceived()) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令响应超时", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + if (motorZOriginResult.getJSONObject("error") != null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令返回错误", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + Boolean motorZOriginStatus = motorZOriginResult.getBool("result"); + if (!motorZOriginStatus) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "z轴回原点指令执行失败", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.complete(); + return; } + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "z轴回原点指令反馈", motorZOriginResult), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "z轴已回到原点", motorZOriginCMDToDevice), MediaType.APPLICATION_JSON); + emitter.complete(); } } 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 f7a7075..1ca7763 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 @@ -134,7 +134,7 @@ public class NozzlePipelinePreFill implements CommandHandler { return; } emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "移动x轴到指定位置指令反馈", motorXPositionSetCmdToDeviceResult), MediaType.APPLICATION_JSON); - CMDToDevice motorYPositionSetCMDToDevice = DeviceCommandGenerator.motor_y_position_set(75.0, 0);//移动y轴到指定位置 (TODO) 废液桶Y轴位置应当可以配置 + CMDToDevice motorYPositionSetCMDToDevice = DeviceCommandGenerator.motor_y_position_set(75.0, 2);//移动y轴到指定位置 (TODO) 废液桶Y轴位置应当可以配置 CommandFuture motorYPositionSetCMDToDeviceFuture = new CommandFuture(); motorYPositionSetCMDToDeviceFuture.setCmdToDevice(motorYPositionSetCMDToDevice); Integer motorYPositionSetCMDToDeviceCmdId = motorYPositionSetCMDToDevice.getCmdId(); diff --git a/src/main/java/com/qyft/ms/app/service/CMDService.java b/src/main/java/com/qyft/ms/app/service/CMDService.java index b23aaec..f091911 100644 --- a/src/main/java/com/qyft/ms/app/service/CMDService.java +++ b/src/main/java/com/qyft/ms/app/service/CMDService.java @@ -227,10 +227,10 @@ public class CMDService { // 玻片范围的实际位置 // 托盘点位 x y z - double left =x+ p.get("x1"); - double right = x+ p.get("x2"); - double top = y+ p.get("y1"); - double bottom = y+ p.get("y2"); + double left = x + p.get("x1"); + double right = x + p.get("x2"); + double top = y + p.get("y1"); + double bottom = y + p.get("y2"); int space = (Integer) params.get("space"); int routeType = (Integer) params.get("routeType"); 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 ae034a4..652891e 100644 --- a/src/main/java/com/qyft/ms/device/handler/DeviceMessageHandler.java +++ b/src/main/java/com/qyft/ms/device/handler/DeviceMessageHandler.java @@ -5,8 +5,6 @@ import cn.hutool.json.JSONUtil; import com.qyft.ms.app.common.command.CommandFuture; import com.qyft.ms.app.common.command.CurrentSendCmdMapInstance; import com.qyft.ms.app.service.WebSocketService; -import com.qyft.ms.device.device.DeviceInstance; -import com.qyft.ms.device.device.MatrixSprayStatusEnum; import com.qyft.ms.device.model.bo.DeviceFeedback; import com.qyft.ms.device.service.DeviceStatusService; import io.netty.buffer.ByteBuf; @@ -15,56 +13,57 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.CharsetUtil; import lombok.RequiredArgsConstructor; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; @Slf4j @Component @ChannelHandler.Sharable @RequiredArgsConstructor public class DeviceMessageHandler extends ChannelInboundHandlerAdapter { - public final Map> responseMap = new ConcurrentHashMap<>(); private final DeviceStatusService deviceStatusService; private final WebSocketService webSocketService; + @Setter + private volatile CountDownLatch latch; + + @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf buf = (ByteBuf) msg; String serverMsg = buf.toString(CharsetUtil.UTF_8); + log.info("serverMsg:{}", serverMsg); try { -// JsonRpcResponse jsonRpcResponse = JSONUtil.toBean(serverMsg, JsonRpcResponse.class); -// if (TcpMessageType.STATUS.equals(jsonRpcResponse.getType())) {//设备状态 -// DeviceStatus deviceStatus = JSONUtil.toBean(jsonRpcResponse.getData(), DeviceStatus.class); -// deviceStatusService.updateDeviceStatus(deviceStatus); // 更新设备状态 -// } else if (TcpMessageType.ALARM.equals(jsonRpcResponse.getType())) {//设备报警 -// log.error("设备报警: {}", serverMsg); -// DeviceAlarm deviceAlarm = JSONUtil.toBean(jsonRpcResponse.getData(), DeviceAlarm.class); -// webSocketService.pushMsg(WebSocketMessageType.WARN, deviceAlarm); -// } else if (TcpMessageType.FEEDBACK.equals(jsonRpcResponse.getType())) {//设备指令反馈 -// DeviceFeedback deviceFeedback = JSONUtil.toBean(jsonRpcResponse.getData(), DeviceFeedback.class); -// this.handleTcpResponse(deviceFeedback); -// } JSONObject deviceResult = JSONUtil.parseObj(serverMsg); JSONObject payload = deviceResult.getJSONObject("payload"); String tag = deviceResult.get("tag").toString(); - if ("event".equals(tag)) { - //设备上报事件 - } else if ("ack".equals(tag)) { - //设备指令反馈 + if ("event".equals(tag)) { //设备上报事件 + String eventType = payload.getJSONObject("result").getStr("event_type"); + if (latch != null) { + if ("motor_x_move_finished".equals(eventType)) { + latch.countDown(); // X 轴指令完成 + } else if ("motor_y_move_finished".equals(eventType)) { + latch.countDown(); // Y 轴指令完成 + } else if ("motor_z_move_finished".equals(eventType)) { + latch.countDown(); // Y 轴指令完成 + } + } + } else if ("ack".equals(tag)) {//设备指令反馈 Integer cmdId = payload.getInt("cmdId"); CommandFuture commandFuture = CurrentSendCmdMapInstance.getInstance().getCommand(cmdId); - if(commandFuture!=null){ + if (commandFuture != null) { commandFuture.setReceived(true); commandFuture.setCallbackResult(payload); commandFuture.commandContinue(); } - } else if ("status".equals(tag)) { - //设备上报状态 + } else if ("status".equals(tag)) { //设备上报状态 } } catch (Exception e) { log.error("TCP服务消息处理错误: {}, error: {}", serverMsg, e.getMessage(), e); @@ -72,14 +71,4 @@ public class DeviceMessageHandler extends ChannelInboundHandlerAdapter { buf.release(); } } - - private void handleTcpResponse(DeviceFeedback deviceFeedback) { - String requestId = deviceFeedback.getId(); - CompletableFuture future = responseMap.remove(requestId); - if (future != null) { - future.complete(deviceFeedback); - } else { - log.error("未找到 requestId: {} 对应的等待请求", requestId); - } - } } \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 5c2950c..610082e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -37,7 +37,8 @@ tcp: enable: true # 是否开启 TCP 连接 server-enable: true # 是否开启 TCP 连接 # host: 127.0.0.1 - host: 192.168.1.168 +# host: 192.168.1.168 + host: 192.168.1.140 port: 9080 reconnect: 5000 # 断线重连间隔(单位:毫秒) timeout: 10000 # 连接超时时间(单位:毫秒)