c83b5b39
Liu Haoyu
项目创建, 集成spring-se...
|
1
2
3
|
package com.objecteye.websocket;
import com.alibaba.fastjson.JSON;
|
40c853a1
Liu Haoyu
去掉MySQL相关内容, 去掉my...
|
4
5
6
7
|
import com.objecteye.entity.CountMsg;
import com.objecteye.entity.DeployAlarmMsg;
import com.objecteye.entity.PlateAlarmMsg;
import com.objecteye.entity.VisualStatisticsMsg;
|
c83b5b39
Liu Haoyu
项目创建, 集成spring-se...
|
8
|
import com.objecteye.pojo.RabbitMQVehicle;
|
c83b5b39
Liu Haoyu
项目创建, 集成spring-se...
|
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
|
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* @Author: lr
* @Date: 2019/9/3 10:45
* @Version 1.0
* @Message:
*/
@Component
public class MyWebSocketHandler extends TextWebSocketHandler {
@Autowired
private RedisTemplate redisTemplate;
|
c83b5b39
Liu Haoyu
项目创建, 集成spring-se...
|
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
|
@Autowired
private SelectMongoData selectMongoData;
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message)
throws Exception {
super.handleTextMessage(session, message);
}
/**
* 抓拍数据和报警数据的json格式
*/
public String getRealData(Object countMsg, Object captureMsg, Object deployAlarmMsg) {
JSONObject jsonObject = new JSONObject();
boolean isNull = true;
if (countMsg != null) {
isNull = false;
jsonObject.put("countMsg", countMsg);
jsonObject.put("countCode", 0);
} else {
jsonObject.put("countCode", 1);
}
if (captureMsg != null) {
isNull = false;
jsonObject.put("captureMsg", captureMsg);
jsonObject.put("captureCode", 0);
} else {
jsonObject.put("captureCode", 1);
}
if (deployAlarmMsg != null) {
isNull = false;
jsonObject.put("deployAlarmMsg", deployAlarmMsg);
jsonObject.put("deployAlarmCode", 0);
} else {
jsonObject.put("deployAlarmCode", 1);
}
if (isNull) {
return null;
}
return jsonObject.toString();
}
@Override
public void afterConnectionEstablished(WebSocketSession session) {
//获取请求头中节点id
String deviceId = session.getAttributes().get("deviceId").toString();
if (deviceId.equals("0")) { //可视化大屏
while (session.isOpen()) {
String data = null;
try {
data = (String) redisTemplate.opsForValue().get("data");
if (data == null) { //缓存为空,从库中取数据,存入缓存
VisualStatisticsMsg displayData = getDisplayData();
data = JSON.toJSONString(displayData);
redisTemplate.opsForValue().set("data", data);
} else {
//推送数据
session.sendMessage(new TextMessage(data));
Thread.sleep(5000);
}
} catch (Exception e) {
//e.printStackTrace();
}
}
} else { //实时预览
while (session.isOpen()) {
Object captureMsg = null;
Object countMsg = null;
Object deployAlarmMsg = null;
//对于流量统计和实时抓拍先判断设备是否有效
|
40c853a1
Liu Haoyu
去掉MySQL相关内容, 去掉my...
|
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
//抓拍数据
boolean havaIpKey = redisTemplate.hasKey("deviceId");
if (havaIpKey) {
Long size = redisTemplate.opsForList().size("deviceId");
if (size != 0) {
//从redis取数据(直接获取json字符串)
captureMsg = redisTemplate.opsForList().rightPop("deviceId");
RabbitMQVehicle captureRedis = JSON.parseObject(captureMsg.toString(), RabbitMQVehicle.class);
String id = captureRedis.getId();
String picurl = captureRedis.getPicurl();
JSONObject object = new JSONObject();
object.put("id", id);
object.put("picurl", picurl);
captureMsg = object.toString();
|
c83b5b39
Liu Haoyu
项目创建, 集成spring-se...
|
123
124
125
|
}
}
|
40c853a1
Liu Haoyu
去掉MySQL相关内容, 去掉my...
|
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
//流量统计
CountMsg countMsg1 = new CountMsg();
long vehicleCount = selectMongoData.getVehicleCountBydeviceId();
String strVehicleCount = selectMongoData.changeType(vehicleCount);
countMsg1.setVehicleCount(strVehicleCount);
long vehicleCountInHours = selectMongoData.getVehicleCountInHours();
String strVehicleCountInHours = selectMongoData.changeType(vehicleCountInHours);
countMsg1.setVehicleCountInHours(strVehicleCountInHours);
long violateCountByDeviceId = selectMongoData.getViolateCountByDeviceId();
String strViolateCounts = selectMongoData.changeType(violateCountByDeviceId);
countMsg1.setViolateCount(strViolateCounts);
countMsg = JSON.toJSON(countMsg1);
|
c83b5b39
Liu Haoyu
项目创建, 集成spring-se...
|
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
|
//布控任务报警
boolean havedeployKey = redisTemplate.hasKey("ALARM");
if (havedeployKey) {
Long size = redisTemplate.opsForList().size("ALARM");
if (size != 0) {
//从redis取数据
deployAlarmMsg = redisTemplate.opsForList().rightPop("ALARM");
DeployAlarmMsg deployMsg = new DeployAlarmMsg();
PlateAlarmMsg plateAlarmMsg = JSON.parseObject(deployAlarmMsg.toString(), PlateAlarmMsg.class);
String alarmTime = plateAlarmMsg.getAlarmTime();
deployMsg.setAlarmTime(alarmTime);
String equipmentName = plateAlarmMsg.getEquipmentName();
deployMsg.setAlarmPlace(equipmentName);
Integer alarmType = plateAlarmMsg.getAlarmType();
deployMsg.setDeployType(alarmType);
String snapshotUrl = plateAlarmMsg.getSnapshotUrl();
deployMsg.setCaptureUrl(snapshotUrl);
if (alarmType == 0) {
String plateNum = plateAlarmMsg.getPlateNum();
deployMsg.setDeployUrl(plateNum);
} else {
String libUrl = plateAlarmMsg.getLibUrl();
deployMsg.setDeployUrl(libUrl);
double similarity = plateAlarmMsg.getSimilarity();
deployMsg.setSimilarity(Double.parseDouble(String.format("%.2f", similarity)));
}
deployAlarmMsg = JSON.toJSON(deployMsg);
}
}
String realData = getRealData(countMsg, captureMsg, deployAlarmMsg);
if (realData != null) {
try { //每隔2秒,推送一次
session.sendMessage(new TextMessage(realData));
//Thread.sleep(2000);
} catch (IOException e) {
//e.printStackTrace();
}
}
}
}
}
//抛出异常后处理
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
if (session.isOpen()) {
session.close();
}
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
if (session.isOpen()) {
session.close();
}
}
//从库里查数据
public VisualStatisticsMsg getDisplayData() {
//左上角那一块
VisualStatisticsMsg visualStatisticsMsg = new VisualStatisticsMsg();
long vehicleCount = selectMongoData.getVehicleCount();
String strVehicleCount = selectMongoData.changeType(vehicleCount);
visualStatisticsMsg.setVehicleCount(strVehicleCount);
long monitorTotalDays = selectMongoData.getMonitorTotalDays();
String strMonitorTotalDays = selectMongoData.changeType(monitorTotalDays);
visualStatisticsMsg.setMonitorTotalDays(strMonitorTotalDays);
|
c83b5b39
Liu Haoyu
项目创建, 集成spring-se...
|
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
int deployCount = selectMongoData.getDeployCount();
String strDeployCount = selectMongoData.changeType(deployCount);
visualStatisticsMsg.setDeployCount(strDeployCount);
long alarmCount = selectMongoData.getAlarmCount();
String strAlarmCount = selectMongoData.changeType(alarmCount);
visualStatisticsMsg.setAlarmCount(strAlarmCount);
long vehicleCountInMonth = selectMongoData.getVehicleCountInMonth();
String strVehicleCountInMonth = selectMongoData.changeType(vehicleCountInMonth);
visualStatisticsMsg.setVehicleCountInMonth(strVehicleCountInMonth);
long vehicleCountInWeek = selectMongoData.getVehicleCountInWeek();
String strVehicleCountInWeek = selectMongoData.changeType(vehicleCountInWeek);
visualStatisticsMsg.setVehicleCountInWeek(strVehicleCountInWeek);
//区域和七日过车
|
c83b5b39
Liu Haoyu
项目创建, 集成spring-se...
|
223
224
225
226
|
List<Long> oneWeekVehicleCount = selectMongoData.getOneWeekVehicleCount();
visualStatisticsMsg.setOneWeekVehicleCount(oneWeekVehicleCount);
List<long[]> oneWeekSpecialtyVehicleCount = selectMongoData.getOneWeekSpecialtyVehicleCount();
visualStatisticsMsg.setOneWeekSpecialtyVehicleCount(oneWeekSpecialtyVehicleCount);
|
c83b5b39
Liu Haoyu
项目创建, 集成spring-se...
|
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
//今日相关数据
Map<String, Object> todayData = selectMongoData.getTodayData();
visualStatisticsMsg.setTodayData(todayData);
return visualStatisticsMsg;
}
//隔一分钟更新缓存
@Scheduled(fixedRate = 60000)
public void putCache() {
VisualStatisticsMsg displayData = getDisplayData();
String displaydata = JSON.toJSONString(displayData);
redisTemplate.opsForValue().set("data", displaydata);
}
}
|