CustomAsyncService.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package com.gyee.alarm.service;
  2. import com.gyee.alarm.feigns.IAlarmService;
  3. import com.gyee.alarm.init.CacheContext;
  4. import com.gyee.alarm.model.auto.ProBasicEquipmentPoint;
  5. import com.gyee.alarm.model.vo.*;
  6. import com.gyee.alarm.rule.AlarmFunction;
  7. import com.gyee.alarm.rule.expression.AlarmExpression;
  8. import com.gyee.alarm.rule.expression.Analyzer;
  9. import com.gyee.alarm.service.auto.IAlarmTsService;
  10. import com.gyee.alarm.task.thread.AlarmThread;
  11. import com.gyee.alarm.task.thread.ReadWtDataThread;
  12. import com.gyee.alarm.util.realtimesource.IEdosUtil;
  13. import com.gyee.common.model.PointData;
  14. import com.gyee.common.model.StringUtils;
  15. import lombok.SneakyThrows;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.scheduling.annotation.Async;
  21. import org.springframework.stereotype.Service;
  22. import javax.annotation.Resource;
  23. import java.util.*;
  24. import java.util.concurrent.*;
  25. @Service
  26. @Slf4j
  27. public class CustomAsyncService {
  28. private Logger logger = LoggerFactory.getLogger(this.getClass());
  29. @Async("taskExecutor")
  30. public CompletableFuture<String> readCall(IEdosUtil edosUtil, Integer readRows, List<ProBasicEquipmentPoint> pointList,String str) throws Exception {
  31. Integer result = 1;
  32. try {
  33. List<String> ls=new ArrayList<>();
  34. List<PointData> values=new ArrayList<>();
  35. int times=0;
  36. for(ProBasicEquipmentPoint vo:pointList)
  37. {
  38. ls.add(vo.getNemCode());
  39. times++;
  40. if(times==readRows)
  41. {
  42. List<PointData> templs=edosUtil.getRealData(ls);
  43. values.addAll(templs);
  44. ls=new ArrayList<>();
  45. times=0;
  46. }
  47. }
  48. if(!ls.isEmpty())
  49. {
  50. List<PointData> templs=edosUtil.getRealData(ls);
  51. values.addAll(templs);
  52. }
  53. if(values.size() == pointList.size())
  54. {
  55. for(int i=0;i<pointList.size();i++)
  56. {
  57. ProBasicEquipmentPoint point=pointList.get(i);
  58. PointData value=values.get(i);
  59. if(CacheContext.wtDataValueMap.containsKey(point.getWindturbineId()))
  60. {
  61. Map<String, PointData> map=CacheContext.wtDataValueMap.get(point.getWindturbineId());
  62. map.put(point.getUniformCode(),value);
  63. }else
  64. {
  65. Map<String, PointData> map=new HashMap<>();
  66. map.put(point.getUniformCode(),value);
  67. CacheContext.wtDataValueMap.put(point.getWindturbineId(),map);
  68. }
  69. }
  70. }
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. result=0;
  74. }
  75. return CompletableFuture.completedFuture(str+"Task completed");
  76. }
  77. @Async("taskExecutor")
  78. public CompletableFuture<String> checkCall(IAlarmService alarmService, List<AlarmCustomTag> alarmVoList,
  79. StringBuilder str, Integer readRows, IAlarmTsService alarmTsService) throws Exception {
  80. Integer result = 1;
  81. try {
  82. List<AlarmTag> saveAlarmTags = new CopyOnWriteArrayList<>();
  83. List<AlarmTag> updateAlarmTags = new CopyOnWriteArrayList<>();
  84. StringBuilder sb = new StringBuilder();
  85. List<AlarmSimpleVo> alarmls = new CopyOnWriteArrayList<>();
  86. int times = 0;
  87. for (AlarmCustomTag alarm : alarmVoList) {
  88. sb.append("'").append(alarm.getId().toLowerCase()).append("',");
  89. times++;
  90. if (times == readRows) {
  91. String ids = sb.substring(0, sb.length() - 1);
  92. List<AlarmSimpleVo> templs=new ArrayList<>();
  93. templs = alarmTsService.selectLastRowByTbname(AlarmSuperTalbeType.CT.getCode(), ids);
  94. alarmls.addAll(templs);
  95. sb.setLength(0);
  96. times = 0;
  97. }
  98. }
  99. if (sb.length()>0) {
  100. String ids = sb.substring(0, sb.length() - 1);
  101. List<AlarmSimpleVo> templs=new ArrayList<>();
  102. templs = alarmTsService.selectLastRowByTbname(AlarmSuperTalbeType.CT.getCode(), ids);
  103. alarmls.addAll(templs);
  104. }
  105. Map<String, AlarmSimpleVo> map = new ConcurrentHashMap<>();
  106. if (!alarmls.isEmpty()) {
  107. for (AlarmSimpleVo vo : alarmls) {
  108. map.put(vo.getTbName().toLowerCase(), vo);
  109. }
  110. }
  111. for (AlarmCustomTag vo : alarmVoList) {
  112. checkRule(saveAlarmTags, updateAlarmTags, vo,map);
  113. }
  114. if (!saveAlarmTags.isEmpty()) {
  115. alarmService.saveAlarm(saveAlarmTags);
  116. }
  117. if (!updateAlarmTags.isEmpty()) {
  118. alarmService.updateAlarm(updateAlarmTags);
  119. }
  120. logger.info(str+ "新增报警数量:" + saveAlarmTags.size());
  121. logger.info(str+ "结束报警数量:" + updateAlarmTags.size());
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. }
  125. return CompletableFuture.completedFuture(str+"Task completed");
  126. }
  127. private void checkRule(List<AlarmTag> saveAlarmTags, List<AlarmTag> updateAlarmTags, AlarmCustomTag ar,Map<String, AlarmSimpleVo> map) {
  128. try {
  129. AlarmExpression alarmExpression = Analyzer.getAlarmExpression(ar.getTagId());
  130. AlarmFunction alarmFunction = null;
  131. if (ar.getDevicetype().equals(DeviceTypeValue.WT.getCode())) {
  132. alarmFunction = new AlarmFunction(alarmExpression, DeviceTypeValue.WT, ar.getDeviceid());
  133. }else if (ar.getDevicetype().equals(DeviceTypeValue.IN.getCode())) {
  134. alarmFunction = new AlarmFunction(alarmExpression, DeviceTypeValue.IN, ar.getDeviceid());
  135. } else if (ar.getDevicetype().equals(DeviceTypeValue.BT.getCode())) {
  136. alarmFunction = new AlarmFunction(alarmExpression, DeviceTypeValue.BT, ar.getStationid());
  137. }
  138. try {
  139. Object obj = alarmFunction.explain();
  140. if (obj instanceof Boolean) {
  141. boolean endStatus = true;
  142. if(map.containsKey(ar.getId().toLowerCase()))
  143. {
  144. AlarmSimpleVo vo=map.get(ar.getId().toLowerCase());
  145. if(StringUtils.notEmp(vo.getEndts()))
  146. {
  147. endStatus=true;
  148. }else
  149. {
  150. endStatus=false;
  151. }
  152. }else
  153. {
  154. endStatus = true;
  155. }
  156. if (true == (boolean) obj) {
  157. if(endStatus)
  158. {
  159. AlarmTag po = new AlarmTag();
  160. po.setId(ar.getId());
  161. po.setTs(new Date().getTime());
  162. po.setEndts(null);
  163. po.setVal(1.0);
  164. po.setOval(1.0);
  165. po.setTimeLong(0.0);
  166. saveAlarmTags.add(po);
  167. }
  168. } else {
  169. if(!endStatus)
  170. {
  171. AlarmTag po = new AlarmTag();
  172. po.setId(ar.getId());
  173. po.setTs(new Date().getTime());
  174. po.setEndts(new Date().getTime());
  175. po.setVal(0.0);
  176. po.setOval(0.0);
  177. po.setTimeLong(0.0);
  178. updateAlarmTags.add(po);
  179. }
  180. }
  181. } else {
  182. // System.out.print(obj.toString());
  183. }
  184. } catch (Exception ex) {
  185. // ex.printStackTrace();
  186. logger.error("自定义报警规则执行时出错!" + alarmFunction.getThingId() + "--" + ar.getTagId() + "----" + ex.getMessage());
  187. }
  188. } catch (Exception ex) {
  189. logger.error("生成自定义报警规则失败!规则id:" + ar.getId());
  190. }
  191. }
  192. }