From ac68f479752dc8c3602d6e68d8fad0fe46fa417d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Thu, 13 Mar 2025 11:42:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=81=E5=BC=8F=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ms/app/common/annotation/CommandMapping.java | 12 ++ .../java/com/qyft/ms/app/config/AsyncConfig.java | 23 ++++ .../qyft/ms/app/controller/FrontCmdController.java | 98 ++++++++------ .../frontcommandhandle/CommandHandler.java | 8 ++ .../frontcommandhandle/DehumidifierStart.java | 143 --------------------- .../frontcommandhandle/MatrixSprayChangeParam.java | 46 ------- .../frontcommandhandle/MatrixSprayContinue.java | 47 ------- .../frontcommandhandle/MatrixSprayPause.java | 47 ------- .../frontcommandhandle/MatrixSprayStart.java | 47 ------- .../frontcommandhandle/MatrixSprayStop.java | 47 ------- .../frontcommandhandle/NozzlePipelinePreFill.java | 49 ------- .../controller/frontcommandhandle/SlideTrayIn.java | 47 ------- .../frontcommandhandle/SlideTrayOut.java | 47 ------- .../frontcommandhandle/SprayPipelineWash.java | 52 -------- .../frontcommandhandle/SyringePipelineWash.java | 47 ------- .../frontcommandhandle/impl/DehumidifierStart.java | 138 ++++++++++++++++++++ .../impl/MatrixSprayChangeParam.java | 45 +++++++ .../impl/MatrixSprayContinue.java | 47 +++++++ .../frontcommandhandle/impl/MatrixSprayPause.java | 47 +++++++ .../frontcommandhandle/impl/MatrixSprayStart.java | 47 +++++++ .../frontcommandhandle/impl/MatrixSprayStop.java | 47 +++++++ .../impl/NozzlePipelinePreFill.java | 49 +++++++ .../frontcommandhandle/impl/SlideTrayIn.java | 47 +++++++ .../frontcommandhandle/impl/SlideTrayOut.java | 47 +++++++ .../frontcommandhandle/impl/SprayPipelineWash.java | 46 +++++++ .../impl/SyringePipelineWash.java | 47 +++++++ .../frontcommandhandle/impl/TestDemoHandler.java | 47 +++++++ .../app/core/registry/CommandHandlerRegistry.java | 43 +++++++ .../java/com/qyft/ms/device/client/TcpClient.java | 8 ++ 29 files changed, 809 insertions(+), 656 deletions(-) create mode 100644 src/main/java/com/qyft/ms/app/common/annotation/CommandMapping.java create mode 100644 src/main/java/com/qyft/ms/app/config/AsyncConfig.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/CommandHandler.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/DehumidifierStart.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayChangeParam.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayContinue.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayPause.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayStart.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayStop.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/NozzlePipelinePreFill.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SlideTrayIn.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SlideTrayOut.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SprayPipelineWash.java delete mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SyringePipelineWash.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/DehumidifierStart.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayChangeParam.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayContinue.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayPause.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayStart.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayStop.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/NozzlePipelinePreFill.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SlideTrayIn.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SlideTrayOut.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SprayPipelineWash.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SyringePipelineWash.java create mode 100644 src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/TestDemoHandler.java create mode 100644 src/main/java/com/qyft/ms/app/core/registry/CommandHandlerRegistry.java diff --git a/src/main/java/com/qyft/ms/app/common/annotation/CommandMapping.java b/src/main/java/com/qyft/ms/app/common/annotation/CommandMapping.java new file mode 100644 index 0000000..1fb91a5 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/common/annotation/CommandMapping.java @@ -0,0 +1,12 @@ +package com.qyft.ms.app.common.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface CommandMapping { + String value(); +} \ No newline at end of file diff --git a/src/main/java/com/qyft/ms/app/config/AsyncConfig.java b/src/main/java/com/qyft/ms/app/config/AsyncConfig.java new file mode 100644 index 0000000..0d26e3c --- /dev/null +++ b/src/main/java/com/qyft/ms/app/config/AsyncConfig.java @@ -0,0 +1,23 @@ +package com.qyft.ms.app.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; + +@Configuration +@EnableAsync +public class AsyncConfig { + // 可选:配置线程池 Bean + @Bean("asyncExecutor") + public Executor asyncExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(5); + executor.setMaxPoolSize(10); + executor.setQueueCapacity(25); + executor.initialize(); + return executor; + } +} \ No newline at end of file diff --git a/src/main/java/com/qyft/ms/app/controller/FrontCmdController.java b/src/main/java/com/qyft/ms/app/controller/FrontCmdController.java index 68b62e9..9783fc8 100644 --- a/src/main/java/com/qyft/ms/app/controller/FrontCmdController.java +++ b/src/main/java/com/qyft/ms/app/controller/FrontCmdController.java @@ -1,14 +1,18 @@ package com.qyft.ms.app.controller; import com.qyft.ms.app.common.command.CurrentSendCmdMapInstance; -import com.qyft.ms.app.controller.frontcommandhandle.DehumidifierStart; +import com.qyft.ms.app.controller.frontcommandhandle.CommandHandler; +import com.qyft.ms.app.core.registry.CommandHandlerRegistry; import com.qyft.ms.app.model.form.CMDFormV2; import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; @Tag(name = "前端调用指令") @@ -17,53 +21,73 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter @RequiredArgsConstructor @Slf4j public class FrontCmdController { - private final DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2; + private final CommandHandlerRegistry registry; @Operation(summary = "前端统一调用一个接口") @PostMapping("/control") public ResponseBodyEmitter controlMethod(@RequestBody CMDFormV2 cmdForm) { ResponseBodyEmitter emitter = new ResponseBodyEmitter(); - String frontCmdName = cmdForm.getCmdName(); - - if ("device_status_get".equals(frontCmdName)) { - - DehumidifierStart.handle(deviceTcpCMDServiceV2, emitter, cmdForm); - - } else if ("dehumidifier_start".equals(frontCmdName)) { - /* - 除湿,该方法接收Double类型参数 humidity - */ - - } else if ("matrix_prefill".equals(frontCmdName)) { - - } else if ("spray_pipeline_wash".equals(frontCmdName)) { - - } else if ("syringe_pipeline_wash".equals(frontCmdName)) { - - } else if ("matrix_spray_start".equals(frontCmdName)) { - - } else if ("matrix_spray_change_param".equals(frontCmdName)) { - - } else if ("matrix_spray_stop".equals(frontCmdName)) { - - } else if ("matrix_spray_pause".equals(frontCmdName)) { - - } else if ("matrix_spray_continue".equals(frontCmdName)) { - - } else if ("slide_tray_in".equals(frontCmdName)) { - - } else if ("slide_tray_out".equals(frontCmdName)) { - + String frontCmdName = cmdForm.getCmdName(); // 获取前端传入的命令字符串 + + CommandHandler handler = registry.getHandler(frontCmdName); + if (handler != null) { + handler.handle(cmdForm, emitter); + } else { + try { + emitter.send("未找到对应的业务指令: " + frontCmdName); + emitter.complete(); + } catch (Exception e) { + emitter.completeWithError(e); + } } - - return emitter; } +// @Operation(summary = "前端统一调用一个接口") +// @PostMapping("/control") +// public ResponseBodyEmitter controlMethod(@RequestBody CMDFormV2 cmdForm) { +// ResponseBodyEmitter emitter = new ResponseBodyEmitter(); +// String frontCmdName = cmdForm.getCmdName(); +// +// if ("device_status_get".equals(frontCmdName)) { +// +// +// } else if ("dehumidifier_start".equals(frontCmdName)) { +// /* +// 除湿,该方法接收Double类型参数 humidity +// */ +// DehumidifierStart dehumidifierStart = new DehumidifierStart(); +// DehumidifierStart.handle(deviceTcpCMDServiceV2, emitter, cmdForm); +// +// } else if ("matrix_prefill".equals(frontCmdName)) { +// +// } else if ("spray_pipeline_wash".equals(frontCmdName)) { +// +// } else if ("syringe_pipeline_wash".equals(frontCmdName)) { +// +// } else if ("matrix_spray_start".equals(frontCmdName)) { +// +// } else if ("matrix_spray_change_param".equals(frontCmdName)) { +// +// } else if ("matrix_spray_stop".equals(frontCmdName)) { +// +// } else if ("matrix_spray_pause".equals(frontCmdName)) { +// +// } else if ("matrix_spray_continue".equals(frontCmdName)) { +// +// } else if ("slide_tray_in".equals(frontCmdName)) { +// +// } else if ("slide_tray_out".equals(frontCmdName)) { +// +// } +// +// return emitter; +// } + @RequestMapping("/controlTest") - public void controlMethodTest(){ + public void controlMethodTest() { CurrentSendCmdMapInstance.getInstance().getCommand(1).commandContinue(); } diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/CommandHandler.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/CommandHandler.java new file mode 100644 index 0000000..417bb56 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/CommandHandler.java @@ -0,0 +1,8 @@ +package com.qyft.ms.app.controller.frontcommandhandle; + +import com.qyft.ms.app.model.form.CMDFormV2; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +public interface CommandHandler { + void handle(CMDFormV2 cmdForm, ResponseBodyEmitter emitter); +} \ No newline at end of file diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/DehumidifierStart.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/DehumidifierStart.java deleted file mode 100644 index f1d662f..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/DehumidifierStart.java +++ /dev/null @@ -1,143 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import cn.hutool.json.JSONObject; -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.common.command.FrontCommandAck; -import com.qyft.ms.app.common.command.DeviceCommandGenerator; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.bo.CMDToDevice; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; - -public class DehumidifierStart { - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - /** - * 1:判断参数是否合法 - */ - Integer frontHumidity = (Integer) param.get("humidity"); - if (frontHumidity == null) { - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 humidity 必填"), MediaType.APPLICATION_JSON); - emitter.complete(); - return; - } - - - /** - * 2:获取设备湿度 - */ - - CMDToDevice humidityCmdToDevice = DeviceCommandGenerator.humidity_get();//生成指令 获取当前湿度 - CommandFuture humidityGetFuture = new CommandFuture(); - humidityGetFuture.setCmdToDevice(humidityCmdToDevice); - Integer toDeviceCmdId = humidityCmdToDevice.getCmdId(); - CurrentSendCmdMapInstance.getInstance().putCommand(toDeviceCmdId, humidityGetFuture);//将指令放入map - deviceTcpCMDServiceV2.send(humidityCmdToDevice); //发送指令给设备 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了获取湿度指令:" + JSONUtil.toJsonStr(humidityCmdToDevice)), MediaType.APPLICATION_JSON); - - - humidityGetFuture.waitForContinue();//等待设备的反馈 - - - - /** - * 3:处理返回结果或者没有响应 - */ - JSONObject deviceResult = humidityGetFuture.getCallbackResult();//拿到设备返回结果 - CurrentSendCmdMapInstance.getInstance().removeCommand(toDeviceCmdId);//将指令从map中删除 - - if (humidityGetFuture.isReceived() || humidityGetFuture.isGetResult()) { - //设备已经收到指令并且执行成功 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "设备指令反馈:" + JSONUtil.toJsonStr(deviceResult)), MediaType.APPLICATION_JSON); - - Double deviceHumidity = deviceResult.getDouble("humidity");//拿到设备返回的湿度数值 - if (frontHumidity > deviceHumidity) { - - //如果小于设置值,提醒用户不用除湿 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "设定湿度大于当前湿度,无需除湿。"), MediaType.APPLICATION_JSON); - - - } - else{ - - /** - * 4:如果大于设置值,开始除湿;打开除湿阀门 - */ - - CMDToDevice dehumidifier_valve_control = DeviceCommandGenerator.dehumidifier_valve_control("open");//生成指令 开启除湿阀 - CommandFuture dehumidifier_valve_control_Future = new CommandFuture(); - humidityGetFuture.setCmdToDevice(dehumidifier_valve_control); - Integer dehumidifier_valve_control_cmdId = dehumidifier_valve_control.getCmdId(); - CurrentSendCmdMapInstance.getInstance().putCommand(dehumidifier_valve_control_cmdId, dehumidifier_valve_control_Future);//将指令放入map - deviceTcpCMDServiceV2.send(dehumidifier_valve_control); //发送指令给设备 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了开启除湿阀指令:" + JSONUtil.toJsonStr(dehumidifier_valve_control)), MediaType.APPLICATION_JSON); - - - dehumidifier_valve_control_Future.waitForContinue();//等待设备的反馈 - - /** - * 5:处理打开除湿阀门返回结果 - */ - JSONObject dehumidifier_valve_control_result = dehumidifier_valve_control_Future.getCallbackResult();//拿到设备返回结果 - CurrentSendCmdMapInstance.getInstance().removeCommand(dehumidifier_valve_control_cmdId);//将指令从map中删除 - - if (dehumidifier_valve_control_Future.isReceived() || dehumidifier_valve_control_Future.isGetResult()) { - //设备已经收到指令并且执行成功 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "开启除湿阀指令指令设备反馈:" + JSONUtil.toJsonStr(dehumidifier_valve_control_result)), MediaType.APPLICATION_JSON); - - } - else{ - //设备未收到指令或者执行失败 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启除湿阀指令没有响应:" + JSONUtil.toJsonStr(deviceResult)), MediaType.APPLICATION_JSON); - - } - - - - - - - } - - }else{ - //设备未收到指令或者执行失败 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "设备没有响应:" + JSONUtil.toJsonStr(deviceResult)), MediaType.APPLICATION_JSON); - - - } - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayChangeParam.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayChangeParam.java deleted file mode 100644 index 9cc86ee..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayChangeParam.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import com.qyft.ms.app.common.command.FrontCommandAck; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.device.DeviceInstance; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; - -/** - * 实时修改参数 - */ -public class MatrixSprayChangeParam { - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayContinue.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayContinue.java deleted file mode 100644 index 078bac8..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayContinue.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import com.qyft.ms.app.common.command.FrontCommandAck; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; - -/** - * 前端继续基质喷涂 - */ -public class MatrixSprayContinue { - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayPause.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayPause.java deleted file mode 100644 index eaafa70..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayPause.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import com.qyft.ms.app.common.command.FrontCommandAck; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; - -/** - * 前端暂停基质喷涂 - */ -public class MatrixSprayPause { - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayStart.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayStart.java deleted file mode 100644 index b6d2670..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayStart.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import com.qyft.ms.app.common.command.FrontCommandAck; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; - -/** - * 前端开始基质喷涂 - */ -public class MatrixSprayStart { - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayStop.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayStop.java deleted file mode 100644 index 5670a0e..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/MatrixSprayStop.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import com.qyft.ms.app.common.command.FrontCommandAck; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; - -/** - * 前端主动结束基质喷涂 - */ -public class MatrixSprayStop { - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/NozzlePipelinePreFill.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/NozzlePipelinePreFill.java deleted file mode 100644 index e2f3ea8..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/NozzlePipelinePreFill.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import com.qyft.ms.app.common.command.FrontCommandAck; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; - -/** - * 处理前端喷嘴管路预充指令 - */ -public class NozzlePipelinePreFill { - - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SlideTrayIn.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SlideTrayIn.java deleted file mode 100644 index be82caf..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SlideTrayIn.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import com.qyft.ms.app.common.command.FrontCommandAck; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; - -/** - * 处理前端推入托盘 - */ -public class SlideTrayIn { - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SlideTrayOut.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SlideTrayOut.java deleted file mode 100644 index 4ba7fe7..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SlideTrayOut.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import com.qyft.ms.app.common.command.FrontCommandAck; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; - -/** - * 处理前端推出托盘 - */ -public class SlideTrayOut { - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SprayPipelineWash.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SprayPipelineWash.java deleted file mode 100644 index 6056824..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SprayPipelineWash.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import cn.hutool.json.JSONObject; -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.common.command.DeviceCommandGenerator; -import com.qyft.ms.app.common.command.FrontCommandAck; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.bo.CMDToDevice; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; -/** - * 处理前端清洗喷嘴管路 - */ -public class SprayPipelineWash { - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SyringePipelineWash.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SyringePipelineWash.java deleted file mode 100644 index bf8eeaa..0000000 --- a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/SyringePipelineWash.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.qyft.ms.app.controller.frontcommandhandle; - -import com.qyft.ms.app.common.command.FrontCommandAck; -import com.qyft.ms.app.common.constant.CommandStatus; -import com.qyft.ms.app.model.form.CMDFormV2; -import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import java.io.IOException; -import java.util.Map; - -/** - * 处理前端清洗注射器管路 - */ -public class SyringePipelineWash { - public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { - - String frontCmdId = cmdForm.getCmdId(); - String frontCmdName = cmdForm.getCmdName(); - Map param = cmdForm.getParam(); - - new Thread(() -> { - try { - - //向前端发送接收到指令 - emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); - - - - - - - - - - - } catch (IOException e) { - emitter.completeWithError(e); - } - }).start(); - - - //对返回结果处理 - emitter.complete(); - } -} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/DehumidifierStart.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/DehumidifierStart.java new file mode 100644 index 0000000..f5572fc --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/DehumidifierStart.java @@ -0,0 +1,138 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +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.controller.frontcommandhandle.CommandHandler; +import com.qyft.ms.app.model.bo.CMDToDevice; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.client.TcpClient; +import lombok.RequiredArgsConstructor; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; + +@Component +@RequiredArgsConstructor +@CommandMapping("dehumidifier_start")//指令 +public class DehumidifierStart implements CommandHandler { + /** + * 设备通信client + */ + private final TcpClient deviceClient; + + @Override + public void handle(CMDFormV2 cmdForm, ResponseBodyEmitter emitter) { + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + new Thread(() -> { + try { + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + /** + * 1:判断参数是否合法 + */ + Integer frontHumidity = (Integer) param.get("humidity"); + if (frontHumidity == null) { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "参数 humidity 必填"), MediaType.APPLICATION_JSON); + emitter.complete(); + return; + } + + + /** + * 2:获取设备湿度 + */ + + CMDToDevice humidityCmdToDevice = DeviceCommandGenerator.humidity_get();//生成指令 获取当前湿度 + CommandFuture humidityGetFuture = new CommandFuture(); + humidityGetFuture.setCmdToDevice(humidityCmdToDevice); + Integer toDeviceCmdId = humidityCmdToDevice.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(toDeviceCmdId, humidityGetFuture);//将指令放入map + deviceClient.sendToJSON(humidityCmdToDevice); //发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了获取湿度指令:" + JSONUtil.toJsonStr(humidityCmdToDevice)), MediaType.APPLICATION_JSON); + + + humidityGetFuture.waitForContinue();//等待设备的反馈 + + + /** + * 3:处理返回结果或者没有响应 + */ + JSONObject deviceResult = humidityGetFuture.getCallbackResult();//拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(toDeviceCmdId);//将指令从map中删除 + + if (humidityGetFuture.isReceived() || humidityGetFuture.isGetResult()) { + //设备已经收到指令并且执行成功 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "设备指令反馈:" + JSONUtil.toJsonStr(deviceResult)), MediaType.APPLICATION_JSON); + + Double deviceHumidity = deviceResult.getDouble("humidity");//拿到设备返回的湿度数值 + if (frontHumidity > deviceHumidity) { + + //如果小于设置值,提醒用户不用除湿 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "设定湿度大于当前湿度,无需除湿。"), MediaType.APPLICATION_JSON); + + + } else { + + /** + * 4:如果大于设置值,开始除湿;打开除湿阀门 + */ + + CMDToDevice dehumidifier_valve_control = DeviceCommandGenerator.dehumidifier_valve_control("open");//生成指令 开启除湿阀 + CommandFuture dehumidifier_valve_control_Future = new CommandFuture(); + humidityGetFuture.setCmdToDevice(dehumidifier_valve_control); + Integer dehumidifier_valve_control_cmdId = dehumidifier_valve_control.getCmdId(); + CurrentSendCmdMapInstance.getInstance().putCommand(dehumidifier_valve_control_cmdId, dehumidifier_valve_control_Future);//将指令放入map + deviceClient.sendToJSON(dehumidifier_valve_control); //发送指令给设备 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.SEND, "已向设备发送了开启除湿阀指令:" + JSONUtil.toJsonStr(dehumidifier_valve_control)), MediaType.APPLICATION_JSON); + + + dehumidifier_valve_control_Future.waitForContinue();//等待设备的反馈 + + /** + * 5:处理打开除湿阀门返回结果 + */ + JSONObject dehumidifier_valve_control_result = dehumidifier_valve_control_Future.getCallbackResult();//拿到设备返回结果 + CurrentSendCmdMapInstance.getInstance().removeCommand(dehumidifier_valve_control_cmdId);//将指令从map中删除 + + if (dehumidifier_valve_control_Future.isReceived() || dehumidifier_valve_control_Future.isGetResult()) { + //设备已经收到指令并且执行成功 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RESULT, "开启除湿阀指令指令设备反馈:" + JSONUtil.toJsonStr(dehumidifier_valve_control_result)), MediaType.APPLICATION_JSON); + + } else { + //设备未收到指令或者执行失败 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "开启除湿阀指令没有响应:" + JSONUtil.toJsonStr(deviceResult)), MediaType.APPLICATION_JSON); + + } + + + } + + } else { + //设备未收到指令或者执行失败 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.ERROR, "设备没有响应:" + JSONUtil.toJsonStr(deviceResult)), MediaType.APPLICATION_JSON); + + + } + + + } catch (IOException e) { + emitter.completeWithError(e); + } + //对返回结果处理 + emitter.complete(); + }).start(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayChangeParam.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayChangeParam.java new file mode 100644 index 0000000..c3c900c --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayChangeParam.java @@ -0,0 +1,45 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; + +/** + * 实时修改参数 + */ +public class MatrixSprayChangeParam { + public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { + + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + new Thread(() -> { + try { + + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + + + + + + + } catch (IOException e) { + emitter.completeWithError(e); + } + }).start(); + + + //对返回结果处理 + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayContinue.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayContinue.java new file mode 100644 index 0000000..aafb8c1 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayContinue.java @@ -0,0 +1,47 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; + +/** + * 前端继续基质喷涂 + */ +public class MatrixSprayContinue { + public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { + + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + new Thread(() -> { + try { + + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + + + + + + + + + } catch (IOException e) { + emitter.completeWithError(e); + } + }).start(); + + + //对返回结果处理 + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayPause.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayPause.java new file mode 100644 index 0000000..c8c8099 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayPause.java @@ -0,0 +1,47 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; + +/** + * 前端暂停基质喷涂 + */ +public class MatrixSprayPause { + public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { + + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + new Thread(() -> { + try { + + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + + + + + + + + + } catch (IOException e) { + emitter.completeWithError(e); + } + }).start(); + + + //对返回结果处理 + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayStart.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayStart.java new file mode 100644 index 0000000..4bded7e --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayStart.java @@ -0,0 +1,47 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; + +/** + * 前端开始基质喷涂 + */ +public class MatrixSprayStart { + public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { + + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + new Thread(() -> { + try { + + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + + + + + + + + + } catch (IOException e) { + emitter.completeWithError(e); + } + }).start(); + + + //对返回结果处理 + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayStop.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayStop.java new file mode 100644 index 0000000..e759cfd --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/MatrixSprayStop.java @@ -0,0 +1,47 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; + +/** + * 前端主动结束基质喷涂 + */ +public class MatrixSprayStop { + public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { + + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + new Thread(() -> { + try { + + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + + + + + + + + + } catch (IOException e) { + emitter.completeWithError(e); + } + }).start(); + + + //对返回结果处理 + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/NozzlePipelinePreFill.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/NozzlePipelinePreFill.java new file mode 100644 index 0000000..89d651f --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/NozzlePipelinePreFill.java @@ -0,0 +1,49 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; + +/** + * 处理前端喷嘴管路预充指令 + */ +public class NozzlePipelinePreFill { + + public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { + + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + new Thread(() -> { + try { + + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + + + + + + + + + + } catch (IOException e) { + emitter.completeWithError(e); + } + }).start(); + + + //对返回结果处理 + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SlideTrayIn.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SlideTrayIn.java new file mode 100644 index 0000000..d31b1a4 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SlideTrayIn.java @@ -0,0 +1,47 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; + +/** + * 处理前端推入托盘 + */ +public class SlideTrayIn { + public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { + + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + new Thread(() -> { + try { + + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + + + + + + + + + } catch (IOException e) { + emitter.completeWithError(e); + } + }).start(); + + + //对返回结果处理 + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SlideTrayOut.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SlideTrayOut.java new file mode 100644 index 0000000..4a3155c --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SlideTrayOut.java @@ -0,0 +1,47 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; + +/** + * 处理前端推出托盘 + */ +public class SlideTrayOut { + public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { + + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + new Thread(() -> { + try { + + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + + + + + + + + + } catch (IOException e) { + emitter.completeWithError(e); + } + }).start(); + + + //对返回结果处理 + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SprayPipelineWash.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SprayPipelineWash.java new file mode 100644 index 0000000..98d10dd --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SprayPipelineWash.java @@ -0,0 +1,46 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; +/** + * 处理前端清洗喷嘴管路 + */ +public class SprayPipelineWash { + public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { + + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + new Thread(() -> { + try { + + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + + + + + + + + + } catch (IOException e) { + emitter.completeWithError(e); + } + }).start(); + + + //对返回结果处理 + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SyringePipelineWash.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SyringePipelineWash.java new file mode 100644 index 0000000..d027e3a --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/SyringePipelineWash.java @@ -0,0 +1,47 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.model.form.CMDFormV2; +import com.qyft.ms.device.service.DeviceTcpCMDServiceV2; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; + +import java.io.IOException; +import java.util.Map; + +/** + * 处理前端清洗注射器管路 + */ +public class SyringePipelineWash { + public static void handle(DeviceTcpCMDServiceV2 deviceTcpCMDServiceV2, ResponseBodyEmitter emitter, CMDFormV2 cmdForm) { + + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + Map param = cmdForm.getParam(); + + new Thread(() -> { + try { + + //向前端发送接收到指令 + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令"), MediaType.APPLICATION_JSON); + + + + + + + + + + + } catch (IOException e) { + emitter.completeWithError(e); + } + }).start(); + + + //对返回结果处理 + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/TestDemoHandler.java b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/TestDemoHandler.java new file mode 100644 index 0000000..5ac508d --- /dev/null +++ b/src/main/java/com/qyft/ms/app/controller/frontcommandhandle/impl/TestDemoHandler.java @@ -0,0 +1,47 @@ +package com.qyft.ms.app.controller.frontcommandhandle.impl; + +import cn.hutool.json.JSONUtil; +import com.qyft.ms.app.common.annotation.CommandMapping; +import com.qyft.ms.app.common.command.FrontCommandAck; +import com.qyft.ms.app.common.constant.CommandStatus; +import com.qyft.ms.app.controller.frontcommandhandle.CommandHandler; +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; + +@Slf4j +@Component +@RequiredArgsConstructor +@CommandMapping("test_test")//指令 +public class TestDemoHandler implements CommandHandler { + /** + * 设备通信client + */ + private final TcpClient deviceClient; + + @Override + public void handle(CMDFormV2 cmdForm, ResponseBodyEmitter emitter) { + asyncHandle(cmdForm, emitter); + } + + @Async("asyncExecutor") + public void asyncHandle(CMDFormV2 cmdForm, ResponseBodyEmitter emitter) { + String frontCmdId = cmdForm.getCmdId(); + String frontCmdName = cmdForm.getCmdName(); + try { + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令1"), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令2"), MediaType.APPLICATION_JSON); + emitter.send(FrontCommandAck.backstageAck(frontCmdId, frontCmdName, CommandStatus.RECEIVE, "后台已收到指令3"), MediaType.APPLICATION_JSON); + } catch (Exception e) { + log.error("指令执行失败:{}", JSONUtil.toJsonStr(cmdForm), e); + emitter.completeWithError(e); + return; + } + emitter.complete(); + } +} diff --git a/src/main/java/com/qyft/ms/app/core/registry/CommandHandlerRegistry.java b/src/main/java/com/qyft/ms/app/core/registry/CommandHandlerRegistry.java new file mode 100644 index 0000000..6954759 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/core/registry/CommandHandlerRegistry.java @@ -0,0 +1,43 @@ +package com.qyft.ms.app.core.registry; + +import com.qyft.ms.app.common.annotation.CommandMapping; +import com.qyft.ms.app.controller.frontcommandhandle.CommandHandler; +import io.micrometer.common.lang.NonNull; +import jakarta.annotation.PostConstruct; +import org.springframework.aop.support.AopUtils; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +@Component +public class CommandHandlerRegistry implements ApplicationContextAware { + + private ApplicationContext applicationContext; + private final Map handlerMap = new HashMap<>(); + + @Override + public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + @PostConstruct + public void init() { + Map beans = applicationContext.getBeansWithAnnotation(CommandMapping.class); + for (Object bean : beans.values()) { + // 获取实际目标类,而不是代理类 + Class targetClass = AopUtils.getTargetClass(bean); + CommandMapping mapping = targetClass.getAnnotation(CommandMapping.class); + if (mapping != null && bean instanceof CommandHandler) { + handlerMap.put(mapping.value(), (CommandHandler) bean); + } + } + } + + public CommandHandler getHandler(String command) { + return handlerMap.get(command); + } +} \ No newline at end of file diff --git a/src/main/java/com/qyft/ms/device/client/TcpClient.java b/src/main/java/com/qyft/ms/device/client/TcpClient.java index 0bcda2c..6e69100 100644 --- a/src/main/java/com/qyft/ms/device/client/TcpClient.java +++ b/src/main/java/com/qyft/ms/device/client/TcpClient.java @@ -101,6 +101,14 @@ public class TcpClient { } } + /** + * 将obj转换成json后发送 + */ + public boolean sendToJSON(Object request) { + String msg = JSONUtil.toJsonStr(request); + return this.send(msg); + } + // 发送字符串请求到TCP服务器的方法 public boolean send(String request) { if (channel != null && channel.isActive()) {