123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package com.jiayue.client.job;
- import cn.hutool.core.convert.Convert;
- import cn.hutool.http.HttpRequest;
- import cn.hutool.http.HttpUtil;
- import cn.hutool.json.JSONArray;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
- import com.jiayue.client.entity.FileCreateLog;
- import com.jiayue.client.service.QueryDataService;
- import com.jiayue.client.util.FileUtil;
- import com.jiayue.client.util.Scpclient;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
- import org.springframework.core.io.PathResource;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Service;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- /**
- * 青海集中功率预测从云平台下载气象文件数据:
- * 1.每天凌晨5点执行,下载当天气象文件传给功率预测服务器
- */
- @Service
- @Slf4j
- public class DownloadFileJob {
- protected final static Properties properties;
- static {
- YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
- yaml.setResources(new PathResource("./focus-client.yml"));
- properties = yaml.getObject();
- }
- public final QueryDataService queryDataService;
- //获取下载到文件,从临时目录移动到目标目录
- public final List<String> filedownLoadList = new ArrayList<>();
- public final List<String> isDownFileId = new ArrayList<>();
- public DownloadFileJob(QueryDataService queryDataService) {
- this.queryDataService = queryDataService;
- }
- /**
- * 每天凌晨2点执行
- */
- @Scheduled(cron = "0 0/5 * * * ?")
- public void download() {
- log.info("上午文件下载定时任务开始");
- List<Map<String, Object>> stations = queryDataService.getStations();
- for (Map<String, Object> station : stations) {
- try {
- String stationCode = Convert.toStr(station.get("C_STATION_CODE"));
- log.info("开始执行定时任务:http下载场站:{} 的文件。", stationCode);
- String body = HttpRequest.get(properties.getProperty("v3cloud.filelogUrl") + stationCode).execute().body();
- JSONObject json = JSONUtil.parseObj(body);
- String code = json.get("code").toString();
- String data = json.get("data").toString();
- if (code.equals("0") && data.length() > 0) {
- JSONArray array = JSONUtil.parseArray(data);
- List<FileCreateLog> list = array.toList(FileCreateLog.class);
- String path = FileUtil.judeDirExists(properties.getProperty("local.downFilePath"));
- for (FileCreateLog fileCreateLog : list) {
- if (isDownFileId.contains(fileCreateLog.getId())) {
- log.info("文件:{}已下载。", fileCreateLog.getFileName());
- continue;
- }
- log.info("开始下载文件:{}。", fileCreateLog.getFileName());
- String url = properties.getProperty("v3cloud.fileDownUrl") + fileCreateLog.getId();
- String targetPath = path + File.separatorChar + stationCode + File.separatorChar + "new" + File.separatorChar + fileCreateLog.getFileName();
- long size = HttpUtil.downloadFile(url, targetPath);
- if (size > 0) {
- isDownFileId.add(fileCreateLog.getId());
- log.debug("成功下载文件:{}", targetPath);
- filedownLoadList.add(targetPath);
- }
- }
- }
- if (!filedownLoadList.isEmpty()) {
- log.debug("开始移动下载文件到功率预测,共:{} 个文件。", filedownLoadList.size());
- for (String filePath : filedownLoadList) {
- if ("flase".equals(properties.getProperty("remote.ip"))) {
- log.warn("备机配置为空,不需要传文件到备机上。");
- break;
- }
- Scpclient scpclient = Scpclient.getInstance(properties.getProperty("remote.ip"),
- Convert.toInt(properties.getProperty("remote.port")), properties.getProperty("remote.user"),
- properties.getProperty("remote.pwd"));
- scpclient.putFile(filePath, properties.getProperty("remote.path") + stationCode + "/new/");
- log.info("移动文件:{} 到远程服务器 {} 成功", filePath, properties.getProperty("remote.path") + stationCode + "/new/");
- }
- log.debug("所有文件移动完成,清空文件临时filedownLoadList");
- filedownLoadList.clear();
- } else {
- log.info("没有新文件下载,不需要移动到远程功率预测服务器。");
- }
- } catch (Exception e) {
- log.error("文件下载失败:{}", e);
- }
- }
- }
- }
|