whyclxw
2025-05-07 45ef9e340f9bb4b98fd88a758343470dfe125cb3
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
package com.whyc.webSocket;
 
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Response;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.Battinf;
import com.whyc.pojo.UserInf;
import com.whyc.service.*;
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.io.IOException;
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;
 
/**
 * 苏州地铁更新版
 * 首页大屏展示 Socket
 */
@Component
@ServerEndpoint(value = "/screen_sz2",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
public class ScreenForSZ2Socket {
 
    private Session session;
 
    private Thread thread;
 
    private static BattalarmDataService battAlarmDataService;
 
 
 
    private static BattInfService battInfService;
 
 
    private static BattRtstateService battRtStateService;
 
 
    @Autowired
    public void setBattAlarmDataService(BattalarmDataService battAlarmDataService) {
        ScreenForSZ2Socket.battAlarmDataService = battAlarmDataService;
    }
 
 
    @Autowired
    public void setBattInfService(BattInfService battInfService) {
        ScreenForSZ2Socket.battInfService = battInfService;
    }
 
 
 
    @Autowired
    public void setBattRtStateService(BattRtstateService battRtStateService) {
        ScreenForSZ2Socket.battRtStateService = battRtStateService;
    }
 
    @OnOpen
    public void onOpen(Session session, EndpointConfig config){
        this.session = session;
        //HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession");
        //UserInf user = (UserInf) httpSession.getAttribute("user");
        //final int userId = user.getUId().intValue();
        final int userId = 1001;
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    while (!currentThread().isInterrupted()) {
                        Map<String, Object> res =getStatic(userId);
                        session.getBasicRemote().sendObject(new Response().set(1, res));
                        sleep(4000);
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = thread;
    }
 
    public Map<String, Object> getStatic(Integer userId) throws InterruptedException {
        Map<String, Object> res = new HashMap<>();
        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(4);
        poolExecutor.execute(()->{
            //1统计机房个数,设备个数,电池组个数
            Response<Map> res_inf = battInfService.getAllInfInSz2(userId);
            res.put("res_inf", res_inf);
            latch.countDown();
        });
        poolExecutor.execute(()->{
            //2.统计电池告警(119001,119002,119003,119004,119005,119006,119007)
            Response res_battAlarm = battAlarmDataService.getAllBattAlarmInSz2(userId);
            res.put("res_battAlarm", res_battAlarm);
            latch.countDown();
        });
        poolExecutor.execute(()->{
            //3.统计:电池状态
            Response<Map> res_battState = battRtStateService.getAllBattStateInSz2(userId);
            res.put("res_battState", res_battState);
            latch.countDown();
        });
        poolExecutor.execute(()->{
            //4.中间地图
            Response<Map> res_station = battInfService.getAllStationInSz2(userId);
            res.put("res_station", res_station);
            latch.countDown();
        });
 
        latch.await(10, TimeUnit.MINUTES);
        return res;
    }
    @OnClose
    public void onClose(CloseReason closeReason) throws IOException {
        //System.err.println("closeReason = " + closeReason);
        if(session.isOpen()){
            session.close();
        }
    }
 
    @OnError
    public void onError(Throwable error) throws IOException {
        //error.printStackTrace();
        thread.isInterrupted();
        if(session.isOpen()){
            session.close();
        }
    }
 
}