2025-07-04 14:57:13 +08:00
|
|
|
|
package com.zhgd.xmgl.task;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
2025-11-22 16:40:49 +08:00
|
|
|
|
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;
|
2025-07-04 14:57:13 +08:00
|
|
|
|
import com.zhgd.xmgl.call.api.PoliceCameraManufacturer;
|
|
|
|
|
|
import com.zhgd.xmgl.call.factory.PoliceCameraManufacturerFactory;
|
2025-11-22 16:40:49 +08:00
|
|
|
|
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;
|
2025-07-04 14:57:13 +08:00
|
|
|
|
import com.zhgd.xmgl.modules.policecamera.service.IProjectPoliceCameraConfigService;
|
|
|
|
|
|
import com.zhgd.xmgl.modules.project.entity.Project;
|
|
|
|
|
|
import com.zhgd.xmgl.modules.project.service.IProjectService;
|
2025-10-24 17:56:25 +08:00
|
|
|
|
import com.zhgd.xmgl.util.AsyncTaskUtil;
|
2025-11-22 16:40:49 +08:00
|
|
|
|
import com.zhgd.xmgl.util.HikVideoUtil;
|
|
|
|
|
|
import com.zhgd.xmgl.util.PathUtil;
|
2025-07-04 14:57:13 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2025-12-02 18:11:51 +08:00
|
|
|
|
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
|
2025-07-04 14:57:13 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2025-07-25 10:35:03 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
2025-11-22 16:40:49 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2025-07-04 14:57:13 +08:00
|
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
2025-07-25 10:35:03 +08:00
|
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
2025-07-04 14:57:13 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
import java.util.List;
|
2025-11-22 16:40:49 +08:00
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
import java.util.stream.Collectors;
|
2025-07-04 14:57:13 +08:00
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
@RestController
|
|
|
|
|
|
@RequestMapping("xmgl/task")
|
|
|
|
|
|
public class Mcs8Task {
|
|
|
|
|
|
@Lazy
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
IProjectService projectService;
|
|
|
|
|
|
@Lazy
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private IProjectPoliceCameraConfigService projectPoliceCameraConfigService;
|
|
|
|
|
|
@Lazy
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private PoliceCameraManufacturerFactory policeCameraManufacturerFactory;
|
2025-07-25 10:35:03 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
@Qualifier("doubleCarbonExecutor")
|
|
|
|
|
|
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
2025-11-22 16:40:49 +08:00
|
|
|
|
@Lazy
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private IPoliceCameraVideoConfigService policeCameraVideoConfigService;
|
|
|
|
|
|
@Lazy
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private IPoliceCameraItemService policeCameraItemService;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 截取rtsp流的host
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Value("${screenshotUrl:http://127.0.0.1:51111}")
|
|
|
|
|
|
private String screenshotUrl;
|
2025-07-04 14:57:13 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 定时从Mcs8获取执法记录仪文件
|
|
|
|
|
|
*/
|
2025-12-02 18:11:51 +08:00
|
|
|
|
@SchedulerLock(name = "getPoliceCameraItemFile", lockAtMostFor = "PT300S", lockAtLeastFor = "PT120S")
|
2025-07-04 14:57:13 +08:00
|
|
|
|
@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) {
|
2025-10-24 17:56:25 +08:00
|
|
|
|
AsyncTaskUtil.runAsync(() -> {
|
2025-07-25 10:35:03 +08:00
|
|
|
|
manufacturer.pullFile(DateUtil.offsetDay(now, -7), now);
|
2025-10-24 17:56:25 +08:00
|
|
|
|
});
|
2025-07-04 14:57:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.error("定时从Mcs8获取执法记录仪文件err,{}", project.getProjectName(), e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-22 16:40:49 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 更新执法记录仪的封面
|
|
|
|
|
|
*/
|
2025-12-02 18:11:51 +08:00
|
|
|
|
@SchedulerLock(name = "updatePoliceCameraItemCoverUrl", lockAtMostFor = "PT600S", lockAtLeastFor = "PT300S")
|
2025-11-22 16:40:49 +08:00
|
|
|
|
@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;
|
|
|
|
|
|
}
|
2025-07-04 14:57:13 +08:00
|
|
|
|
|
2025-11-05 17:32:55 +08:00
|
|
|
|
// /**
|
|
|
|
|
|
// * 定时更新执法仪设备状态
|
|
|
|
|
|
// */
|
|
|
|
|
|
// @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);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
2025-07-04 14:57:13 +08:00
|
|
|
|
|
|
|
|
|
|
}
|