Search in sources :

Example 6 with AbstractEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO in project ma-modules-public by infiniteautomation.

the class EventDetectorRestV2Controller method delete.

@ApiOperation(value = "Delete an Event Detector", notes = "")
@RequestMapping(method = RequestMethod.DELETE, value = { "/{xid}" })
public ResponseEntity<AbstractEventDetectorModel<?>> delete(@ApiParam(value = "Valid Event Detector XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
    // Check to see if it already exists
    AbstractEventDetectorVO<?> existing = this.dao.getByXid(xid);
    if (existing == null) {
        throw new NotFoundRestException();
    }
    // Check permission
    DataPointVO dp = DataPointDao.instance.get(existing.getSourceId());
    Permissions.ensureDataSourcePermission(user, dp.getDataSourceId());
    // TODO Fix this when we have other types of detectors
    AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) existing;
    ped.njbSetDataPoint(dp);
    // Remove it from the data point, if it isn't replaced we fail.
    boolean removed = false;
    DataPointDao.instance.setEventDetectors(dp);
    ListIterator<AbstractPointEventDetectorVO<?>> it = dp.getEventDetectors().listIterator();
    while (it.hasNext()) {
        AbstractPointEventDetectorVO<?> ed = it.next();
        if (ed.getId() == ped.getId()) {
            it.remove();
            removed = true;
            break;
        }
    }
    if (!removed)
        throw new ServerErrorException(new TranslatableMessage("rest.error.eventDetectorNotAssignedToThisPoint"));
    // Save the data point
    Common.runtimeManager.saveDataPoint(dp);
    return new ResponseEntity<>(existing.asModel(), HttpStatus.OK);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with AbstractEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO in project ma-core-public by infiniteautomation.

the class DataPointDwr method getEventDetectorOptions.

/**
 * Get a list of available Point Event Detectors for this point
 *
 * @param vo
 * @return
 */
@DwrPermission(user = true)
public ProcessResult getEventDetectorOptions(int dataTypeId) {
    ProcessResult response = new ProcessResult();
    List<EventDetectorDefinition<?>> definitions = ModuleRegistry.getEventDetectorDefinitions();
    List<StringStringPair> list = new ArrayList<StringStringPair>();
    for (EventDetectorDefinition<?> definition : definitions) {
        AbstractEventDetectorVO<?> vo = definition.baseCreateEventDetectorVO();
        if (vo instanceof AbstractPointEventDetectorVO) {
            AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) vo;
            if (ped.supports(dataTypeId)) {
                list.add(new StringStringPair(definition.getDescriptionKey(), definition.getEventDetectorTypeName()));
            }
        }
    }
    response.addData("options", list);
    return response;
}
Also used : StringStringPair(com.serotonin.db.pair.StringStringPair) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) EventDetectorDefinition(com.serotonin.m2m2.module.EventDetectorDefinition) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ArrayList(java.util.ArrayList) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 8 with AbstractEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO in project ma-core-public by infiniteautomation.

the class EventDetectorImporter method importImpl.

@Override
protected void importImpl() {
    String dataPointXid = json.getString("dataPointXid");
    DataPointVO dpvo;
    // Everyone is in the same thread so no synchronization on dataPointMap required.
    if (dataPointMap.containsKey(dataPointXid))
        dpvo = dataPointMap.get(dataPointXid);
    else if (StringUtils.isEmpty(dataPointXid) || (dpvo = DataPointDao.instance.getByXid(dataPointXid)) == null) {
        addFailureMessage("emport.error.missingPoint", dataPointXid);
        return;
    } else {
        dataPointMap.put(dataPointXid, dpvo);
        // We're only going to use this to house event detectors imported in the eventDetectors object.
        dpvo.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>());
    }
    String typeStr = json.getString("type");
    if (typeStr == null)
        addFailureMessage("emport.error.ped.missingAttr", "type");
    EventDetectorDefinition<?> def = ModuleRegistry.getEventDetectorDefinition(typeStr);
    if (def == null) {
        addFailureMessage("emport.error.ped.invalid", "type", typeStr, ModuleRegistry.getEventDetectorDefinitionTypes());
        return;
    }
    JsonArray handlerXids = json.getJsonArray("handlers");
    if (handlerXids != null)
        for (int k = 0; k < handlerXids.size(); k += 1) {
            AbstractEventHandlerVO<?> eh = EventHandlerDao.instance.getByXid(handlerXids.getString(k));
            if (eh == null) {
                addFailureMessage("emport.eventHandler.missing", handlerXids.getString(k));
                return;
            }
        }
    AbstractEventDetectorVO<?> importing = def.baseCreateEventDetectorVO();
    importing.setDefinition(def);
    String xid = json.getString("xid");
    // Create a new one
    importing.setId(Common.NEW_ID);
    importing.setXid(xid);
    AbstractPointEventDetectorVO<?> dped = (AbstractPointEventDetectorVO<?>) importing;
    dped.njbSetDataPoint(dpvo);
    dpvo.getEventDetectors().add(dped);
    try {
        ctx.getReader().readInto(importing, json);
    // try {
    // if(Common.runtimeManager.getState() == RuntimeManager.RUNNING){
    // Common.runtimeManager.saveDataPoint(dpvo);
    // addSuccessMessage(isNew, "emport.eventDetector.prefix", xid);
    // }else{
    // addFailureMessage(new ProcessMessage("Runtime Manager not running point with xid: " + xid + " not saved."));
    // }
    // } catch(LicenseViolatedException e) {
    // addFailureMessage(new ProcessMessage(e.getErrorMessage()));
    // }
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.eventDetector.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.eventDetector.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) JsonArray(com.serotonin.json.type.JsonArray) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ArrayList(java.util.ArrayList) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)

Example 9 with AbstractEventDetectorVO

use of com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO in project ma-core-public by infiniteautomation.

the class DataPointDao method copy.

@Override
public int copy(final int existingId, final String newXid, final String newName) {
    TransactionCallback<Integer> callback = new TransactionCallback<Integer>() {

        @Override
        public Integer doInTransaction(TransactionStatus status) {
            DataPointVO dataPoint = get(existingId);
            // Copy the vo
            DataPointVO copy = dataPoint.copy();
            copy.setId(Common.NEW_ID);
            copy.setXid(generateUniqueXid());
            copy.setName(dataPoint.getName());
            copy.setDeviceName(dataPoint.getDeviceName());
            copy.setDataSourceId(dataPoint.getDataSourceId());
            copy.setEnabled(dataPoint.isEnabled());
            copy.getComments().clear();
            // Copy the event detectors
            List<AbstractEventDetectorVO<?>> existing = EventDetectorDao.instance.getWithSourceId(EventType.EventTypeNames.DATA_POINT, dataPoint.getId());
            List<AbstractPointEventDetectorVO<?>> detectors = new ArrayList<AbstractPointEventDetectorVO<?>>(existing.size());
            for (AbstractEventDetectorVO<?> ed : existing) {
                AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) ed;
                ped.setId(Common.NEW_ID);
                ped.njbSetDataPoint(copy);
            }
            copy.setEventDetectors(detectors);
            Common.runtimeManager.saveDataPoint(copy);
            // Copy permissions.
            return copy.getId();
        }
    };
    return getTransactionTemplate().execute(callback);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TransactionCallback(org.springframework.transaction.support.TransactionCallback) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) AbstractEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus)

Aggregations

AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)7 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)4 ArrayList (java.util.ArrayList)3 ResponseEntity (org.springframework.http.ResponseEntity)3 ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)2 JsonArray (com.serotonin.json.type.JsonArray)2 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 AbstractEventHandlerVO (com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)2 AbstractEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO)2 URI (java.net.URI)2 AlreadyExistsRestException (com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException)1 StringStringPair (com.serotonin.db.pair.StringStringPair)1 JsonException (com.serotonin.json.JsonException)1 JsonObject (com.serotonin.json.type.JsonObject)1 JsonValue (com.serotonin.json.type.JsonValue)1 EventDetectorDao (com.serotonin.m2m2.db.dao.EventDetectorDao)1