PushDataSevice.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package com.gyee.alarm.service;
  2. import com.gyee.alarm.init.CacheContext;
  3. import com.gyee.alarm.model.auto.ProBasicEquipment;
  4. import com.gyee.alarm.model.auto.ProBasicPowerstation;
  5. import com.gyee.alarm.model.auto.ProBasicSubStation;
  6. import com.gyee.alarm.model.vo.AlarmTag;
  7. import com.gyee.alarm.service.auto.ISysUserService;
  8. import com.gyee.alarm.util.DateUtils;
  9. import com.gyee.alarm.websocket.PushParams;
  10. import com.gyee.alarm.websocket.WebSocket;
  11. import com.gyee.alarm.websocket.WebSocketBt;
  12. import com.gyee.common.model.StringUtils;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.stereotype.Service;
  15. import javax.annotation.Resource;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.websocket.EncodeException;
  18. import java.io.IOException;
  19. import java.util.*;
  20. import java.util.concurrent.ConcurrentHashMap;
  21. @Service
  22. public class PushDataSevice {
  23. @Resource
  24. private ISysUserService sysUserService;
  25. @Value("${pushinterval}")
  26. private Double pushinterval;
  27. @Resource
  28. private WebSocket webSocket;
  29. @Resource
  30. private WebSocketBt webSocketBt;
  31. public void pushWtData(AlarmTag po) throws EncodeException, IOException {
  32. ConcurrentHashMap<String, WebSocket> webSocketPushMap = WebSocket.webSocketMap;
  33. ConcurrentHashMap<String, PushParams> webSocketPushParamsMap = WebSocket.webSocketParamsMap;
  34. if (!webSocketPushMap.isEmpty()) {
  35. for (String key : webSocketPushMap.keySet()) {
  36. // 根据ws连接用户ID获取推送参数
  37. synchronized (po.getId()) {
  38. // if(webSocketPushParamsMap.contains(po.getId()))
  39. // {
  40. // PushParams pp=webSocketPushParamsMap.get(po.getId());
  41. //
  42. // double hours= DateUtils.hoursDiff(pp.getStartTime(),new Date());
  43. // if(hours>pushinterval)
  44. // {
  45. // if(checkWtId(key,po.getDeviceId()))
  46. // {
  47. // webSocket.sendMessageByUserId(key,po);
  48. // pp.setStartTime(new Date(po.getTs()));
  49. // }
  50. // }
  51. // }else
  52. // {
  53. // if(checkWtId(key,po.getDeviceId()))
  54. // {
  55. // webSocket.sendMessageByUserId(key,po);
  56. // PushParams pp=new PushParams();
  57. // pp.setStartTime(new Date());
  58. // webSocketPushParamsMap.put(po.getId(),pp);
  59. //
  60. //
  61. // }
  62. //
  63. // }
  64. if (checkWtId(key, po.getDeviceId())) {
  65. if(webSocketPushMap.containsKey(key))
  66. {
  67. webSocket.sendMessageByUserId(key, po);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. public void pushBtData(AlarmTag po) throws EncodeException, IOException {
  75. ConcurrentHashMap<String, WebSocket> webSocketPushMap = WebSocket.webSocketMap;
  76. ConcurrentHashMap<String, WebSocketBt> webSocketPushBtMap = WebSocketBt.webSocketBtMap;
  77. ConcurrentHashMap<String, PushParams> webSocketPushParamsMap = WebSocket.webSocketParamsMap;
  78. ConcurrentHashMap<String, List<String>> btParamsMap = WebSocketBt.btParamsMap;
  79. if (!webSocketPushMap.isEmpty()) {
  80. for (String key : webSocketPushMap.keySet()) {
  81. // 根据ws连接用户ID获取推送参数
  82. // PushParams pushParams = webSocketPushParamsMap.get(key);
  83. synchronized (po.getId()) {
  84. if (checkBtId(key, po.getWpId())) {
  85. webSocket.sendMessageByUserId(key, po);
  86. }
  87. }
  88. }
  89. }
  90. if (!webSocketPushBtMap.isEmpty()) {
  91. synchronized (po.getId()) {
  92. if (po.getRank() > 3) {
  93. if (btParamsMap.containsKey(po.getWpId())) {
  94. List<String> btls = btParamsMap.get(po.getWpId());
  95. if (btls.contains(po.getAlarmId())) {
  96. btls.remove(po.getAlarmId());
  97. } else {
  98. btls.add(po.getAlarmId());
  99. }
  100. if (btls.isEmpty()) {
  101. for (String key : webSocketPushBtMap.keySet()) {
  102. if (checkBtId(key, po.getWpId())) {
  103. Map<String, Boolean> map = new HashMap<>();
  104. map.put(po.getWpId(), false);
  105. webSocketBt.sendMessageByUserId(key, map);
  106. }
  107. }
  108. } else {
  109. for (String key : webSocketPushBtMap.keySet()) {
  110. if (checkBtId(key, po.getWpId())) {
  111. Map<String, Boolean> map = new HashMap<>();
  112. map.put(po.getWpId(), true);
  113. webSocketBt.sendMessageByUserId(key, map);
  114. }
  115. }
  116. }
  117. } else {
  118. List<String> btls = new ArrayList<>();
  119. btls.add(po.getAlarmId());
  120. btParamsMap.put(po.getWpId(), btls);
  121. if (StringUtils.empty(po.getIsClose())) {
  122. for (String key : webSocketPushBtMap.keySet()) {
  123. if (checkBtId(key, po.getWpId())) {
  124. Map<String, Boolean> map = new HashMap<>();
  125. map.put(po.getWpId(), false);
  126. webSocketBt.sendMessageByUserId(key, map);
  127. }
  128. }
  129. } else {
  130. for (String key : webSocketPushBtMap.keySet()) {
  131. if (checkBtId(key, po.getWpId())) {
  132. Map<String, Boolean> map = new HashMap<>();
  133. map.put(po.getWpId(), true);
  134. webSocketBt.sendMessageByUserId(key, map);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }
  143. private boolean checkWtId(String userId, String wtId) {
  144. boolean result = false;
  145. if (StringUtils.notEmp(userId)) {
  146. String[] str = userId.split("_");
  147. if (str.length == 2) {
  148. List<String> depls = sysUserService.getUserByuserId(Long.valueOf(str[0]));
  149. if (!depls.isEmpty()) {
  150. String depId = depls.get(0);
  151. if (CacheContext.wpwtmap.containsKey(depId)) {
  152. List<ProBasicEquipment> wtls = CacheContext.wpwtmap.get(depId);
  153. for (ProBasicEquipment wt : wtls) {
  154. if (wt.getId().equals(wtId)) {
  155. result = true;
  156. break;
  157. }
  158. }
  159. } else if (CacheContext.cmwtlsmap.containsKey(depId)) {
  160. List<ProBasicEquipment> wtls = CacheContext.cmwtlsmap.get(depId);
  161. for (ProBasicEquipment wt : wtls) {
  162. if (wt.getId().equals(wtId)) {
  163. result = true;
  164. break;
  165. }
  166. }
  167. } else if (CacheContext.rgwtlsmap.containsKey(depId)) {
  168. List<ProBasicEquipment> wtls = CacheContext.rgwtlsmap.get(depId);
  169. for (ProBasicEquipment wt : wtls) {
  170. if (wt.getId().equals(wtId)) {
  171. result = true;
  172. break;
  173. }
  174. }
  175. } else if (depId.equals("0")) {
  176. for (ProBasicEquipment wt : CacheContext.wtls) {
  177. if (wt.getId().equals(wtId)) {
  178. result = true;
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. return result;
  187. }
  188. private boolean checkBtId(String userId, String btId) {
  189. boolean result = false;
  190. if (StringUtils.notEmp(userId)) {
  191. String[] str = userId.split("_");
  192. if (str.length == 2) {
  193. List<String> depls = sysUserService.getUserByuserId(Long.valueOf(str[0]));
  194. if (!depls.isEmpty()) {
  195. String depId = depls.get(0);
  196. if (CacheContext.wpmap.containsKey(depId)) {
  197. ProBasicPowerstation wp = CacheContext.wpmap.get(depId);
  198. if (CacheContext.subwpmap.containsKey(btId)) {
  199. ProBasicSubStation sub = CacheContext.subwpmap.get(btId);
  200. if (sub.getWindpowerstationId().equals(wp.getId())) {
  201. result = true;
  202. }
  203. }
  204. } else if (CacheContext.cpwpmap.containsKey(depId)) {
  205. List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(depId);
  206. for (ProBasicPowerstation wp : wpls) {
  207. if (CacheContext.subwpmap.containsKey(btId)) {
  208. ProBasicSubStation sub = CacheContext.subwpmap.get(btId);
  209. if (sub.getWindpowerstationId().equals(wp.getId())) {
  210. result = true;
  211. break;
  212. }
  213. }
  214. }
  215. } else if (CacheContext.rgwpmap.containsKey(depId)) {
  216. List<ProBasicPowerstation> wpls = CacheContext.rgwpmap.get(depId);
  217. for (ProBasicPowerstation wp : wpls) {
  218. if (CacheContext.subwpmap.containsKey(btId)) {
  219. ProBasicSubStation sub = CacheContext.subwpmap.get(btId);
  220. if (sub.getWindpowerstationId().equals(wp.getId())) {
  221. result = true;
  222. break;
  223. }
  224. }
  225. }
  226. } else if (depId.equals("0")) {
  227. result = true;
  228. }
  229. }
  230. }
  231. }
  232. return result;
  233. }
  234. }