AlarmCtService.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. package com.gyee.alarm.service;
  2. import com.gyee.alarm.init.CacheContext;
  3. import com.gyee.alarm.model.auto.*;
  4. import com.gyee.alarm.model.vo.*;
  5. import com.gyee.alarm.service.auto.IAlarmTsService;
  6. import com.gyee.alarm.service.auto.IProEconAlarmInfoService;
  7. import com.gyee.alarm.service.auto.IProEconAlarmRuleService;
  8. import com.gyee.alarm.util.DateUtils;
  9. import com.gyee.alarm.util.StringUtils;
  10. import com.gyee.alarm.websocket.PushParams;
  11. import com.gyee.alarm.websocket.WebSocket;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Service;
  14. import javax.annotation.Resource;
  15. import javax.websocket.EncodeException;
  16. import java.io.IOException;
  17. import java.sql.SQLException;
  18. import java.sql.Timestamp;
  19. import java.util.*;
  20. import java.util.concurrent.ConcurrentHashMap;
  21. @Service
  22. public class AlarmCtService {
  23. @Resource
  24. private IAlarmTsService alarmTsService;
  25. @Resource
  26. private IProEconAlarmInfoService proEconAlarmInfoService;
  27. @Value("${initialcode}")
  28. private String initialcode;
  29. @Value("${pushinterval}")
  30. private Double pushinterval;
  31. @Resource
  32. private WebSocket webSocket;
  33. @Resource
  34. private PushDataSevice pushDataSevice;
  35. public void dropSuperTable() {
  36. alarmTsService.dropSuperTable(AlarmSuperTalbeType.CT.getCode());
  37. }
  38. public void createSuperTable() {
  39. alarmTsService.createSuperTable(AlarmSuperTalbeType.CT.getCode());
  40. }
  41. public void initalCtAlarm() throws SQLException {
  42. List<AlarmVo> alarmVoList = CacheContext.alarmCtList;
  43. List<AlarmTag> alarmTags = new ArrayList<>();
  44. if (!alarmVoList.isEmpty()) {
  45. for (AlarmVo vo : alarmVoList) {
  46. alarmTsService.createTable(vo.getTbName(), vo.getAlarmid(), vo.getAlarmtype(),
  47. vo.getCharacteristic(), vo.getComponents(),
  48. vo.getDescription(),
  49. vo.getDeviceid(), vo.getDevicename(),
  50. vo.getDevicetype(), vo.getEnabled(),
  51. vo.getLineid(), vo.getLinename(),
  52. vo.getModelId(), vo.getProjectid(),
  53. vo.getProjectname(), vo.getRank(),
  54. vo.getResettable(), vo.getStationid(),
  55. vo.getStationname(), vo.getSubcomponents(),
  56. vo.getSuffix(), vo.getTagid(),
  57. vo.getTriggertype(), vo.getUniformcode(),
  58. vo.getSuperTableName(),vo.getName(),
  59. vo.getNemCode(),vo.getFaultCause(),vo.getResolvent(),vo.getFaultType(),vo.getAname());
  60. // AlarmTag po = new AlarmTag();
  61. // po.setId(vo.getTbName());
  62. // po.setVal(0.0);
  63. // po.setTriggerType(vo.getTriggertype());
  64. // po.setTagId(vo.getTagid());
  65. // po.setVal(0.0);
  66. // po.setRank(vo.getRank());
  67. // po.setTs(new Date().getTime());
  68. // alarmTags.add(po);
  69. }
  70. }
  71. // alarmTsService.insertList(alarmTags);
  72. }
  73. public List<AlarmVo> initalAlarmVo() {
  74. List<AlarmVo> alarmVoList = new ArrayList<>();
  75. Map<String, List<ProEconAlarmRule>> alarmRulesMap = CacheContext.alarmRulesMap;
  76. //风机自定义报警
  77. wtCustomAlarm(alarmVoList, alarmRulesMap);
  78. //逆变器自定义报警
  79. lnCustomAlarm(alarmVoList, alarmRulesMap);
  80. // //场站自定义报警
  81. // wpCustomAlarm(alarmVoList, alarmRulesMap);
  82. //升压站自定义报警
  83. btCustomAlarm(alarmVoList, alarmRulesMap);
  84. return alarmVoList;
  85. }
  86. private void wtCustomAlarm(List<AlarmVo> alarmVoList, Map<String, List<ProEconAlarmRule>> alarmRulesMap) {
  87. Map<String, List<ProEconAlarmRule>> armap = new HashMap<>();
  88. if (alarmRulesMap.containsKey(DeviceTypeValue.WT.getCode())) {
  89. List<ProEconAlarmRule> arls = alarmRulesMap.get(DeviceTypeValue.WT.getCode());
  90. if (!arls.isEmpty()) {
  91. for (ProEconAlarmRule ar : arls) {
  92. StringBuilder sb = new StringBuilder();
  93. sb.append(ar.getStationId() + ar.getModelId());
  94. if (armap.containsKey(String.valueOf(sb))) {
  95. List<ProEconAlarmRule> ls = armap.get(String.valueOf(sb));
  96. ls.add(ar);
  97. } else {
  98. List<ProEconAlarmRule> ls = new ArrayList<>();
  99. ls.add(ar);
  100. armap.put(String.valueOf(sb), ls);
  101. }
  102. }
  103. }
  104. }
  105. for (ProBasicEquipment wt : CacheContext.wtls) {
  106. StringBuilder sb = new StringBuilder();
  107. sb.append(wt.getWindpowerstationId() + wt.getModelId());
  108. if (armap.containsKey(String.valueOf(sb))) {
  109. List<ProEconAlarmRule> alarmls = armap.get(String.valueOf(sb));
  110. if (!alarmls.isEmpty()) {
  111. for (ProEconAlarmRule alarm : alarmls) {
  112. AlarmVo vo = new AlarmVo();
  113. StringBuilder tbName = new StringBuilder();
  114. tbName.append(wt.getId()).append("_").append(alarm.getId());
  115. vo.setTbName(String.valueOf(tbName));
  116. vo.setAlarmid(alarm.getId());
  117. vo.setTagid(alarm.getExpression());
  118. vo.setAlarmtype(AlarmTypeValue.CT.getCode());
  119. vo.setCharacteristic(alarm.getName());
  120. vo.setComponents(alarm.getRelatedParts());
  121. vo.setDescription(alarm.getDescription());
  122. vo.setDeviceid(wt.getId());
  123. vo.setDevicename(wt.getName());
  124. vo.setDevicetype(DeviceTypeValue.WT.getCode());
  125. vo.setEnabled(alarm.getEnable());
  126. vo.setLineid(wt.getLineId());
  127. if (CacheContext.lnmap.containsKey(wt.getLineId())) {
  128. ProBasicLine ln = CacheContext.lnmap.get(wt.getLineId());
  129. vo.setLinename(ln.getName());
  130. }
  131. vo.setModelId(wt.getModelId());
  132. vo.setProjectid(wt.getProjectId());
  133. if (CacheContext.pjmap.containsKey(wt.getProjectId())) {
  134. ProBasicProject pj = CacheContext.pjmap.get(wt.getProjectId());
  135. vo.setProjectname(pj.getName());
  136. }
  137. vo.setRank(alarm.getRank());
  138. vo.setResettable(false);
  139. vo.setStationid(wt.getWindpowerstationId());
  140. if (CacheContext.wpmap.containsKey(wt.getWindpowerstationId())) {
  141. ProBasicPowerstation wp = CacheContext.wpmap.get(wt.getWindpowerstationId());
  142. vo.setStationname(wp.getName());
  143. }
  144. vo.setSubcomponents("");
  145. vo.setSuffix("");
  146. vo.setTriggertype(1);
  147. vo.setUniformcode("");
  148. vo.setSuperTableName(AlarmSuperTalbeType.CT.getCode());
  149. vo.setVal(0);
  150. vo.setCharacteristic("自定义");
  151. vo.setFaultCause(alarm.getDescription());
  152. vo.setResolvent(alarm.getExpression());
  153. alarmVoList.add(vo);
  154. }
  155. }
  156. }
  157. }
  158. }
  159. private void lnCustomAlarm(List<AlarmVo> alarmVoList, Map<String, List<ProEconAlarmRule>> alarmRulesMap) {
  160. Map<String, List<ProEconAlarmRule>> armap = new HashMap<>();
  161. if (alarmRulesMap.containsKey(DeviceTypeValue.IN.getCode())) {
  162. List<ProEconAlarmRule> arls = alarmRulesMap.get(DeviceTypeValue.IN.getCode());
  163. if (!arls.isEmpty()) {
  164. for (ProEconAlarmRule ar : arls) {
  165. StringBuilder sb = new StringBuilder();
  166. sb.append(ar.getStationId() + ar.getModelId());
  167. if (armap.containsKey(String.valueOf(sb))) {
  168. List<ProEconAlarmRule> ls = armap.get(String.valueOf(sb));
  169. ls.add(ar);
  170. } else {
  171. List<ProEconAlarmRule> ls = new ArrayList<>();
  172. ls.add(ar);
  173. armap.put(String.valueOf(sb), ls);
  174. }
  175. }
  176. }
  177. }
  178. for (ProBasicEquipment wt : CacheContext.wtls) {
  179. StringBuilder sb = new StringBuilder();
  180. sb.append(wt.getWindpowerstationId() + wt.getModelId());
  181. if (armap.containsKey(String.valueOf(sb))) {
  182. List<ProEconAlarmRule> alarmls = armap.get(String.valueOf(sb));
  183. if (!alarmls.isEmpty()) {
  184. for (ProEconAlarmRule alarm : alarmls) {
  185. AlarmVo vo = new AlarmVo();
  186. StringBuilder tbName = new StringBuilder();
  187. tbName.append(wt.getId()).append("_").append(alarm.getId());
  188. vo.setTbName(String.valueOf(tbName));
  189. vo.setAlarmid(alarm.getId());
  190. vo.setTagid(alarm.getExpression());
  191. vo.setAlarmtype(AlarmTypeValue.CT.getCode());
  192. vo.setCharacteristic(alarm.getName());
  193. vo.setComponents(alarm.getRelatedParts());
  194. vo.setDescription(alarm.getDescription());
  195. vo.setDeviceid(wt.getId());
  196. vo.setDevicename(wt.getName());
  197. vo.setDevicetype(DeviceTypeValue.IN.getCode());
  198. vo.setEnabled(alarm.getEnable());
  199. vo.setLineid(wt.getLineId());
  200. if (CacheContext.lnmap.containsKey(wt.getLineId())) {
  201. ProBasicLine ln = CacheContext.lnmap.get(wt.getLineId());
  202. vo.setLinename(ln.getName());
  203. }
  204. vo.setModelId(wt.getModelId());
  205. vo.setProjectid(wt.getProjectId());
  206. if (CacheContext.pjmap.containsKey(wt.getProjectId())) {
  207. ProBasicProject pj = CacheContext.pjmap.get(wt.getProjectId());
  208. vo.setProjectname(pj.getName());
  209. }
  210. vo.setRank(alarm.getRank());
  211. vo.setResettable(false);
  212. vo.setStationid(wt.getWindpowerstationId());
  213. if (CacheContext.wpmap.containsKey(wt.getWindpowerstationId())) {
  214. ProBasicPowerstation wp = CacheContext.wpmap.get(wt.getWindpowerstationId());
  215. vo.setStationname(wp.getName());
  216. }
  217. vo.setSubcomponents("");
  218. vo.setSuffix("");
  219. vo.setTriggertype(1);
  220. vo.setUniformcode("");
  221. vo.setSuperTableName(AlarmSuperTalbeType.CT.getCode());
  222. vo.setVal(0);
  223. vo.setCharacteristic("自定义");
  224. vo.setFaultCause(alarm.getDescription());
  225. vo.setResolvent(alarm.getExpression());
  226. alarmVoList.add(vo);
  227. }
  228. }
  229. }
  230. }
  231. }
  232. private void wpCustomAlarm(List<AlarmVo> alarmVoList, Map<String, List<ProEconAlarmRule>> alarmRulesMap) {
  233. List<ProEconAlarmRule> alarmls = new ArrayList<>();
  234. if (alarmRulesMap.containsKey(DeviceTypeValue.WP.getCode())) {
  235. alarmls = alarmRulesMap.get(DeviceTypeValue.WP.getCode());
  236. }
  237. for (ProBasicPowerstation wp : CacheContext.wpls) {
  238. if (!alarmls.isEmpty()) {
  239. for (ProEconAlarmRule alarm : alarmls) {
  240. AlarmVo vo = new AlarmVo();
  241. StringBuilder tbName = new StringBuilder();
  242. tbName.append(wp.getId()).append("_").append(alarm.getId());
  243. vo.setTbName(String.valueOf(tbName));
  244. vo.setAlarmid(alarm.getId());
  245. vo.setTagid(alarm.getExpression());
  246. vo.setAlarmtype(AlarmTypeValue.CT.getCode());
  247. vo.setCharacteristic(alarm.getName());
  248. vo.setComponents(alarm.getRelatedParts());
  249. vo.setDescription(alarm.getDescription());
  250. vo.setDeviceid("");
  251. vo.setDevicename("");
  252. vo.setDevicetype(DeviceTypeValue.WP.getCode());
  253. vo.setEnabled(alarm.getEnable());
  254. vo.setLineid("");
  255. vo.setLinename("");
  256. vo.setModelId("");
  257. vo.setProjectid("");
  258. vo.setProjectname("");
  259. vo.setRank(alarm.getRank());
  260. vo.setResettable(false);
  261. vo.setStationid(wp.getId());
  262. vo.setStationname(wp.getName());
  263. vo.setSubcomponents("");
  264. vo.setSuffix("");
  265. vo.setTriggertype(1);
  266. vo.setUniformcode("");
  267. vo.setSuperTableName(AlarmSuperTalbeType.CT.getCode());
  268. vo.setVal(0);
  269. alarmVoList.add(vo);
  270. }
  271. }
  272. }
  273. }
  274. private void btCustomAlarm(List<AlarmVo> alarmVoList, Map<String, List<ProEconAlarmRule>> alarmRulesMap) {
  275. Map<String,List<ProEconAlarmRule>> alarmMap=new HashMap<>();
  276. if (alarmRulesMap.containsKey(DeviceTypeValue.BT.getCode())) {
  277. List<ProEconAlarmRule> alarmls = alarmRulesMap.get(DeviceTypeValue.BT.getCode());
  278. if(!alarmls.isEmpty())
  279. {
  280. for(ProEconAlarmRule alarm:alarmls)
  281. {
  282. if (StringUtils.notEmp(alarm) && StringUtils.notEmp(alarm.getElectricalId()))
  283. {
  284. String [] ids=alarm.getElectricalId().split(",");
  285. if(ids.length>0)
  286. {
  287. for(int i=0;i<ids.length;i++)
  288. {
  289. if(alarmMap.containsKey(ids[i]))
  290. {
  291. List<ProEconAlarmRule> arls=alarmMap.get(ids[i]);
  292. arls.add(alarm);
  293. }else
  294. {
  295. List<ProEconAlarmRule> arls=new ArrayList<>();
  296. arls.add(alarm);
  297. alarmMap.put(ids[i],arls);
  298. }
  299. }
  300. }
  301. }
  302. }
  303. }
  304. }
  305. for (ProBasicSubStation wp : CacheContext.subwpls) {
  306. if (!alarmMap.isEmpty() && alarmMap.containsKey(wp.getId())) {
  307. List<ProEconAlarmRule> alarmls = alarmMap.get(wp.getId());
  308. for (ProEconAlarmRule alarm : alarmls) {
  309. AlarmVo vo = new AlarmVo();
  310. StringBuilder tbName = new StringBuilder();
  311. tbName.append(wp.getId()).append("_").append(alarm.getId());
  312. vo.setTbName(String.valueOf(tbName));
  313. vo.setAlarmid(alarm.getId());
  314. vo.setTagid(alarm.getExpression());
  315. vo.setAlarmtype(AlarmTypeValue.CT.getCode());
  316. vo.setCharacteristic(alarm.getName());
  317. vo.setComponents(alarm.getRelatedParts());
  318. vo.setDescription(alarm.getDescription());
  319. vo.setDeviceid("");
  320. vo.setDevicename("");
  321. vo.setDevicetype(DeviceTypeValue.BT.getCode());
  322. vo.setEnabled(alarm.getEnable());
  323. vo.setLineid("");
  324. vo.setLinename("");
  325. vo.setModelId("");
  326. vo.setProjectid("");
  327. vo.setProjectname("");
  328. vo.setRank(alarm.getRank());
  329. vo.setResettable(false);
  330. vo.setStationid(wp.getId());
  331. vo.setStationname(wp.getName());
  332. vo.setSubcomponents("");
  333. vo.setSuffix("");
  334. vo.setTriggertype(1);
  335. vo.setUniformcode("");
  336. vo.setSuperTableName(AlarmSuperTalbeType.CT.getCode());
  337. vo.setVal(0);
  338. vo.setOval(0);
  339. vo.setCharacteristic("自定义");
  340. vo.setFaultCause(alarm.getDescription());
  341. vo.setResolvent(alarm.getExpression());
  342. alarmVoList.add(vo);
  343. }
  344. }
  345. }
  346. }
  347. public List<AlarmCustomTag> findTags(String stbaleName) {
  348. List<AlarmCustomTag> ls = new ArrayList<>();
  349. if (StringUtils.notEmp(stbaleName)) {
  350. // List<AlarmVo> vos= alarmTsService.findTags(stbaleName);
  351. List<AlarmVo> vos = CacheContext.alarmCtList;
  352. if (!vos.isEmpty()) {
  353. for (AlarmVo vo : vos) {
  354. AlarmCustomTag po = new AlarmCustomTag();
  355. po.setId(vo.getTbName());
  356. po.setDescription(vo.getDescription());
  357. po.setTagId(vo.getTagid());
  358. po.setDeviceid(vo.getDeviceid());
  359. po.setDevicetype(vo.getDevicetype());
  360. po.setModelId(vo.getModelId());
  361. po.setAlarmtype(vo.getAlarmtype());
  362. po.setStationid(vo.getStationid());
  363. po.setVal(null);
  364. ls.add(po);
  365. }
  366. }
  367. }
  368. return ls;
  369. }
  370. public synchronized void saveAlarms(List<AlarmTag> ls) throws EncodeException, IOException {
  371. if (StringUtils.notEmp(ls) && !ls.isEmpty()) {
  372. alarmTsService.insertList(ls);
  373. Map<String,ProEconAlarmInfo> tempmap=new HashMap<>();
  374. if (StringUtils.notEmp(ls) && !ls.isEmpty()) {
  375. List<ProEconAlarmInfo> valuels = new ArrayList<>();
  376. for (AlarmTag alarm : ls) {
  377. if (CacheContext.alarmCtMap.containsKey(alarm.getId())) {
  378. AlarmVo vo = CacheContext.alarmCtMap.get(alarm.getId());
  379. ProEconAlarmInfo po = new ProEconAlarmInfo();
  380. po.setTbname(vo.getTbName());
  381. po.setLastUpdateTime(new Date(alarm.getTs()));
  382. po.setTagId(alarm.getTagId());
  383. po.setTriggerType(alarm.getTriggerType());
  384. po.setAlarmId(vo.getAlarmid());
  385. po.setDeviceId(vo.getDeviceid());
  386. po.setSubcomponents(vo.getSuffix());
  387. po.setRank(vo.getRank());
  388. po.setConfirmed(false);
  389. po.setUniformCode(vo.getUniformcode());
  390. po.setAlarmType(vo.getAlarmtype());
  391. po.setDeviceType(vo.getDevicetype());
  392. po.setComponents(vo.getComponents());
  393. po.setSubcomponents(vo.getSubcomponents());
  394. po.setDescription(vo.getDescription());
  395. po.setModelId(vo.getModelId());
  396. po.setResetTable(vo.getResettable());
  397. po.setEnable(vo.getEnabled());
  398. po.setCharacteristic(vo.getCharacteristic());
  399. po.setStationId(vo.getStationid());
  400. po.setProjectId(vo.getProjectid());
  401. po.setLineId(vo.getLineid());
  402. po.setStationName(vo.getStationname());
  403. po.setProjectiName(vo.getProjectname());
  404. po.setLineidName(vo.getLinename());
  405. po.setDeviceName(vo.getDevicename());
  406. po.setOval(alarm.getOval().intValue());
  407. po.setVal(alarm.getVal().intValue());
  408. po.setIsOpen(1);
  409. alarm.setRank(vo.getRank());
  410. alarm.setDescription(vo.getDescription());
  411. alarm.setAlarmType(vo.getAlarmtype());
  412. alarm.setDeviceType(vo.getDevicetype());
  413. alarm.setCharacteristic(vo.getCharacteristic());
  414. alarm.setFaultCause(vo.getFaultCause());
  415. alarm.setResolvent(vo.getResolvent());
  416. alarm.setIsClose(false);
  417. alarm.setDeviceId(vo.getDeviceid());
  418. alarm.setAlarmId(vo.getAlarmid());
  419. if(StringUtils.notEmp(vo.getDeviceid()) && CacheContext.wtmap.containsKey(vo.getDeviceid()))
  420. {
  421. alarm.setCode(CacheContext.wtmap.get(vo.getDeviceid()).getNemCode());
  422. }
  423. if(CacheContext.wpmap.containsKey(vo.getStationid()))
  424. {
  425. alarm.setWpName(CacheContext.wpmap.get(vo.getStationid()).getName());
  426. }
  427. if(po.getDeviceType().equals(DeviceTypeValue.IN.getCode()) || po.getDeviceType().equals(DeviceTypeValue.WT.getCode()))
  428. {
  429. pushDataSevice.pushWtData(alarm);
  430. }else if(po.getDeviceType().equals(DeviceTypeValue.BT.getCode()))
  431. {
  432. pushDataSevice.pushBtData(alarm);
  433. }
  434. tempmap.put(po.getTbname(),po);
  435. }
  436. }
  437. for (Map.Entry<String, ProEconAlarmInfo> entry : tempmap.entrySet()) {
  438. valuels.add(entry.getValue());
  439. }
  440. List<String> idls = new ArrayList<>();
  441. for (ProEconAlarmInfo vo : valuels) {
  442. idls.add(vo.getTbname());
  443. }
  444. if (!idls.isEmpty()) {
  445. proEconAlarmInfoService.removeByIds(idls);
  446. }
  447. List<ProEconAlarmInfo> templs = new ArrayList<>();
  448. for (ProEconAlarmInfo vo : valuels) {
  449. templs.add(vo);
  450. if (templs.size() == 100) {
  451. proEconAlarmInfoService.saveBatch(templs);
  452. templs = new ArrayList<>();
  453. }
  454. }
  455. if (!templs.isEmpty()) {
  456. proEconAlarmInfoService.saveBatch(templs);
  457. }
  458. }
  459. }
  460. }
  461. public synchronized void updateAlarms(List<AlarmTag> ls) throws EncodeException, IOException {
  462. if (StringUtils.notEmp(ls) && !ls.isEmpty()) {
  463. if (StringUtils.notEmp(ls) && !ls.isEmpty()) {
  464. StringBuilder sb = new StringBuilder();
  465. Map<String, AlarmSimpleVo> map = new HashMap<>();
  466. for (AlarmTag alarm : ls) {
  467. if (CacheContext.alarmCtMap.containsKey(alarm.getId())) {
  468. AlarmVo vo = CacheContext.alarmCtMap.get(alarm.getId());
  469. if(vo.getAlarmtype().equals(AlarmTypeValue.CT.getCode()) )
  470. {
  471. sb.append("'").append(alarm.getId().toLowerCase()).append("',");
  472. }
  473. }
  474. }
  475. String ids=null;
  476. if(sb.length()>0)
  477. {
  478. ids=sb.substring(0,sb.length()-1);
  479. }
  480. List<AlarmSimpleVo> alarmls=new ArrayList<>();
  481. if( null !=ids)
  482. {
  483. alarmls=alarmTsService.selectLastRowByTbname(AlarmSuperTalbeType.CT.getCode(),ids);
  484. }
  485. if(!alarmls.isEmpty())
  486. {
  487. for(AlarmSimpleVo vo:alarmls)
  488. {
  489. map.put(vo.getTbName().toLowerCase(),vo);
  490. }
  491. }
  492. List<AlarmTag> alarmTagls=new ArrayList<>();
  493. for (AlarmTag alarm : ls) {
  494. if(map.containsKey(alarm.getId().toLowerCase()))
  495. {
  496. AlarmSimpleVo tag=map.get(alarm.getId().toLowerCase());
  497. if(StringUtils.notEmp(tag.getTs()))
  498. {
  499. alarm.setTs(tag.getTs());
  500. Date begin = new Date(alarm.getEndts());
  501. Date end = new Date(alarm.getTs());
  502. double value= DateUtils.secondsDiff(begin,end);
  503. alarm.setTimeLong(value);
  504. alarm.setConfirmed(tag.getConfirmed());
  505. alarm.setSuperTalbe(AlarmSuperTalbeType.CT.getCode());
  506. alarmTagls.add(alarm);
  507. }
  508. }
  509. }
  510. if (StringUtils.notEmp(alarmTagls) && !alarmTagls.isEmpty()) {
  511. alarmTsService.insertList(alarmTagls);
  512. }
  513. Map<String,ProEconAlarmInfo> tempmap=new HashMap<>();
  514. if (StringUtils.notEmp(alarmTagls) && !alarmTagls.isEmpty()) {
  515. List<ProEconAlarmInfo> valuels = new ArrayList<>();
  516. for (AlarmTag alarm : alarmTagls) {
  517. if (CacheContext.alarmCtMap.containsKey(alarm.getId())) {
  518. AlarmVo vo = CacheContext.alarmCtMap.get(alarm.getId());
  519. ProEconAlarmInfo po = new ProEconAlarmInfo();
  520. po.setTbname(vo.getTbName());
  521. po.setLastUpdateTime(new Date(alarm.getTs()));
  522. po.setTagId(alarm.getTagId());
  523. po.setTriggerType(alarm.getTriggerType());
  524. po.setAlarmId(vo.getAlarmid());
  525. po.setDeviceId(vo.getDeviceid());
  526. po.setSubcomponents(vo.getSuffix());
  527. po.setRank(vo.getRank());
  528. po.setConfirmed(alarm.getConfirmed());
  529. po.setUniformCode(vo.getUniformcode());
  530. po.setAlarmType(vo.getAlarmtype());
  531. po.setDeviceType(vo.getDevicetype());
  532. po.setComponents(vo.getComponents());
  533. po.setSubcomponents(vo.getSubcomponents());
  534. po.setDescription(vo.getDescription());
  535. po.setModelId(vo.getModelId());
  536. po.setResetTable(vo.getResettable());
  537. po.setEnable(vo.getEnabled());
  538. po.setCharacteristic(vo.getCharacteristic());
  539. po.setStationId(vo.getStationid());
  540. po.setProjectId(vo.getProjectid());
  541. po.setLineId(vo.getLineid());
  542. po.setStationName(vo.getStationname());
  543. po.setProjectiName(vo.getProjectname());
  544. po.setLineidName(vo.getLinename());
  545. po.setDeviceName(vo.getDevicename());
  546. po.setOval(alarm.getOval().intValue());
  547. po.setVal(alarm.getVal().intValue());
  548. po.setCloseTime(new Date(alarm.getEndts()));
  549. po.setIsOpen(0);
  550. alarm.setRank(vo.getRank());
  551. alarm.setDescription(vo.getDescription());
  552. alarm.setAlarmType(vo.getAlarmtype());
  553. alarm.setDeviceType(vo.getDevicetype());
  554. alarm.setCharacteristic(vo.getCharacteristic());
  555. alarm.setFaultCause(vo.getFaultCause());
  556. alarm.setResolvent(vo.getResolvent());
  557. alarm.setDeviceId(vo.getDeviceid());
  558. alarm.setAlarmId(vo.getAlarmid());
  559. alarm.setIsClose(true);
  560. if(StringUtils.notEmp(vo.getDeviceid()) && CacheContext.wtmap.containsKey(vo.getDeviceid()))
  561. {
  562. alarm.setCode(CacheContext.wtmap.get(vo.getDeviceid()).getNemCode());
  563. }
  564. if(CacheContext.wpmap.containsKey(vo.getStationid()))
  565. {
  566. alarm.setWpName(CacheContext.wpmap.get(vo.getStationid()).getName());
  567. }
  568. if(po.getDeviceType().equals(DeviceTypeValue.IN.getCode()) || po.getDeviceType().equals(DeviceTypeValue.WT.getCode()))
  569. {
  570. pushDataSevice.pushWtData(alarm);
  571. }else if(po.getDeviceType().equals(DeviceTypeValue.BT.getCode()))
  572. {
  573. pushDataSevice.pushBtData(alarm);
  574. }
  575. tempmap.put(po.getTbname(),po);
  576. }
  577. }
  578. for (Map.Entry<String, ProEconAlarmInfo> entry : tempmap.entrySet()) {
  579. valuels.add(entry.getValue());
  580. }
  581. List<String> idls = new ArrayList<>();
  582. for (ProEconAlarmInfo vo : valuels) {
  583. idls.add(vo.getTbname());
  584. }
  585. if (!idls.isEmpty()) {
  586. proEconAlarmInfoService.removeByIds(idls);
  587. }
  588. List<ProEconAlarmInfo> templs = new ArrayList<>();
  589. for (ProEconAlarmInfo vo : valuels) {
  590. templs.add(vo);
  591. if (templs.size() == 100) {
  592. proEconAlarmInfoService.saveBatch(templs);
  593. templs = new ArrayList<>();
  594. }
  595. }
  596. if (!templs.isEmpty()) {
  597. proEconAlarmInfoService.saveBatch(templs);
  598. }
  599. }
  600. }
  601. }
  602. }
  603. // public void pushData(AlarmTag po) throws EncodeException, IOException {
  604. // ConcurrentHashMap<String, WebSocket> webSocketPushMap = WebSocket.webSocketMap;
  605. // ConcurrentHashMap<String, PushParams> webSocketPushParamsMap = WebSocket.webSocketParamsMap;
  606. // if(!webSocketPushMap.isEmpty()){
  607. // for(String key : webSocketPushMap.keySet()){
  608. // // 根据ws连接用户ID获取推送参数
  609. // PushParams pushParams = webSocketPushParamsMap.get(key);
  610. //
  611. // if(webSocketPushParamsMap.contains(po.getId()))
  612. // {
  613. // PushParams pp=webSocketPushParamsMap.get(po.getId());
  614. //
  615. // double hours=DateUtils.hoursDiff(pp.getStartTime(),new Date());
  616. // if(hours>pushinterval)
  617. // {
  618. //
  619. // synchronized (key)
  620. // {
  621. // webSocket.sendMessageByUserId(key,po);
  622. // pp.setStartTime(new Date(po.getTs()));
  623. // }
  624. //
  625. //
  626. // }
  627. // }else
  628. // {
  629. // synchronized (key)
  630. // {
  631. // webSocket.sendMessageByUserId(key,po);
  632. // PushParams pp=new PushParams();
  633. // pp.setStartTime(new Date());
  634. // webSocketPushParamsMap.put(po.getId(),pp);
  635. // }
  636. //
  637. // }
  638. // }
  639. // }
  640. //
  641. // }
  642. }