VaServerCheckRunner.java 2.47 KB
package com.viontech.fanxing.task.runner;

import com.alibaba.fastjson.JSONObject;
import com.viontech.fanxing.task.model.vaserver.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;

/**
 * .
 *
 * @author 谢明辉
 * @date 2021/10/19
 */

@Component
@Slf4j
@Profile("!test")
public class VaServerCheckRunner {

    @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);
                    } else {
                        log.info("设备处于离线状态:{}", devId);
                    }
                } catch (Exception e) {
                    log.info("", e);
                } finally {
                    if (devLock != null) {
                        devLock.forceUnlock();
                    }
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }
    }
}