whyclxw
2024-06-15 aa7b92b0e52a1f79f92bffb5d72df849b42243d6
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
package com.whyc.webSocket;
 
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Response;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.RtState;
import com.whyc.pojo.StationInf;
import com.whyc.service.BattInfService;
import com.whyc.service.RtDataService;
import com.whyc.service.RtStateService;
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.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * packageName com.whyc.websocket
 *
 * @author lxw
 * @version JDK 8
 * @className BattSocket (此处以class为例)
 * @date 2024/6/15
 * @description TODO
 */
@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 volatile Map<Long, Boolean> threadFlagMap = new HashMap<>();
 
    private static final int executeTime = 5000;
 
    private static RtStateService rtStateService;
 
    private static RtDataService rtDataService;
 
 
    @Autowired
    public void setRtStateService(RtStateService rtStateService) {
        BattSocket.rtStateService = rtStateService;
    }
 
    @Autowired
    public void setRtDataService(RtDataService rtDataService) {
        BattSocket.rtDataService = rtDataService;
    }
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
    }
 
    @OnMessage
    public void onMessage(Session session, String message) {
        int binfId=Integer.valueOf(message);
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    Map<String, Object> res = new HashMap<>();
                    while (!currentThread().isInterrupted()) {
                        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
                        CountDownLatch latch = new CountDownLatch(2);
                        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();
                        });
                        latch.await(10, TimeUnit.MINUTES);
                        session.getBasicRemote().sendObject(new Response().set(1, res));
                        sleep(executeTime);
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = 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());
    }
}