123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- package com.gyee.alarm.service;
- import com.gyee.alarm.init.CacheContext;
- import com.gyee.alarm.model.auto.ProBasicEquipment;
- import com.gyee.alarm.model.auto.ProBasicPowerstation;
- import com.gyee.alarm.model.auto.ProBasicSubStation;
- import com.gyee.alarm.model.vo.AlarmTag;
- import com.gyee.alarm.service.auto.ISysUserService;
- import com.gyee.alarm.util.DateUtils;
- import com.gyee.alarm.websocket.PushParams;
- import com.gyee.alarm.websocket.WebSocket;
- import com.gyee.alarm.websocket.WebSocketBt;
- import com.gyee.common.model.StringUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.websocket.EncodeException;
- import java.io.IOException;
- import java.util.*;
- import java.util.concurrent.ConcurrentHashMap;
- @Service
- public class PushDataSevice {
- @Resource
- private ISysUserService sysUserService;
- @Value("${pushinterval}")
- private Double pushinterval;
- @Resource
- private WebSocket webSocket;
- @Resource
- private WebSocketBt webSocketBt;
- public void pushWtData(AlarmTag po) throws EncodeException, IOException {
- ConcurrentHashMap<String, WebSocket> webSocketPushMap = WebSocket.webSocketMap;
- ConcurrentHashMap<String, PushParams> webSocketPushParamsMap = WebSocket.webSocketParamsMap;
- if (!webSocketPushMap.isEmpty()) {
- for (String key : webSocketPushMap.keySet()) {
- // 根据ws连接用户ID获取推送参数
- synchronized (po.getId()) {
- // if(webSocketPushParamsMap.contains(po.getId()))
- // {
- // PushParams pp=webSocketPushParamsMap.get(po.getId());
- //
- // double hours= DateUtils.hoursDiff(pp.getStartTime(),new Date());
- // if(hours>pushinterval)
- // {
- // if(checkWtId(key,po.getDeviceId()))
- // {
- // webSocket.sendMessageByUserId(key,po);
- // pp.setStartTime(new Date(po.getTs()));
- // }
- // }
- // }else
- // {
- // if(checkWtId(key,po.getDeviceId()))
- // {
- // webSocket.sendMessageByUserId(key,po);
- // PushParams pp=new PushParams();
- // pp.setStartTime(new Date());
- // webSocketPushParamsMap.put(po.getId(),pp);
- //
- //
- // }
- //
- // }
- if (checkWtId(key, po.getDeviceId())) {
- if(webSocketPushMap.containsKey(key))
- {
- webSocket.sendMessageByUserId(key, po);
- }
- }
- }
- }
- }
- }
- public void pushBtData(AlarmTag po) throws EncodeException, IOException {
- ConcurrentHashMap<String, WebSocket> webSocketPushMap = WebSocket.webSocketMap;
- ConcurrentHashMap<String, WebSocketBt> webSocketPushBtMap = WebSocketBt.webSocketBtMap;
- ConcurrentHashMap<String, PushParams> webSocketPushParamsMap = WebSocket.webSocketParamsMap;
- ConcurrentHashMap<String, List<String>> btParamsMap = WebSocketBt.btParamsMap;
- if (!webSocketPushMap.isEmpty()) {
- for (String key : webSocketPushMap.keySet()) {
- // 根据ws连接用户ID获取推送参数
- // PushParams pushParams = webSocketPushParamsMap.get(key);
- synchronized (po.getId()) {
- if (checkBtId(key, po.getWpId())) {
- webSocket.sendMessageByUserId(key, po);
- }
- }
- }
- }
- if (!webSocketPushBtMap.isEmpty()) {
- synchronized (po.getId()) {
- if (po.getRank() > 3) {
- if (btParamsMap.containsKey(po.getWpId())) {
- List<String> btls = btParamsMap.get(po.getWpId());
- if (btls.contains(po.getAlarmId())) {
- btls.remove(po.getAlarmId());
- } else {
- btls.add(po.getAlarmId());
- }
- if (btls.isEmpty()) {
- for (String key : webSocketPushBtMap.keySet()) {
- if (checkBtId(key, po.getWpId())) {
- Map<String, Boolean> map = new HashMap<>();
- map.put(po.getWpId(), false);
- webSocketBt.sendMessageByUserId(key, map);
- }
- }
- } else {
- for (String key : webSocketPushBtMap.keySet()) {
- if (checkBtId(key, po.getWpId())) {
- Map<String, Boolean> map = new HashMap<>();
- map.put(po.getWpId(), true);
- webSocketBt.sendMessageByUserId(key, map);
- }
- }
- }
- } else {
- List<String> btls = new ArrayList<>();
- btls.add(po.getAlarmId());
- btParamsMap.put(po.getWpId(), btls);
- if (StringUtils.empty(po.getIsClose())) {
- for (String key : webSocketPushBtMap.keySet()) {
- if (checkBtId(key, po.getWpId())) {
- Map<String, Boolean> map = new HashMap<>();
- map.put(po.getWpId(), false);
- webSocketBt.sendMessageByUserId(key, map);
- }
- }
- } else {
- for (String key : webSocketPushBtMap.keySet()) {
- if (checkBtId(key, po.getWpId())) {
- Map<String, Boolean> map = new HashMap<>();
- map.put(po.getWpId(), true);
- webSocketBt.sendMessageByUserId(key, map);
- }
- }
- }
- }
- }
- }
- }
- }
- private boolean checkWtId(String userId, String wtId) {
- boolean result = false;
- if (StringUtils.notEmp(userId)) {
- String[] str = userId.split("_");
- if (str.length == 2) {
- List<String> depls = sysUserService.getUserByuserId(Long.valueOf(str[0]));
- if (!depls.isEmpty()) {
- String depId = depls.get(0);
- if (CacheContext.wpwtmap.containsKey(depId)) {
- List<ProBasicEquipment> wtls = CacheContext.wpwtmap.get(depId);
- for (ProBasicEquipment wt : wtls) {
- if (wt.getId().equals(wtId)) {
- result = true;
- break;
- }
- }
- } else if (CacheContext.cmwtlsmap.containsKey(depId)) {
- List<ProBasicEquipment> wtls = CacheContext.cmwtlsmap.get(depId);
- for (ProBasicEquipment wt : wtls) {
- if (wt.getId().equals(wtId)) {
- result = true;
- break;
- }
- }
- } else if (CacheContext.rgwtlsmap.containsKey(depId)) {
- List<ProBasicEquipment> wtls = CacheContext.rgwtlsmap.get(depId);
- for (ProBasicEquipment wt : wtls) {
- if (wt.getId().equals(wtId)) {
- result = true;
- break;
- }
- }
- } else if (depId.equals("0")) {
- for (ProBasicEquipment wt : CacheContext.wtls) {
- if (wt.getId().equals(wtId)) {
- result = true;
- break;
- }
- }
- }
- }
- }
- }
- return result;
- }
- private boolean checkBtId(String userId, String btId) {
- boolean result = false;
- if (StringUtils.notEmp(userId)) {
- String[] str = userId.split("_");
- if (str.length == 2) {
- List<String> depls = sysUserService.getUserByuserId(Long.valueOf(str[0]));
- if (!depls.isEmpty()) {
- String depId = depls.get(0);
- if (CacheContext.wpmap.containsKey(depId)) {
- ProBasicPowerstation wp = CacheContext.wpmap.get(depId);
- if (CacheContext.subwpmap.containsKey(btId)) {
- ProBasicSubStation sub = CacheContext.subwpmap.get(btId);
- if (sub.getWindpowerstationId().equals(wp.getId())) {
- result = true;
- }
- }
- } else if (CacheContext.cpwpmap.containsKey(depId)) {
- List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(depId);
- for (ProBasicPowerstation wp : wpls) {
- if (CacheContext.subwpmap.containsKey(btId)) {
- ProBasicSubStation sub = CacheContext.subwpmap.get(btId);
- if (sub.getWindpowerstationId().equals(wp.getId())) {
- result = true;
- break;
- }
- }
- }
- } else if (CacheContext.rgwpmap.containsKey(depId)) {
- List<ProBasicPowerstation> wpls = CacheContext.rgwpmap.get(depId);
- for (ProBasicPowerstation wp : wpls) {
- if (CacheContext.subwpmap.containsKey(btId)) {
- ProBasicSubStation sub = CacheContext.subwpmap.get(btId);
- if (sub.getWindpowerstationId().equals(wp.getId())) {
- result = true;
- break;
- }
- }
- }
- } else if (depId.equals("0")) {
- result = true;
- }
- }
- }
- }
- return result;
- }
- }
|