package com.xhpc.pp.server;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Map;
import java.util.Set;

import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.evcs.dto.ConnectorStatusInfo.OFF_LINE;
import static com.xhpc.pp.config.EarlierBeanConf.getLocalIPAndPort;
import static com.xhpc.pp.logic.RegisterLogic.DISCONNECTED;

@Component
public class HBCheckTask {

    @Scheduled(fixedRate = 10000)
    protected void run() {

        String svcSrvKey = "svcSrvGuns:".concat(getLocalIPAndPort());
        Set<String> cacheGunkeySet = REDIS.getCacheSet(svcSrvKey);
        Long now = Calendar.getInstance().getTimeInMillis();
        for (String gunkey : cacheGunkeySet) {
            Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
            Long hbtime = (Long) cacheGun.get("hbtime");
            hbtime = hbtime == null ? 0 : hbtime;
            if ((now - hbtime) > 75000) {
                cacheGun.put("statusInt", OFF_LINE);
                cacheGun.put("status", DISCONNECTED);
                REDIS.setCacheMap(gunkey, cacheGun);
            }
        }
    }

}
