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");
|
}*/
|
|
}
|