Browse Source

websocket和建筑列表调整

hanzhengyi 1 year ago
parent
commit
ffaaa69157

+ 6 - 1
service-iot/service-iot-biz/pom.xml

@@ -56,7 +56,12 @@
             <artifactId>spring-integration-mqtt</artifactId>
             <version>5.5.5</version>
         </dependency>
-
+        <!--websocket依赖-->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-websocket</artifactId>
+            <version>5.2.8.RELEASE</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 3 - 2
service-iot/service-iot-biz/src/main/java/com/usky/iot/controller/web/BaseBuildController.java

@@ -64,11 +64,12 @@ public class BaseBuildController {
 
     /**
      * 列表
+     * @param id
      * @return
      */
     @GetMapping("dataList")
-    ApiResult<List<BaseBuild>> dataList(){
-        return ApiResult.success(baseBuildService.dataList());
+    ApiResult<List<BaseBuild>> dataList(@RequestParam(value = "id",required = false) Integer id){
+        return ApiResult.success(baseBuildService.dataList(id));
     }
 
     /**

+ 1 - 1
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/BaseBuildService.java

@@ -27,7 +27,7 @@ public interface BaseBuildService extends CrudService<BaseBuild> {
 
     void remove(Integer id);
 
-    List<BaseBuild> dataList();
+    List<BaseBuild> dataList(Integer id);
 
     CommonPage<BuildFacilityRelateResponeVO> buildFacilityRelateList(BuildFacilityRelateRequestVO requestVO);
 

+ 66 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/config/websocket/WebSocket.java

@@ -0,0 +1,66 @@
+package com.usky.iot.service.config.websocket;
+
+import cn.hutool.json.JSONUtil;
+import org.springframework.stereotype.Component;
+
+import javax.websocket.OnClose;
+import javax.websocket.OnMessage;
+import javax.websocket.OnOpen;
+import javax.websocket.Session;
+import javax.websocket.server.PathParam;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+
+@ServerEndpoint(value = "/websocket/{userId}")
+@Component
+public class WebSocket {
+    private static ConcurrentHashMap<String, WebSocket> webSocketMap = new ConcurrentHashMap<>();
+    //实例一个session,这个session是websocket的session
+    private Session session;
+
+    //新增一个方法用于主动向客户端发送消息
+    public void sendMessage(Object message, String userId) {
+        WebSocket webSocket = webSocketMap.get(userId);
+        if (webSocket != null) {
+            try {
+                webSocket.session.getBasicRemote().sendText(JSONUtil.toJsonStr(message));
+                System.out.println("【websocket消息】发送消息成功,用户=" + userId + ",消息内容" + message.toString());
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public static ConcurrentHashMap<String, WebSocket> getWebSocketMap() {
+        return webSocketMap;
+    }
+
+    public static void setWebSocketMap(ConcurrentHashMap<String, WebSocket> webSocketMap) {
+        WebSocket.webSocketMap = webSocketMap;
+    }
+
+    //前端请求时一个websocket时
+    @OnOpen
+    public void onOpen(Session session, @PathParam("userId") String userId) {
+        this.session = session;
+        webSocketMap.put(userId, this);
+        sendMessage("CONNECT_SUCCESS", userId);
+        System.out.println("【websocket消息】有新的连接,连接id" + userId);
+    }
+
+    //前端关闭时一个websocket时
+    @OnClose
+    public void onClose(@PathParam("userId") String userId) {
+        webSocketMap.remove(userId);
+        System.out.println("【websocket消息】连接断开,总数:" + webSocketMap.size());
+    }
+
+    //前端向后端发送消息
+    @OnMessage
+    public void onMessage(String message) {
+        if (!message.equals("ping")) {
+            System.out.println("【websocket消息】收到客户端发来的消息:" + message);
+        }
+    }
+}

+ 16 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/config/websocket/WebSocketConfig.java

@@ -0,0 +1,16 @@
+package com.usky.iot.service.config.websocket;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+//开启WebSocket的支持,并把该类注入到spring容器中
+@Configuration
+public class WebSocketConfig {
+
+    @Bean
+    public ServerEndpointExporter serverEndpointExporter() {
+        return new ServerEndpointExporter();
+    }
+
+}

+ 7 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/BaseAlarmServiceImpl.java

@@ -13,12 +13,14 @@ import com.usky.iot.service.BaseAlarmService;
 import com.usky.common.mybatis.core.AbstractCrudService;
 import com.usky.iot.service.DmpDeviceInfoService;
 import com.usky.iot.service.DmpProductInfoService;
+import com.usky.iot.service.config.websocket.WebSocket;
 import com.usky.iot.service.vo.BaseAlarmListVO;
 import com.usky.iot.service.vo.BaseAlarmRequestVO;
 import com.usky.iot.service.vo.BaseAlarmResponeVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
 import java.time.LocalDateTime;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -40,6 +42,9 @@ public class BaseAlarmServiceImpl extends AbstractCrudService<BaseAlarmMapper, B
     @Autowired
     private DmpDeviceInfoService dmpDeviceInfoService;
 
+    @Autowired
+    private WebSocket webSocket;
+
     @Override
     public CommonPage<BaseAlarmResponeVO> statistic(BaseAlarmRequestVO baseAlarmRequestVO){
 
@@ -214,6 +219,7 @@ public class BaseAlarmServiceImpl extends AbstractCrudService<BaseAlarmMapper, B
     @Override
     public boolean add(BaseAlarm baseAlarm) {
         baseAlarm.setHandleStatus(0);
+
         LambdaQueryWrapper<DmpProductInfo> lambdaQuery = Wrappers.lambdaQuery();
         lambdaQuery.eq(StringUtils.isNotBlank(baseAlarm.getProductCode()),DmpProductInfo::getProductCode,baseAlarm.getProductCode())
                 .eq(DmpProductInfo::getDeleteFlag,0);
@@ -229,6 +235,7 @@ public class BaseAlarmServiceImpl extends AbstractCrudService<BaseAlarmMapper, B
                         .eq(BaseAlarm::getAlarmGrade,baseAlarm.getAlarmGrade());
                 return this.update(baseAlarm,queryWrapper);
             }else {
+                webSocket.sendMessage(baseAlarm,"123");
                 return this.save(baseAlarm);
             }
         }else {

+ 4 - 2
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/BaseBuildServiceImpl.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -93,10 +94,11 @@ public class BaseBuildServiceImpl extends AbstractCrudService<BaseBuildMapper, B
     }
 
     @Override
-    public List<BaseBuild> dataList(){
+    public List<BaseBuild> dataList(Integer id){
         LambdaQueryWrapper<BaseBuild> queryWrapper = Wrappers.lambdaQuery();
         queryWrapper.eq(BaseBuild::getDeleteFlag,0)
-            .eq(BaseBuild::getTenantId,SecurityUtils.getTenantId());
+                .eq(!ObjectUtils.isEmpty(id),BaseBuild::getId,id)
+                .eq(BaseBuild::getTenantId,SecurityUtils.getTenantId());
         List<BaseBuild> list = this.list(queryWrapper);
         if(CollectionUtils.isNotEmpty(list)){
             for(int i=0;i<list.size();i++){