监控状态更新频率改成10秒
This commit is contained in:
parent
ed4e521948
commit
9db2d419bd
@ -0,0 +1,9 @@
|
||||
package com.zhgd.xmgl.modules.project.entity.bo;
|
||||
|
||||
import com.zhgd.xmgl.modules.project.entity.ProjectVideoConfig;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ProjectVideoConfigBo extends ProjectVideoConfig {
|
||||
private String ids;
|
||||
}
|
||||
@ -1,9 +1,12 @@
|
||||
package com.zhgd.xmgl.modules.project.mapper;
|
||||
|
||||
import com.zhgd.xmgl.modules.project.entity.bo.ProjectVideoConfigBo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import com.zhgd.xmgl.modules.project.entity.ProjectVideoConfig;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description: 视频配置
|
||||
* @author: pds
|
||||
@ -12,5 +15,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
*/
|
||||
@Mapper
|
||||
public interface ProjectVideoConfigMapper extends BaseMapper<ProjectVideoConfig> {
|
||||
|
||||
/**
|
||||
* 查询所有的开启的ISC的配置
|
||||
* @return
|
||||
*/
|
||||
List<ProjectVideoConfigBo> getIscConfigsGroupByConfig();
|
||||
}
|
||||
|
||||
@ -1,5 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.zhgd.xmgl.modules.project.mapper.ProjectVideoConfigMapper">
|
||||
|
||||
</mapper>
|
||||
<select id="getIscConfigsGroupByConfig" resultType="com.zhgd.xmgl.modules.project.entity.bo.ProjectVideoConfigBo">
|
||||
select group_concat(distinct id) ids,account, password, app_id, app_secret
|
||||
from project_video_config
|
||||
where is_enable = 1
|
||||
and video_type = 3
|
||||
and account != ''
|
||||
and password != ''
|
||||
and app_id != ''
|
||||
and app_secret != ''
|
||||
and account is not null
|
||||
and password is not null
|
||||
and app_id is not null
|
||||
and app_secret is not null
|
||||
group by account, password, app_id, app_secret
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
@ -1,13 +1,22 @@
|
||||
package com.zhgd.xmgl.task;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.zhgd.xmgl.async.AsyncEnvironment;
|
||||
import com.zhgd.xmgl.modules.project.entity.ProjectVideoConfig;
|
||||
import com.zhgd.xmgl.modules.project.entity.bo.ProjectVideoConfigBo;
|
||||
import com.zhgd.xmgl.modules.project.enums.ProjectVideoConfigVideoTypeEnum;
|
||||
import com.zhgd.xmgl.modules.project.mapper.ProjectVideoConfigMapper;
|
||||
import com.zhgd.xmgl.modules.video.entity.VideoItem;
|
||||
import com.zhgd.xmgl.modules.video.mapper.VideoItemMapper;
|
||||
import com.zhgd.xmgl.modules.video.service.impl.VideoItemServiceImpl;
|
||||
import com.zhgd.xmgl.util.HikVideoUtil;
|
||||
import com.zhgd.xmgl.util.YsVideoUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -15,11 +24,13 @@ import net.javacrumbs.shedlock.core.SchedulerLock;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @program: wisdomSite
|
||||
@ -41,6 +52,9 @@ public class VideoTask {
|
||||
private String videoAnalysisUrl;
|
||||
@Value("${serverUrl}")
|
||||
private String serverUrl;
|
||||
@Lazy
|
||||
@Autowired
|
||||
private VideoItemServiceImpl videoItemService;
|
||||
|
||||
@SchedulerLock(name = "updateVideoState", lockAtMostFor = 1000 * 60 * 60, lockAtLeastFor = 1000 * 60 * 5)
|
||||
@Scheduled(cron = "0 */3 * * * ?")
|
||||
@ -56,7 +70,7 @@ public class VideoTask {
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("error:",e);
|
||||
log.error("error:", e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,7 +87,7 @@ public class VideoTask {
|
||||
videoItem.setDeviceState(state);
|
||||
videoItemMapper.updateById(videoItem);
|
||||
}
|
||||
}else if (Objects.equals(videoConfig.getVideoType(), ProjectVideoConfigVideoTypeEnum.ISC.getValue())) {
|
||||
} else if (Objects.equals(videoConfig.getVideoType(), ProjectVideoConfigVideoTypeEnum.ISC.getValue())) {
|
||||
if (StringUtils.isNotEmpty(videoConfig.getAccount()) && StringUtils.isNotEmpty(videoConfig.getPassword())
|
||||
&& StringUtils.isNotEmpty(videoConfig.getAppId()) && StringUtils.isNotEmpty(videoConfig.getAppSecret())) {
|
||||
for (VideoItem videoItem : list) {
|
||||
@ -106,6 +120,83 @@ public class VideoTask {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 高频更新isc监控状态
|
||||
*/
|
||||
@Scheduled(cron = "*/10 * * * * ?")
|
||||
@RequestMapping("updateIscVideoState")
|
||||
public void updateIscVideoState() {
|
||||
try {
|
||||
List<ProjectVideoConfigBo> projectVideoConfigs = projectVideoConfigMapper.getIscConfigsGroupByConfig();
|
||||
Map<Long, List<VideoItem>> videoIdMap = videoItemService.list(new LambdaQueryWrapper<VideoItem>()
|
||||
.select(VideoItem::getItemId, VideoItem::getVideoId, VideoItem::getDeviceState, VideoItem::getSerialNumber))
|
||||
.stream()
|
||||
.collect(Collectors.groupingBy(VideoItem::getVideoId));
|
||||
for (ProjectVideoConfigBo videoConfig : projectVideoConfigs) {
|
||||
// 收集需要更新的视频设备
|
||||
List<VideoItem> videoItemsToUpdate = new ArrayList<>();
|
||||
JSONObject resultJo;
|
||||
JSONArray alls = new JSONArray();
|
||||
int pageNo = 0;
|
||||
if (StrUtil.isBlank(videoConfig.getIds())) {
|
||||
continue;
|
||||
}
|
||||
List<VideoItem> itemDbs = StrUtil.split(videoConfig.getIds(), ",").stream().flatMap(vid -> {
|
||||
List<VideoItem> videoItems = videoIdMap.get(Convert.toLong(vid));
|
||||
if (CollUtil.isNotEmpty(videoItems)) {
|
||||
return videoItems.stream();
|
||||
}
|
||||
return null;
|
||||
}).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
if (CollUtil.isEmpty(itemDbs)) {
|
||||
continue;
|
||||
}
|
||||
do {
|
||||
resultJo = HikVideoUtil.callPostApiOnlineStatus(videoConfig.getAccount(),
|
||||
videoConfig.getPassword(), videoConfig.getAppId(),
|
||||
videoConfig.getAppSecret(), ++pageNo);
|
||||
if (resultJo != null && "0".equals(resultJo.getString("code"))) {
|
||||
alls.addAll(resultJo.getJSONObject("data").getJSONArray("list"));
|
||||
} else {
|
||||
log.error("高频更新isc监控状态返回错误:项目sn:{}:code:{},msg:{}",
|
||||
videoConfig.getProjectSn(), resultJo != null ? resultJo.getString("code") : "",
|
||||
resultJo != null ? resultJo.getString("msg") : "");
|
||||
break;
|
||||
}
|
||||
} while (pageNo * 1000 < resultJo.getJSONObject("data").getInteger("total"));
|
||||
|
||||
Map<String, JSONObject> indexCodeMap = alls.stream().collect(Collectors.toMap(j -> ((JSONObject) j).getString("indexCode"), j -> ((JSONObject) j), (o1, o2) -> o1));
|
||||
for (VideoItem videoItem : itemDbs) {
|
||||
JSONObject jsonObject = indexCodeMap.get(videoItem.getSerialNumber());
|
||||
Integer newOnlineStatus = jsonObject != null && Objects.equals(jsonObject.getInteger("online"), 1) ? 1 : 2;
|
||||
// 只有当设备状态发生变化时才更新
|
||||
if (!newOnlineStatus.equals(videoItem.getDeviceState())) {
|
||||
videoItem.setDeviceState(newOnlineStatus);
|
||||
videoItemsToUpdate.add(videoItem);
|
||||
}
|
||||
}
|
||||
// 批量更新
|
||||
if (CollUtil.isNotEmpty(videoItemsToUpdate)) {
|
||||
List<Long> onlineIds = videoItemsToUpdate.stream().filter(v -> Objects.equals(v.getDeviceState(), 1)).map(VideoItem::getItemId).collect(Collectors.toList());
|
||||
if (CollUtil.isNotEmpty(onlineIds)) {
|
||||
videoItemService.update(null, new LambdaUpdateWrapper<VideoItem>()
|
||||
.set(VideoItem::getDeviceState, 1)
|
||||
.in(VideoItem::getItemId, onlineIds)
|
||||
);
|
||||
} List<Long> offlineIds = videoItemsToUpdate.stream().filter(v -> Objects.equals(v.getDeviceState(), 2)).map(VideoItem::getItemId).collect(Collectors.toList());
|
||||
if (CollUtil.isNotEmpty(offlineIds)) {
|
||||
videoItemService.update(null, new LambdaUpdateWrapper<VideoItem>()
|
||||
.set(VideoItem::getDeviceState, 2)
|
||||
.in(VideoItem::getItemId, offlineIds)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("高频更新isc监控状态错误", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendVideoData(VideoItem videoItem, ProjectVideoConfig videoConfig) {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
Map<String, Object> video = new HashMap<>(16);
|
||||
|
||||
@ -149,6 +149,26 @@ public class HikVideoUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据条件获取监控点在线状态接口
|
||||
*/
|
||||
public static JSONObject callPostApiOnlineStatus(String Ip, String port, String appke, String appSecret, int pageNo) {
|
||||
final String getCamsApi = ARTEMIS_PATH + "/api/nms/v1/online/camera/get";
|
||||
Map<String, Object> paramMap = new HashMap<String, Object>();// post请求Form表单参数
|
||||
paramMap.put("pageNo", pageNo);
|
||||
paramMap.put("pageSize", 1000);
|
||||
String body = JSON.toJSON(paramMap).toString();
|
||||
Map<String, String> path = new HashMap<String, String>(2) {
|
||||
{
|
||||
put("https://", getCamsApi);
|
||||
}
|
||||
};
|
||||
String host = Ip + ":" + port;
|
||||
String result = doPostStringArtemis(host, path, body, null, null, "application/json", appke, appSecret);
|
||||
log.debug("callPostApiOnlineStatus:{}", result);
|
||||
return JSONObject.parseObject(result);
|
||||
}
|
||||
|
||||
public static String setSubscriptionByEvent(String backUrl, String Ip, String port, String appke, String appSecret, Integer[] eventTypes) {
|
||||
String message = null;
|
||||
final String getCamsApi = ARTEMIS_PATH + "/api/eventService/v1/eventSubscriptionByEventTypes";
|
||||
@ -279,7 +299,7 @@ public class HikVideoUtil {
|
||||
}
|
||||
|
||||
public static String doPostStringArtemis(String host, Map<String, String> path, String body, Map<String, String> querys, String accept, String contentType, String appKey, String appSecret) {
|
||||
log.info("海康isc调用http开始>>>>>>>>> host:{},path:{},body:{}", host, JSON.toJSONString(path), body);
|
||||
log.debug("海康isc调用http开始>>>>>>>>> host:{},path:{},body:{}", host, JSON.toJSONString(path), body);
|
||||
String httpSchema = (String) path.keySet().toArray()[0];
|
||||
if (httpSchema != null && !StringUtils.isEmpty(httpSchema)) {
|
||||
String responseStr = null;
|
||||
@ -307,7 +327,7 @@ public class HikVideoUtil {
|
||||
} catch (Exception var10) {
|
||||
log.error("海康isc调用http错误>>>>>>>" + var10);
|
||||
}
|
||||
log.info("海康isc调用http结果>>>>> {}", responseStr);
|
||||
log.debug("海康isc调用http结果>>>>> {}", responseStr);
|
||||
return responseStr;
|
||||
} else {
|
||||
throw new RuntimeException("http和https参数错误httpSchema: " + httpSchema);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user