RzTest.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package com.jjt.task;
  2. import cn.hutool.json.JSONArray;
  3. import cn.hutool.json.JSONObject;
  4. import com.alibaba.fastjson2.JSON;
  5. import com.jjt.JjtApplication;
  6. import com.jjt.biz.service.IApiYrService;
  7. import com.jjt.biz.vo.YrCompareBackReq;
  8. import com.jjt.rz.domain.TwinCalcHourRz;
  9. import com.jjt.rz.domain.TwinDeviceRz;
  10. import com.jjt.rz.domain.TwinDeviceTypeData;
  11. import com.jjt.rz.service.ITwinCalcHourRzService;
  12. import com.jjt.rz.service.ITwinDeviceRzService;
  13. import com.jjt.rz.service.ITwinDeviceTypeService;
  14. import com.jjt.rz.vo.CompareVO;
  15. import com.jjt.utils.IotService;
  16. import javafx.util.Pair;
  17. import org.junit.jupiter.api.Test;
  18. import org.springframework.boot.test.context.SpringBootTest;
  19. import javax.annotation.Resource;
  20. import java.time.LocalDate;
  21. import java.time.LocalDateTime;
  22. import java.time.LocalTime;
  23. import java.time.ZoneOffset;
  24. import java.util.*;
  25. import java.util.stream.Collectors;
  26. /**
  27. * DataProcess$
  28. *
  29. * @author wukai
  30. * @date 2024/5/7 11:49
  31. */
  32. @SpringBootTest(classes = JjtApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  33. public class RzTest {
  34. @Resource
  35. private IotService iotService;
  36. @Resource
  37. private ITwinDeviceRzService rzService;
  38. @Resource
  39. private ITwinCalcHourRzService hourRzService;
  40. @Resource
  41. private ITwinDeviceTypeService typeService;
  42. /**
  43. * 2 RZ 4节定型机 root.tl.suxi.dingxingji4**
  44. * 3 RZ 双棍烫光机 root.tl.suxi.sgtgj**
  45. * 4 RZ 高梳机 root.tl.suxi.gaoShu**
  46. * 5 RZ 两棍刷毛机 root.tl.suxi.lgsmj**
  47. * 6 RZ 6节定型机 root.tl.suxi.dingxingji6**
  48. * 7 RZ 起毛机 root.tl.suxi.qiMaoJi**
  49. * 8 RZ 烘固机 root.tl.suxi.hongGuJi**
  50. * 9 RZ 烫剪机 root.tl.suxi.tangJianJi**
  51. */
  52. @Test
  53. public void curr() {
  54. iotService.setToken();
  55. List<TwinDeviceRz> list = rzService.selectTwinDeviceRzList(new TwinDeviceRz());
  56. List<String> strList = new ArrayList<>();
  57. Map<String, TwinDeviceRz> rzMap = new HashMap<>();
  58. list.forEach(item -> {
  59. String str = item.getDeviceCode() + ".Capacity_data_1";
  60. String mapId = item.getDevicePath() + ".Capacity_data_1";
  61. item.setOnline(false);
  62. rzMap.put(mapId, item);
  63. strList.add(str);
  64. });
  65. String sql = "select last %s from root.tl.suxi";
  66. sql = String.format(sql, String.join(",", strList));
  67. iotService.query(sql);
  68. JSONObject jsonObject = iotService.query(sql);
  69. JSONObject data = jsonObject.getJSONObject("data");
  70. System.err.println(data);
  71. JSONArray values = data.getJSONArray("values");
  72. JSONArray columnNames = data.getJSONArray("columnNames");
  73. JSONArray timestamps = data.getJSONArray("timestamps");
  74. for (int i = 0; i < values.size(); i++) {
  75. JSONArray da = values.getJSONArray(i);
  76. String type = da.getStr(0);
  77. TwinDeviceRz rz = rzMap.get(type);
  78. if (rz != null) {
  79. if (rz.getTypeId() == 3 || rz.getTypeId() == 5 || rz.getTypeId() == 9) {
  80. //双棍烫光和两棍刷毛需要按照机器状态来判断是否开机
  81. //=1 设备故障停机  ,=2 人工停机 ,=3是缺布/断布停机 ,=4满布停机,=5设备上电待机中  =6设备自动运行中,=7设备过站中
  82. if (da.getStr(1).equals("6")) {
  83. rz.setOnline(true);
  84. }
  85. } else {
  86. //其他类型只需要有值就表示开机
  87. rz.setOnline(true);
  88. }
  89. }
  90. // da.getStr(1);
  91. // da.getStr(2);
  92. // for (int j = 0; j < da.size(); j++) {
  93. //
  94. // }
  95. }
  96. // 按 line 分组并统计数量
  97. // Map<String, Long> deviceCountByLine = list.stream()
  98. // .collect(Collectors.groupingBy(TwinDeviceRz::getLine, Collectors.counting()));
  99. // 按 line 分组,分别统计总数量和 online 为 true 的数量
  100. Map<String, Pair<Integer, Integer>> deviceCountByLine = list.stream()
  101. .collect(Collectors.groupingBy(
  102. TwinDeviceRz::getLine,
  103. Collectors.collectingAndThen(
  104. Collectors.partitioningBy(TwinDeviceRz::getOnline),
  105. map -> {
  106. int total = map.get(true).size() + map.get(false).size();
  107. int open = map.get(true).size();
  108. return new Pair<>(total, open);
  109. }
  110. )
  111. ));
  112. // 打印结果
  113. deviceCountByLine.forEach((line, count) ->
  114. System.out.println("Line: " + line + ", Device Count: " + count.getKey() + ",online:" + count.getValue()));
  115. list.forEach(item -> {
  116. if (item.getLine().equals("8")) {
  117. System.err.println("device:" + item.getDeviceName() + ",online:" + item.getOnline());
  118. }
  119. });
  120. }
  121. @Test
  122. public void hour() {
  123. iotService.setToken();
  124. String st = "2025-06-05";
  125. String ed = "2025-06-06";
  126. LocalDate localDate = LocalDate.parse(st);
  127. LocalDate endDate = LocalDate.parse(ed);
  128. LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN).plusHours(13);
  129. // hourRzService.hour(start, start.plusHours(1));
  130. LocalDateTime end = LocalDateTime.of(endDate.plusDays(1), LocalTime.MIN).plusHours(6);
  131. LocalDateTime curr = LocalDateTime.now();
  132. if (end.isAfter(curr)) {
  133. end = curr.minusHours(1);
  134. }
  135. do {
  136. int i = start.getHour();
  137. System.err.println(start.toLocalDate().toString() + "\t" + i);
  138. hourRzService.hour(start, start.plusHours(1));
  139. start = start.plusHours(1);
  140. } while (!start.isAfter(end));
  141. }
  142. @Test
  143. public void data() {
  144. List<TwinDeviceTypeData> all = typeService.selectTwinDeviceTypeDataAll();
  145. Map<Long, TwinDeviceTypeData> dataMap = all.stream()
  146. .collect(Collectors.toMap(
  147. TwinDeviceTypeData::getDataId,
  148. item -> item,
  149. (existing, replacement) -> existing
  150. ));
  151. String st = "2025-06-03";
  152. LocalDate localDate = LocalDate.parse(st);
  153. Date date = Date.from(localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant());
  154. TwinCalcHourRz search = new TwinCalcHourRz();
  155. search.setDataDate(date);
  156. search.setHour(19);
  157. List<TwinDeviceRz> deviceList = rzService.selectTwinDeviceRzList(new TwinDeviceRz());
  158. List<TwinCalcHourRz> list = hourRzService.selectTwinCalcHourRzList(search);
  159. list.forEach(item -> {
  160. System.err.println(item.getData());
  161. TreeMap<String, String> map = JSON.parseObject(item.getData(), TreeMap.class);
  162. TreeMap<String, String> treeMap = new TreeMap<>();
  163. map.forEach((id, value) -> {
  164. TwinDeviceTypeData data = dataMap.get(Long.parseLong(id));
  165. if (data != null) {
  166. treeMap.put(data.getDataName(), value);
  167. }
  168. });
  169. item.setPara(treeMap);
  170. treeMap.forEach((name, value) -> {
  171. System.err.println(name + "\t" + value);
  172. });
  173. });
  174. }
  175. @Resource
  176. private IApiYrService apiYrService;
  177. @Test
  178. public void test() {
  179. String ed = "2025-06-03";
  180. LocalDate ld = LocalDate.parse(ed);
  181. LocalDateTime start = LocalDateTime.of(ld, LocalTime.MIN).plusHours(18);
  182. LocalDateTime end = LocalDateTime.of(ld, LocalTime.MIN).plusHours(19);
  183. List<YrCompareBackReq> list = new ArrayList<>();
  184. YrCompareBackReq req = new YrCompareBackReq();
  185. req.setLine(8);
  186. req.setStart(start);
  187. req.setEnd(end);
  188. list.add(req);
  189. req = new YrCompareBackReq();
  190. req.setLine(7);
  191. req.setStart(start);
  192. req.setEnd(end);
  193. list.add(req);
  194. List<CompareVO> vos = apiYrService.compare(list,true);
  195. System.err.println(vos.size());
  196. }
  197. }