whycxzp
2025-03-06 dfd45b0998e4b9279e8ed8a33fbf82fc4e8b6015
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.whyc.webSocket;
 
import com.whyc.dto.Response;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.Battinf;
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.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 = "/screenJy",encoders = WebSocketEncoder.class)
public class ScreenJYSocket {
 
    private Session session;
 
    private Thread thread;
    private static PwrdevAlarmService powerAlarmService;
 
    private static BattalarmDataService battAlarmDataService;
 
    private static Fbs9100StateService fbs9100StateService;
 
    private static DevalarmDataService devAlarmDataService;
 
    private static BattInfService battInfService;
 
    private static BattEnduranceService battEnduranceService;
 
    private static BaoJiGroupService baoJiGroupService;
 
    @Autowired
    public void setPowerAlarmService(PwrdevAlarmService powerAlarmService) {
        ScreenJYSocket.powerAlarmService = powerAlarmService;
    }
    @Autowired
    public void setBattAlarmDataService(BattalarmDataService battAlarmDataService) {
        ScreenJYSocket.battAlarmDataService = battAlarmDataService;
    }
    @Autowired
    public void setFbs9100StateService(Fbs9100StateService fbs9100StateService) {
        ScreenJYSocket.fbs9100StateService = fbs9100StateService;
    }
    @Autowired
    public void setDevAlarmDataService(DevalarmDataService devAlarmDataService) {
        ScreenJYSocket.devAlarmDataService = devAlarmDataService;
    }
    @Autowired
    public void setBattInfService(BattInfService battInfService) {
        ScreenJYSocket.battInfService = battInfService;
    }
 
    @Autowired
    public void setBattEnduranceService(BattEnduranceService battEnduranceService) {
        ScreenJYSocket.battEnduranceService = battEnduranceService;
    }
    @Autowired
    public void setBaoJiGroupService(BaoJiGroupService baoJiGroupService) {
        ScreenJYSocket.baoJiGroupService = baoJiGroupService;
    }
 
    @OnOpen
    public void onOpen(Session session){
        this.session = session;
        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(9);
                        poolExecutor.execute(()->{
                            //一体   告警:交流ABC
                            //Response res_acABC = powerAlarmService.getAcABCAnalysis(userId);
                            Response res_acABC = powerAlarmService.getAcABCAnalysisJY();
                            res.put("acABC", res_acABC);
                            latch.countDown();
                        });
                        poolExecutor.execute(()->{
                            //一体   组端容量
                            //Response res_groupCap = battEnduranceService.getGroupCapList(userId);
                            Response res_groupCap = battEnduranceService.getGroupCapListJY();
                            res.put("groupCap", res_groupCap);
                            latch.countDown();
                        });
                        poolExecutor.execute(()->{
                            //一体   告警:单体温度内阻温度
                            //Response res_monVRTAnalysis = battAlarmDataService.getMonVRTAnalysis(userId);
                            Response res_monVRTAnalysis = battAlarmDataService.getMonVRTAnalysisJY();
                            res.put("monVRTAnalysis", res_monVRTAnalysis);
                            latch.countDown();
                        });
                        poolExecutor.execute(()->{
                            //一体   统计:三种设备类型 告警数统计
                            Response res_devAlarmNum = new Response();
                            Map<String, Integer> devAlarmNumMap = new HashMap<>();
                            try {
                                //int battAlarmNum = battAlarmDataService.getRealTimeWithLevel1(userId).getCode();
                                //int devAlarmNum = devAlarmDataService.getRealTimeWithLevel1(userId);
                                //int powerAlarmNum = powerAlarmService.getRealTimeWithLevel1(userId);
 
                                int battAlarmNum = battAlarmDataService.getRealTimeWithLevel1JY().getCode();
                                int devAlarmNum = devAlarmDataService.getRealTimeWithLevel1JY();
                                int powerAlarmNum = powerAlarmService.getRealTimeWithLevel1JY();
                                devAlarmNumMap.put("battAlarmNum",battAlarmNum);
                                devAlarmNumMap.put("devAlarmNum",devAlarmNum);
                                devAlarmNumMap.put("powerAlarmNum",powerAlarmNum);
                                res_devAlarmNum.setII(1,true,devAlarmNumMap,null);
                            }catch (Exception e){
                                res_devAlarmNum.set(1,false,"发生异常:"+e.getCause());
                            }
                            res.put("devAlarmNum", res_devAlarmNum);
                            latch.countDown();
                        });
                        poolExecutor.execute(()->{
                            //一体   统计:电池统计 分为品牌和标称电压
                            Response res_battGroupInfo = new Response();
                            try {
                                //List<Battinf> battinfList = battInfService.getBattGroupList(userId);
                                List<Battinf> battinfList = battInfService.getBattGroupListJY();
                                Map<String, List<Battinf>> battProducerMap = battinfList.stream().collect(Collectors.groupingBy(Battinf::getBattProducer));
                                Map<Float, List<Battinf>> battMonVolStdMap = battinfList.stream().collect(Collectors.groupingBy(Battinf::getMonVolStd));
                                Map<String, Object> battGroupProducerMap = new HashMap<>();
                                Map<String, Object> battGroupMonVolMap = new HashMap<>();
                                Map<String, Object> battGroupInfoMap = new HashMap<>();
                                int battSize = battinfList.size();
                                for (String producer : battProducerMap.keySet()) {
                                    battGroupProducerMap.put(producer, battProducerMap.get(producer).size());
                                }
                                for (Float monVolStd : battMonVolStdMap.keySet()) {
                                    battGroupMonVolMap.put(Float.toString(monVolStd), battMonVolStdMap.get(monVolStd).size());
                                }
                                battGroupInfoMap.put("producer",battGroupProducerMap);
                                battGroupInfoMap.put("monVol",battGroupMonVolMap);
                                battGroupInfoMap.put("totalNum",battSize);
                                res_battGroupInfo.setII(1,true,battGroupInfoMap,null);
                            }catch (Exception e){
                                res_battGroupInfo.set(1,false,"发生异常:"+e.getCause());
                            }
                            res.put("battGroupInfo", res_battGroupInfo);
                            latch.countDown();
                        });
                        poolExecutor.execute(()->{
                            //一体   统计:设备状态
                            //Response<Map> res_devStates = fbs9100StateService.getBTSEquipStatus2(userId,1);
                            Response<Map> res_devStates = fbs9100StateService.getBTSEquipStatus2JY(1);
                            res.put("devStates", res_devStates);
                            latch.countDown();
                        });
                        poolExecutor.execute(()->{
                            //一体   统计:各种设备类型总数 电池 设备 电源 站点
                            //Response<Map> res_devCountMap = battInfService.getDevCountMap(userId);
                            Response<Map> res_devCountMap = battInfService.getDevCountMapJY();
                            res.put("devCountMap", res_devCountMap);
                            latch.countDown();
                        });
                        poolExecutor.execute(()->{
                            //Response res_endurance = battEnduranceService.getEnduranceTimeLong(userId,4);
                            Response res_endurance = battEnduranceService.getEnduranceTimeLongJY(4);
                            res.put("enduranceLow", res_endurance);
                            latch.countDown();
                        });
                        poolExecutor.execute(()->{
                            //Response res_groupInfo = baoJiGroupService.getGroupWithStationAndAlarmForScreen(userId);
                            Response res_groupInfo = baoJiGroupService.getGroupWithStationAndAlarmForScreenJY();
                            res.put("res_groupInfo", res_groupInfo);
                            latch.countDown();
                        });
 
                        latch.await(10, TimeUnit.MINUTES);
                        session.getBasicRemote().sendObject(new Response().set(1, res));
                        sleep(4000);
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = thread;
    }
 
    @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();
        }
    }
 
}