GetDataTest.java 16 KB

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