whycxzp
2021-10-08 524e0bb84da4506e9e04420a8e55fcb9ced8b55f
更新电池告警webSocket接口
2个文件已修改
3个文件已添加
226 ■■■■■ 已修改文件
pom.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/config/WebSocketConfig.java 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/schedule/AnalysisSchedule.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/websocket/BatteryAlarmWebSocket.java 174 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/websocket/WebSocketEncoder.java 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -147,6 +147,11 @@
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--websocket-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
    </dependencies>
    <build>
src/main/java/com/whyc/config/WebSocketConfig.java
New file
@@ -0,0 +1,15 @@
package com.whyc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}
src/main/java/com/whyc/schedule/AnalysisSchedule.java
@@ -53,7 +53,7 @@
     * 查询所有stop表中所有单体的最后一笔的单体容量,求最小值,最大值,平均值
     * 将结果放置到容量表
     */
    @Scheduled(cron = "0 * * * * ?")
    @Scheduled(cron = "0 0 0 * * ?")
    @Transactional
    public void analysisMonCap(){
        List<AnalysisMonFieldDTO> analysisMonCapList = dataService.monCapStatistics();
src/main/java/com/whyc/websocket/BatteryAlarmWebSocket.java
New file
@@ -0,0 +1,174 @@
package com.whyc.websocket;
import com.whyc.service.BatteryAlarmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
@ServerEndpoint(value = "/batteryAlarm/{method}/{userId}",encoders = WebSocketEncoder.class)
@Component
@Slf4j
public class BatteryAlarmWebSocket {
    private Session session;
    private static BatteryAlarmService service;
    private volatile Thread thread;
    private static final int executeTime = 4000;
    @Autowired
    public void setService(BatteryAlarmService service) {
        BatteryAlarmWebSocket.service = service;
    }
    @OnOpen
    public void onOpen(Session session, @PathParam("method") String method,@PathParam("userId")Integer userId){
        switch (method){
            case "monCapacity":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryAlarm") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getMonCapacityLowAnalysis(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "onlinegroupVolAnalysis":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryAlarm") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getOnlineAndGroupVolAnalysis(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "btsStatus":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryAlarm") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getBTSEquipStatus(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "dischargeAnalysis":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryAlarm") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getDischargeAnalysis(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "chargeAnalysis":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryAlarm") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getChargeAnalysis(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
            case "monVRTAnalysis":{
                if(session!=null) {
                    thread = new Thread("Thread_batteryAlarm") {
                        public void run() {
                            while (!thread.isInterrupted()) {
                                try {
                                    if (session.isOpen()) {
                                        session.getBasicRemote().sendObject(service.getMonVRTAnalysis(userId));
                                    }
                                    sleep(executeTime);
                                } catch (IOException | InterruptedException | EncodeException e) {
                                    interrupt();
                                }
                            }
                        }
                    };
                    thread.start();
                }
            }
            break;
        }
    }
    @OnClose
    public void onClose(){
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        log.warn("webSocket会话关闭了:{}",session);
    }
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
}
src/main/java/com/whyc/websocket/WebSocketEncoder.java
New file
@@ -0,0 +1,30 @@
package com.whyc.websocket;
import com.google.gson.GsonBuilder;
import com.whyc.dto.Response;
import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;
/**
 * 编译器
 */
public class WebSocketEncoder implements Encoder.Text<Response> {
    @Override
    public String encode(Response o) throws EncodeException {
        return new GsonBuilder().create().toJson(o);
    }
    @Override
    public void init(EndpointConfig endpointConfig) {
    }
    @Override
    public void destroy() {
    }
}