wangrong
2021-11-24 749179ef60bca3074f8e12325b92b0d406d9246c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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;
    }
 
 
}