lxw
2023-04-24 bb6f09aeedfd58b871ad0afa08392056da39e77f
src/main/java/com/whyc/webSocket/SendAlarmDataSocket.java
@@ -1,5 +1,6 @@
package com.whyc.webSocket;
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Response;
import com.whyc.service.BattalarmDataService;
import com.whyc.service.DevalarmDataService;
@@ -7,13 +8,14 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpSession;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.HashMap;
import java.util.Map;
@Component
@ServerEndpoint(value = "/sendAlarmData",encoders = WebSocketEncoder.class)
@ServerEndpoint(value = "/sendAlarmData", encoders = WebSocketEncoder.class, configurator = WebSocketConfig.class)
public class SendAlarmDataSocket {
    private volatile Thread thread;
@@ -23,7 +25,7 @@
    private volatile Map<String,Thread> threadMap = new HashMap<>();
    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
    private volatile Map<Long, Boolean> threadFlagMap = new HashMap<>();
    private static BattalarmDataService battalarmDataService;
@@ -32,51 +34,60 @@
    private static PwrdevAlarmService pwrdevAlarmService;
    private Session session;
    private HttpSession httpSession;
    @Autowired
    public void setService(BattalarmDataService service){
    public void setService(BattalarmDataService service) {
        SendAlarmDataSocket.battalarmDataService = service;
    }
    @Autowired
    public void setService(DevalarmDataService service){
    public void setService(DevalarmDataService service) {
        SendAlarmDataSocket.devalarmDataService = service;
    }
    @Autowired
    public void setService(PwrdevAlarmService service){
    public void setService(PwrdevAlarmService service) {
        SendAlarmDataSocket.pwrdevAlarmService = service;
    }
    @OnOpen
    public void onOpen(Session session){
        this.session=session;
    public void onOpen(Session session, EndpointConfig config) {
        this.session = session;
        this.httpSession = (HttpSession) config.getUserProperties().get("httpSession");
    }
    @OnMessage
    public synchronized void onMessage(Session session, String uId){
    public synchronized void onMessage(Session session, String uId) {
        int userId = Integer.parseInt(uId);
        String lang = (String) this.httpSession.getAttribute("lang");
        thread = new Thread("Thread_sendAlarmData") {
            @Override
            public void run() {
                while (runFlag&&!isInterrupted()) {
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(),true);
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        Map<String, Response> res=new HashMap<>();
                        Map<String, Response> res = new HashMap<>();
                        //电池告警
                        Response battAlarm=battalarmDataService.getSendBattAlarmOfWebSocket(userId);
                        res.put("battAlarm",battAlarm);
                        Response battAlarm = battalarmDataService.getSendBattAlarmOfWebSocket(userId, lang);
                        res.put("battAlarm", battAlarm);
                        //设备告警
                        Response devAlarm=devalarmDataService.getSendDevAlarmOfWebSocket(userId);
                        res.put("devAlarm",devAlarm);
                        Response devAlarm = devalarmDataService.getSendDevAlarmOfWebSocket(userId);
                        res.put("devAlarm", devAlarm);
                        //电源告警
                        Response pwrAlarm = pwrdevAlarmService.getSendPwrAlarmOfWebSocket(userId);
                        res.put("pwrAlarm",pwrAlarm);
                        res.put("pwrAlarm", pwrAlarm);
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(new Response().set(1, res));
                            }
                            threadFlagMap.put(thread.getId(),false);
                            threadFlagMap.put(thread.getId(), false);
                        }
                        sleep(executeTime);
                    //} catch (IOException | InterruptedException | EncodeException e) {
                        //} catch (IOException | InterruptedException | EncodeException e) {
                    } catch (Exception e) {
                        interrupt();
                    }