2025-12-02 18:11:51 +08:00

192 lines
8.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.task;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zhgd.xmgl.call.api.PoliceCameraManufacturer;
import com.zhgd.xmgl.call.factory.PoliceCameraManufacturerFactory;
import com.zhgd.xmgl.modules.policecamera.entity.PoliceCameraItem;
import com.zhgd.xmgl.modules.policecamera.entity.PoliceCameraVideoConfig;
import com.zhgd.xmgl.modules.policecamera.service.IPoliceCameraItemService;
import com.zhgd.xmgl.modules.policecamera.service.IPoliceCameraVideoConfigService;
import com.zhgd.xmgl.modules.policecamera.service.IProjectPoliceCameraConfigService;
import com.zhgd.xmgl.modules.project.entity.Project;
import com.zhgd.xmgl.modules.project.service.IProjectService;
import com.zhgd.xmgl.util.AsyncTaskUtil;
import com.zhgd.xmgl.util.HikVideoUtil;
import com.zhgd.xmgl.util.PathUtil;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("xmgl/task")
public class Mcs8Task {
@Lazy
@Autowired
IProjectService projectService;
@Lazy
@Autowired
private IProjectPoliceCameraConfigService projectPoliceCameraConfigService;
@Lazy
@Autowired
private PoliceCameraManufacturerFactory policeCameraManufacturerFactory;
@Autowired
@Qualifier("doubleCarbonExecutor")
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Lazy
@Autowired
private IPoliceCameraVideoConfigService policeCameraVideoConfigService;
@Lazy
@Autowired
private IPoliceCameraItemService policeCameraItemService;
/**
* 截取rtsp流的host
*/
@Value("${screenshotUrl:http://127.0.0.1:51111}")
private String screenshotUrl;
/**
* 定时从Mcs8获取执法记录仪文件
*/
@SchedulerLock(name = "getPoliceCameraItemFile", lockAtMostFor = "PT300S", lockAtLeastFor = "PT120S")
@Scheduled(cron = "0 */5 * * * ?")
@RequestMapping("getPoliceCameraItemFile")
public void getPoliceCameraItemFile() {
Date now = new Date();
List<Project> projects = projectService.list();
for (Project project : projects) {
try {
PoliceCameraManufacturer manufacturer = policeCameraManufacturerFactory.getPoliceCameraManufacturer(project.getProjectSn());
if (manufacturer != null) {
AsyncTaskUtil.runAsync(() -> {
manufacturer.pullFile(DateUtil.offsetDay(now, -7), now);
});
}
} catch (Exception e) {
log.error("定时从Mcs8获取执法记录仪文件err{}", project.getProjectName(), e);
}
}
}
/**
* 更新执法记录仪的封面
*/
@SchedulerLock(name = "updatePoliceCameraItemCoverUrl", lockAtMostFor = "PT600S", lockAtLeastFor = "PT300S")
@Scheduled(cron = "* */30 * * * ?")
@RequestMapping("updatePoliceCameraItemCoverUrl")
public void updatePoliceCameraItemCoverUrl() {
List<PoliceCameraVideoConfig> configs = policeCameraVideoConfigService.list(new LambdaQueryWrapper<PoliceCameraVideoConfig>());
Map<String, List<PoliceCameraItem>> projectSn2ItemsMap = policeCameraItemService.list(new LambdaQueryWrapper<PoliceCameraItem>().eq(PoliceCameraItem::getDeviceState, 1)).stream().collect(Collectors.groupingBy(PoliceCameraItem::getProjectSn));
for (PoliceCameraVideoConfig config : configs) {
List<PoliceCameraItem> items = projectSn2ItemsMap.get(config.getProjectSn());
if (items != null) {
for (PoliceCameraItem item : items) {
if (Objects.equals(item.getDeviceState(), 1)) {
String url = null;
try {
url = HikVideoUtil.callPostApiGetPreviewURL(item.getMonitoringNumber(), "rtsp",
1, config.getIp(),
config.getPort(), config.getAppId(), config.getAppSecret(), 1);
if (StrUtil.isNotBlank(url)) {
String screenshot = downloadScreenshot(url);
if (StrUtil.isNotBlank(screenshot)) {
try {
if (StrUtil.isNotBlank(item.getCoverUrl())) {
FileUtil.del(PathUtil.getBasePath() + "/" + item.getCoverUrl());
}
} catch (Exception e) {
}
policeCameraItemService.update(null, new LambdaUpdateWrapper<PoliceCameraItem>()
.set(PoliceCameraItem::getCoverUrl, screenshot)
.eq(PoliceCameraItem::getItemId, item.getItemId())
);
}
}
} catch (Exception e) {
log.error("更新执法记录仪的封面错误:{}", e.getMessage());
log.info("更新执法记录仪的封面错误", e);
}
}
}
}
}
}
/**
* 截取rtsp流的图片
*
* @param playUrl
* @return
*/
public String downloadScreenshot(String playUrl) {
String apiUrl = screenshotUrl + "/screenshot";
String name = IdUtil.randomUUID() + ".jpg";
String savePath = PathUtil.getBasePath() + "/" + name;
// 构造请求参数
String jsonBody = "{\"playUrl\":\"" + playUrl + "\"}";
try {
log.info("发送http请求截取rtsp流的图片url:{},body:{}", apiUrl, jsonBody);
// 发送 POST 请求并下载文件
HttpResponse response = HttpRequest.post(apiUrl)
.body(jsonBody)
.execute();
if (response.isOk()) {
// 将响应内容保存为文件
FileUtil.writeFromStream(response.bodyStream(), savePath);
System.out.println("图片下载成功: " + savePath);
} else {
System.out.println("下载失败,状态码: " + response.getStatus());
return null;
}
} catch (Exception e) {
log.error("截取rtsp流错误{}", e.getMessage());
log.info("截取rtsp流错误", e);
return null;
}
return name;
}
// /**
// * 定时更新执法仪设备状态
// */
// @Scheduled(cron = "*/5 * * * * ?")
// @RequestMapping("updateStatus")
// public void updateStatus() {
// List<Project> projects = projectService.list();
// for (Project project : projects) {
// try {
// PoliceCameraManufacturer manufacturer = policeCameraManufacturerFactory.getPoliceCameraManufacturer(project.getProjectSn());
// if (manufacturer != null) {
// manufacturer.updateStatus(3L);
// }
// } catch (Exception e) {
// log.error("定时更新执法仪设备状态err{}", project.getProjectName(), e);
// }
// }
// }
}