|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+package com.water.production.service;
|
|
|
2
|
+
|
|
|
3
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
4
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
5
|
+import com.water.production.entity.VideoCamera;
|
|
|
6
|
+import com.water.production.entity.VideoRecording;
|
|
|
7
|
+import com.water.production.mapper.VideoCameraMapper;
|
|
|
8
|
+import com.water.production.mapper.VideoRecordingMapper;
|
|
|
9
|
+import lombok.RequiredArgsConstructor;
|
|
|
10
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
11
|
+import org.springframework.stereotype.Service;
|
|
|
12
|
+import org.springframework.util.StringUtils;
|
|
|
13
|
+
|
|
|
14
|
+import java.time.Duration;
|
|
|
15
|
+import java.time.LocalDateTime;
|
|
|
16
|
+import java.util.*;
|
|
|
17
|
+import java.util.stream.Collectors;
|
|
|
18
|
+
|
|
|
19
|
+/**
|
|
|
20
|
+ * 视频监控管理服务
|
|
|
21
|
+ * 提供摄像头CRUD、视频流地址管理、状态监控、录像查询、统计等能力
|
|
|
22
|
+ */
|
|
|
23
|
+@Slf4j
|
|
|
24
|
+@Service
|
|
|
25
|
+@RequiredArgsConstructor
|
|
|
26
|
+public class VideoMonitorService {
|
|
|
27
|
+
|
|
|
28
|
+ private final VideoCameraMapper cameraMapper;
|
|
|
29
|
+ private final VideoRecordingMapper recordingMapper;
|
|
|
30
|
+
|
|
|
31
|
+ // ==================== 摄像头 CRUD ====================
|
|
|
32
|
+
|
|
|
33
|
+ /**
|
|
|
34
|
+ * 分页查询摄像头列表
|
|
|
35
|
+ */
|
|
|
36
|
+ public Page<VideoCamera> pageCameras(int current, int size, String area, Integer status, String keyword) {
|
|
|
37
|
+ Page<VideoCamera> page = new Page<>(current, size);
|
|
|
38
|
+ LambdaQueryWrapper<VideoCamera> wrapper = new LambdaQueryWrapper<>();
|
|
|
39
|
+ wrapper.eq(area != null, VideoCamera::getArea, area)
|
|
|
40
|
+ .eq(status != null, VideoCamera::getStatus, status)
|
|
|
41
|
+ .and(StringUtils.hasText(keyword), w ->
|
|
|
42
|
+ w.like(VideoCamera::getName, keyword)
|
|
|
43
|
+ .or().like(VideoCamera::getCameraId, keyword))
|
|
|
44
|
+ .orderByDesc(VideoCamera::getCreatedTime);
|
|
|
45
|
+ return cameraMapper.selectPage(page, wrapper);
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ /**
|
|
|
49
|
+ * 获取所有摄像头
|
|
|
50
|
+ */
|
|
|
51
|
+ public List<VideoCamera> listAllCameras() {
|
|
|
52
|
+ return cameraMapper.selectList(new LambdaQueryWrapper<VideoCamera>()
|
|
|
53
|
+ .orderByDesc(VideoCamera::getCreatedTime));
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ /**
|
|
|
57
|
+ * 获取摄像头详情
|
|
|
58
|
+ */
|
|
|
59
|
+ public VideoCamera getCameraById(Long id) {
|
|
|
60
|
+ return cameraMapper.selectById(id);
|
|
|
61
|
+ }
|
|
|
62
|
+
|
|
|
63
|
+ /**
|
|
|
64
|
+ * 创建摄像头
|
|
|
65
|
+ */
|
|
|
66
|
+ public VideoCamera createCamera(VideoCamera camera) {
|
|
|
67
|
+ camera.setStatus(camera.getStatus() != null ? camera.getStatus() : 0);
|
|
|
68
|
+ camera.setAiEnabled(camera.getAiEnabled() != null ? camera.getAiEnabled() : 0);
|
|
|
69
|
+ camera.setCreatedTime(LocalDateTime.now());
|
|
|
70
|
+ camera.setUpdatedTime(LocalDateTime.now());
|
|
|
71
|
+ camera.setDeleted(0);
|
|
|
72
|
+ cameraMapper.insert(camera);
|
|
|
73
|
+ log.info("创建摄像头: {} ({})", camera.getName(), camera.getCameraId());
|
|
|
74
|
+ return camera;
|
|
|
75
|
+ }
|
|
|
76
|
+
|
|
|
77
|
+ /**
|
|
|
78
|
+ * 更新摄像头
|
|
|
79
|
+ */
|
|
|
80
|
+ public boolean updateCamera(VideoCamera camera) {
|
|
|
81
|
+ camera.setUpdatedTime(LocalDateTime.now());
|
|
|
82
|
+ int rows = cameraMapper.updateById(camera);
|
|
|
83
|
+ if (rows > 0) {
|
|
|
84
|
+ log.info("更新摄像头: id={}", camera.getId());
|
|
|
85
|
+ }
|
|
|
86
|
+ return rows > 0;
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ /**
|
|
|
90
|
+ * 删除摄像头
|
|
|
91
|
+ */
|
|
|
92
|
+ public boolean deleteCamera(Long id) {
|
|
|
93
|
+ int rows = cameraMapper.deleteById(id);
|
|
|
94
|
+ if (rows > 0) {
|
|
|
95
|
+ log.info("删除摄像头: id={}", id);
|
|
|
96
|
+ }
|
|
|
97
|
+ return rows > 0;
|
|
|
98
|
+ }
|
|
|
99
|
+
|
|
|
100
|
+ // ==================== 视频流地址管理 ====================
|
|
|
101
|
+
|
|
|
102
|
+ /**
|
|
|
103
|
+ * 获取摄像头的所有视频流地址
|
|
|
104
|
+ */
|
|
|
105
|
+ public Map<String, Object> getStreamUrls(Long cameraId) {
|
|
|
106
|
+ VideoCamera camera = cameraMapper.selectById(cameraId);
|
|
|
107
|
+ if (camera == null) {
|
|
|
108
|
+ return Collections.emptyMap();
|
|
|
109
|
+ }
|
|
|
110
|
+ Map<String, Object> urls = new LinkedHashMap<>();
|
|
|
111
|
+ urls.put("cameraId", camera.getCameraId());
|
|
|
112
|
+ urls.put("name", camera.getName());
|
|
|
113
|
+ urls.put("rtsp", camera.getStreamUrlRtsp());
|
|
|
114
|
+ urls.put("hls", camera.getStreamUrlHls());
|
|
|
115
|
+ urls.put("flv", camera.getStreamUrlFlv());
|
|
|
116
|
+ urls.put("status", camera.getStatus());
|
|
|
117
|
+ return urls;
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
|
120
|
+ /**
|
|
|
121
|
+ * 更新视频流地址
|
|
|
122
|
+ */
|
|
|
123
|
+ public boolean updateStreamUrls(Long cameraId, String rtsp, String hls, String flv) {
|
|
|
124
|
+ VideoCamera camera = new VideoCamera();
|
|
|
125
|
+ camera.setId(cameraId);
|
|
|
126
|
+ camera.setStreamUrlRtsp(rtsp);
|
|
|
127
|
+ camera.setStreamUrlHls(hls);
|
|
|
128
|
+ camera.setStreamUrlFlv(flv);
|
|
|
129
|
+ camera.setUpdatedTime(LocalDateTime.now());
|
|
|
130
|
+ return cameraMapper.updateById(camera) > 0;
|
|
|
131
|
+ }
|
|
|
132
|
+
|
|
|
133
|
+ // ==================== 状态监控 ====================
|
|
|
134
|
+
|
|
|
135
|
+ /**
|
|
|
136
|
+ * 更新摄像头在线状态(模拟心跳检测)
|
|
|
137
|
+ */
|
|
|
138
|
+ public boolean updateCameraStatus(Long cameraId, Integer status) {
|
|
|
139
|
+ VideoCamera camera = new VideoCamera();
|
|
|
140
|
+ camera.setId(cameraId);
|
|
|
141
|
+ camera.setStatus(status);
|
|
|
142
|
+ if (status != null && status == 1) {
|
|
|
143
|
+ camera.setLastOnlineTime(LocalDateTime.now());
|
|
|
144
|
+ }
|
|
|
145
|
+ camera.setUpdatedTime(LocalDateTime.now());
|
|
|
146
|
+ return cameraMapper.updateById(camera) > 0;
|
|
|
147
|
+ }
|
|
|
148
|
+
|
|
|
149
|
+ /**
|
|
|
150
|
+ * 批量刷新摄像头状态(模拟)
|
|
|
151
|
+ * 实际场景: 从流媒体服务器获取在线状态
|
|
|
152
|
+ */
|
|
|
153
|
+ public Map<String, Object> refreshAllStatus() {
|
|
|
154
|
+ List<VideoCamera> cameras = cameraMapper.selectList(null);
|
|
|
155
|
+ int online = 0, offline = 0, fault = 0;
|
|
|
156
|
+ for (VideoCamera cam : cameras) {
|
|
|
157
|
+ // 模拟: 根据最后在线时间判断状态
|
|
|
158
|
+ if (cam.getLastOnlineTime() != null
|
|
|
159
|
+ && Duration.between(cam.getLastOnlineTime(), LocalDateTime.now()).toMinutes() < 5) {
|
|
|
160
|
+ online++;
|
|
|
161
|
+ } else if (cam.getStatus() != null && cam.getStatus() == 2) {
|
|
|
162
|
+ fault++;
|
|
|
163
|
+ } else {
|
|
|
164
|
+ offline++;
|
|
|
165
|
+ }
|
|
|
166
|
+ }
|
|
|
167
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
168
|
+ result.put("total", cameras.size());
|
|
|
169
|
+ result.put("online", online);
|
|
|
170
|
+ result.put("offline", offline);
|
|
|
171
|
+ result.put("fault", fault);
|
|
|
172
|
+ result.put("refreshTime", LocalDateTime.now());
|
|
|
173
|
+ return result;
|
|
|
174
|
+ }
|
|
|
175
|
+
|
|
|
176
|
+ // ==================== 视频录像 ====================
|
|
|
177
|
+
|
|
|
178
|
+ /**
|
|
|
179
|
+ * 分页查询录像记录
|
|
|
180
|
+ */
|
|
|
181
|
+ public Page<VideoRecording> pageRecordings(int current, int size, Long cameraId,
|
|
|
182
|
+ String recordType, String startDate, String endDate) {
|
|
|
183
|
+ Page<VideoRecording> page = new Page<>(current, size);
|
|
|
184
|
+ LambdaQueryWrapper<VideoRecording> wrapper = new LambdaQueryWrapper<>();
|
|
|
185
|
+ wrapper.eq(cameraId != null, VideoRecording::getCameraId, cameraId)
|
|
|
186
|
+ .eq(recordType != null, VideoRecording::getRecordType, recordType)
|
|
|
187
|
+ .ge(StringUtils.hasText(startDate), VideoRecording::getStartTime,
|
|
|
188
|
+ startDate != null ? startDate + " 00:00:00" : null)
|
|
|
189
|
+ .le(StringUtils.hasText(endDate), VideoRecording::getEndTime,
|
|
|
190
|
+ endDate != null ? endDate + " 23:59:59" : null)
|
|
|
191
|
+ .orderByDesc(VideoRecording::getStartTime);
|
|
|
192
|
+ return recordingMapper.selectPage(page, wrapper);
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ /**
|
|
|
196
|
+ * 生成回放地址
|
|
|
197
|
+ */
|
|
|
198
|
+ public Map<String, Object> getPlaybackUrl(Long recordingId) {
|
|
|
199
|
+ VideoRecording recording = recordingMapper.selectById(recordingId);
|
|
|
200
|
+ if (recording == null) {
|
|
|
201
|
+ return Collections.emptyMap();
|
|
|
202
|
+ }
|
|
|
203
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
204
|
+ result.put("recordingId", recording.getId());
|
|
|
205
|
+ result.put("cameraId", recording.getCameraId());
|
|
|
206
|
+ result.put("cameraName", recording.getCameraName());
|
|
|
207
|
+ result.put("startTime", recording.getStartTime());
|
|
|
208
|
+ result.put("endTime", recording.getEndTime());
|
|
|
209
|
+ result.put("durationSec", recording.getDurationSec());
|
|
|
210
|
+ result.put("playbackUrl", recording.getPlaybackUrl());
|
|
|
211
|
+ result.put("fileSizeMb", recording.getFileSizeMb());
|
|
|
212
|
+ return result;
|
|
|
213
|
+ }
|
|
|
214
|
+
|
|
|
215
|
+ /**
|
|
|
216
|
+ * 创建录像记录
|
|
|
217
|
+ */
|
|
|
218
|
+ public VideoRecording createRecording(VideoRecording recording) {
|
|
|
219
|
+ recording.setCreatedTime(LocalDateTime.now());
|
|
|
220
|
+ recording.setUpdatedTime(LocalDateTime.now());
|
|
|
221
|
+ recording.setDeleted(0);
|
|
|
222
|
+ recordingMapper.insert(recording);
|
|
|
223
|
+ return recording;
|
|
|
224
|
+ }
|
|
|
225
|
+
|
|
|
226
|
+ /**
|
|
|
227
|
+ * 删除录像记录
|
|
|
228
|
+ */
|
|
|
229
|
+ public boolean deleteRecording(Long id) {
|
|
|
230
|
+ return recordingMapper.deleteById(id) > 0;
|
|
|
231
|
+ }
|
|
|
232
|
+
|
|
|
233
|
+ // ==================== 统计 ====================
|
|
|
234
|
+
|
|
|
235
|
+ /**
|
|
|
236
|
+ * 设备在线率统计
|
|
|
237
|
+ */
|
|
|
238
|
+ public Map<String, Object> getDeviceOnlineStats() {
|
|
|
239
|
+ List<VideoCamera> cameras = cameraMapper.selectList(null);
|
|
|
240
|
+ long total = cameras.size();
|
|
|
241
|
+ long online = cameras.stream().filter(c -> c.getStatus() != null && c.getStatus() == 1).count();
|
|
|
242
|
+ long offline = cameras.stream().filter(c -> c.getStatus() != null && c.getStatus() == 0).count();
|
|
|
243
|
+ long fault = cameras.stream().filter(c -> c.getStatus() != null && c.getStatus() == 2).count();
|
|
|
244
|
+ double onlineRate = total > 0 ? (double) online / total * 100 : 0;
|
|
|
245
|
+
|
|
|
246
|
+ Map<String, Object> stats = new LinkedHashMap<>();
|
|
|
247
|
+ stats.put("total", total);
|
|
|
248
|
+ stats.put("online", online);
|
|
|
249
|
+ stats.put("offline", offline);
|
|
|
250
|
+ stats.put("fault", fault);
|
|
|
251
|
+ stats.put("onlineRate", Math.round(onlineRate * 100.0) / 100.0);
|
|
|
252
|
+ return stats;
|
|
|
253
|
+ }
|
|
|
254
|
+
|
|
|
255
|
+ /**
|
|
|
256
|
+ * 按区域统计摄像头分布
|
|
|
257
|
+ */
|
|
|
258
|
+ public List<Map<String, Object>> getCameraStatsByArea() {
|
|
|
259
|
+ List<VideoCamera> cameras = cameraMapper.selectList(null);
|
|
|
260
|
+ Map<String, List<VideoCamera>> grouped = cameras.stream()
|
|
|
261
|
+ .filter(c -> c.getArea() != null)
|
|
|
262
|
+ .collect(Collectors.groupingBy(VideoCamera::getArea));
|
|
|
263
|
+
|
|
|
264
|
+ List<Map<String, Object>> result = new ArrayList<>();
|
|
|
265
|
+ for (Map.Entry<String, List<VideoCamera>> entry : grouped.entrySet()) {
|
|
|
266
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
267
|
+ item.put("area", entry.getKey());
|
|
|
268
|
+ item.put("total", entry.getValue().size());
|
|
|
269
|
+ long online = entry.getValue().stream().filter(c -> c.getStatus() != null && c.getStatus() == 1).count();
|
|
|
270
|
+ item.put("online", online);
|
|
|
271
|
+ item.put("onlineRate", entry.getValue().isEmpty() ? 0 :
|
|
|
272
|
+ Math.round((double) online / entry.getValue().size() * 10000.0) / 100.0);
|
|
|
273
|
+ result.add(item);
|
|
|
274
|
+ }
|
|
|
275
|
+ return result;
|
|
|
276
|
+ }
|
|
|
277
|
+}
|