From 326b4aba4869f68f5dd333c536af1d9d2ae71d23 Mon Sep 17 00:00:00 2001 From: sige Date: Wed, 20 Dec 2023 16:47:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../boditech/controller/DeviceController.java | 6 +++ .../boditech/controller/TmpController.java | 29 ++++++++++++- .../com/dreamworks/boditech/driver/Device.java | 2 +- .../boditech/driver/consumable/CsmTestCardBox.java | 2 + .../driver/consumable/CsmTestCardManager.java | 37 ++-------------- .../boditech/service/WebsocketServerService.java | 45 ++++++------------- .../boditech/utils/MyWsServerConnection.java | 50 ++++++++++++++++++++++ 7 files changed, 104 insertions(+), 67 deletions(-) create mode 100644 src/main/java/com/dreamworks/boditech/utils/MyWsServerConnection.java diff --git a/src/main/java/com/dreamworks/boditech/controller/DeviceController.java b/src/main/java/com/dreamworks/boditech/controller/DeviceController.java index 3fb5784..5da9f15 100644 --- a/src/main/java/com/dreamworks/boditech/controller/DeviceController.java +++ b/src/main/java/com/dreamworks/boditech/controller/DeviceController.java @@ -181,6 +181,12 @@ public class DeviceController extends BaseController { } @ResponseBody + @PostMapping("/api/device/load") + public ApiResponse load() { + return this.success(); + } + + @ResponseBody @PostMapping("/api/device/unload") public ApiResponse unload() { return this.success(); diff --git a/src/main/java/com/dreamworks/boditech/controller/TmpController.java b/src/main/java/com/dreamworks/boditech/controller/TmpController.java index f120ba3..0d20149 100644 --- a/src/main/java/com/dreamworks/boditech/controller/TmpController.java +++ b/src/main/java/com/dreamworks/boditech/controller/TmpController.java @@ -1,2 +1,29 @@ -package com.dreamworks.boditech.controller;public class TmpController { +package com.dreamworks.boditech.controller; +import com.dreamworks.boditech.controller.entity.ApiResponse; +import com.dreamworks.boditech.service.WebsocketServerService; +import jakarta.annotation.Resource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.ResponseBody; +import java.util.Map; +@Controller +public class TmpController extends BaseController { + @Resource + private WebsocketServerService wsServer; + + @ResponseBody + @PostMapping("/api/tmp/trigger-event") + public ApiResponse triggerEvent(@RequestBody Map params ) { + this.wsServer.send("{\"type\":\"event\",\"name\":\"" + params.get("name") + "\"}"); + return this.success(); + } + + @ResponseBody + @PostMapping("/api/tmp/trigger-error") + public ApiResponse triggerError(@RequestBody Map params ) { + this.wsServer.send("{\"type\":\"error\",\"name\":\"" + params.get("name") + "\",\"message\":\"DEMO_ERROR_MESSAGE\"}"); + return this.success(); + } } diff --git a/src/main/java/com/dreamworks/boditech/driver/Device.java b/src/main/java/com/dreamworks/boditech/driver/Device.java index 1c20ff7..a7dc476 100644 --- a/src/main/java/com/dreamworks/boditech/driver/Device.java +++ b/src/main/java/com/dreamworks/boditech/driver/Device.java @@ -187,6 +187,6 @@ public class Device { public void errorAlert(String code) { WebsocketServerMessageErrorEvent event = new WebsocketServerMessageErrorEvent(); event.code = code; - this.websocketServerService.send(event); +// this.websocketServerService.send(event); } } diff --git a/src/main/java/com/dreamworks/boditech/driver/consumable/CsmTestCardBox.java b/src/main/java/com/dreamworks/boditech/driver/consumable/CsmTestCardBox.java index f78ce6c..bb0bd49 100644 --- a/src/main/java/com/dreamworks/boditech/driver/consumable/CsmTestCardBox.java +++ b/src/main/java/com/dreamworks/boditech/driver/consumable/CsmTestCardBox.java @@ -7,6 +7,8 @@ public class CsmTestCardBox { public Integer projectId; public String projectName; + public String projectColor="red"; + public String lotCode; public Integer cardAmount = 0; diff --git a/src/main/java/com/dreamworks/boditech/driver/consumable/CsmTestCardManager.java b/src/main/java/com/dreamworks/boditech/driver/consumable/CsmTestCardManager.java index 8afe739..bd4c78c 100644 --- a/src/main/java/com/dreamworks/boditech/driver/consumable/CsmTestCardManager.java +++ b/src/main/java/com/dreamworks/boditech/driver/consumable/CsmTestCardManager.java @@ -8,9 +8,6 @@ import java.util.List; public class CsmTestCardManager { // device instance private final Device device; - // @TODO : 删除下面这个属性,使用box代替 - // list of test cards - private final List testCards = new ArrayList<>(); // list of test card boxes private final List testCardBoxes = new ArrayList<>(); @@ -86,8 +83,9 @@ public class CsmTestCardManager { - - + // @TODO : 删除下面这个属性,使用box代替 + // list of test cards + private final List testCards = new ArrayList<>(); @@ -116,33 +114,4 @@ public class CsmTestCardManager { } return null; } - - /** - * append test card by box code - * @param position position of test card in cell - * @param code box code string - */ -// public void appendByBoxCode( Integer position, String code ) { -// // code example : 1||CAGGB66U||2024.03.26||1279||06 -// // code format : projectCodeNum||batchNumber||manufacturingDate||unknown||unknown -// String[] parts = code.split("\\|\\|"); -// if (parts.length != 5) { -// throw new RuntimeException("invalid test card box code"); -// } -// -// // as default, we have 25 test cards in one box -// for ( int i=0; i<25; i++ ) { -// CsmTestCard testCard = new CsmTestCard(this); -// Integer projectCodeNum = Integer.parseInt(parts[0]); -// Project project = this.device.projectService.findByCodeNum(projectCodeNum); -// testCard.projectId = project.id; -// testCard.projectName = project.name; -// testCard.batchNumber = parts[1]; -// testCard.manufacturingDate = parts[2]; -// testCard.position = position; -// this.testCards.add(testCard); -// } -// } - - } diff --git a/src/main/java/com/dreamworks/boditech/service/WebsocketServerService.java b/src/main/java/com/dreamworks/boditech/service/WebsocketServerService.java index 72223eb..753b08d 100644 --- a/src/main/java/com/dreamworks/boditech/service/WebsocketServerService.java +++ b/src/main/java/com/dreamworks/boditech/service/WebsocketServerService.java @@ -1,44 +1,27 @@ package com.dreamworks.boditech.service; -import jakarta.websocket.*; -import jakarta.websocket.server.ServerEndpoint; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.dreamworks.boditech.utils.MyWsServerConnection; +import jakarta.annotation.PostConstruct; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; @Service -@ServerEndpoint("/ws") public class WebsocketServerService { - private static final Logger LOG = LoggerFactory.getLogger(WebsocketServerService.class); + // connections + private List connections; - // only one session is all we need - private Session session; - - @OnOpen - public void onOpen(Session session) { - this.session = session; - LOG.info("ws server : on open"); - } - - @OnClose - public void onClose() { - LOG.info("ws server : on close"); - } - - @OnMessage - public void onMessage(String message) { - LOG.info("ws server : on message > " + message); + @PostConstruct + public void init() { + this.connections = new ArrayList<>(); } - @OnError - public void onError(Session session, Throwable error) { - LOG.info("ws server : on error"); + public void appendConnection(MyWsServerConnection connection) { + this.connections.add(connection); } // send message to client - public void send(Object message) { - try { - this.session.getBasicRemote().sendObject(message); - } catch (Exception e) { - LOG.error("ws server : send error", e); + public void send(String message) { + for (MyWsServerConnection connection : this.connections) { + connection.send(message); } } } diff --git a/src/main/java/com/dreamworks/boditech/utils/MyWsServerConnection.java b/src/main/java/com/dreamworks/boditech/utils/MyWsServerConnection.java new file mode 100644 index 0000000..e4ba241 --- /dev/null +++ b/src/main/java/com/dreamworks/boditech/utils/MyWsServerConnection.java @@ -0,0 +1,50 @@ +package com.dreamworks.boditech.utils; +import com.dreamworks.boditech.service.WebsocketServerService; +import jakarta.websocket.*; +import jakarta.websocket.server.ServerEndpoint; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; +@Component +@ServerEndpoint("/ws") +public class MyWsServerConnection implements ApplicationContextAware { + // spring application context + private static ApplicationContext applicationContext; + // websocket server service + private WebsocketServerService wsServer; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + MyWsServerConnection.applicationContext = applicationContext; + } + + // only one session is all we need + private Session session; + + @OnOpen + public void onOpen(Session session) { + this.session = session; + this.wsServer = MyWsServerConnection.applicationContext.getBean(WebsocketServerService.class); + this.wsServer.appendConnection(this); + } + + @OnClose + public void onClose() { + System.out.println("ws server : on close"); + } + + @OnMessage + public void onMessage(String message) { + System.out.println("ws server : on message"); + } + + @OnError + public void onError(Session session, Throwable error) { + System.out.println("ws server : on error"); + } + + public void send( String text ) { + this.session.getAsyncRemote().sendText(text); + } +}