From ed227d88003e1c9cc08d549bba6d058c7ac1b785 Mon Sep 17 00:00:00 2001
From: Junjie <xjj@123>
Date: 星期一, 25 三月 2024 08:35:34 +0800
Subject: [PATCH] #
---
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/security/SecurityConfig.java | 3
zy-asrs-wcs/pom.xml | 5 +
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/config/WebSocketConfig.java | 22 ++++
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/service/impl/ShuttleServiceImpl.java | 51 ++++++++++
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/service/ShuttleService.java | 11 ++
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/ws/ShuttleWebSocket.java | 170 ++++++++++++++++++++++++++++++++++
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/ws/model/WebSocketMessage.java | 12 ++
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/controller/ShuttleController.java | 22 ---
8 files changed, 277 insertions(+), 19 deletions(-)
diff --git a/zy-asrs-wcs/pom.xml b/zy-asrs-wcs/pom.xml
index 93571b5..ae111f0 100644
--- a/zy-asrs-wcs/pom.xml
+++ b/zy-asrs-wcs/pom.xml
@@ -67,6 +67,11 @@
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-websocket</artifactId>
+ </dependency>
+
</dependencies>
<build>
diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/config/WebSocketConfig.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/config/WebSocketConfig.java
new file mode 100644
index 0000000..2df1e87
--- /dev/null
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/config/WebSocketConfig.java
@@ -0,0 +1,22 @@
+package com.zy.asrs.wcs.common.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.config.annotation.*;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+@Configuration
+@EnableWebSocketMessageBroker
+public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
+
+ @Bean
+ public ServerEndpointExporter serverEndpointExporter() {
+ return new ServerEndpointExporter();
+ }
+
+ @Override
+ public void registerStompEndpoints(StompEndpointRegistry registry) {
+ registry.addEndpoint("/api/socket");
+ }
+}
+
diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/security/SecurityConfig.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/security/SecurityConfig.java
index e25ef31..50cd64c 100644
--- a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/security/SecurityConfig.java
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/security/SecurityConfig.java
@@ -41,7 +41,8 @@
"/webjars/**",
"/v2/api-docs",
"/v3/api-docs",
- "/swagger-ui/**"
+ "/swagger-ui/**",
+ "/ws/**"
};
@Resource
diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/controller/ShuttleController.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/controller/ShuttleController.java
index f7e64ab..a01dbf9 100644
--- a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/controller/ShuttleController.java
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/controller/ShuttleController.java
@@ -9,6 +9,7 @@
import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
import com.zy.asrs.wcs.rcs.service.DeviceService;
import com.zy.asrs.wcs.rcs.service.DeviceTypeService;
+import com.zy.asrs.wcs.rcs.service.ShuttleService;
import com.zy.asrs.wcs.rcs.thread.ShuttleThread;
import com.zy.asrs.wcs.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,27 +26,12 @@
private DeviceService deviceService;
@Autowired
private DeviceTypeService deviceTypeService;
+ @Autowired
+ private ShuttleService shuttleService;
@GetMapping("/shuttle/status/list")
public R getShuttleStatusList() {
- DeviceType deviceType = deviceTypeService.getOne(new LambdaQueryWrapper<DeviceType>()
- .eq(DeviceType::getHostId, getHostId())
- .eq(DeviceType::getStatus, 1)
- .eq(DeviceType::getFlag, String.valueOf(SlaveType.Shuttle)));
- if (deviceType == null) {
- return R.error("璁惧绫诲瀷涓嶅瓨鍦�");
- }
-
- ArrayList<ShuttleProtocol> data = new ArrayList<>();
- List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>()
- .eq(Device::getHostId, getHostId())
- .eq(Device::getStatus, 1)
- .eq(Device::getDeviceType, deviceType.getId()));
- for (Device device : list) {
- ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
- ShuttleProtocol status = shuttleThread.getStatus();
- data.add(status);
- }
+ List<ShuttleProtocol> data = shuttleService.getShuttleStatusList(getHostId());
return R.ok().add(data);
}
diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/service/ShuttleService.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/service/ShuttleService.java
new file mode 100644
index 0000000..6be54ad
--- /dev/null
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/service/ShuttleService.java
@@ -0,0 +1,11 @@
+package com.zy.asrs.wcs.rcs.service;
+
+import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
+
+import java.util.List;
+
+public interface ShuttleService {
+
+ List<ShuttleProtocol> getShuttleStatusList(Long hostId);
+
+}
diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/service/impl/ShuttleServiceImpl.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/service/impl/ShuttleServiceImpl.java
new file mode 100644
index 0000000..1680bb6
--- /dev/null
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/service/impl/ShuttleServiceImpl.java
@@ -0,0 +1,51 @@
+package com.zy.asrs.wcs.rcs.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.zy.asrs.framework.common.R;
+import com.zy.asrs.framework.exception.CoolException;
+import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
+import com.zy.asrs.wcs.rcs.entity.Device;
+import com.zy.asrs.wcs.rcs.entity.DeviceType;
+import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
+import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
+import com.zy.asrs.wcs.rcs.service.DeviceService;
+import com.zy.asrs.wcs.rcs.service.DeviceTypeService;
+import com.zy.asrs.wcs.rcs.service.ShuttleService;
+import com.zy.asrs.wcs.rcs.thread.ShuttleThread;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service("ShuttleService")
+public class ShuttleServiceImpl implements ShuttleService {
+
+ @Autowired
+ private DeviceService deviceService;
+ @Autowired
+ private DeviceTypeService deviceTypeService;
+
+ @Override
+ public List<ShuttleProtocol> getShuttleStatusList(Long hostId) {
+ DeviceType deviceType = deviceTypeService.getOne(new LambdaQueryWrapper<DeviceType>()
+ .eq(DeviceType::getHostId, hostId)
+ .eq(DeviceType::getStatus, 1)
+ .eq(DeviceType::getFlag, String.valueOf(SlaveType.Shuttle)));
+ if (deviceType == null) {
+ throw new CoolException("璁惧绫诲瀷涓嶅瓨鍦�");
+ }
+
+ ArrayList<ShuttleProtocol> data = new ArrayList<>();
+ List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>()
+ .eq(Device::getHostId, hostId)
+ .eq(Device::getStatus, 1)
+ .eq(Device::getDeviceType, deviceType.getId()));
+ for (Device device : list) {
+ ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
+ ShuttleProtocol status = shuttleThread.getStatus();
+ data.add(status);
+ }
+ return data;
+ }
+}
diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/ws/ShuttleWebSocket.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/ws/ShuttleWebSocket.java
new file mode 100644
index 0000000..b17a131
--- /dev/null
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/ws/ShuttleWebSocket.java
@@ -0,0 +1,170 @@
+package com.zy.asrs.wcs.rcs.ws;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.zy.asrs.framework.common.R;
+import com.zy.asrs.framework.common.SpringUtils;
+import com.zy.asrs.wcs.common.config.ConfigProperties;
+import com.zy.asrs.wcs.common.security.JwtSubject;
+import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
+import com.zy.asrs.wcs.rcs.service.ShuttleService;
+import com.zy.asrs.wcs.rcs.ws.model.WebSocketMessage;
+import com.zy.asrs.wcs.utils.JwtUtil;
+import io.jsonwebtoken.Claims;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+
+import javax.websocket.*;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArraySet;
+
+@Component
+@Slf4j
+@Service
+@ServerEndpoint("/ws/shuttle/websocket")
+@Data
+public class ShuttleWebSocket {
+
+ //瀹㈡埛绔湪绾夸汉鏁�
+ private static int onlineClient = 0;
+
+ //瀹㈡埛绔睜
+ private static CopyOnWriteArraySet<ShuttleWebSocket> webSocketServers = new CopyOnWriteArraySet<>();
+
+ private Session session;
+
+ private String username;
+
+ private Long hostId;
+
+ //绠¢亾ID
+ private String sessionId;
+
+ @OnOpen
+ public void onOpen(Session session) {
+ this.session = session;
+ this.sessionId = session.getId();
+
+ //灏唚ebsocket瀵硅薄杩涜淇濆瓨
+ webSocketServers.add(this);
+ //娣诲姞鍦ㄧ嚎浜烘暟
+ addOnlineClient();
+ log.info("鏈夋柊绐楀彛寮�濮嬬洃鍚�:" + session.getId() + ",褰撳墠鍦ㄧ嚎浜烘暟涓�:" + getOnlineClient());
+ }
+
+ /**
+ * 杩炴帴鍏抽棴璋冪敤鐨勬柟娉�
+ */
+ @OnClose
+ public void onClose() {
+ webSocketServers.remove(this); //浠巗et涓垹闄�
+ subOnlineClient(); //鍦ㄧ嚎鏁板噺1
+ log.info("鍏抽棴鐨勮繛鎺ワ細" + sessionId);
+ log.info("鏈変竴杩炴帴鍏抽棴锛佸綋鍓嶅湪绾夸汉鏁颁负" + getOnlineClient());
+ }
+
+ /**
+ * 鏀跺埌瀹㈡埛绔秷鎭悗璋冪敤鐨勬柟娉�
+ * @ Param message 瀹㈡埛绔彂閫佽繃鏉ョ殑娑堟伅
+ */
+ @OnMessage
+ public void onMessage(String message, Session session) throws IOException {
+ ShuttleService shuttleService = SpringUtils.getBean(ShuttleService.class);
+ WebSocketMessage socketMessage = JSON.parseObject(message, WebSocketMessage.class);
+ if (socketMessage.getUrl().equals("login")) {
+ try {
+ // 瑙f瀽token
+ ConfigProperties configProperties = SpringUtils.getBean(ConfigProperties.class);
+ JSONObject data = JSON.parseObject(socketMessage.getData());
+ Claims claims = JwtUtil.parseToken(data.getString("token"), configProperties.getTokenKey());
+ JwtSubject jwtSubject = JwtUtil.getJwtSubject(claims);
+ this.username = jwtSubject.getUsername();
+ this.hostId = jwtSubject.getHostId();
+ socketMessage.setData(JSON.toJSONString(R.ok("auth success")));
+ } catch (Exception e) {
+ e.printStackTrace();
+ socketMessage.setData(JSON.toJSONString(R.error("auth fail")));
+ }
+ this.sendMessage(JSON.toJSONString(socketMessage));
+ } else if (socketMessage.getUrl().equals("/shuttle/status/list")) {
+ if (this.hostId != null) {
+ List<ShuttleProtocol> data = shuttleService.getShuttleStatusList(this.hostId);
+ socketMessage.setData(JSON.toJSONString(data));
+ this.sendMessage(JSON.toJSONString(socketMessage));
+ }
+ }
+// log.info("鏀跺埌鏉ヨ嚜杩炴帴锛�" + sessionId + "鐨勪俊鎭�:" + message);
+ }
+
+ /**
+ * @ Param session
+ * @ Param error
+ */
+ @OnError
+ public void onError(Session session, Throwable error) {
+ log.error("鍙戠敓閿欒");
+ error.printStackTrace();
+ }
+
+ /**
+ * 瀹炵幇鏈嶅姟鍣ㄤ富鍔ㄦ帹閫�
+ */
+ public void sendMessage(String message) throws IOException {
+ this.session.getBasicRemote().sendText(message);
+ }
+
+ /**
+ * 鏈嶅姟鍣ㄤ富鍔ㄦ帹閫佺粰鎸囧畾鐢ㄦ埛
+ */
+ public void sendMessage(String message, String account) throws IOException {
+ for (ShuttleWebSocket item : webSocketServers) {
+ item.sendMessage(message);
+ }
+ }
+
+ public void sendMessage(String message, int userId) throws IOException {
+ for (ShuttleWebSocket item : webSocketServers) {
+ item.sendMessage(message);
+ }
+ }
+
+ /**
+ * 鏈嶅姟鍣ㄤ富鍔ㄦ帹閫佺粰鎸囧畾鐢ㄦ埛
+ */
+ public static boolean sendMessageGlobal(String message, String account) throws IOException {
+ boolean tag = false;
+ for (ShuttleWebSocket item : webSocketServers) {
+ tag = true;
+ item.sendMessage(message);
+ }
+ return tag;
+ }
+
+ public static boolean sendMessageGlobal(String message, int userId) throws IOException {
+ boolean tag = false;
+ for (ShuttleWebSocket item : webSocketServers) {
+ tag = true;
+ item.sendMessage(message);
+ }
+ return tag;
+ }
+
+ public static synchronized int getOnlineClient() {
+ return onlineClient;
+ }
+
+ public static synchronized void addOnlineClient() {
+ ShuttleWebSocket.onlineClient++;
+ }
+
+ public static synchronized void subOnlineClient() {
+ if (ShuttleWebSocket.onlineClient > 0) {
+ ShuttleWebSocket.onlineClient--;
+ }
+ }
+
+}
diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/ws/model/WebSocketMessage.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/ws/model/WebSocketMessage.java
new file mode 100644
index 0000000..2336c76
--- /dev/null
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/ws/model/WebSocketMessage.java
@@ -0,0 +1,12 @@
+package com.zy.asrs.wcs.rcs.ws.model;
+
+import lombok.Data;
+
+@Data
+public class WebSocketMessage {
+
+ private String url;
+
+ private String data;
+
+}
--
Gitblit v1.9.1