ApiServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package com.ruoyi.biz.service.impl;
  2. import com.ruoyi.biz.domain.*;
  3. import com.ruoyi.biz.service.*;
  4. import com.ruoyi.biz.tools.Tools;
  5. import com.ruoyi.biz.vo.StopDetailVO;
  6. import com.ruoyi.common.constant.Constants;
  7. import com.ruoyi.common.utils.CacheUtils;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.core.env.Environment;
  10. import org.springframework.stereotype.Service;
  11. import javax.annotation.PostConstruct;
  12. import javax.annotation.Resource;
  13. import java.math.BigDecimal;
  14. import java.math.RoundingMode;
  15. import java.time.LocalDate;
  16. import java.time.LocalDateTime;
  17. import java.time.ZoneOffset;
  18. import java.util.*;
  19. import java.util.concurrent.ExecutionException;
  20. import java.util.concurrent.Future;
  21. import java.util.stream.Collectors;
  22. /**
  23. * 首页统计数据
  24. *
  25. * @author wukai
  26. * @date 2024/5/4 20:35
  27. */
  28. @Service
  29. @Slf4j
  30. public class ApiServiceImpl implements IApiService {
  31. @Resource
  32. private ITwinCalcDayService twinCalcDayService;
  33. @Resource
  34. private ITwinPanHeadInfoService panHeadInfoService;
  35. @Resource
  36. private ITwinDeviceService deviceService;
  37. @Resource
  38. private IIotService iotService;
  39. @Resource
  40. private AsyncServiceImpl asyncService;
  41. @Resource
  42. private ITaskService taskService;
  43. @Resource
  44. private Environment env;
  45. /**
  46. * 首页统计数据
  47. */
  48. @Override
  49. public void indexCalc() {
  50. List<WeekData> weekDataList = new ArrayList<>();
  51. IndexData indexData = new IndexData();
  52. IndexEfficiency efficiency = new IndexEfficiency();
  53. TwinCalcDay calcDay = twinCalcDayService.calcToday();
  54. /*
  55. 获取当天产量数据
  56. */
  57. efficiency.setAEfficiency(calcDay.getEfficiencyA().floatValue());
  58. efficiency.setBEfficiency(calcDay.getEfficiencyB().floatValue());
  59. efficiency.setTotalLength(calcDay.getLength().floatValue());
  60. efficiency.setALength(calcDay.getLengthA().floatValue());
  61. efficiency.setBLength(calcDay.getLengthB().floatValue());
  62. efficiency.setTotalWeight(calcDay.getWeight().floatValue());
  63. efficiency.setAWeight(calcDay.getWeightA().floatValue());
  64. efficiency.setBWeight(calcDay.getWeightB().floatValue());
  65. efficiency.setATime(calcDay.getOpenTimeA().divide(BigDecimal.valueOf(3600), 2, BigDecimal.ROUND_HALF_UP).floatValue());
  66. efficiency.setBTime(calcDay.getOpenTimeB().divide(BigDecimal.valueOf(3600), 2, BigDecimal.ROUND_HALF_UP).floatValue());
  67. indexData.setEfficiency(efficiency);
  68. /*
  69. *获取前面7天的数据
  70. */
  71. LocalDateTime ldt = LocalDateTime.now();
  72. if (ldt.getHour() < 7) {
  73. ldt = ldt.minusDays(1);
  74. }
  75. LocalDate localDate = ldt.toLocalDate().minusDays(7);
  76. Date date = Date.from(localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant());
  77. List<TwinCalcDay> list = twinCalcDayService.selectTwinCalcDayListByTime(date);
  78. Map<Date, List<TwinCalcDay>> dayGroup = list.stream().collect(Collectors.groupingBy(TwinCalcDay::getTime, LinkedHashMap::new, Collectors.toList()));
  79. for (Map.Entry<Date, List<TwinCalcDay>> entry : dayGroup.entrySet()) {
  80. TwinCalcDay day = new TwinCalcDay(entry.getKey());
  81. List<TwinCalcDay> days = entry.getValue();
  82. day.calcDays(days);
  83. WeekData weekData = new WeekData();
  84. weekData.convert(day);
  85. weekDataList.add(weekData);
  86. }
  87. indexData.setWeekData(weekDataList);
  88. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_CALC, indexData);
  89. }
  90. /**
  91. * 首页告警数据
  92. */
  93. @Override
  94. public void indexAlarms() {
  95. IndexData indexData = new IndexData();
  96. List<IndexAlarm> alarmList = new ArrayList<>();
  97. List<IndexPan> panList = new ArrayList<>();
  98. //配方统计数据
  99. List<FormulaTotal> formulaTotal = new ArrayList<>();
  100. //配方明细数据
  101. List<FormulaDetail> formulaDetail = new ArrayList<>();
  102. //送经量
  103. List<WarpRunIn> warpList = new ArrayList<>();
  104. //平方米克重统计数据
  105. List<GramMass> gramMasses = new ArrayList<>();
  106. //平方米克重明细数据
  107. List<GramMassDetail> gramMassDetails = new ArrayList<>();
  108. iotService.getToken();
  109. TwinDevice searchDevice = new TwinDevice();
  110. searchDevice.setOnline("1");
  111. List<TwinDevice> list = deviceService.selectTwinDeviceList(searchDevice);
  112. Map<String, Long> panMap = new HashMap<>(16);
  113. panHeadInfoService.selectTwinPanHeadInfoList(new TwinPanHeadInfo()).forEach(pan -> {
  114. String key = pan.getDeviceId() + "_" + pan.getPhNum();
  115. panMap.put(key, pan.getPhMax());
  116. });
  117. List<Future<Map<String, Object>>> futureList = new ArrayList<>();
  118. for (int i = 0; i < list.size(); i++) {
  119. TwinDevice twinDevice = list.get(i);
  120. futureList.add(asyncService.currData(twinDevice));
  121. }
  122. int stop1 = 0, stop2 = 0, stop6 = 0, stop8 = 0, onlineNum = 0, otherStop = 0, stopPan = 0;
  123. //停机明细
  124. List<StopDetailVO> stopList = new ArrayList<>();
  125. try {
  126. for (Future<Map<String, Object>> future : futureList) {
  127. Map<String, Object> map = future.get();
  128. TwinDevice device = (TwinDevice) map.get("device");
  129. StopDetailVO sdVO = new StopDetailVO();
  130. sdVO.setDeviceId(device.getDeviceId());
  131. sdVO.setDeviceName(device.getDeviceName());
  132. sdVO.setHasData(true);
  133. int total = (int) map.get("total");
  134. if (total == 0) {
  135. //可能会出现接口返回无数据的情况,需要设置设备状态为离线
  136. sdVO.setHasData(false);
  137. stopList.add(sdVO);
  138. otherStop++;
  139. continue;
  140. }
  141. float speed = (float) map.get("Capacity_data_1");
  142. int stopStatus = (int) map.get("Capacity_data_48");
  143. sdVO.setStop(stopStatus);
  144. sdVO.setSpeed(speed);
  145. stopList.add(sdVO);
  146. //处理配方明细数据
  147. FormulaDetail detail = new FormulaDetail(map);
  148. formulaDetail.add(detail);
  149. //处理送经量数据
  150. WarpRunIn warpRunIn = new WarpRunIn(map);
  151. warpList.add(warpRunIn);
  152. //处理平方米克重明细数据
  153. GramMassDetail gramMassDetail = new GramMassDetail(map);
  154. gramMassDetails.add(gramMassDetail);
  155. int data4 = (int) map.get("Capacity_data_4");
  156. if (data4 == 0) {
  157. //Capacity_data_4,如果设定落布米数为0,则证明当前数据无效
  158. //不能跟上面判断合并,不然报空指针
  159. continue;
  160. }
  161. for (int i = 0; i < Tools.ALLOW_ALARM.length; i++) {
  162. //允许的告警编号
  163. boolean flag = (boolean) map.get("Alarm_unit_" + Tools.ALLOW_ALARM[i]);
  164. if (flag) {
  165. IndexAlarm indexAlarm = new IndexAlarm();
  166. indexAlarm.setCode(device.getDeviceCode());
  167. indexAlarm.setName(device.getDeviceName());
  168. indexAlarm.setType(i);
  169. alarmList.add(indexAlarm);
  170. }
  171. }
  172. int alarm27 = (int) map.get("Alarm_unit_27");
  173. IndexAlarm indexAlarm;
  174. if (alarm27 != 0) {
  175. indexAlarm = new IndexAlarm();
  176. indexAlarm.setCode(device.getDeviceCode());
  177. indexAlarm.setName(device.getDeviceName());
  178. indexAlarm.setType(27);
  179. alarmList.add(indexAlarm);
  180. }
  181. indexAlarm = new IndexAlarm();
  182. indexAlarm.setCode(device.getDeviceCode());
  183. indexAlarm.setName(device.getDeviceName());
  184. int alarmType = 0;
  185. if (speed > 0f || stopStatus == 0) {
  186. onlineNum++;
  187. } else {
  188. switch (stopStatus) {
  189. case 1:
  190. stop1++;
  191. alarmType = 10001;
  192. break;
  193. case 2:
  194. stop2++;
  195. alarmType = 10002;
  196. break;
  197. case 6:
  198. stop6++;
  199. alarmType = 10006;
  200. break;
  201. case 7:
  202. //7-盘头剩余圈数达到停机,表示停机叫料
  203. stop8++;
  204. alarmType = 10008;
  205. break;
  206. default:
  207. otherStop++;
  208. }
  209. }
  210. if (alarmType != 0) {
  211. indexAlarm.setType(alarmType);
  212. alarmList.add(indexAlarm);
  213. }
  214. int[] curr = new int[5];
  215. int[] max = new int[5];
  216. float[] panPercent = new float[5];
  217. boolean panFlag = false;
  218. for (int i = 0; i < curr.length; i++) {
  219. int pos = 15 + i;
  220. curr[i] = (int) map.get("Capacity_data_" + pos);
  221. if (curr[i] < 200) {
  222. //盘头小于200圈
  223. panFlag = true;
  224. }
  225. String key = device.getDeviceId() + "_" + (i + 1);
  226. max[i] = 15000;
  227. if (panMap.get(key) != null) {
  228. max[i] = Math.toIntExact(panMap.get(key));
  229. }
  230. panPercent[i] = BigDecimal.valueOf(curr[i] * 100).divide(BigDecimal.valueOf(max[i]), 2, RoundingMode.HALF_UP).floatValue();
  231. if (panPercent[i] > 100) {
  232. panPercent[i] = 100;
  233. }
  234. }
  235. if (panFlag) {
  236. stopPan++;
  237. }
  238. IndexPan pan = new IndexPan();
  239. pan.setCode(device.getDeviceCode());
  240. pan.setName(device.getDeviceName());
  241. pan.setPanPercent(panPercent);
  242. panList.add(pan);
  243. }
  244. } catch (InterruptedException e) {
  245. throw new RuntimeException(e);
  246. } catch (ExecutionException e) {
  247. throw new RuntimeException(e);
  248. }
  249. indexData.setAlarm(alarmList);
  250. indexData.setPan(panList);
  251. IndexDevice device = new IndexDevice();
  252. device.setTotal(list.size());
  253. device.setOnline(onlineNum);
  254. device.setStop1(stop1);
  255. device.setStop2(stop2);
  256. device.setStop6(stop6);
  257. device.setStop8(stop8);
  258. device.setStopPan(stopPan);
  259. // 2024-11-18 告警数改为其他停机数
  260. // device.setAlarm(alarmList.size());
  261. device.setAlarm(otherStop);
  262. indexData.setDevice(device);
  263. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_ALARM, indexData);
  264. Map<Float, Long> temp = formulaDetail.stream().collect(Collectors.groupingBy(FormulaDetail::getFormula_data_15, Collectors.counting()));
  265. for (Float v : temp.keySet()) {
  266. FormulaTotal total = new FormulaTotal();
  267. total.setHeight(v);
  268. int num = Math.toIntExact(temp.get(v));
  269. total.setNum(num);
  270. float percent = BigDecimal.valueOf(num).divide(BigDecimal.valueOf(list.size()), 2, RoundingMode.HALF_UP).floatValue();
  271. total.setPercent(percent);
  272. formulaTotal.add(total);
  273. }
  274. temp = gramMassDetails.stream().collect(Collectors.groupingBy(GramMassDetail::getGramMass, Collectors.counting()));
  275. for (Float v : temp.keySet()) {
  276. GramMass total = new GramMass();
  277. total.setGramMass(v);
  278. int num = Math.toIntExact(temp.get(v));
  279. total.setNum(num);
  280. float percent = BigDecimal.valueOf(num).divide(BigDecimal.valueOf(list.size()), 2, RoundingMode.HALF_UP).floatValue();
  281. total.setPercent(percent);
  282. gramMasses.add(total);
  283. }
  284. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_FORMULA_TOTAL, formulaTotal);
  285. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_FORMULA_DETAIL, formulaDetail);
  286. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_WARP_RUN_IN, warpList);
  287. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_GRAM_MASS, gramMasses);
  288. CacheUtils.put(Constants.IOT_TOKEN, Constants.INDEX_GRAM_MASS_DETAIL, gramMassDetails);
  289. CacheUtils.put(Constants.IOT_TOKEN, Constants.STOP_DETAIL, stopList);
  290. }
  291. @PostConstruct
  292. public void init() {
  293. String flag = "false";
  294. String bl = env.getProperty("data.bl");
  295. if (bl != null && flag.equals(bl)) {
  296. return;
  297. }
  298. taskService.calc2Curr();
  299. indexCalc();
  300. indexAlarms();
  301. }
  302. }