Gb1400KeepAlive.java 3.08 KB
package com.viontech.scheduled;

import com.alibaba.fastjson.JSONObject;
import com.viontech.config.Gb1400Config;
import com.viontech.constant.Gb1400Constants;
import com.viontech.service.Gb1400Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Slf4j
@Component
public class Gb1400KeepAlive {
    @Autowired
    private Gb1400Config gb1400Config;
    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private Gb1400Service gb1400Service;
    private String registerLock = new String("REGISTERLOCK");

    @Scheduled(cron = "10 * * * * ?")
    public void keepalive() {
        if("0".equals(gb1400Config.getEnable())){
            return;
        }
        if (!Gb1400Constants.LinkStatus) {
            return;
        }
        //保活
        String lkh = gb1400Config.getUserIdentify();
        if(StringUtils.isBlank(lkh)){
            return;
        }
        try{
            HttpHeaders headers = new HttpHeaders();
            headers.add("User-Identify",lkh);
            headers.add("Content-Type","application/VIID+JSON;charset=utf-8");
//                String currentTime = DateUtil.getCurrentTime(DateUtil.TIMESTAMP_NO_DELIMITER);
//                headers.add("User-SendTime", currentTime);
//                String content = "{\"DeviceID\":\""+lkh+"\"}";
            String content = "{\"KeepaliveObject\":{\"DeviceID\":\""+lkh+"\"}}";
            HttpEntity<String> entity = new HttpEntity<>(content, headers);
            String url = "http://"+gb1400Config.getIpPort()+"/VIID/System/Keepalive";
            ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, entity, String.class);
            String body = postForEntity.getBody();
            log.info("keepalive.result:{}", body);
            JSONObject aliveResult = JSONObject.parseObject(body);
            if (aliveResult != null && aliveResult.getJSONObject("ResponseStatusObject") != null) {
                JSONObject responseStatusObject = aliveResult.getJSONObject("ResponseStatusObject");
                if (!"0".equals(responseStatusObject.getString("StatusCode"))) {
                    Gb1400Constants.LinkStatus = false;
                }
            }
        }catch (Exception e){
            Gb1400Constants.LinkStatus = false;
            log.error("keepalive.Exception",e);
        }
    }

    @Scheduled(cron = "0/30 * * * * ? ")
    public void register() throws Exception{
        if("0".equals(gb1400Config.getEnable())){
            return;
        }
        synchronized (registerLock) {
            if(!Gb1400Constants.LinkStatus) {
                gb1400Service.register(gb1400Config.getUserIdentify());
            } else {
                return;
            }
        }
    }
}