package com.example.demo.utils;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.example.demo.dao.ArticleRepository;
|
import com.example.demo.dao.WeatherRepository;
|
import com.example.demo.service.WeatherService;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.client.RestTemplate;
|
import xyz.erupt.core.util.DateUtil;
|
|
import javax.annotation.Resource;
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.*;
|
import java.util.concurrent.TimeUnit;
|
|
|
@Component
|
public class SchedulerUtil {
|
|
private static Logger logger = LoggerFactory.getLogger(SchedulerUtil.class);
|
|
@Autowired
|
private ArticleRepository articleRepository;
|
|
@Autowired
|
private WeatherService weatherService;
|
|
@Autowired
|
private RestTemplate restTemplate;
|
|
@Value("${gis.staturl}")
|
private String staturl;
|
|
@Value("${gis.cacu_cover_rate_url}")
|
private String cacuCoverRateUrl;
|
|
@Value("${gis.recordurl}")
|
private String recordurl;
|
|
@Value("${gis.getfreqsurl}")
|
private String getfreqsurl;
|
|
@Value("${gis.dealdataurl}")
|
private String dealdataurl;
|
|
|
// @Scheduled(fixedDelay = 300*1000)
|
public void checkUsermlTask() {
|
|
}
|
|
@Scheduled(cron = "00 05 * * * ?")
|
public void updateUserTokenTask() {
|
logger.info("-------------Scheduled---------------getWeather");
|
weatherService.getWeather();
|
}
|
|
/**
|
* 统计频率指标 每小时的1分30秒执行一次
|
*/
|
@Scheduled(cron = "30 01 * * * ?")
|
// @Scheduled(fixedDelay = 20*1000)
|
public void getRemoteToken() {
|
logger.info("-------------Scheduled---------------statByHour");
|
// ?bid=statByHour&starttime=#{starttime}&endtime=#{endtime}
|
String strength_stat_url = staturl + "?bid=statByHour&starttime=#{starttime}&endtime=#{endtime}";
|
LocalDateTime localDateTime = LocalDateTime.now();
|
String timeStr2= localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
|
String timeStr1= localDateTime.plusHours(-1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
|
RestTemplateUtil.getDataByRestTemplate(strength_stat_url.replace("#{starttime}",timeStr1).replace("#{endtime}",timeStr2), restTemplate);
|
}
|
|
/**
|
* 计算频率压制覆盖率 每小时的1分01秒执行一次
|
*/
|
@Scheduled(cron = "01 01 * * * ?")
|
// @Scheduled(fixedDelay = 20*1000)
|
public void setFreqCoverRate() {
|
logger.info("-------------Scheduled---------------cacuCoverRate");
|
// ?bid=statByHour&starttime=#{starttime}&endtime=#{endtime}
|
RestTemplateUtil.getDataByRestTemplate(cacuCoverRateUrl, restTemplate);
|
}
|
|
/**
|
* 创建评分任务
|
*/
|
// @Scheduled(cron = "30 10 1 * * ?")
|
@Scheduled(fixedDelay = 300*1000)
|
public void createAssessTask() {
|
logger.info("-------------Scheduled---------------autoCreateAssessTask");
|
String strength_stat_url = staturl + "?bid=autoCreateAssessTask&starttime=#{starttime}&endtime=#{endtime}";
|
LocalDateTime localDateTime = LocalDateTime.now();
|
String timeStr2= localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
|
String timeStr1= localDateTime.plusMinutes(-5L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
|
RestTemplateUtil.getDataByRestTemplate(strength_stat_url.replace("#{starttime}",timeStr1).replace("#{endtime}",timeStr2), restTemplate);
|
}
|
|
@Scheduled(fixedDelay = 300*1000)
|
public void autoRecordTask() {
|
logger.info("-------------Scheduled---------------autoRecordTask \\r\\n");
|
String strength_stat_url = staturl + "?bid=toRecordList";
|
JSONObject retjson = RestTemplateUtil.getDataByRestTemplate(strength_stat_url, restTemplate);
|
JSONArray jsonArr = retjson.getJSONObject("data").getJSONArray("data").getJSONArray(0);
|
if(jsonArr.size() == 0) {
|
return ;
|
}
|
StringBuffer monitorpoint_ids = new StringBuffer();
|
StringBuffer ids = new StringBuffer();
|
StringBuffer feqs = new StringBuffer();
|
for (int i = 0; i < jsonArr.size(); i++) {
|
JSONObject jsonObject = jsonArr.getJSONObject(i);
|
feqs.append(i==0 ? "" :",").append(jsonObject.getString("frequency"));
|
String monitorpoint_id = jsonObject.getString("monitorpoint_id");
|
monitorpoint_ids.append(i==0 ? "" :",").append(monitorpoint_id);
|
ids.append(i==0 ? "" :",").append(jsonObject.get("id").toString());
|
}
|
logger.info("record points:"+monitorpoint_ids.toString());
|
String recordUrl_ = recordurl + "&method=record"+"&frequencyValue="+feqs.toString()+"&monitorpointId="+monitorpoint_ids.toString()+"&isUnderWay=0&recordTimeLength=1";
|
JSONObject retjson_ = RestTemplateUtil.getDataByRestTemplate(recordUrl_, restTemplate);
|
JSONArray jsons = retjson_.getJSONArray("data");
|
for (int i = 0; i < jsons.size(); i++) {
|
JSONObject recordResult = jsons.getJSONObject(i);
|
Map<String, String> params = new HashMap<String, String>();
|
params.put("bid","setChannelRecord");
|
params.put("frequency",recordResult.getString("frequencyValue"));
|
params.put("monitorpointId",recordResult.getString("monitorpointId"));
|
params.put("userid","admin");
|
params.put("requesturl",recordUrl_);
|
params.put("success",recordResult.getBoolean("success").toString());
|
params.put("msg",recordResult.getString("msg"));
|
params.put("flvurl","");
|
RestTemplateUtil.postDataByRestTemplate(dealdataurl,params, restTemplate);
|
if(recordResult.getBoolean("success")) {
|
String nowTime = DateUtil.getSimpleFormatDate(new Date());
|
RestTemplateUtil.getDataByRestTemplate(staturl + "?bid=updateRecordTime&recordtime="+nowTime+"&id="+ids.toString(), restTemplate);
|
}
|
}
|
|
logger.info("--------------------------------over-----------------------------");
|
}
|
|
// @Scheduled(cron = "01 00 * * * ?")
|
@Scheduled(fixedDelay = 18000*1000)
|
public void autoRecordAllFreqs() { //getfreqsurl
|
logger.info("-------------Scheduled---------------autoRecordAllFreqs \\r\\n");
|
LocalDateTime localDateTime = LocalDateTime.now();
|
String wdate = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
String whour = localDateTime.getHour()<10 ? "0"+localDateTime.getHour() : ""+localDateTime.getHour();
|
String getPoints_url = staturl+"?bid=getPointData&frequency=1557&wdate="+wdate+"&whour="+whour+"&userid=admin";
|
JSONObject retjson = RestTemplateUtil.getDataByRestTemplate(getPoints_url, restTemplate);
|
JSONArray jsonArr = retjson.getJSONObject("data").getJSONArray("data").getJSONArray(0);
|
StringBuffer feqs = new StringBuffer();
|
StringBuffer pointIds = new StringBuffer();
|
for (int i = 0; i < jsonArr.size(); i++) {
|
JSONObject jsonObject = jsonArr.getJSONObject(i);
|
pointIds.append(i==0 ? "" :",").append(jsonObject.getString("monitorpointId"));
|
feqs.append(i==0 ? "" :",").append("aa");
|
}
|
//获取频率
|
JSONObject freqsjson = RestTemplateUtil.getDataByRestTemplate(getfreqsurl, restTemplate);
|
JSONArray freqs_array = freqsjson.getJSONObject("data").getJSONArray("data").getJSONArray(0);
|
String frequency = "";
|
logger.info("record points:"+pointIds.toString());
|
for (int i = 0; i < freqs_array.size(); i++) {
|
JSONObject freqJson = freqs_array.getJSONObject(i);
|
frequency = freqJson.getString("frequencyValue");
|
String recordUrl_ = recordurl + "&method=record"+"&frequencyValue="+feqs.toString().replaceAll("aa",frequency)+"&monitorpointId="+pointIds.toString()+"&isUnderWay=4&recordTimeLength=4";
|
JSONObject retjson_ = RestTemplateUtil.getDataByRestTemplate(recordUrl_, restTemplate);
|
//记录录制日志
|
JSONArray jsons = retjson_.getJSONArray("data");
|
for (int j = 0; j < jsons.size(); j++) {
|
JSONObject recordResult = jsons.getJSONObject(j);
|
Map<String, String> params = new HashMap<String, String>();
|
params.put("bid","setTempRecord");
|
params.put("frequency",recordResult.getString("frequencyValue"));
|
params.put("monitorpointId",recordResult.getString("monitorpointId"));
|
params.put("userid","admin");
|
params.put("requesturl",recordUrl_);
|
params.put("success",recordResult.getBoolean("success").toString());
|
params.put("msg",recordResult.getString("msg"));
|
params.put("flvurl","");
|
RestTemplateUtil.postDataByRestTemplate(dealdataurl,params, restTemplate);
|
}
|
try {
|
TimeUnit.SECONDS.sleep(5);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
|
logger.info("-------------Scheduled---------------autoRecordAllFreqs over");
|
}
|
|
public Boolean sendCMDtoDevice(String url) {
|
Boolean isSuccess = false;
|
JSONObject retjson = RestTemplateUtil.getDataByRestTemplate(url, restTemplate);
|
if(retjson != null && retjson.getJSONArray("data") !=null && retjson.getJSONArray("data").getJSONObject(0) != null) {
|
logger.info(retjson.getJSONArray("data").getJSONObject(0).toJSONString());
|
isSuccess = retjson.getJSONArray("data").getJSONObject(0).getBoolean("success");
|
}
|
return isSuccess;
|
}
|
|
|
}
|