GetDataTest.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package com.ruoyi;
  2. import cn.hutool.core.date.DateField;
  3. import cn.hutool.core.date.DateRange;
  4. import cn.hutool.core.date.DateTime;
  5. import cn.hutool.core.date.DateUtil;
  6. import cn.hutool.core.text.csv.CsvUtil;
  7. import cn.hutool.core.text.csv.CsvWriter;
  8. import cn.hutool.core.thread.ExecutorBuilder;
  9. import cn.hutool.core.thread.ThreadUtil;
  10. import cn.hutool.core.util.CharsetUtil;
  11. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  12. import com.ruoyi.ucp.entity.GlycSj;
  13. import com.ruoyi.ucp.entity.PointData;
  14. import com.ruoyi.ucp.entity.PointInfo;
  15. import com.ruoyi.ucp.feign.AdapterApi;
  16. import com.ruoyi.ucp.service.IPointInfoService;
  17. import com.ruoyi.web.controller.JavaFunctionJobHandler;
  18. import org.junit.Test;
  19. import org.junit.runner.RunWith;
  20. import org.springframework.boot.test.context.SpringBootTest;
  21. import org.springframework.test.context.junit4.SpringRunner;
  22. import javax.annotation.Resource;
  23. import java.net.URI;
  24. import java.util.*;
  25. import java.util.concurrent.ExecutionException;
  26. import java.util.concurrent.ExecutorService;
  27. import java.util.concurrent.Future;
  28. import java.util.function.Function;
  29. import java.util.stream.Collectors;
  30. @SpringBootTest
  31. @RunWith(SpringRunner.class)
  32. public class GetDataTest {
  33. @Resource
  34. private IPointInfoService pointInfoService;
  35. @Resource
  36. private AdapterApi adapterApi;
  37. @Resource
  38. private JavaFunctionJobHandler task;
  39. @Test
  40. public void test2() {
  41. // DateTime begin = DateUtil.parse("2023-03-21");
  42. DateTime begin = DateUtil.parse("2023-04-18");
  43. // DateTime begin = DateUtil.parse("2024-08-19 11:00:00");
  44. // DateTime end = DateUtil.parse("2024-08-08 14:00:00");
  45. DateTime end = DateUtil.parse("2024-11-03");
  46. // DateTime end = DateUtil.parse("2024-08-19 11:30:00");
  47. // DateTime end = DateUtil.parse("2024-08-08 13:45:00");
  48. //LJS,FSG,JSL,ZZ,XZ,CSL
  49. int i = 1;
  50. DateTime minBegin = begin, minEnd = DateUtil.offsetMinute(begin, 15);
  51. while (minEnd.isBefore(end)) {
  52. minEnd = DateUtil.offsetMinute(begin, i * 15);
  53. task.calcStationSjglAgcPjfsHjwdDlMin(minBegin, minEnd, "GJNY_SXGS_CSL_FDC_STA");
  54. System.out.println(minBegin.toString() + "," + minEnd.toString());
  55. i++;
  56. minBegin = minEnd;
  57. }
  58. }
  59. @Test
  60. public void test5() {
  61. task.getPoint();
  62. }
  63. @Test
  64. public void test6() {
  65. DateTime begin = DateUtil.parse("2023-04-18");
  66. DateTime end = DateUtil.parse("2024-11-03");
  67. //LJS,FSG,JSL
  68. int i = 1;
  69. DateTime minBegin = begin, minEnd = DateUtil.offsetMinute(begin, 15);
  70. while (minEnd.isBefore(end)) {
  71. minEnd = DateUtil.offsetMinute(begin, i * 15);
  72. task.calcStationMinZtts(minEnd, "GJNY_SXGS_JSL_FDC_STA");
  73. System.out.println(minBegin.toString() + "," + minEnd.toString());
  74. i++;
  75. minBegin = minEnd;
  76. }
  77. }
  78. @Test
  79. public void test3() {
  80. DateTime start = DateUtil.parse("2024-10-31");
  81. DateTime end = DateUtil.parse("2024-11-03");
  82. DateRange range = DateUtil.range(start, end, DateField.DAY_OF_YEAR);
  83. for (DateTime date : range) {
  84. task.calcPredictExamin(date);
  85. }
  86. }
  87. @Test
  88. public void test() {
  89. DateTime start = DateUtil.parse("2023-03-20");
  90. DateTime end = DateUtil.parse("2024-10-16");
  91. // DateTime start = DateUtil.offsetMonth(end, -30);
  92. // String[] names = {"AI004", "AI005", "AI006", "AI007", "AI008", "AI009", "AI073", "AI037", "AI039", "AI038", "AI047", "AI048", "AI056", "AI057", "AI060", "DI022", "AI066", "AI067", "AI069", "AI072", "AI074", "DI025", "DI080", "AI082", "AI084", "AI083", "AI065","AI114"};
  93. String[] names = {"AI114", "AI066", "AI060", "AI067", "AI082", "AI073", "AI004", "AI047", "AI072", "AI074"};
  94. // String[] names = {"AI067"};
  95. QueryWrapper<PointInfo> wrapper = new QueryWrapper<>();
  96. wrapper.lambda().eq(PointInfo::getInstitutionType, "turbine")
  97. // .eq(PointInfo::getTurbineId, "GJNY_SXGS_LJS_F_WT_0011_EQ")
  98. .in(PointInfo::getUniformCode, names);
  99. List<PointInfo> list = pointInfoService.list(wrapper);
  100. // String a = "GJNY_SXGS_ZZ_F_WT_0005_EQGJNY_SXGS_ZZ_F_WT_0028_EQGJNY_SXGS_ZZ_F_WT_0022_EQGJNY_SXGS_ZZ_F_WT_0045_EQGJNY_SXGS_ZZ_F_WT_0038_EQGJNY_SXGS_ZZ_F_WT_0035_EQGJNY_SXGS_ZZ_F_WT_0050_EQGJNY_SXGS_ZZ_F_WT_0019_EQGJNY_SXGS_ZZ_F_WT_0012_EQGJNY_SXGS_ZZ_F_WT_0002_EQGJNY_SXGS_ZZ_F_WT_0031_EQ";
  101. // String a = "GJNY_SXGS_FSG_FDC_STA";
  102. String a = "GJNY_SXGS_FSG_F_WT_0022_EQ\n" +
  103. "GJNY_SXGS_FSG_F_WT_0064_EQ\n";
  104. // list=list.stream().filter(pi->a.equals(pi.getStationId())).collect(Collectors.toList());
  105. list = list.stream().filter(pi -> a.contains(pi.getTurbineId())).collect(Collectors.toList());
  106. // list = list.stream().filter(pi -> "GJNY_SXGS_LJS_F_WT_0007_EQ".equals(pi.getTurbineId())).collect(Collectors.toList());
  107. Map<String, Map<String, List<PointInfo>>> collect = list.stream().collect(Collectors.groupingBy(PointInfo::getStationId, Collectors.groupingBy(PointInfo::getTurbineId)));
  108. ExecutorService executor = ExecutorBuilder.create()
  109. .setCorePoolSize(3)
  110. .setMaxPoolSize(3)
  111. .build();
  112. Vector<Future<String>> fts = new Vector<>();
  113. Vector<Future<String>> ftfs = new Vector<>();
  114. collect.forEach((stId, tbPiMap) -> {
  115. tbPiMap.forEach((tbId, Pis) -> {
  116. Future<String> future = executor.submit(() -> {
  117. List<PointData> zt = task.calcTurbineSimpleZt(start, end, 60 * 5, tbId);
  118. Map<String, GlycSj> map = new TreeMap<>();
  119. for (PointInfo info : Pis) {
  120. try {
  121. long j = start.getTime();
  122. List<PointData> s = adapterApi.getHistorySnap(URI.create("http://172.16.12.103:8017/ts"), info.getPointKey(),
  123. j, end.getTime(), 60 * 5);
  124. String dt = null;
  125. GlycSj glycSj = null;
  126. //AI066 风速,AI060 发电机转速,AI065 风机状态点,AI067 风向,AI082 桨叶1角度,AI073 机舱方向/对风角度
  127. //AI004 U1绕组温度,AI047 电机温度2,AI072 环境温度,AI074 机舱温度
  128. if (s == null || s.isEmpty()) {
  129. System.out.println(tbId + "," + info.getUniformCode());
  130. }
  131. switch (info.getUniformCode()) {
  132. case "AI114":
  133. for (int i = 0; i < s.size(); i++) {
  134. dt = DateUtil.formatDateTime(new Date(j));
  135. glycSj = map.get(dt);
  136. if (glycSj == null) {
  137. glycSj = new GlycSj();
  138. map.put(dt, glycSj);
  139. }
  140. glycSj.setGl(s.get(i).getValue());
  141. glycSj.setZt(zt.get(i) == null ? null : zt.get(i).getValue());
  142. // glycSj.setActualTime(DateUtil.formatDateTime(DateUtil.date(zt.get(i)==null?0L:zt.get(i).getTs())));
  143. j += 60 * 1000 * 5;//GJNY_SXGS_LJS_F_WT_
  144. // glycSj.setTurbineId(info.getTurbineId().replaceFirst("GJNY_SXGS_","").replaceFirst("F_WT_","").replaceFirst("_EQ",""));
  145. glycSj.setTurbineId(info.getTurbineId().replaceFirst("GJNY_SXGS_.+_F_WT_", "").replaceFirst("_EQ", ""));
  146. }
  147. break;
  148. case "AI066":
  149. for (int i = 0; i < s.size(); i++) {
  150. dt = DateUtil.formatDateTime(new Date(j));
  151. glycSj = map.get(dt);
  152. if (glycSj == null) {
  153. glycSj = new GlycSj();
  154. map.put(dt, glycSj);
  155. }
  156. glycSj.setFs(s.get(i).getValue());
  157. j += 60 * 1000 * 5;
  158. }
  159. break;
  160. case "AI060":
  161. for (int i = 0; i < s.size(); i++) {
  162. dt = DateUtil.formatDateTime(new Date(j));
  163. glycSj = map.get(dt);
  164. if (glycSj == null) {
  165. glycSj = new GlycSj();
  166. map.put(dt, glycSj);
  167. }
  168. glycSj.setFdjzs(s.get(i).getValue());
  169. j += 60 * 1000 * 5;
  170. }
  171. break;
  172. case "AI065":
  173. for (int i = 0; i < s.size(); i++) {
  174. dt = DateUtil.formatDateTime(new Date(j));
  175. glycSj = map.get(dt);
  176. if (glycSj == null) {
  177. glycSj = new GlycSj();
  178. map.put(dt, glycSj);
  179. }
  180. glycSj.setZt(s.get(i).getValue());
  181. j += 60 * 1000 * 5;
  182. }
  183. break;
  184. case "AI067":
  185. for (int i = 0; i < s.size(); i++) {
  186. dt = DateUtil.formatDateTime(new Date(j));
  187. glycSj = map.get(dt);
  188. if (glycSj == null) {
  189. glycSj = new GlycSj();
  190. map.put(dt, glycSj);
  191. }
  192. glycSj.setFx(s.get(i).getValue());
  193. j += 60 * 1000 * 5;
  194. }
  195. break;
  196. case "AI082":
  197. for (int i = 0; i < s.size(); i++) {
  198. dt = DateUtil.formatDateTime(new Date(j));
  199. glycSj = map.get(dt);
  200. if (glycSj == null) {
  201. glycSj = new GlycSj();
  202. map.put(dt, glycSj);
  203. }
  204. glycSj.setJyjd(s.get(i).getValue());
  205. j += 60 * 1000 * 5;
  206. }
  207. break;
  208. case "AI073":
  209. for (int i = 0; i < s.size(); i++) {
  210. dt = DateUtil.formatDateTime(new Date(j));
  211. glycSj = map.get(dt);
  212. if (glycSj == null) {
  213. glycSj = new GlycSj();
  214. map.put(dt, glycSj);
  215. }
  216. glycSj.setDfjd(s.get(i).getValue());
  217. j += 60 * 1000 * 5;
  218. }
  219. break;
  220. case "AI004":
  221. for (int i = 0; i < s.size(); i++) {
  222. dt = DateUtil.formatDateTime(new Date(j));
  223. glycSj = map.get(dt);
  224. if (glycSj == null) {
  225. glycSj = new GlycSj();
  226. map.put(dt, glycSj);
  227. }
  228. glycSj.setNjwd(s.get(i).getValue());
  229. j += 60 * 1000 * 5;
  230. }
  231. break;
  232. case "AI047":
  233. for (int i = 0; i < s.size(); i++) {
  234. dt = DateUtil.formatDateTime(new Date(j));
  235. glycSj = map.get(dt);
  236. if (glycSj == null) {
  237. glycSj = new GlycSj();
  238. map.put(dt, glycSj);
  239. }
  240. glycSj.setDjwd(s.get(i).getValue());
  241. j += 60 * 1000 * 5;
  242. }
  243. break;
  244. case "AI072":
  245. for (int i = 0; i < s.size(); i++) {
  246. dt = DateUtil.formatDateTime(new Date(j));
  247. glycSj = map.get(dt);
  248. if (glycSj == null) {
  249. glycSj = new GlycSj();
  250. map.put(dt, glycSj);
  251. }
  252. glycSj.setHjwd(s.get(i).getValue());
  253. j += 60 * 1000 * 5;
  254. }
  255. break;
  256. case "AI074":
  257. for (int i = 0; i < s.size(); i++) {
  258. dt = DateUtil.formatDateTime(new Date(j));
  259. glycSj = map.get(dt);
  260. if (glycSj == null) {
  261. glycSj = new GlycSj();
  262. map.put(dt, glycSj);
  263. }
  264. glycSj.setJcwd(s.get(i).getValue());
  265. j += 60 * 1000 * 5;
  266. }
  267. break;
  268. }
  269. map.forEach((k, v) -> {
  270. v.setTime(k);
  271. });
  272. } catch (Exception e) {
  273. e.printStackTrace();
  274. }
  275. }
  276. Future<String> b = ThreadUtil.execAsync(() -> {
  277. writerData(map.values(), stId, tbId);
  278. return "ok";
  279. });
  280. ftfs.add(b);
  281. return "ok";
  282. });
  283. fts.add(future);
  284. });
  285. });
  286. for (Future<String> ft : fts) {
  287. try {
  288. ft.get();
  289. } catch (InterruptedException e) {
  290. throw new RuntimeException(e);
  291. } catch (ExecutionException e) {
  292. throw new RuntimeException(e);
  293. }
  294. }
  295. for (Future<String> ft : ftfs) {
  296. try {
  297. ft.get();
  298. } catch (InterruptedException e) {
  299. throw new RuntimeException(e);
  300. } catch (ExecutionException e) {
  301. throw new RuntimeException(e);
  302. }
  303. }
  304. //按行写出
  305. // writer.write(new String[]{"时间", "时间戳", "对齐时间戳", "值"});
  306. /*writer.write(new String[]{"时间", "时间戳", "对齐时间戳", "值"});
  307. for (PointInfo pi : list) {
  308. if (pi.getPointKey().equals("INITIAL")) {
  309. //指定路径和编码
  310. writer = CsvUtil.getWriter("D:/数据/" + pi.getTurbineId() + "/" + pi.getName() + "无测点" +
  311. ".csv", CharsetUtil.CHARSET_GBK);
  312. continue;
  313. }
  314. List<PointData> s = null;
  315. try {
  316. s = adapterApi.getHistorySnap(URI.create("http://172.16.12.103:8011/ts"), pi.getPointKey(),
  317. start.getTime(), end.getTime(), 60*5);
  318. } catch (Exception e) {
  319. e.printStackTrace();
  320. //指定路径和编码
  321. writer = CsvUtil.getWriter("D:/数据/" + pi.getTurbineId() + "/" + pi.getName() + "获取数据出错" +
  322. ".csv", CharsetUtil.CHARSET_GBK);
  323. continue;
  324. }
  325. //指定路径和编码
  326. writer = CsvUtil.getWriter("D:/数据/" + pi.getTurbineId() + "/" + pi.getName() +
  327. ".csv", CharsetUtil.CHARSET_GBK);
  328. long j = start.getTime();
  329. for (int i = 0; i < s.size(); i++) {
  330. String[] r = new String[4];
  331. r[0] = DateUtil.formatDateTime(new Date(j));
  332. r[1] = Long.toString(s.get(i).getTs());
  333. r[2] = Long.toString(j);
  334. r[3] = Double.toString(s.get(i).getValue());
  335. writer.write(r);
  336. j += 60 * 1000 * 5;
  337. }
  338. }*/
  339. }
  340. // @Async
  341. void writerData(Collection<GlycSj> values, String stId, String tbId) {
  342. CsvWriter writer = CsvUtil.getWriter("D:/数据/" + stId + "/" + tbId + ".csv", CharsetUtil.CHARSET_UTF_8);
  343. writer.writeBeans(values);
  344. writer.flush();
  345. }
  346. }