lipenghui 1 өдөр өмнө
parent
commit
c64277523c

+ 4 - 1
yanfan-framework/pom.xml

@@ -103,7 +103,10 @@
             <groupId>org.springframework.session</groupId>
             <artifactId>spring-session-data-redis</artifactId>
         </dependency>
-
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-websocket</artifactId>
+        </dependency>
     </dependencies>
 
 </project>

+ 37 - 0
yanfan-framework/src/main/java/com/yanfan/framework/config/StompWebSocketConfig.java

@@ -0,0 +1,37 @@
+package com.yanfan.framework.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.messaging.simp.config.MessageBrokerRegistry;
+import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
+import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
+import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
+
+/**
+ * STOMP WebSocket 配置:支持设备编码动态路径
+ */
+@Configuration
+@EnableWebSocketMessageBroker // 启用 STOMP 消息代理
+public class StompWebSocketConfig implements WebSocketMessageBrokerConfigurer {
+
+    /**
+     * 配置消息代理(订阅主题前缀)
+     */
+    @Override
+    public void configureMessageBroker(MessageBrokerRegistry registry) {
+        // 1. 启用简单消息代理,前端订阅以下前缀的主题可接收推送
+        registry.enableSimpleBroker("/topic"); // 广播主题(按设备编码细分)
+        // 2. 客户端发送消息的前缀:前端发消息需以 /app 开头,映射到 @MessageMapping
+        registry.setApplicationDestinationPrefixes("/app");
+    }
+
+    /**
+     * 注册 STOMP 端点(前端连接的 WS 路径)
+     */
+    @Override
+    public void registerStompEndpoints(StompEndpointRegistry registry) {
+        // 核心:支持动态设备编码路径 {deviceCode},允许跨域,启用 SockJS 降级
+        registry.addEndpoint("/ws/stomp/{deviceCode}")
+                .setAllowedOriginPatterns("*") // 跨域(生产环境建议限定具体域名)
+                .withSockJS(); // 兼容不支持 WebSocket 的浏览器(自动切换 SockJS)
+    }
+}

+ 10 - 2
yanfan-service/yanfan-iot-service/src/main/java/com/yanfan/iot/service/impl/DeviceServiceImpl.java

@@ -59,6 +59,7 @@ import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.cache.annotation.Caching;
 import org.springframework.context.annotation.Lazy;
+import org.springframework.messaging.simp.SimpMessagingTemplate;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -80,7 +81,8 @@ import static com.yanfan.common.utils.SecurityUtils.*;
 @Service
 public class DeviceServiceImpl implements IDeviceService {
     private static final Logger log = LoggerFactory.getLogger(DeviceServiceImpl.class);
-
+    @Autowired
+    private SimpMessagingTemplate messagingTemplate;
     @Autowired
     private DeviceMapper deviceMapper;
     @Autowired
@@ -483,7 +485,13 @@ public class DeviceServiceImpl implements IDeviceService {
                     tdLogDto.setSerialNumber(input.getDeviceNumber());
                     tdLogDto.setList(deviceLogList);
                     log.debug("llllllllllllllll{},mmmmmmmmmmmm{}", JSON.toJSONString(deviceLogList), input.getDeviceNumber());
-                    logService.saveBatch(tdLogDto);
+            Device device1 = deviceMapper.selectDeviceBySerialNumber(input.getDeviceNumber());
+            if ("连续油管".equals(device1.getProductName())){
+                //如果是连油设备发送ws
+                String destination = String.format("/topic/device/%s", input.getDeviceNumber());
+                messagingTemplate.convertAndSend(destination, JSON.toJSONString(deviceLogList));
+            }
+            logService.saveBatch(tdLogDto);
 //                }
             //});
         }