admin
2024-02-21 2272213fea7ab9ea6250eefad55bb113ce2b1837
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
package com.ruoyi.activeMQ;
 
/**
 * 消息的消费者
 * @author Administrator
 */
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.aibrain.domain.AiAlarmInfo;
import com.ruoyi.aibrain.domain.AiCamera;
import com.ruoyi.aibrain.service.IAiAlarmInfoService;
import com.ruoyi.aibrain.service.IAiCameraService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
import com.ruoyi.framework.websocket.WebSocketServer;
import com.ruoyi.framework.websocket.WebSocketUsers;
import com.ruoyi.system.domain.AiRule;
import com.ruoyi.system.service.IAiRuleService;
import com.ruoyi.utils.Base64Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
 
import javax.websocket.EncodeException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
 
 
/**
 * Created by beyondLi
 * Date 2017/12/21
 * Desc 生产者rest .
 */
@Service
public class CustomerRest {
 
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);
 
    @Autowired
    private IAiCameraService aiCameraService;
 
    @Autowired
    private IAiRuleService aiRuleService;
 
    @Autowired
    private IAiAlarmInfoService aiAlarmInfoService;
 
    public static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
 
 
    /**
     * 监听 - 摄像头控制结果队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
//    @JmsListener(destination = "VideoRealTopic1921683036",containerFactory="jmsListenerContainerTopic")
//    public void VideoRealTopic(String info) {
//
//        log.info("CameraResultTopic接收到的json字符串{}",info);
//    }
 
 
    /**
     * 监听 - 摄像头控制结果队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    @JmsListener(destination = "CameraResultTopic",containerFactory="jmsListenerContainerTopic")
    public void cameraControlResultTopic(String info) {
        log.info("CameraResultTopic接收到的json字符串{}",info);
        JSONObject json = JSON.parseObject(info);
        long id = Long.parseLong(json.getString("cameraId"));//摄像头ID
        String success = json.getString("success");//是否成功 1连接中 2成功 3失败 4连接中断
 
        AiCamera camera = aiCameraService.selectAiCameraById(id);
        JSONObject result = new JSONObject();
        if(success.equals("1")){
            result.put("nowMessage",camera.getRegionName()+"AI识别设备"+camera.getName()+"连接成功。");
            camera.setStatus("1");//在线
        }else if(success.equals("2")){
            result.put("nowMessage",camera.getRegionName()+"AI识别设备"+camera.getName()+"连接失败。");
            camera.setStatus("0");//离线
        }else if(success.equals("3")){
            result.put("nowMessage",camera.getRegionName()+"AI识别设备"+camera.getName()+"连接中断。");
            camera.setStatus("0");//离线
        }
        camera.setUpdateBy("cameraControlResultTopic");
        aiCameraService.updateAiCameraStatus(camera);
//        WebSocketUsers.sendMessageToUsersByText(result.toJSONString());
    }
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    @JmsListener(destination = "AlarmResultTopic",containerFactory="jmsListenerContainerTopic")
    public void alarmInfoTopic(String info) {
        log.info("AlarmResultTopic接收到的json字符串{}",info);
        JSONObject json = JSON.parseObject(info);
        long cameraId = Long.parseLong(json.getString("cameraId"));//摄像头ID
        long ruleId = Long.parseLong(json.getString("ruleId"));//算法ID
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        String path = json.getString("path");//报警抓拍片段路径
        String newPath = path.substring(0, path.lastIndexOf("."))+"N.mp4";
 
        Date time = new Date(json.getLong("time"));//当前时间戳
 
        AiCamera camera = aiCameraService.selectAiCameraById(cameraId);
        AiRule rule = aiRuleService.selectAiRuleById(ruleId);
        toCompressFile(path,newPath);
        String picture = Base64Util.base64ToJpg(pictureBase64, camera.getCameraNum());
        String video = newPath.replaceAll(RuoYiConfig.getProfile(), Constants.RESOURCE_PREFIX);
 
        AiAlarmInfo alarm = new AiAlarmInfo();
        alarm.setCameraId(cameraId);
        alarm.setRegion(camera.getRegion());
        alarm.setRuleId(ruleId);
        alarm.setContent(rule.getContent());
        alarm.setSmallClass(rule.getRuleType());
        alarm.setAlarmTime(time);
        //10分钟内相同的报警不再新增
        AiAlarmInfo aiAlarmInfo = aiAlarmInfoService.selectAlarmInfo(alarm);
        if(aiAlarmInfo==null){
            alarm.setVideo(video);
            alarm.setPicture(picture);
            aiAlarmInfoService.insertAiAlarmInfo(alarm);
        }else{
            Date alarmTime = aiAlarmInfo.getAlarmTime();
            if((time.getTime()-alarmTime.getTime())>600000){
                //间隔超过10分钟
                alarm.setVideo(video);
                alarm.setPicture(picture);
                aiAlarmInfoService.insertAiAlarmInfo(alarm);
            }
        }
    }
 
    /*
     *  视频转码
     * @param convertFile  待转换的文件
     * @param targetFile  转换后的目标文件
     */
    private static  void toCompressFile(String convertFile,String targetFile){
        try{
            Runtime runtime = Runtime.getRuntime();
            runtime.exec("ffmpeg -version");
            //ffmpeg -i C:\mpeg4record\2022-10-15\192.168.1.64_01_0_20221015_120508_1.mp4 -vcodec libx264  E:\test\test.mp4
            String cutCmd="ffmpeg -i " + convertFile + " -vcodec libx264 "+ targetFile;
            System.out.println("cutCmd:"+cutCmd);
            runtime.exec(cutCmd);
            System.out.println("文件:"+convertFile+" 正在转换中。。。");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("压缩文件出现异常:"+e.getMessage());
        }
    }
    public static void main(String[] args) {
 
        toCompressFile("","");
 
    }
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683036",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683036(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683036{}");
        //WebSocketServer.sendPicMessage(pictureBase64, "1921683036");
 
        writeLogInFile(sdf.format(new Date())+"======="+pictureBase64);
 
    }*/
 
    public static void writeLogInFile(String content){
        File file = new File("/home/aiModel.txt");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Writer writer = null;
        StringBuilder outputString = new StringBuilder();
        try {
            outputString.append(content+"\r\n");
            writer = new FileWriter(file,true);
            writer.write(outputString.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683041",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683041(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683041开始{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683041");
        log.info("VideoRealTopic1921683041结束{}");
    }*/
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683042",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683042(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683042{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683042");
    }*/
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683043",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683043(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683043{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683043");
    }*/
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683044",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683044(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683044{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683044");
    }*/
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683045",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683045(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683045{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683045");
    }*/
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683046",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683046(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683046{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683046");
    }*/
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683047",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683047(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683047{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683047");
    }*/
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683048",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683048(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683048{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683048");
    }*/
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683049",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683049(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683049{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683049");
    }*/
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683050",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683050(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683050{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683050");
    }*/
 
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683051",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683051(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683051{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683051");
    }
*/
    /**
     * 监听 - 报警信息队列
     * destination:监听的队列名称
     * containerFactory:监听模式
     *      jmsListenerContainerFactory P2P模式
     *      jmsListenerContainerTopic 订阅模式
     */
    /*@JmsListener(destination = "VideoRealTopic1921683052",containerFactory="jmsListenerContainerTopic")
    public void VideoRealTopic1921683052(String info) throws EncodeException, IOException {
        JSONObject json = JSON.parseObject(info);
        String pictureBase64 = json.getString("pictureBase64");//报警现场图片
        log.info("VideoRealTopic1921683052{}");
        WebSocketServer.sendPicMessage(pictureBase64, "1921683052");
    }*/
 
}