liuxiaolong
2019-05-06 0bc4f4c791437b39b8c30624f5c21f8c855dc61d
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
package com.cloud.retrieve.websocket;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cloud.common.utils.DateUtil;
import com.cloud.retrieve.service.EsDataService;
import jdk.nashorn.internal.runtime.logging.Loggable;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.net.InetSocketAddress;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
//@Component
public class AlarmSocketServer extends WebSocketServer {
 
    @Autowired
    private EsDataService esDataService;
 
    private Map<WebSocket, Object> conns = new HashMap<>();
 
//    public AlarmSocketServer(){
//        super(new InetSocketAddress(7678));
//
//        new Thread(){
//            @Override
//            public void run(){
//
//                Object startTime =null;
//                Map<String,Object> map = new HashMap<>();
//
//                try {
//                    Thread.sleep(2000);
//                    AlarmSocketServer.this.start();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//                while(true){
//                    try {
//                        Thread.sleep(2000);
//                        JSONObject alarmData = esDataService.findEsAllAlarmDataByPage(1, 200,null,null);
//
//                        for(WebSocket conn : conns.keySet()){
//                            try {
//                                String resourceDescriptor = conn.getResourceDescriptor();
//                                String[] params = resourceDescriptor.split("/");
//                                String method = params[1];
//
//                                try {
//                                    if(conns.get(conn) != null && conns.get(conn) instanceof Map){
//                                        startTime = ((Map<String, Object>)conns.get(conn)).get("startTime");
//                                        map = (Map<String, Object>)(((Map<String, Object>)conns.get(conn)).get("map"));
//                                        if(map == null){
//                                            map = new HashMap<>();
//                                        }
//                                    }
//                                 } catch (Exception e) {
//                                    e.printStackTrace();
//                                    startTime = null;
//                                    map = new HashMap<>();
//                                }
//
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                                try {
//                                    if(conn.isClosed()){
//                                        conns.remove(conn);
//                                    }
//                                } catch (Exception e1) {
//                                    e1.printStackTrace();
//                                }
//                            }
//
//                            try {
//                                Map<String, Object> curMap = new HashMap<>();
//                                if((alarmData !=null && alarmData.get("total")!= null && Integer.parseInt(alarmData.getString("total")) > 0)){
//                                    JSONArray curAlarmList = alarmData.getJSONArray("equList");
//
//                                    List<Object> sendList = new ArrayList<>();
//                                    Timestamp lastLikeDate = null;
//                                    if(startTime !=null){//socket获取过数据
//                                        lastLikeDate = Timestamp.valueOf(startTime.toString());
//                                        for(int i=0;i < curAlarmList.size();i++){
//                                            JSONObject obj = (JSONObject) curAlarmList.get(i);
//                                            Timestamp sTime = Timestamp.valueOf(obj.getString("likeDate"));
//                                            String recordId = obj.get("Id").toString();
//                                                curMap.put(recordId, obj);
//                                            if(sTime.after(lastLikeDate) && !map.containsKey(recordId)){
//                                                sendList.add(obj);
//                                            }
//                                        }
//                                    }else{
//                                        for(int i=0;i < curAlarmList.size();i++){
//                                            JSONObject obj = (JSONObject) curAlarmList.get(i);
//                                            String recordId = obj.get("Id").toString();
//                                            curMap.put(recordId, obj);
//                                            sendList.add(obj);
//                                        }
//
//                                    }
//
//                                    if(sendList.size() > 0){
//                                        JSONObject sData = new JSONObject();
//                                        sData.put("total",alarmData.getString("total"));
//                                        sData.put("data", sendList);
//                                        log.info("websocket推送es数据:"+sendList);
//                                        conn.send(JSON.toJSONString(sData, SerializerFeature.WriteMapNullValue).toString());
//
//                                        Map<String, Object> connMap = new HashMap<>();
//                                        lastLikeDate = Timestamp.valueOf(((JSONObject)sendList.get(sendList.size()-1)).getString("likeDate"));
//                                        log.info("websocket推送es数据 startTime:"+lastLikeDate);
//                                        connMap.put("startTime", lastLikeDate);
//                                        connMap.put("map", curMap);
//                                        conns.put(conn, connMap);
//
//                                    }else{
//                                       /* JSONObject sData = new JSONObject();
//                                        sData.put("total",alarmData.getString("total"));
//                                        sData.put("data", sendList);
//                                        conn.send(JSON.toJSONString(sData, SerializerFeature.WriteMapNullValue).toString());
//                                        log.info("sendList.size=0");*/
//                                    }
//                                }
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }.start();
//    }
    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        try {
            log.info("建立连接");
            conns.put(conn, new HashMap<String, Object>());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        try {
            log.info("连接关闭");
            conns.remove(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public void onMessage(WebSocket conn, String message) {
    }
 
    @Override
    public void onError(WebSocket conn, Exception ex) {
        log.info("exception:"+ex.getMessage());
    }
 
    @Override
    public void onStart() {
    }
}