package com.fzzy.gateway;
|
|
import com.alibaba.fastjson.JSON;
|
import com.fzzy.api.data.GatewayDeviceProtocol;
|
import com.fzzy.data.ConfigData;
|
import com.fzzy.gateway.api.GatewayRemoteManager;
|
import com.fzzy.gateway.data.BaseReqData;
|
import com.fzzy.gateway.data.WeatherWebDto;
|
import com.fzzy.gateway.entity.GatewayConf;
|
import com.fzzy.gateway.entity.GatewayDevice;
|
import com.fzzy.gateway.service.GatewayConfService;
|
import com.fzzy.gateway.util.GatewayHttpUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang.StringUtils;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.util.Collection;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 网关相关的定时器
|
*/
|
@Slf4j
|
@Component(GatewayTimerScheduled.BEAN_ID)
|
public class GatewayTimerScheduled {
|
|
public static String DEFAULT_URL = "https://v1.yiketianqi.com/api?unescape=1&version=v61&appid={appId}&appsecret={appsecret}&cityid={cityid}";
|
public static String DEFAULT_APP_ID = "49421971";
|
public static String DEFAULT_APP_SECRET = "JmJE48Fv";
|
public static String DEFAULT_CITYID = "101270101";//成都
|
|
|
public static final String BEAN_ID = "gateway.timerScheduled";
|
|
@Resource
|
private GatewayConfService confService;
|
@Resource
|
private GatewayRemoteManager gatewayRemoteManager;
|
@Resource
|
private ConfigData configData;
|
|
|
/**
|
* <p>
|
* 固定时间:每间隔10分钟执行一次
|
*/
|
@Scheduled(cron = "0 0/10 * * * ?")
|
public void scheduled() {
|
|
//网关的心跳执行
|
doHeartbeat();
|
|
}
|
|
/**
|
* <p>
|
* 每间隔3秒执行一次事件
|
*/
|
@Scheduled(cron = "0/3 * * * * ?")
|
public void scheduled3() {
|
|
//执行HTTP地磅请求
|
exeHttpWeightReq();
|
}
|
|
|
/**
|
* 每间隔30分钟执行一次
|
*/
|
@Scheduled(cron = "0 0/30 * * * ?")
|
public void scheduled30() {
|
|
//定时获取气象信息
|
doWeatherExe();
|
}
|
|
public void doWeatherExe() {
|
|
try {
|
String url = configData.getWeatherPath();
|
|
if (null == url) {
|
url = DEFAULT_URL;
|
}
|
Map<String ,String> parameMap = new HashMap<>();
|
parameMap.put("unescape","1");
|
parameMap.put("version","v61");
|
parameMap.put("appid",DEFAULT_APP_ID);
|
parameMap.put("appsecret",DEFAULT_APP_SECRET);
|
parameMap.put("cityid",DEFAULT_CITYID);
|
url = url.replace("{appId}", DEFAULT_APP_ID).replace("{appsecret}", DEFAULT_APP_SECRET).replace("{cityid}", DEFAULT_CITYID);
|
|
url = url.replace("{appId}", DEFAULT_APP_ID).replace("{appsecret}", DEFAULT_APP_SECRET);
|
|
if (StringUtils.isNotEmpty(configData.getCityId())) {
|
url = url.replace("{cityid}", configData.getCityId());
|
} else {
|
url = url.replace("{cityid}", DEFAULT_CITYID);
|
}
|
|
log.debug("------气象请求信息URL---{}", url);
|
String result = GatewayHttpUtil.doGet(url,parameMap);
|
|
if (null == result) {
|
log.error("当前外网获取气象信息失败……");
|
return;
|
}
|
WeatherWebDto dto = JSON.parseObject(result, WeatherWebDto.class);
|
if (StringUtils.isNotEmpty(dto.getErrcode())) {
|
log.error("当前外网获取气象信息异常:{}", dto.getErrmsg());
|
return;
|
}
|
|
WeatherWebDto.contextMap.put("default", dto);
|
|
|
log.info("===========================系统定时获获取气象信息===={}==================", dto);
|
|
} catch (Exception e) {
|
log.error(e.getMessage(),e);
|
}
|
}
|
|
|
/**
|
* 执行网关心跳
|
*/
|
private void doHeartbeat() {
|
|
//获取缓存中的网关信息
|
List<GatewayConf> list = confService.getCacheConfList();
|
if (null == list || list.isEmpty()) {
|
log.warn("------系统为获取到网关配置信息,不执行定时心跳-----");
|
return;
|
}
|
|
for (GatewayConf conf : list) {
|
gatewayRemoteManager.getRemoteService(conf.getPushProtocol()).heartbeat(conf);
|
}
|
}
|
|
|
/**
|
* 执行地磅HTTP请求协议
|
*/
|
private void exeHttpWeightReq() {
|
//获取设备
|
Collection<GatewayDevice> list = GatewayUtils.listDeviceWeight();
|
|
if (null == list || list.isEmpty()) return;
|
|
BaseReqData reqData;
|
for (GatewayDevice device : list) {
|
|
if (!GatewayDeviceProtocol.DEVICE_WEIGHT_HTTP.getCode().equals(device.getSyncProtocol())) {
|
continue;
|
}
|
|
reqData = new BaseReqData();
|
reqData.setDevice(device);
|
reqData.setDeviceName(device.getDeviceName());
|
reqData.setProductId(device.getProductId());
|
reqData.setDeviceId(device.getDeviceId());
|
|
gatewayRemoteManager.getSyncWeightService(device.getSyncProtocol()).syncWeightInfo(reqData);
|
}
|
}
|
|
public static void main(String[] args) {
|
try {
|
Map<String ,String> parameMap = new HashMap<>();
|
parameMap.put("unescape","1");
|
parameMap.put("version","v61");
|
parameMap.put("appid","49421971");
|
parameMap.put("appsecret","JmJE48Fv");
|
parameMap.put("cityid","101270101");
|
String result = GatewayHttpUtil.doGet("https://v1.yiketianqi.com/api?unescape=1&version=v61&appid=49421971&appsecret=JmJE48Fv&cityid=101270101", parameMap);
|
|
|
System.out.println(result);
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
|
}
|
}
|