Search in sources :

Example 1 with ConditionParseException

use of com.serotonin.m2m2.rt.event.compound.ConditionParseException in project ma-core-public by infiniteautomation.

the class CompoundEventDetectorVO method validate.

public static void validate(String condition, DwrResponseI18n response) {
    try {
        User user = Common.getUser();
        final boolean admin = Permissions.hasAdmin(user);
        if (!admin)
            Permissions.ensureDataSourcePermission(user);
        LogicalOperator l = CompoundEventDetectorRT.parseConditionStatement(condition);
        List<String> keys = l.getDetectorKeys();
        // Get all of the point event detectors.
        List<DataPointVO> dataPoints = DataPointDao.instance.getDataPoints(null, true);
        // Create a lookup of data sources.
        Map<Integer, DataSourceVO<?>> dss = new HashMap<>();
        for (DataSourceVO<?> ds : DataSourceDao.instance.getAll()) dss.put(ds.getId(), ds);
        for (String key : keys) {
            if (!key.startsWith(SimpleEventDetectorVO.POINT_EVENT_DETECTOR_PREFIX))
                continue;
            boolean found = false;
            for (DataPointVO dp : dataPoints) {
                if (!admin && !Permissions.hasDataSourcePermission(user, dss.get(dp.getDataSourceId())))
                    continue;
                if (found)
                    break;
            }
            if (!found)
                throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.invalidKey"));
        }
    } catch (ConditionParseException e) {
        response.addMessage("condition", e.getLocalizableMessage());
        if (e.isRange()) {
            response.addData("range", true);
            response.addData("from", e.getFrom());
            response.addData("to", e.getTo());
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) LogicalOperator(com.serotonin.m2m2.rt.event.compound.LogicalOperator) ConditionParseException(com.serotonin.m2m2.rt.event.compound.ConditionParseException) LocalizableMessage(com.serotonin.web.i18n.LocalizableMessage)

Example 2 with ConditionParseException

use of com.serotonin.m2m2.rt.event.compound.ConditionParseException in project ma-core-public by infiniteautomation.

the class CompoundEventDetectorRT method initialize.

// 
// 
// /
// / Lifecycle interface
// /
// 
// 
@Override
public void initialize(boolean safe) throws LifecycleException {
    // Validate the condition statement.
    try {
        condition = parseConditionStatement(vo.getCondition());
    } catch (ConditionParseException e) {
        throw new LifecycleException(e);
    }
    try {
        condition.initialize();
    } catch (LocalizableException e) {
        throw new LifecycleException(e);
    }
    condition.initSource(this);
    // Create a convenience reference to the event type.
    eventType = new CompoundDetectorEventType(vo.getId());
    if (!vo.isReturnToNormal())
        eventType.setDuplicateHandling(EventType.DuplicateHandling.ALLOW);
    // Evaluate the current state.
    currentState = condition.evaluate();
    if (currentState)
        raiseEvent(Common.timer.currentTimeMillis());
    else
        returnToNormal(Common.timer.currentTimeMillis());
}
Also used : LifecycleException(com.serotonin.util.LifecycleException) LocalizableException(com.serotonin.web.i18n.LocalizableException) CompoundDetectorEventType(com.serotonin.m2m2.rt.event.type.CompoundDetectorEventType)

Aggregations

ConditionParseException (com.serotonin.m2m2.rt.event.compound.ConditionParseException)1 LogicalOperator (com.serotonin.m2m2.rt.event.compound.LogicalOperator)1 CompoundDetectorEventType (com.serotonin.m2m2.rt.event.type.CompoundDetectorEventType)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 User (com.serotonin.m2m2.vo.User)1 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)1 LifecycleException (com.serotonin.util.LifecycleException)1 LocalizableException (com.serotonin.web.i18n.LocalizableException)1 LocalizableMessage (com.serotonin.web.i18n.LocalizableMessage)1 HashMap (java.util.HashMap)1