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 webSocketPushMap = WebSocket.webSocketMap; ConcurrentHashMap 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 webSocketPushMap = WebSocket.webSocketMap; ConcurrentHashMap webSocketPushBtMap = WebSocketBt.webSocketBtMap; ConcurrentHashMap webSocketPushParamsMap = WebSocket.webSocketParamsMap; ConcurrentHashMap> 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 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 map = new HashMap<>(); map.put(po.getWpId(), false); webSocketBt.sendMessageByUserId(key, map); } } } else { for (String key : webSocketPushBtMap.keySet()) { if (checkBtId(key, po.getWpId())) { Map map = new HashMap<>(); map.put(po.getWpId(), true); webSocketBt.sendMessageByUserId(key, map); } } } } else { List 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 map = new HashMap<>(); map.put(po.getWpId(), false); webSocketBt.sendMessageByUserId(key, map); } } } else { for (String key : webSocketPushBtMap.keySet()) { if (checkBtId(key, po.getWpId())) { Map 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 depls = sysUserService.getUserByuserId(Long.valueOf(str[0])); if (!depls.isEmpty()) { String depId = depls.get(0); if (CacheContext.wpwtmap.containsKey(depId)) { List wtls = CacheContext.wpwtmap.get(depId); for (ProBasicEquipment wt : wtls) { if (wt.getId().equals(wtId)) { result = true; break; } } } else if (CacheContext.cmwtlsmap.containsKey(depId)) { List wtls = CacheContext.cmwtlsmap.get(depId); for (ProBasicEquipment wt : wtls) { if (wt.getId().equals(wtId)) { result = true; break; } } } else if (CacheContext.rgwtlsmap.containsKey(depId)) { List 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 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 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 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; } }