whycxzp
2025-05-28 35a1c88cc80ca675f6173648968ab5394b49e31e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.whyc.webSocket;
 
import com.whyc.dto.Response;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
/**
 * packageName com.whyc.websocket
 *
 * @author lxw
 * @version JDK 8
 * @className BattSocket (此处以class为例)
 * @date 2024/6/15
 * @description
 */
@Component
@ServerEndpoint(value = "/batt", encoders = WebSocketEncoder.class)
public class BattSocket {
    private Session session;
 
    private Thread thread;
 
    private volatile boolean runFlag = true;
 
    private volatile Map<String, Thread> threadMap = new HashMap<>();
 
 
    private static final int executeTime = 5000;
 
    private static RtStateService rtStateService;
 
    private static RtDataService rtDataService;
 
    private static BattAlarmService battAlarmService;
 
    private static RtEnvirmentService rtEnvirmentService;
 
    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
 
 
    @Autowired
    public void setRtStateService(RtStateService rtStateService) {
        BattSocket.rtStateService = rtStateService;
    }
 
    @Autowired
    public void setRtDataService(RtDataService rtDataService) {
        BattSocket.rtDataService = rtDataService;
    }
 
    @Autowired
    public void setBattAlarmService(BattAlarmService battAlarmService) {
        BattSocket.battAlarmService = battAlarmService;
    }
 
    @Autowired
    public void setRtEnvirmentService(RtEnvirmentService rtEnvirmentService) {
        BattSocket.rtEnvirmentService = rtEnvirmentService;
    }
 
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
    }
 
    @OnMessage
    public void onMessage(Session session, String message) {
        int binfId=Integer.valueOf(message);
        thread = new Thread("Thread_BattSocket") {
            @Override
            public void run() {
                Map<String, Object> res = new HashMap<>();
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
                        CountDownLatch latch = new CountDownLatch(4);
                        poolExecutor.execute(() -> {
                            Response resRtState = rtStateService.getResRtState(binfId);
                            res.put("resRtState", resRtState);
                            latch.countDown();
                        });
                        poolExecutor.execute(() -> {
                            Response resRtData = rtDataService.getResRtData(binfId);
                            res.put("resRtData", resRtData);
                            latch.countDown();
                        });
                        poolExecutor.execute(() -> {
                            Response resBattAlm = battAlarmService.getResBattAlm(binfId);
                            res.put("resBattAlm", resBattAlm);
                            latch.countDown();
                        });
                        poolExecutor.execute(() -> {
                            Response resRtEnvir = rtEnvirmentService.getResRtEnvir();
                            res.put("resRtEnvir", resRtEnvir);
                            latch.countDown();
                        });
                        latch.await(10, TimeUnit.MINUTES);
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(new Response().set(1, res));
                            }
                            threadFlagMap.put(thread.getId(), false);
                        }
                        sleep(executeTime);
                    } catch (Exception e) {
                        interrupt();
                    }
                }
            }
        };
        thread.start();
        threadFlagMap.put(thread.getId(),true);
        //停止老的socket线程
        Thread threadBefore = threadMap.get(session.getId());
        if(threadBefore !=null && threadBefore.isAlive()){
            while (threadFlagMap.get(threadBefore.getId())){
            }
            threadBefore.interrupt();
        }
        //将线程存储,便于调用定位
        threadMap.put(session.getId(), this.thread);
    }
 
    @OnClose
    public void onClose(CloseReason closeReason){
        System.err.println("closeReason = " + closeReason);
        runFlag = false;
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
 
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
}