whyclxw
2025-06-04 4288e68cced096a216bebfcd797e78791e192c67
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package com.whyc.webSocket;
 
import com.whyc.config.WebSocketConfig;
import com.whyc.constant.DevStateEnum;
import com.whyc.dto.Real.TopDto;
import com.whyc.dto.RealTimeDto;
import com.whyc.dto.Response;
import com.whyc.factory.BattCapFactory;
import com.whyc.pojo.db_batt_testdata.BattresdataInf;
import com.whyc.pojo.db_batt_testdata.BatttestdataInf;
import com.whyc.pojo.db_ram_db.BattRtdata;
import com.whyc.pojo.db_ram_db.BattRtstate;
import com.whyc.pojo.db_ram_db.DeviceState;
import com.whyc.pojo.db_ram_db.PwrdevAcdcdata;
import com.whyc.pojo.db_station.BattInf;
import com.whyc.pojo.db_station.PowerInf;
import com.whyc.pojo.db_station.StationInf;
import com.whyc.pojo.db_user.User;
import com.whyc.service.*;
import com.whyc.util.ActionUtil;
import com.whyc.util.ThreadLocalUtil;
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;
 
@Component
@ServerEndpoint(value = "/real",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
public class RealTimeSocket {
    private Session session;
 
    private Thread thread;
    //实时数据
    private static BattRtstateService battRtstateService;
 
    private static DeviceStateService  deviceStateService;
 
    private static PwrdevAcdcdataService  pwrdevAcdcdataService;
 
    //实时告警
    private static BattalarmDataService  battalarmDataService;
 
    private static DevalarmDataService devalarmDataService;
 
    private static PwrdevAlarmService  pwrdevAlarmService;
 
    //放电记录
    private static BatttestdataInfService batttestdataInfService;
 
    //电池组信息
    private static BattInfService battInfService;
 
    //机房信息
    private static StationInfService stationInfService;
 
    //电源信息
    private static PowerInfService powerInfService;
 
    //单体信息
    private static BattRtdataService rtdataService;
 
    //内阻测试信息
    private static BattresdataInfService battresdataInfService;
 
 
    private volatile boolean runFlag = true;
 
    private static HttpSession httpSession;
 
    private volatile Map<String, Thread> threadMap = new HashMap<>();
 
    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
 
    @Autowired
    public void setBattRtstateService(BattRtstateService battRtstateService) {
        RealTimeSocket.battRtstateService = battRtstateService;
    }
 
    @Autowired
    public void setDeviceStateService(DeviceStateService deviceStateService) {
        RealTimeSocket.deviceStateService = deviceStateService;
    }
 
    @Autowired
    public void setPwrdevAcdcdataService(PwrdevAcdcdataService pwrdevAcdcdataService) {
        RealTimeSocket.pwrdevAcdcdataService = pwrdevAcdcdataService;
    }
 
    @Autowired
    public void setBattalarmDataService(BattalarmDataService battalarmDataService) {
        RealTimeSocket.battalarmDataService = battalarmDataService;
    }
 
    @Autowired
    public void setDevalarmDataService(DevalarmDataService devalarmDataService) {
        RealTimeSocket.devalarmDataService = devalarmDataService;
    }
 
    @Autowired
    public void setPwrdevAlarmService(PwrdevAlarmService pwrdevAlarmService) {
        RealTimeSocket.pwrdevAlarmService = pwrdevAlarmService;
    }
 
    @Autowired
    public void setBatttestdataInfService(BatttestdataInfService batttestdataInfService) {
        RealTimeSocket.batttestdataInfService = batttestdataInfService;
    }
 
    @Autowired
    public void setBattInfService(BattInfService battInfService) {
        RealTimeSocket.battInfService = battInfService;
    }
 
    @Autowired
    public void setStationInfService(StationInfService stationInfService) {
        RealTimeSocket.stationInfService = stationInfService;
    }
 
    @Autowired
    public void setPowerInfService(PowerInfService powerInfService) {
        RealTimeSocket.powerInfService = powerInfService;
    }
 
    @Autowired
    public void setBattRtdataService(BattRtdataService rtdataService) {
        RealTimeSocket.rtdataService = rtdataService;
    }
 
    @Autowired
    public void setBattresdataInfService(BattresdataInfService battresdataInfService) {
        RealTimeSocket.battresdataInfService = battresdataInfService;
    }
 
 
 
    @OnOpen
    public void onOpen(Session session, EndpointConfig config) {
        this.session = session;
        this.httpSession = (HttpSession) config.getUserProperties().get("httpSession");
    }
    @OnMessage
    public void onMessage(Session session, String message) {
        RealTimeDto realDto= ActionUtil.getGson().fromJson(message,RealTimeDto.class);
        if(httpSession!=null){
            User user = (User) httpSession.getAttribute("user");
            realDto.setUid(user.getId());
        }else{
            realDto.setUid(99);
        }
        thread = new Thread("Thread_realTimeSocket") {
            @Override
            public void run() {
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        Map<String, Object> map=new HashMap<>();
                        //获取头部信息
                        Response topRes=getTop(realDto);
                        map.put("topRes",topRes);
                        //根据pageType获取返回界面
                        Response realRes=getRes(realDto);
                        map.put("realRes",realRes);
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(new Response<>().setII(1,true,map,"实时页面数据推送"));
                            }
                            threadFlagMap.put(thread.getId(), false);
                        }
                        sleep(4000);
                    } 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);
    }
    //获取头部信息
    private Response getTop(RealTimeDto realDto) {
        TopDto topDto = new TopDto();
        try {
            //实时获取电池组信息
            BattRtstate battRtstate = battRtstateService.getBattRealInfo(realDto.getBattgroupId());
            if (battRtstate != null) {
                topDto.setBattState(battRtstate.getBattState());
                topDto.setVbusVol(battRtstate.getVbusVol());
                topDto.setOnlineVol(battRtstate.getOnlineVol());
                topDto.setRecordtime(ThreadLocalUtil.format(battRtstate.getRecDatetime(),1));
            }
            //实时获取电池组信息
            BattInf binf=battInfService.getBinfByBattgroupId(realDto.getBattgroupId());
            if(binf!=null){
                topDto.setBattGroupName(binf.getBattgroupName());
                topDto.setDevName(binf.getDevName());
            }
            //获取机房信息
            StationInf stationInf = stationInfService.getStationInfById(binf.getStationId());
            if(stationInf!=null){
                topDto.setStationName(stationInf.getStationName());
                topDto.setProvice(stationInf.getProvice());
                topDto.setCity(stationInf.getCity());
                topDto.setCounty(stationInf.getCountry());
                topDto.setFullName(stationInf.getFullName());
            }
            //获取电源信息
            PowerInf powerInf = powerInfService.getPowerInfById(binf.getPowerId());
            if(powerInf!=null){
                topDto.setPowerName(powerInf.getPowerName());
            }
            //实时获取设备信息
            DeviceState deviceState = deviceStateService.getDevRealInfo(realDto.getDevId());
            if (deviceState != null) {
                topDto.setDevState(deviceState.getDevWorkstate());
                if (deviceState.getDevWorkstate() == DevStateEnum.DEVSTATE_2.getStateId()) {//核容放电
                    topDto.setCaptestGroupvol(deviceState.getDevCaptestGroupvol());
                    topDto.setCaptestCurr(deviceState.getDevCaptestCurr());
                    topDto.setCaptestTimelong(deviceState.getDevCaptestTimelong());
                    topDto.setCaptestCap(deviceState.getDevCaptestCap());
                    //剩余容量和剩余时间计算
                    Float restCap = batttestdataInfService.getLastTestDataRestCap(realDto.getBattgroupId());
                    topDto.setRestCap(restCap);
                    //实时组端电流,剩余容量,标称容量
                    if(battRtstate!=null){
                        Float restTime= BattCapFactory.getTheoryTime(battRtstate.getGroupCurr(), restCap, binf.getMoncapstd());
                        topDto.setRestTime(restTime);
                    }
                }
 
                //获取一级未处理实时告警数
                Integer battALmNum = battalarmDataService.getBattAlmNum(realDto.getBattgroupId());
                topDto.setBattALmNum(battALmNum);
 
                Integer devALmNum = devalarmDataService.getDevAlmNum(realDto.getDevId());
                topDto.setDevALmNum(devALmNum);
 
                Integer pwrALmNum = pwrdevAlarmService.getPwrAlmNum(realDto.getPowerId());
                topDto.setPwrALmNum(pwrALmNum);
 
                //实时获取电源信息(是否存在电源告警信息)
                //PwrdevAcdcdata pwrdevAcdcdata = pwrdevAcdcdataService.getPwrRealInfo(realDto.getPowerId());
                topDto.setPwrState((pwrALmNum > 0 ? 1 : 0));
 
                Integer allALmNum = battALmNum + devALmNum + pwrALmNum;
                topDto.setAllALmNum(allALmNum);
                topDto.setSystemState((allALmNum > 0 ? 1 : 0));
            }
            return new Response().setII(1, true, topDto, "实时页面头部数据推送");
        } catch (Exception e) {
            return new Response().set(1, false, "实时页面头部数据推送");
        }
    }
 
    //根据pageType获取返回界面
    private Response getRes(RealTimeDto realDto) {
        Response res=new Response();
        switch (realDto.getPageType()){
            case "system":res=getSystemPage(realDto);break;
            case "power":res=getPowerPage(realDto);break;
            case "vol":res=getVolPage(realDto);break;
            case "res":res=getResPage(realDto);break;
            case "tmp":res=getTmpPage(realDto);break;
            case "img":res=getImgPage(realDto);break;
            case "3D":res=get3DPage(realDto);break;
            case "self":res=getSelfPage(realDto);break;
            case "manage":res=getManagePage(realDto);break;
            default:res=new Response().set(1);
        }
        return res;
    }
    //获取管理界面信息
    private Response getManagePage(RealTimeDto realDto) {
        Map<String, Object> map=new HashMap<>();
        try {
            StationInf sinf=stationInfService.getStationInfById(realDto.getStaitonId());
            map.put("sinf",sinf);
            PowerInf pinf=powerInfService.getPowerInfById(realDto.getPowerId());
            map.put("pinf",pinf);
            BattInf binf=battInfService.getBinfByBattgroupId(realDto.getBattgroupId());
            map.put("binf",binf);
            map.put("pageType",realDto.getPageType());
            return new Response().setII(1,true,map,"管理界面信息推送");
        }catch (Exception e){
            return new Response().set(1,false,"管理界面信息推送");
        }
    }
    //获取自愈能力界面信息
    private Response getSelfPage(RealTimeDto realDto) {
        Map<String, Object> map=new HashMap<>();
        map.put("pageType",realDto.getPageType());
        return new Response().setII(1,true,map,"自愈能力界面信息推送");
    }
    //获取3D界面信息
    private Response get3DPage(RealTimeDto realDto) {
        Map<String, Object> map=new HashMap<>();
        map.put("pageType",realDto.getPageType());
        return new Response().setII(1,true,map,"3D界面信息信息推送");
    }
    //获取图片界面信息
    private Response getImgPage(RealTimeDto realDto) {
        Map<String, Object> map=new HashMap<>();
        map.put("pageType",realDto.getPageType());
        return new Response().setII(1,true,map,"图片界面信息信息推送");
    }
    //获取温度界面信息
    private Response getTmpPage(RealTimeDto realDto) {
        Map<String, Object> map=new HashMap<>();
        Float maxData=0f;
        Float minData=0f;
        Float avgData=0f;
        try {
            //实时获取单体信息
            List<BattRtdata> rtdataList = rtdataService.getRtdataRealInfo(realDto.getBattgroupId());
            if(rtdataList!=null&&rtdataList.size()>0){
                for(int i=0;i<rtdataList.size();i++) {
                    BattRtdata battRtdata=rtdataList.get(i);
                    if(i==0){
                        maxData=battRtdata.getMonTmp();
                        minData=battRtdata.getMonTmp();
                    }
                    if (battRtdata.getMonTmp() >= maxData) {
                        maxData = battRtdata.getMonTmp();
                    }
                    if (battRtdata.getMonTmp() <= minData) {
                        minData = battRtdata.getMonTmp();
                    }
                    avgData += battRtdata.getMonTmp();
                }
                avgData=avgData/rtdataList.size();
            }
            map.put("rtdataList",rtdataList!=null?rtdataList:"");
            map.put("maxData",maxData);
            map.put("minData",minData);
            map.put("avgData",avgData);
            map.put("pageType",realDto.getPageType());
            return new Response().setII(1,true,map,"温度界面信息推送");
        }catch (Exception e){
            return new Response().set(1,false,"温度界面信息推送");
        }
    }
    //获取内阻界面信息
    private Response getResPage(RealTimeDto realDto) {
        Map<String, Object> map=new HashMap<>();
        Float maxData=0f;
        Float minData=0f;
        Float avgData=0f;
        try {
            //实时获取单体信息
            List<BattRtdata> rtdataList = rtdataService.getRtdataRealInfo(realDto.getBattgroupId());
            if(rtdataList!=null&&rtdataList.size()>0){
                for(int i=0;i<rtdataList.size();i++) {
                    BattRtdata battRtdata=rtdataList.get(i);
                    if(i==0){
                        maxData=battRtdata.getMonRes();
                        minData=battRtdata.getMonRes();
                    }
                    if (battRtdata.getMonRes() >= maxData) {
                        maxData = battRtdata.getMonRes();
                    }
                    if (battRtdata.getMonRes() <= minData) {
                        minData = battRtdata.getMonRes();
                    }
                    avgData += battRtdata.getMonRes();
                }
                avgData=avgData/rtdataList.size();
            }
            map.put("rtdataList",rtdataList!=null?rtdataList:"");
            map.put("maxData",maxData);
            map.put("minData",minData);
            map.put("avgData",avgData);
            //上一次内阻测试数据
            BattresdataInf rinfData= battresdataInfService.getLastTestData(realDto.getBattgroupId());
            map.put("rinfData",rinfData!=null?rinfData:"");
            map.put("pageType",realDto.getPageType());
            return new Response().setII(1,true,map,"内阻界面信息推送");
        }catch (Exception e){
            return new Response().set(1,false,"内阻界面信息推送");
        }
    }
    //获取电压界面信息
    private Response getVolPage(RealTimeDto realDto) {
        Map<String, Object> map=new HashMap<>();
        Float maxData=0f;
        Float minData=0f;
        Float avgData=0f;
        try {
            //实时获取单体信息
            List<BattRtdata> rtdataList = rtdataService.getRtdataRealInfo(realDto.getBattgroupId());
            if(rtdataList!=null&&rtdataList.size()>0){
                for(int i=0;i<rtdataList.size();i++) {
                    BattRtdata battRtdata=rtdataList.get(i);
                    if(i==0){
                        maxData=battRtdata.getMonVol();
                        minData=battRtdata.getMonVol();
                    }
                    if (battRtdata.getMonVol() >= maxData) {
                        maxData = battRtdata.getMonVol();
                    }
                    if (battRtdata.getMonVol() <= minData) {
                        minData = battRtdata.getMonVol();
                    }
                    avgData += battRtdata.getMonVol();
                }
                avgData=avgData/rtdataList.size();
            }
            map.put("rtdataList",rtdataList!=null?rtdataList:"");
            map.put("maxData",maxData);
            map.put("minData",minData);
            map.put("avgData",avgData);
            map.put("pageType",realDto.getPageType());
            return new Response().setII(1,true,map,"电压界面信息推送");
        }catch (Exception e){
            return new Response().set(1,false,"电压界面信息推送");
        }
    }
    //获取电源界面信息
    private Response getPowerPage(RealTimeDto realDto) {
        Map<String, Object> map=new HashMap<>();
        try {
            //实时获取电源信息
            PwrdevAcdcdata pwrdevAcdcdata = pwrdevAcdcdataService.getPwrRealInfo(realDto.getPowerId());
            map.put("pwrdevAcdcdata",pwrdevAcdcdata!=null?pwrdevAcdcdata:"");
            map.put("pageType",realDto.getPageType());
            return new Response().setII(1,true,map,"电源界面信息推送");
        }catch (Exception e){
            return new Response().set(1,false,"电源界面信息推送");
        }
    }
    //获取系统概述界面信息
    private Response getSystemPage(RealTimeDto realDto) {
        Map<String, Object> map=new HashMap<>();
        map.put("pageType",realDto.getPageType());
        try {
            //实时获取电池组信息
            BattRtstate battRtstate = battRtstateService.getBattRealInfo(realDto.getBattgroupId());
            map.put("battRtstate",battRtstate!=null?battRtstate:"");
            //实时获取设备信息
            DeviceState deviceState = deviceStateService.getDevRealInfo(realDto.getDevId());
            map.put("deviceState",deviceState!=null?deviceState:"");
            //获取上一次标准核容信息
            BatttestdataInf tinfData= batttestdataInfService.getLastStandardTestData(realDto.getBattgroupId());
            map.put("tinfData",tinfData!=null?tinfData:"");
            //获取近半小时电源数据,整流器数据,核容设备数据(推送实时,接口查询最近半小时数据)
            PwrdevAcdcdata pwrdevAcdcdata = pwrdevAcdcdataService.getPwrRealInfo(realDto.getPowerId());
            map.put("pwrdevAcdcdata",pwrdevAcdcdata!=null?pwrdevAcdcdata:"");
            return new Response().setII(1,true,map,"系统概述推送");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new Response().set(1,false,"系统概述推送");
    }
 
    @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());
    }
}