VaServerCheckRunner.java 3.71 KB
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);
        }
    }
}