longyvfengyun
2023-02-28 8ba9295eef9d53f633aeb83e7cf5234da7713ac0
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
package com.whyc.webSocket;
 
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Response;
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.stream.Collectors;
 
/**
 * 大屏展示 Socket
 */
@Component
@ServerEndpoint(value = "/screen",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
public class ScreenSocket {
 
    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;
 
    @Autowired
    public void setPowerAlarmService(PwrdevAlarmService powerAlarmService) {
        ScreenSocket.powerAlarmService = powerAlarmService;
    }
    @Autowired
    public void setBattAlarmDataService(BattalarmDataService battAlarmDataService) {
        ScreenSocket.battAlarmDataService = battAlarmDataService;
    }
    @Autowired
    public void setFbs9100StateService(Fbs9100StateService fbs9100StateService) {
        ScreenSocket.fbs9100StateService = fbs9100StateService;
    }
    @Autowired
    public void setDevAlarmDataService(DevalarmDataService devAlarmDataService) {
        ScreenSocket.devAlarmDataService = devAlarmDataService;
    }
    @Autowired
    public void setBattInfService(BattInfService battInfService) {
        ScreenSocket.battInfService = battInfService;
    }
 
    @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();
        System.out.println(user);
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    Map<String, Response> res = new HashMap<>();
                    Response sessionRes = new Response().set(1, httpSession.getId());
                    while (!currentThread().isInterrupted()) {
                        //一体   告警:交流ABC
                        Response res_acABC = powerAlarmService.getAcABCAnalysis(userId);
                        //一体   告警:单体容量
                        Response res_monCapacityLowAnalysis = battAlarmDataService.getMonCapacityLowAnalysis(userId);
                        //一体   告警:单体温度内阻温度
                        Response<Map> res_monVRTAnalysis = battAlarmDataService.getMonVRTAnalysis(userId);
                        //一体   统计:三种设备类型 告警数统计
                        int battAlarmNum = battAlarmDataService.serchRealTime(userId).getCode();
                        int devAlarmNum = devAlarmDataService.getAlarmNum(userId);
                        int powerAlarmNum = (int) powerAlarmService.getAlarmNum().getData();
                        //一体   统计:电池统计 分为品牌和标称电压
                        List<Battinf> battinfList = battInfService.getBattGroupList(userId);
                        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> battGroupInfoMap = new HashMap<>();
                        for(String producer : battGroupInfoMap.keySet()){
 
                        }
 
                        //一体   统计:设备状态
                        Response<Map> res_devStates = fbs9100StateService.getBTSEquipStatus(userId);
                        //一体   统计:各种设备类型总数 电池 设备 电源 站点
                        Response<Map> res_devCountMap = battInfService.getDevCountMap(userId);
                        //续航检测??? TODO
 
 
                        res.put("acABC", res_acABC);
                        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();
        }
    }
 
}