use of com.serotonin.m2m2.web.dwr.longPoll.LongPollState in project ma-modules-public by infiniteautomation.
the class GraphicalViewLongPollDefinition method handleLongPoll.
@Override
public void handleLongPoll(LongPollData data, Map<String, Object> response, User user) {
boolean view = false;
boolean viewEdit = false;
boolean anon = false;
if (data.getRequest().hasHandler("graphicalView") && user != null)
view = true;
else if (data.getRequest().hasHandler("graphicalViewEdit") && user != null)
viewEdit = true;
else if (data.getRequest().hasHandler("graphicalViewAnon"))
anon = true;
if (view || viewEdit || anon) {
LongPollState state = data.getState();
List<ViewComponentState> graphicalViewStates = GraphicalViewsCommon.getGraphicalViewListStates(data);
List<ViewComponentState> newStates;
synchronized (state) {
if (anon)
newStates = graphicalViewDwr.getViewPointDataAnon(data.getRequest().getRefId());
else
newStates = graphicalViewDwr.getViewPointData(viewEdit);
List<ViewComponentState> differentStates = new ArrayList<ViewComponentState>();
for (ViewComponentState newState : newStates) {
ViewComponentState oldState = getGraphicalViewState(newState.getId(), graphicalViewStates);
if (oldState == null)
differentStates.add(newState);
else {
ViewComponentState copy = newState.clone();
copy.removeEqualValue(oldState);
if (!copy.isEmpty())
differentStates.add(copy);
}
}
if (!differentStates.isEmpty()) {
response.put("viewStates", differentStates);
GraphicalViewsCommon.setGraphicalViewListStates(data, newStates);
}
}
}
}
use of com.serotonin.m2m2.web.dwr.longPoll.LongPollState in project ma-modules-public by infiniteautomation.
the class WatchListLongPollHandler method handleLongPoll.
@Override
public void handleLongPoll(LongPollData data, Map<String, Object> response, User user) {
if (data.getRequest().hasHandler(key) && user != null) {
LongPollState state = data.getState();
List<WatchListState> watchListStates = WatchListCommon.getWatchListStates(data);
synchronized (state) {
List<WatchListState> newStates = watchListDwr.getPointData();
List<WatchListState> differentStates = new ArrayList<WatchListState>();
for (WatchListState newState : newStates) {
WatchListState oldState = getWatchListState(newState.getId(), watchListStates);
if (oldState == null)
differentStates.add(newState);
else {
WatchListState copy = newState.clone();
copy.removeEqualValue(oldState);
if (!copy.isEmpty())
differentStates.add(copy);
}
}
if (!differentStates.isEmpty()) {
response.put("watchListStates", differentStates);
state.setAttribute("watchListStates", newStates);
}
}
}
}
use of com.serotonin.m2m2.web.dwr.longPoll.LongPollState in project ma-core-public by infiniteautomation.
the class BaseDwr method getLongPollData.
protected LongPollData getLongPollData(int pollSessionId, boolean refreshState) {
List<LongPollData> dataList = getLongPollData();
LongPollData data = getDataFromList(dataList, pollSessionId);
if (data == null) {
synchronized (dataList) {
data = getDataFromList(dataList, pollSessionId);
if (data == null) {
data = new LongPollData(pollSessionId);
refreshState = true;
dataList.add(data);
}
}
}
if (refreshState)
data.setState(new LongPollState());
return data;
}
use of com.serotonin.m2m2.web.dwr.longPoll.LongPollState in project ma-core-public by infiniteautomation.
the class BaseDwr method doLongPoll.
@DwrPermission(anonymous = true)
public Map<String, Object> doLongPoll(int pollSessionId) {
Map<String, Object> response = new HashMap<>();
HttpServletRequest httpRequest = WebContextFactory.get().getHttpServletRequest();
User user = Common.getUser(httpRequest);
EventDao eventDao = EventDao.instance;
LongPollData data = getLongPollData(pollSessionId, false);
data.updateTimestamp();
LongPollRequest pollRequest = data.getRequest();
// One minute
long expireTime = Common.timer.currentTimeMillis() + 60000;
LongPollState state = data.getState();
int waitTime = SystemSettingsDao.getIntValue(SystemSettingsDao.UI_PERFORMANCE);
// For users that log in on multiple machines (or browsers), reset the
// last alarm timestamp so that it always
// gets reset with at least each new poll. For now this beats writing
// user-specific event change tracking code.
state.setLastAlarmLevelChange(0);
while (!pollRequest.isTerminated() && Common.timer.currentTimeMillis() < expireTime) {
if (Providers.get(IMangoLifecycle.class).isTerminated()) {
pollRequest.setTerminated(true);
break;
}
if (pollRequest.isMaxAlarm() && user != null) {
// Track the last alarm count to see if we need to update the
// alarm toaster
Integer lastUnsilencedAlarmCount = (Integer) data.getState().getAttribute("lastUnsilencedAlarmCount");
// Ensure we have one, as we won't on first run
if (lastUnsilencedAlarmCount == null)
lastUnsilencedAlarmCount = 0;
// Sort into lists for the different types
List<EventInstance> events = Common.eventManager.getAllActiveUserEvents(user.getId());
int currentUnsilencedAlarmCount = events.size();
int lifeSafetyTotal = 0;
EventInstance lifeSafetyEvent = null;
int criticalTotal = 0;
EventInstance criticalEvent = null;
int urgentTotal = 0;
EventInstance urgentEvent = null;
int warningTotal = 0;
EventInstance warningEvent = null;
int importantTotal = 0;
EventInstance importantEvent = null;
int informationTotal = 0;
EventInstance informationEvent = null;
int noneTotal = 0;
EventInstance noneEvent = null;
int doNotLogTotal = 0;
EventInstance doNotLogEvent = null;
for (EventInstance event : events) {
switch(event.getAlarmLevel()) {
case AlarmLevels.LIFE_SAFETY:
lifeSafetyTotal++;
lifeSafetyEvent = event;
break;
case AlarmLevels.CRITICAL:
criticalTotal++;
criticalEvent = event;
break;
case AlarmLevels.URGENT:
urgentTotal++;
urgentEvent = event;
break;
case AlarmLevels.WARNING:
warningTotal++;
warningEvent = event;
break;
case AlarmLevels.IMPORTANT:
importantTotal++;
importantEvent = event;
break;
case AlarmLevels.INFORMATION:
informationTotal++;
informationEvent = event;
break;
case AlarmLevels.NONE:
noneTotal++;
noneEvent = event;
break;
case AlarmLevels.DO_NOT_LOG:
doNotLogTotal++;
doNotLogEvent = event;
break;
}
}
// If we have some new information we should show it
if (lastUnsilencedAlarmCount != currentUnsilencedAlarmCount) {
data.getState().setAttribute("lastUnsilencedAlarmCount", // Update the value
currentUnsilencedAlarmCount);
// Indicate to UI that
response.put("alarmsUpdated", true);
// there is a new
// alarm
response.put("alarmsDoNotLog", doNotLogTotal);
if (doNotLogTotal == 1)
response.put("doNotLogEvent", doNotLogEvent);
response.put("alarmsNone", noneTotal);
if (noneTotal == 1)
response.put("noneEvent", noneEvent);
response.put("alarmsInformation", informationTotal);
if (informationTotal == 1)
response.put("informationEvent", informationEvent);
response.put("alarmsImportant", importantTotal);
if (importantTotal == 1)
response.put("importantEvent", importantEvent);
response.put("alarmsWarning", warningTotal);
if (warningTotal == 1)
response.put("warningEvent", warningEvent);
response.put("alarmsUrgent", urgentTotal);
if (urgentTotal == 1)
response.put("urgentEvent", urgentEvent);
response.put("alarmsCritical", criticalTotal);
if (criticalTotal == 1)
response.put("criticalEvent", criticalEvent);
response.put("alarmsLifeSafety", lifeSafetyTotal);
if (lifeSafetyTotal == 1)
response.put("lifeSafetyEvent", lifeSafetyEvent);
} else {
// end if new alarm toaster info
// response.put("alarmsUpdated",false);
}
// The events have changed. See if the user's particular max
// alarm level has changed.
int maxAlarmLevel = AlarmLevels.DO_NOT_LOG;
if (lifeSafetyTotal > 0)
maxAlarmLevel = AlarmLevels.LIFE_SAFETY;
else if (criticalTotal > 0)
maxAlarmLevel = AlarmLevels.CRITICAL;
else if (urgentTotal > 0)
maxAlarmLevel = AlarmLevels.URGENT;
else if (warningTotal > 0)
maxAlarmLevel = AlarmLevels.WARNING;
else if (importantTotal > 0)
maxAlarmLevel = AlarmLevels.IMPORTANT;
else if (informationTotal > 0)
maxAlarmLevel = AlarmLevels.INFORMATION;
else if (noneTotal > 0)
maxAlarmLevel = AlarmLevels.NONE;
if (maxAlarmLevel != state.getMaxAlarmLevel()) {
response.put("highestUnsilencedAlarmLevel", maxAlarmLevel);
state.setMaxAlarmLevel(maxAlarmLevel);
}
// Check the max alarm. First check if the events have changed
// since the last time this request checked.
long lastEMUpdate = Common.eventManager.getLastAlarmTimestamp();
// If there is a new alarm then do stuff
if (state.getLastAlarmLevelChange() < lastEMUpdate) {
state.setLastAlarmLevelChange(lastEMUpdate);
} else {
// end no new alarms
// Don't add data for nothing, this will cause tons of
// polls. response.put("alarmsUpdated",false);
}
}
if (pollRequest.isPointDetails() && user != null) {
PointDetailsState newState = DataPointDetailsDwr.getPointData();
PointDetailsState responseState;
PointDetailsState oldState = state.getPointDetailsState();
if (oldState == null)
responseState = newState;
else {
responseState = newState.clone();
responseState.removeEqualValue(oldState);
}
if (!responseState.isEmpty()) {
response.put("pointDetailsState", responseState);
state.setPointDetailsState(newState);
}
}
// TODO This code is used on the legacy alarms page
if (pollRequest.isPendingAlarms() && user != null) {
// Create the list of most current pending alarm content.
Map<String, Object> model = new HashMap<>();
model.put(MODEL_ATTR_EVENTS, eventDao.getPendingEvents(user.getId()));
model.put("pendingEvents", true);
model.put("noContentWhenEmpty", true);
String currentContent = generateContent(httpRequest, "eventList.jsp", model);
currentContent = com.serotonin.util.StringUtils.trimWhitespace(currentContent);
if (!StringUtils.equals(currentContent, state.getPendingAlarmsContent())) {
response.put("newAlarms", true);
response.put("pendingAlarmsContent", currentContent);
state.setPendingAlarmsContent(currentContent);
} else {
response.put("newAlarms", false);
}
}
// Module handlers
for (int i = 0; i < longPollHandlers.size(); i++) {
LongPollHandler handler = longPollHandlers.get(i);
handler.handleLongPoll(data, response, user);
}
if (!response.isEmpty())
break;
synchronized (pollRequest) {
try {
pollRequest.wait(waitTime);
} catch (InterruptedException e) {
// no op
}
}
}
if (pollRequest.isTerminated())
response.put("terminated", true);
return response;
}
use of com.serotonin.m2m2.web.dwr.longPoll.LongPollState in project ma-core-public by infiniteautomation.
the class BaseDwr method resetLastAlarmLevelChange.
protected void resetLastAlarmLevelChange() {
List<LongPollData> data = getLongPollData();
synchronized (data) {
// Check if this user has a current long poll request (very likely)
for (LongPollData lpd : data) {
LongPollState state = lpd.getState();
// Reset the last alarm level change time so that the alarm
// level gets rechecked.
state.setLastAlarmLevelChange(0);
// Notify the long poll thread so that any change
notifyLongPollImpl(lpd.getRequest());
}
}
}
Aggregations