Skip to content

Commit

Permalink
Merge pull request #2935 from SCADA-LTS/feature/#2696_Generation_of_a…
Browse files Browse the repository at this point in the history
…ctive_events_by_handlers

#2696 Generation of active events by handlers:
  • Loading branch information
Limraj authored Jul 5, 2024
2 parents d7c366f + 0ad1095 commit c0c7cad
Show file tree
Hide file tree
Showing 30 changed files with 376 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
import org.apache.commons.logging.LogFactory;

import com.serotonin.mango.Common;
import com.serotonin.mango.db.dao.DataPointDao;
import com.serotonin.mango.rt.RuntimeManager;
import com.serotonin.mango.rt.dataImage.types.MangoValue;
import com.serotonin.mango.vo.permission.Permissions;
import org.mozilla.javascript.NativeObject;
import org.scada_lts.mango.service.DataPointService;

public class DPCommandsScriptContextObject extends ScriptContextObject {
public static final Type TYPE = Type.DATAPOINT_COMMANDS;
private Log LOG = LogFactory.getLog(DPCommandsScriptContextObject.class);
private static final Log LOG = LogFactory.getLog(DPCommandsScriptContextObject.class);

@Override
public Type getType() {
return TYPE;
}

public void writeDataPoint(String xid, String stringValue) {
DataPointVO dataPoint = new DataPointDao().getDataPoint(xid);
DataPointVO dataPoint = new DataPointService().getDataPoint(xid);
if (dataPoint != null) {
Permissions.ensureDataPointSetPermission(user, dataPoint);
RuntimeManager runtimeManager = Common.ctx.getRuntimeManager();
Expand All @@ -39,27 +39,33 @@ public void writeDataPoint(String xid, String stringValue) {
} catch (Exception e) {
LOG.debug("Error while setting point - " + e.getMessage());
}
} else {
logWarn(xid);
}
}

public void enableDataPoint(String xid) {
DataPointVO dataPoint = new DataPointDao().getDataPoint(xid);
DataPointVO dataPoint = new DataPointService().getDataPoint(xid);
if (dataPoint != null) {
Permissions.ensureDataPointReadPermission(user, dataPoint);
RuntimeManager runtimeManager = Common.ctx.getRuntimeManager();
dataPoint.setEnabled(true);
runtimeManager.saveDataPoint(dataPoint);
} else {
logWarn(xid);
}

}

public void disableDataPoint(String xid) {
DataPointVO dataPoint = new DataPointDao().getDataPoint(xid);
DataPointVO dataPoint = new DataPointService().getDataPoint(xid);
if (dataPoint != null) {
Permissions.ensureDataPointReadPermission(user, dataPoint);
RuntimeManager runtimeManager = Common.ctx.getRuntimeManager();
dataPoint.setEnabled(false);
runtimeManager.saveDataPoint(dataPoint);
} else {
logWarn(xid);
}

}
Expand Down Expand Up @@ -137,12 +143,18 @@ public void setDefaultCacheSize(String xid, int defaultCacheSize) {
}

private void updateDataPoint(String xid, DataPointUpdate dataPointUpdate) {
DataPointVO dataPoint = new DataPointDao().getDataPoint(xid);
DataPointVO dataPoint = new DataPointService().getDataPoint(xid);
if (dataPoint != null) {
Permissions.ensureDataPointUpdatePermission(user, dataPoint);
RuntimeManager runtimeManager = Common.ctx.getRuntimeManager();
dataPointUpdate.updateDataPoint(dataPoint);
runtimeManager.saveDataPoint(dataPoint);
} else {
logWarn(xid);
}
}

private static void logWarn(String xid) {
LOG.warn("Not exist data point with xid: " + xid);
}
}
2 changes: 1 addition & 1 deletion src/com/serotonin/mango/MangoContextListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ private void initialized(ServletContextEvent evt) {
new SystemSettingsDAO().setValue(
SystemSettingsDAO.SERVLET_CONTEXT_PATH, ctx.getContextPath());

utilitiesInitialize(ctx);
eventManagerInitialize(ctx);
utilitiesInitialize(ctx);

try {
ApplicationBeans.getPointEventDetectorDaoBean().init();
Expand Down
41 changes: 13 additions & 28 deletions src/com/serotonin/mango/rt/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void raiseEvent(EventType type, long time, boolean rtnApplicable,
time,
false,
getAlarmLevelChangeMessage(
"event.alarmMaxIncreased", oldValue));
"event.alarmMaxIncreased", oldValue, alarmLevel));
}
}

Expand Down Expand Up @@ -234,11 +234,6 @@ private void deactivateEvent(EventInstance evt, long time, int inactiveCause) {
// Call inactiveEvent handlers.
handleInactiveEvent(evt);
}

@Deprecated
public long getLastAlarmTimestamp() {
return lastAlarmTimestamp;
}

public void setLastAlarmTimestamp(long alarmTimestamp) {
this.lastAlarmTimestamp = alarmTimestamp;
Expand Down Expand Up @@ -273,6 +268,14 @@ public void cancelEventsForPublisher(int publisherId) {
}
}

public void cancelEventsForHandler(int handlerId) {
for (EventInstance e : activeEvents) {
if (e.getEventType().getEventHandlerId() == handlerId)
deactivateEvent(e, System.currentTimeMillis(),
EventInstance.RtnCauses.SOURCE_DISABLED);
}
}

private void resetHighestAlarmLevel(long time, boolean init) {
int max = 0;
for (EventInstance e : activeEvents) {
Expand All @@ -290,7 +293,7 @@ private void resetHighestAlarmLevel(long time, boolean init) {
time,
false,
getAlarmLevelChangeMessage("event.alarmMaxIncreased",
oldValue));
oldValue, max));
} else if (max < highestActiveAlarmLevel) {
int oldValue = highestActiveAlarmLevel;
highestActiveAlarmLevel = max;
Expand All @@ -300,16 +303,16 @@ private void resetHighestAlarmLevel(long time, boolean init) {
time,
false,
getAlarmLevelChangeMessage("event.alarmMaxDecreased",
oldValue));
oldValue, max));
}
}
}

private LocalizableMessage getAlarmLevelChangeMessage(String key,
int oldValue) {
int oldValue, int newValue) {
return new LocalizableMessage(key,
AlarmLevels.getAlarmLevelMessage(oldValue),
AlarmLevels.getAlarmLevelMessage(highestActiveAlarmLevel));
AlarmLevels.getAlarmLevelMessage(newValue));
}

//
Expand Down Expand Up @@ -398,11 +401,6 @@ private void setHandlers(EventInstance evt) {
evt.setHandlers(rts);
}

@Deprecated
public void handleRaiseEvent(EventInstance evt) {
handleRaiseEvent(evt, Collections.emptySet());
}

private void handleRaiseEvent(EventInstance evt,
Set<String> defaultAddresses) {
if (evt.getHandlers() != null) {
Expand Down Expand Up @@ -500,14 +498,6 @@ public void notifyEventToggle(EventInstance evt, User user) {
NotifyEventUtils.notifyEventToggle(highestAlarmLevelService, evt, user, userEventServiceWebSocket);
}

@Deprecated
public void notifyEventToggle(int eventId, User user) {
if(eventId != Common.NEW_ID) {
EventInstance evt = eventService.getEvent(eventId);
notifyEventToggle(evt, user);
}
}

public void notifyEventToggle(int eventId, int userId) {
if(eventId != Common.NEW_ID) {
EventInstance evt = eventService.getEvent(eventId);
Expand All @@ -520,11 +510,6 @@ public void notifyEventUpdate(User user, WsEventMessage message) {
NotifyEventUtils.notifyEventUpdate(user, message, userEventServiceWebSocket);
}

@Deprecated
public void notifyEventReset() {
NotifyEventUtils.notifyEventReset(userService, userEventServiceWebSocket);
}

@Override
public void notifyWebSocketSubscribers(String message) {
eventsServiceWebSocket.notifyEventsSubscribers(message);
Expand Down
34 changes: 22 additions & 12 deletions src/com/serotonin/mango/rt/event/handlers/EmailHandlerRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import com.serotonin.mango.Common;
import com.serotonin.mango.db.dao.MailingListDao;
import com.serotonin.mango.rt.event.EventInstance;
import com.serotonin.mango.rt.event.type.SystemEventType;
import com.serotonin.mango.rt.maint.work.AfterWork;
import com.serotonin.mango.util.MsgContentUtils;
import com.serotonin.mango.util.timeout.ModelTimeoutClient;
import com.serotonin.mango.util.timeout.ModelTimeoutTask;
import com.serotonin.mango.vo.event.EventHandlerVO;
import com.serotonin.mango.web.email.IMsgSubjectContent;
import com.serotonin.timer.TimerTask;
import com.serotonin.web.i18n.LocalizableMessage;
import freemarker.template.TemplateException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -87,21 +89,20 @@ public IMsgSubjectContent createContent(EventInstance evt, String alias) throws
private Set<String> inactiveRecipients;

public EmailHandlerRT(EventHandlerVO vo) {
this.vo = vo;
this.service = ScheduledExecuteInactiveEventService.getInstance();
this.mailingListService = new MailingListService();
this(vo, SystemEventType.duplicateIgnoreEventType(SystemEventType.TYPE_EMAIL_SEND_FAILURE, vo.getId()));
}

public EmailHandlerRT(EventHandlerVO vo, MailingListService mailingListService) {
this.vo = vo;
protected EmailHandlerRT(EventHandlerVO vo, SystemEventType systemEventType) {
super(vo, systemEventType);
this.service = ScheduledExecuteInactiveEventService.getInstance();
this.mailingListService = mailingListService;
this.mailingListService = new MailingListService();
}

public EmailHandlerRT(EventHandlerVO vo,
ScheduledExecuteInactiveEventService service,
MailingListService mailingListService) {
this.vo = vo;
protected EmailHandlerRT(EventHandlerVO vo,
ScheduledExecuteInactiveEventService service,
MailingListService mailingListService,
SystemEventType systemEventType) {
super(vo, systemEventType);
this.service = service;
this.mailingListService = mailingListService;
}
Expand Down Expand Up @@ -200,8 +201,17 @@ private void sendEmail(EventInstance evt, NotificationType notificationType, Set
sendMsg(evt, notificationType, addresses, vo.getAlias(), new AfterWork() {
@Override
public void workFail(Throwable exception) {
LOG.error("Failed sending email for " + eventHandlerInfo(getVo()) + ", " + eventInfo(evt)
+ ", error: " + exception.getMessage());
String msg = "Failed sending email for " + eventHandlerInfo(getVo()) + ", " + eventInfo(evt)
+ ", error: " + exception.getMessage();
LOG.error(msg);
LocalizableMessage message = new LocalizableMessage("event.email.failure",
vo.getAlias(), addresses, msg);
SystemEventType.raiseEvent(getEventType(), System.currentTimeMillis(), true, message);
}

@Override
public void workSuccess() {
SystemEventType.returnToNormal(getEventType(), System.currentTimeMillis());
}
}, () -> eventHandlerInfo(getVo()) + ", " + eventInfo(evt));
}
Expand Down
21 changes: 16 additions & 5 deletions src/com/serotonin/mango/rt/event/handlers/EmailToSmsHandlerRT.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.serotonin.mango.rt.event.handlers;

import com.serotonin.mango.rt.event.EventInstance;
import com.serotonin.mango.rt.event.type.SystemEventType;
import com.serotonin.mango.rt.maint.work.AfterWork;
import com.serotonin.mango.util.MsgContentUtils;
import com.serotonin.mango.vo.event.EventHandlerVO;
import com.serotonin.mango.web.email.IMsgSubjectContent;
import com.serotonin.web.i18n.LocalizableMessage;
import freemarker.template.TemplateException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -57,16 +59,15 @@ public IMsgSubjectContent createContent(EventInstance evt, String alias) throws
private final SystemSettingsService systemSettingsService;
private final MailingListService mailingListService;

@Deprecated
public EmailToSmsHandlerRT(EventHandlerVO vo) {
super(vo);
super(vo, SystemEventType.duplicateIgnoreEventType(SystemEventType.TYPE_SMS_SEND_FAILURE, vo.getId()));
this.systemSettingsService = new SystemSettingsService();
this.mailingListService = new MailingListService();
}

public EmailToSmsHandlerRT(EventHandlerVO vo, ScheduledExecuteInactiveEventService service,
MailingListService mailingListService, SystemSettingsService systemSettingsService) {
super(vo, service, mailingListService);
super(vo, service, mailingListService, SystemEventType.duplicateIgnoreEventType(SystemEventType.TYPE_SMS_SEND_FAILURE, vo.getId()));
this.systemSettingsService = systemSettingsService;
this.mailingListService = mailingListService;
}
Expand All @@ -90,9 +91,19 @@ protected void sendEmail(EventInstance evt, Set<String> addresses) {
sendMsg(evt, SmsNotificationType.MSG_FROM_EVENT, addresses, vo.getAlias(), new AfterWork() {
@Override
public void workFail(Throwable exception) {
LOG.error("Failed sending sms for " + eventHandlerInfo(getVo()) + ", " + eventInfo(evt)
+ ", error: " + exception.getMessage());
String msg = "Failed sending sms for " + eventHandlerInfo(getVo()) + ", " + eventInfo(evt)
+ ", error: " + exception.getMessage();
LOG.error(msg);
LocalizableMessage message = new LocalizableMessage("event.sms.failure",
vo.getAlias(), addresses, msg);
SystemEventType.raiseEvent(getEventType(), System.currentTimeMillis(), true, message);
}

@Override
public void workSuccess() {
SystemEventType.returnToNormal(getEventType(), System.currentTimeMillis());
}

}, () -> eventHandlerInfo(getVo()) + ", " + eventInfo(evt));
}

Expand Down
26 changes: 23 additions & 3 deletions src/com/serotonin/mango/rt/event/handlers/EventHandlerRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,47 @@
*/
package com.serotonin.mango.rt.event.handlers;

import com.serotonin.mango.Common;
import com.serotonin.mango.rt.EventManager;
import com.serotonin.mango.rt.event.EventInstance;
import com.serotonin.mango.rt.event.type.EventType;
import com.serotonin.mango.vo.event.EventHandlerVO;

abstract public class EventHandlerRT {
public abstract class EventHandlerRT {
protected EventHandlerVO vo;

private final EventType eventType;

protected EventHandlerRT(EventHandlerVO vo, EventType eventType) {
this.vo = vo;
this.eventType = eventType;
}

protected EventHandlerVO getVo() {
return vo;
}

protected EventType getEventType() {
return eventType;
}

/**
* Not all events that are raised are made active. It depends on the event's alarm level and duplicate handling.
*
* @see EventManager.raiseEvent for details.
* @param evt
*/
abstract public void eventRaised(EventInstance evt);
public abstract void eventRaised(EventInstance evt);

/**
* Called when the event is considered inactive.
*
* @see EventManager.raiseEvent for details.
* @param evt
*/
abstract public void eventInactive(EventInstance evt);
public abstract void eventInactive(EventInstance evt);

public int getId() {
return getVo() == null ? Common.NEW_ID : getVo().getId();
}
}
Loading

0 comments on commit c0c7cad

Please sign in to comment.