PredictController.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. package com.ruoyi.web.controller;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import com.alibaba.fastjson2.JSON;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.fasterxml.jackson.databind.ObjectMapper;
  8. import com.ruoyi.ucp.entity.*;
  9. import com.ruoyi.ucp.feign.AdapterApi;
  10. import com.ruoyi.ucp.service.IHistoryPredictService;
  11. import com.ruoyi.ucp.service.IPointInfoService;
  12. import com.ruoyi.ucp.service.IRealtimePredictService;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.web.bind.annotation.*;
  15. import javax.annotation.Resource;
  16. import java.io.IOException;
  17. import java.net.URI;
  18. import java.util.*;
  19. import java.util.concurrent.ConcurrentHashMap;
  20. import java.util.function.Function;
  21. import java.util.stream.Collectors;
  22. @RestController
  23. @RequestMapping("/predict")
  24. public class PredictController {
  25. @Resource
  26. private IPointInfoService pointInfoService;
  27. @Resource
  28. private AdapterApi adapterApi;
  29. @Resource
  30. private JavaFunctionJobHandler task;
  31. @Resource
  32. private IRealtimePredictService realtimePredictService;
  33. @Resource
  34. private IHistoryPredictService historyPredictService;
  35. private Map<String, PointInfo> tbInfos = new ConcurrentHashMap<>();
  36. @GetMapping("/stbs")
  37. public Map<String, List<Integer>> stbs(){
  38. QueryWrapper<PointInfo> wrapper = new QueryWrapper<>();
  39. wrapper.lambda().eq(PointInfo::getInstitutionType, "turbine")
  40. .in(PointInfo::getUniformCode, "AI114");
  41. List<PointInfo> list = pointInfoService.list(wrapper);
  42. return list.stream().collect(Collectors.groupingBy(pi -> pi.getStationId().replaceFirst("GJNY_SXGS_", "")
  43. .replaceFirst("_FDC_STA", ""), Collectors.mapping(pi -> Integer.parseInt(pi.getTurbineId()
  44. .replaceFirst("GJNY_SXGS_.+_F_WT_", "").replaceFirst("_EQ", "")), Collectors.toList())));
  45. }
  46. @GetMapping("/stb")
  47. public Map<String, List<String>> stb(){
  48. QueryWrapper<PointInfo> wrapper = new QueryWrapper<>();
  49. wrapper.lambda().eq(PointInfo::getInstitutionType, "turbine")
  50. .in(PointInfo::getUniformCode, "AI114");
  51. List<PointInfo> list = pointInfoService.list(wrapper);
  52. return list.stream().collect(Collectors.groupingBy(pi -> pi.getStationId(), Collectors.mapping(pi -> pi.getTurbineId(), Collectors.toList())));
  53. }
  54. @GetMapping("/snap")
  55. public Map<String, Collection<GlycSj>> haha(@RequestParam(value = "stationId",required = false) String stationId,
  56. @RequestParam(value = "beginTime",required = false) String beginTime,
  57. @RequestParam(value = "endTime",required = false) String endTime,
  58. @RequestParam(value = "interval",required = false) Integer interval) {
  59. DateTime start = DateUtil.parse(beginTime);
  60. DateTime end = DateUtil.parse(endTime);
  61. String[] names = {"AI114", "AI066", "AI060", "AI067", "AI082", "AI073", "AI004", "AI047", "AI072", "AI074"};
  62. QueryWrapper<PointInfo> wrapper = new QueryWrapper<>();
  63. wrapper.lambda().eq(PointInfo::getInstitutionType, "turbine")
  64. .eq(PointInfo::getStationId, stationId)
  65. .in(PointInfo::getUniformCode, names);
  66. List<PointInfo> list = pointInfoService.list(wrapper);
  67. Map<String, List<PointInfo>> tbPis = list.stream().collect(Collectors.groupingBy(PointInfo::getTurbineId));
  68. Map<String, Collection<GlycSj>> result = new HashMap<>();
  69. tbPis.forEach((tbId,pis)->{
  70. List<PointData> zt = task.calcTurbineSimpleZt(start, end, interval, tbId);
  71. Map<String, GlycSj> map =new TreeMap<>();
  72. for (PointInfo info : pis) {
  73. try {
  74. List<PointData> s = adapterApi.getHistorySnap(URI.create("http://172.16.12.103:8011/ts"), info.getPointKey(),
  75. start.getTime(), end.getTime(), interval);
  76. long j = start.getTime();
  77. String dt=null;GlycSj glycSj=null;
  78. //AI066 风速,AI060 发电机转速,AI065 风机状态点,AI067 风向,AI082 桨叶1角度,AI073 机舱方向/对风角度
  79. //AI004 U1绕组温度,AI047 电机温度2,AI072 环境温度,AI074 机舱温度
  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(s.get(i).getTs())));
  92. j += interval * 1000;//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 += interval * 1000;
  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 += interval * 1000;
  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 += interval * 1000;
  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 += interval * 1000;
  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 += interval * 1000;
  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 += interval * 1000;
  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 += interval * 1000;
  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 += interval * 1000;
  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 += interval * 1000;
  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 += interval * 1000;
  215. }
  216. break;
  217. }
  218. map.forEach((k, v) -> {
  219. v.setTime(k);
  220. });
  221. } catch (Exception e) {
  222. e.printStackTrace();
  223. }
  224. }
  225. result.put(tbId,map.values());
  226. });
  227. return result;
  228. }
  229. @GetMapping("/latest")
  230. public Map<String, GlycSj> latest(@RequestParam(value = "stationId",required = false) String stationId) {
  231. String[] names = {"AI114", "AI066", "AI060", "AI067", "AI082", "AI073", "AI004", "AI047", "AI072", "AI074"};
  232. QueryWrapper<PointInfo> wrapper = new QueryWrapper<>();
  233. wrapper.lambda().eq(PointInfo::getInstitutionType, "turbine")
  234. .eq(PointInfo::getStationId, stationId)
  235. .in(PointInfo::getUniformCode, names);
  236. List<PointInfo> list = pointInfoService.list(wrapper);
  237. Map<String, List<PointInfo>> tbPis = list.stream().collect(Collectors.groupingBy(PointInfo::getTurbineId));
  238. String keys = list.stream().map(PointInfo::getPointKey).collect(Collectors.joining(","));
  239. Map<String, GlycSj> result = new HashMap<>();
  240. Map<String, Double> zt = task.calcRealtimeTurbineZt(stationId);
  241. Map<String, PointData> latest = adapterApi.getLatest(URI.create("http://172.16.12.103:8011/ts"),keys);
  242. tbPis.forEach((tbId,pis)->{
  243. GlycSj glycSj = new GlycSj();
  244. for (PointInfo info : pis) {
  245. PointData data = latest.get(info.getPointKey());
  246. Double v1 = 0.0;
  247. if(data!=null) v1=data.getValue();
  248. try {
  249. switch (info.getUniformCode()) {
  250. case "AI114":
  251. glycSj.setGl(v1);
  252. glycSj.setZt(zt.get(tbId));
  253. glycSj.setTurbineId(tbId.replaceFirst("GJNY_SXGS_.+_F_WT_", "").replaceFirst("_EQ", ""));
  254. break;
  255. case "AI066":
  256. glycSj.setFs(v1);
  257. break;
  258. case "AI060":
  259. glycSj.setFdjzs(v1);
  260. break;
  261. case "AI065":
  262. glycSj.setZt(zt.get(tbId));
  263. break;
  264. case "AI067":
  265. glycSj.setFx(v1);
  266. break;
  267. case "AI082":
  268. glycSj.setJyjd(v1);
  269. break;
  270. case "AI073":
  271. glycSj.setDfjd(v1);
  272. break;
  273. case "AI004":
  274. glycSj.setNjwd(v1);
  275. break;
  276. case "AI047":
  277. glycSj.setDjwd(v1);
  278. break;
  279. case "AI072":
  280. glycSj.setHjwd(v1);
  281. break;
  282. case "AI074":
  283. glycSj.setJcwd(v1);
  284. break;
  285. }
  286. } catch (Exception e) {
  287. e.printStackTrace();
  288. }
  289. }
  290. result.put(tbId,glycSj);
  291. });
  292. return result;
  293. }
  294. @GetMapping("/haha2")
  295. public Map<String, Collection<GlycSj>> haha2(@RequestParam(value = "stationId",required = false) String stationId,
  296. @RequestParam(value = "beginTime",required = false) String beginTime,
  297. @RequestParam(value = "endTime",required = false) String endTime,
  298. @RequestParam(value = "interval",required = false) Integer interval) {
  299. String a="[\n" +
  300. " {\n" +
  301. " \"turbineId\": \"0011\",\n" +
  302. " \"time\": \"2024-10-14 22:22:00\",\n" +
  303. " \"gl\": 1744.0,\n" +
  304. " \"fs\": 6.982665061950684,\n" +
  305. " \"fdjzs\": 169.0,\n" +
  306. " \"zt\": 4.0,\n" +
  307. " \"fx\": 0.13901925086975098,\n" +
  308. " \"jyjd\": 0.0,\n" +
  309. " \"dfjd\": 0.1688677817583084,\n" +
  310. " \"njwd\": 61.0,\n" +
  311. " \"djwd\": 32.0,\n" +
  312. " \"hjwd\": 18.921875,\n" +
  313. " \"jcwd\": 29.0\n" +
  314. " },\n" +
  315. " {\n" +
  316. " \"turbineId\": \"0011\",\n" +
  317. " \"time\": \"2024-10-14 22:27:00\",\n" +
  318. " \"gl\": 2144.0,\n" +
  319. " \"fs\": 7.244823455810547,\n" +
  320. " \"fdjzs\": 176.0,\n" +
  321. " \"zt\": 4.0,\n" +
  322. " \"fx\": -0.04913616180419922,\n" +
  323. " \"jyjd\": 0.0,\n" +
  324. " \"dfjd\": -0.04758832976222038,\n" +
  325. " \"njwd\": 63.0,\n" +
  326. " \"djwd\": 32.0,\n" +
  327. " \"hjwd\": 19.046875,\n" +
  328. " \"jcwd\": 29.225000381469727\n" +
  329. " },\n" +
  330. " {\n" +
  331. " \"turbineId\": \"0011\",\n" +
  332. " \"time\": \"2024-10-14 22:32:00\",\n" +
  333. " \"gl\": 1848.0,\n" +
  334. " \"fs\": 7.532032012939453,\n" +
  335. " \"fdjzs\": 171.0,\n" +
  336. " \"zt\": 4.0,\n" +
  337. " \"fx\": 0.024447917938232422,\n" +
  338. " \"jyjd\": 0.0,\n" +
  339. " \"dfjd\": 0.0644155889749527,\n" +
  340. " \"njwd\": 65.0,\n" +
  341. " \"djwd\": 33.0,\n" +
  342. " \"hjwd\": 19.15625,\n" +
  343. " \"jcwd\": 29.225000381469727\n" +
  344. " },\n" +
  345. " {\n" +
  346. " \"turbineId\": \"0011\",\n" +
  347. " \"time\": \"2024-10-14 22:37:00\",\n" +
  348. " \"gl\": 1528.0,\n" +
  349. " \"fs\": 6.8463616371154785,\n" +
  350. " \"fdjzs\": 159.0,\n" +
  351. " \"zt\": 4.0,\n" +
  352. " \"fx\": 0.11313247680664062,\n" +
  353. " \"jyjd\": 0.0,\n" +
  354. " \"dfjd\": 0.11275594681501389,\n" +
  355. " \"njwd\": 64.5,\n" +
  356. " \"djwd\": 33.0,\n" +
  357. " \"hjwd\": 19.125,\n" +
  358. " \"jcwd\": 29.149999618530273\n" +
  359. " },\n" +
  360. " {\n" +
  361. " \"turbineId\": \"0011\",\n" +
  362. " \"time\": \"2024-10-14 22:42:00\",\n" +
  363. " \"gl\": 1280.0,\n" +
  364. " \"fs\": 6.929559230804443,\n" +
  365. " \"fdjzs\": 152.0,\n" +
  366. " \"zt\": 4.0,\n" +
  367. " \"fx\": 0.03271770477294922,\n" +
  368. " \"jyjd\": 0.0,\n" +
  369. " \"dfjd\": 0.031964123249053955,\n" +
  370. " \"njwd\": 63.0,\n" +
  371. " \"djwd\": 33.0,\n" +
  372. " \"hjwd\": 19.078125,\n" +
  373. " \"jcwd\": 29.39999961853027\n" +
  374. " }\n" +
  375. "]";
  376. QueryWrapper<PointInfo> wrapper = new QueryWrapper<>();
  377. wrapper.lambda().eq(PointInfo::getInstitutionType, "turbine")
  378. .eq(PointInfo::getStationId, stationId);
  379. List<PointInfo> list = pointInfoService.list(wrapper);
  380. Map<String, Collection<GlycSj>> result = new HashMap<>();
  381. ObjectMapper objectMapper = new ObjectMapper();
  382. List<GlycSj> glycSjList = null;
  383. try {
  384. glycSjList = objectMapper.readValue(a, objectMapper.getTypeFactory().constructCollectionType(List.class, GlycSj.class));
  385. // 现在你可以使用glycSjList了
  386. } catch (IOException e) {
  387. e.printStackTrace();
  388. // 处理异常,比如记录日志或抛出运行时异常
  389. }
  390. result.put(list.get(0).getTurbineId(),glycSjList);
  391. return result;
  392. }
  393. @GetMapping("/haha")
  394. public Map<String, GlycSj> haha(@RequestParam(value = "stationId",required = false) String stationId) {
  395. String a=" {\n" +
  396. " \"turbineId\": \"0011\",\n" +
  397. " \"time\": \"2024-10-14 22:22:00\",\n" +
  398. " \"gl\": 1744.0,\n" +
  399. " \"fs\": 6.982665061950684,\n" +
  400. " \"fdjzs\": 169.0,\n" +
  401. " \"zt\": 4.0,\n" +
  402. " \"fx\": 0.13901925086975098,\n" +
  403. " \"jyjd\": 0.0,\n" +
  404. " \"dfjd\": 0.1688677817583084,\n" +
  405. " \"njwd\": 61.0,\n" +
  406. " \"djwd\": 32.0,\n" +
  407. " \"hjwd\": 18.921875,\n" +
  408. " \"jcwd\": 29.0\n" +
  409. " }";
  410. QueryWrapper<PointInfo> wrapper = new QueryWrapper<>();
  411. wrapper.lambda().eq(PointInfo::getInstitutionType, "turbine")
  412. .eq(PointInfo::getStationId, stationId);
  413. List<PointInfo> list = pointInfoService.list(wrapper);
  414. Map<String, GlycSj> result = new HashMap<>();
  415. ObjectMapper objectMapper = new ObjectMapper();
  416. GlycSj glycSjList = null;
  417. try {
  418. glycSjList = objectMapper.readValue(a, GlycSj.class);
  419. // 现在你可以使用glycSjList了
  420. } catch (IOException e) {
  421. e.printStackTrace();
  422. // 处理异常,比如记录日志或抛出运行时异常
  423. }
  424. result.put(list.get(0).getTurbineId(),glycSjList);
  425. return result;
  426. }
  427. @PostMapping("/write")
  428. public boolean write(@RequestBody Map<String, List<GlycSj2>> data) {
  429. try {
  430. DateTime date = DateUtil.beginOfMinute(DateUtil.date());
  431. int i = Math.floorDiv(date.minute(), 15) * 15;
  432. date.setMinutes(i);
  433. if (CollUtil.isEmpty(tbInfos)){
  434. QueryWrapper<PointInfo> wrapper = new QueryWrapper<>();
  435. wrapper.lambda().eq(PointInfo::getInstitutionType, "turbine")
  436. .in(PointInfo::getUniformCode, "AI121");
  437. List<PointInfo> list = pointInfoService.list(wrapper);
  438. tbInfos = list.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  439. }
  440. List<RealtimePredict> rpList = new ArrayList<>();
  441. List<HistoryPredict> hpList = new ArrayList<>();
  442. List<String> ids=new ArrayList<>();
  443. data.forEach((tbId,glycsjs)->{
  444. PointInfo info = tbInfos.get(tbId);
  445. for (GlycSj2 glycsj : glycsjs) {
  446. RealtimePredict rp = new RealtimePredict();
  447. rp.setId(tbId+glycsj.getTime());
  448. ids.add(rp.getId());
  449. rp.setModelId("1");
  450. rp.setPredictType("CDQ");
  451. rp.setDeviceType("turbine");
  452. rp.setTime(DateUtil.parseDateTime(glycsj.getTime()));
  453. rp.setSiteId(info.getStationId());
  454. rp.setPeriodId(info.getProjectId());
  455. rp.setLineId(info.getLineId());
  456. rp.setTurbineId(tbId);
  457. rp.setPredictPower(glycsj.getPower());
  458. rp.setPredictEnergy(glycsj.getPower()/4);
  459. rpList.add(rp);
  460. HistoryPredict hp = new HistoryPredict();
  461. hp.setModelId("1");
  462. hp.setPredictType("CDQ");
  463. hp.setDeviceType("turbine");
  464. hp.setDataTime(date);
  465. hp.setPredictTime(DateUtil.parseDateTime(glycsj.getTime()));
  466. hp.setSiteId(info.getStationId());
  467. hp.setPeriodId(info.getProjectId());
  468. hp.setLineId(info.getLineId());
  469. hp.setTurbineId(tbId);
  470. hp.setPredictPower(glycsj.getPower());
  471. hp.setPredictEnergy(glycsj.getPower()/4);
  472. hpList.add(hp);
  473. }
  474. });
  475. realtimePredictService.removeBatchByIds(ids);
  476. realtimePredictService.saveBatch(rpList);
  477. historyPredictService.saveBatch(hpList);
  478. } catch (Exception e) {
  479. e.printStackTrace();
  480. return false;
  481. }
  482. return true;
  483. }
  484. }