VaServerCheckRunner.java
3.71 KB
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
package com.viontech.fanxing.task.runner;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.viontech.fanxing.commons.model.main.StreamInfo;
import com.viontech.fanxing.commons.model.main.VaServerInfo;
import com.viontech.fanxing.task.service.VAServerService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RMap;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* .
*
* @author 谢明辉
* @date 2021/10/19
*/
@Component
@Slf4j
@Profile("!test")
public class VaServerCheckRunner {
public static final ConcurrentHashMap<String, StreamInfo> STREAM_INFO_MAP = new ConcurrentHashMap<>();
@Resource
private VAServerService vaServerService;
@Scheduled(cron = "3 * * * * ? ")
public void check() {
try {
RMap<String, VaServerInfo> vaServerInfoMap = vaServerService.getVaServerRedisRepository().getVaServerInfoMap();
Set<Map.Entry<String, VaServerInfo>> entries = vaServerInfoMap.readAllEntrySet();
for (Map.Entry<String, VaServerInfo> entry : entries) {
RLock devLock = null;
try {
String devId = entry.getKey();
VaServerInfo vaServerInfo = entry.getValue();
if (vaServerInfo.getStatus() == 1) {
devLock = vaServerService.getVaServerRedisRepository().getDevLock(devId);
JSONObject status = vaServerService.getStatus(devId);
JSONObject resource = status.getJSONObject("resource");
JSONObject brief = resource.getJSONObject("brief");
float videoResource = brief.getFloatValue("video_total");
float availableResource = brief.getFloatValue("video_free");
vaServerInfo.setAvailableResources(availableResource);
vaServerInfo.setVideoResource(videoResource);
vaServerService.getVaServerRedisRepository().addOrUpdate(devId, vaServerInfo);
// 统计帧率和视频源状态
if (!status.containsKey("tasks")) {
continue;
}
JSONArray tasks = status.getJSONArray("tasks");
for (int i = 0; i < tasks.size(); i++) {
JSONObject jsonObject = tasks.getJSONObject(i);
String taskUnid = jsonObject.getString("task_unid");
Float sourceStreamFrameRate = jsonObject.getFloat("source_stream_frame_rate");
Float analyseStreamFrameRate = jsonObject.getFloat("analyse_stream_frame_rate");
String channelUnid = jsonObject.getString("channel_unid");
StreamInfo streamInfo = new StreamInfo(taskUnid, channelUnid, sourceStreamFrameRate, analyseStreamFrameRate);
STREAM_INFO_MAP.put(taskUnid, streamInfo);
}
} else {
log.info("设备处于离线状态:{}", devId);
}
} catch (Exception e) {
log.info("", e);
} finally {
if (devLock != null) {
devLock.forceUnlock();
}
}
}
} catch (Exception e) {
log.error("", e);
}
}
}