Search in sources :

Example 41 with DataType

use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.

the class PointValueTimeSerializer method serialize.

@Override
public void serialize(PointValueTime pointValueTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    jsonGenerator.writeStartObject();
    DataValue value = pointValueTime.getValue();
    switch(value.getDataType()) {
        case ALPHANUMERIC:
            jsonGenerator.writeStringField("dataType", "ALPHANUMERIC");
            jsonGenerator.writeStringField("value", value.getStringValue());
            break;
        case BINARY:
            jsonGenerator.writeStringField("dataType", "BINARY");
            jsonGenerator.writeBooleanField("value", value.getBooleanValue());
            break;
        case MULTISTATE:
            jsonGenerator.writeStringField("dataType", "MULTISTATE");
            jsonGenerator.writeNumberField("value", value.getIntegerValue());
            break;
        case NUMERIC:
            jsonGenerator.writeStringField("dataType", "NUMERIC");
            jsonGenerator.writeNumberField("value", value.getDoubleValue());
            break;
    }
    jsonGenerator.writeNumberField("timestamp", pointValueTime.getTime());
    if (pointValueTime instanceof IAnnotated) {
        jsonGenerator.writeStringField("serializedAnnotation", ((IAnnotated) pointValueTime).getSourceMessage().serialize());
    }
    jsonGenerator.writeEndObject();
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) IAnnotated(com.serotonin.m2m2.rt.dataImage.IAnnotated)

Example 42 with DataType

use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.

the class PointEventDetectorVO method getImplementations.

public static List<ImplDefinition> getImplementations(DataType dataType) {
    initializeDefinitions();
    List<ImplDefinition> impls = new ArrayList<>();
    for (ImplDefinition def : definitions) {
        if (def.supports(dataType))
            impls.add(def);
    }
    return impls;
}
Also used : ImplDefinition(com.serotonin.m2m2.view.ImplDefinition) ArrayList(java.util.ArrayList)

Example 43 with DataType

use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.

the class AbstractPointLocatorVO method readDataType.

protected DataType readDataType(JsonObject json) throws JsonException {
    String text = json.getString("dataType");
    if (text == null) {
        throw new TranslatableJsonException("emport.error.missing", "dataType", DataType.formatNames());
    }
    DataType dataType;
    try {
        dataType = DataType.valueOf(text);
    } catch (IllegalArgumentException e) {
        throw new TranslatableJsonException("emport.error.invalid", "dataType", text, DataType.formatNames());
    }
    return dataType;
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) DataType(com.serotonin.m2m2.DataType)

Example 44 with DataType

use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.

the class DataPointService method validate.

@Override
public ProcessResult validate(DataPointVO existing, DataPointVO vo) {
    ProcessResult result = commonValidation(vo);
    // Don't allow moving to new data source
    if (existing.getDataSourceId() != vo.getDataSourceId()) {
        result.addContextualMessage("dataSourceId", "validate.dataPoint.pointChangeDataSource");
    }
    DataSourceVO dsvo = dataSourceDao.get(vo.getDataSourceId());
    if (dsvo == null) {
        result.addContextualMessage("dataSourceId", "validate.invalidValue");
        return result;
    }
    // Validate pl if there is one
    if (vo.getPointLocator() != null) {
        DataSourceDefinition<? extends DataSourceVO> def = ModuleRegistry.getDataSourceDefinition(vo.getPointLocator().getDataSourceType());
        if (def == null) {
            throw new ShouldNeverHappenException("No data source definition for type " + vo.getPointLocator().getDataSourceType());
        } else {
            def.validate(result, existing, vo, dsvo);
            // Validate the point locator
            if (existing.getPointLocator().getDataType() != vo.getPointLocator().getDataType()) {
                result.addContextualMessage("dataType", "validate.cantChangeDataType");
            } else if (vo.getPointLocator().getDataType() == null && !result.hasContextualMessage("dataType")) {
                result.addContextualMessage("dataType", "validate.invalidValueWithAcceptable", DataType.formatNames());
            }
        }
    }
    // Validate permissions
    PermissionHolder user = Common.getUser();
    permissionService.validatePermission(result, "readPermission", user, existing.getReadPermission(), vo.getReadPermission());
    permissionService.validatePermission(result, "editPermission", user, existing.getEditPermission(), vo.getEditPermission());
    permissionService.validatePermission(result, "setPermission", user, existing.getSetPermission(), vo.getSetPermission(), false);
    return result;
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 45 with DataType

use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.

the class SetPointHandlerRT method eventInactive.

@Override
public void eventInactive(EventInstance evt) {
    if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE)
        return;
    // Validate that the target point is available.
    DataPointRT targetPoint = Common.runtimeManager.getDataPoint(vo.getTargetPointId());
    if (targetPoint == null) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetPointMissing"), evt.getEventType());
        return;
    }
    if (!targetPoint.getPointLocator().isSettable()) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetNotSettable"), evt.getEventType());
        return;
    }
    DataType targetDataType = targetPoint.getVO().getPointLocator().getDataType();
    DataValue value = null;
    if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
        // Get the source data point.
        DataPointRT sourcePoint = Common.runtimeManager.getDataPoint(vo.getInactivePointId());
        if (sourcePoint == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.inactivePointMissing"), evt.getEventType());
            return;
        }
        PointValueTime valueTime = sourcePoint.getPointValue();
        if (valueTime == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.inactivePointValue"), evt.getEventType());
            return;
        }
        if (valueTime.getValue().getDataType() != targetDataType) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.inactivePointDataType"), evt.getEventType());
            return;
        }
        value = valueTime.getValue();
    } else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE)
        value = DataValue.stringToValue(vo.getInactiveValueToSet(), targetDataType);
    else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
        ArrayList<JsonImportExclusion> importExclusions = new ArrayList<JsonImportExclusion>();
        importExclusions.add(new JsonImportExclusion("xid", vo.getXid()) {

            @Override
            public String getImporterType() {
                return ConfigurationExportData.EVENT_HANDLERS;
            }
        });
        Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
        context.put("target", targetPoint);
        Map<String, Object> additionalContext = new HashMap<String, Object>();
        additionalContext.put(EventInstance.CONTEXT_KEY, evt);
        additionalContext.put(EventInstanceWrapper.CONTEXT_KEY, new EventInstanceWrapper(evt));
        try (ScriptLog scriptLog = new ScriptLog("setPointHandler-" + evt.getId())) {
            for (IntStringPair cxt : vo.getAdditionalContext()) {
                DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
                if (dprt != null)
                    context.put(cxt.getValue(), dprt);
            }
            CompiledMangoJavaScript inactiveScript = new CompiledMangoJavaScript(new SetCallback(vo.getScriptRoles()), scriptLog, additionalContext, null, importExclusions, false, service, vo.getScriptRoles());
            inactiveScript.compile(vo.getInactiveScript(), true);
            inactiveScript.initialize(context);
            MangoJavaScriptResult result = inactiveScript.execute(Common.timer.currentTimeMillis(), evt.getRtnTimestamp(), targetPoint.getDataType());
            PointValueTime pvt = (PointValueTime) result.getResult();
            if (pvt != null)
                value = pvt.getValue();
        } catch (ScriptPermissionsException e) {
            raiseFailureEvent(e.getTranslatableMessage(), evt.getEventType());
            return;
        } catch (ScriptError e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidInactiveScriptError", e.getTranslatableMessage()), evt.getEventType());
            return;
        } catch (ResultTypeException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidInactiveScriptError", e.getMessage()), evt.getEventType());
            return;
        }
    } else
        throw new ShouldNeverHappenException("Unknown active action: " + vo.getInactiveAction());
    if (MangoJavaScriptService.UNCHANGED != value)
        Common.backgroundProcessing.addWorkItem(new SetPointWorkItem(vo.getTargetPointId(), new PointValueTime(value, evt.getRtnTimestamp()), this));
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SetPointWorkItem(com.serotonin.m2m2.rt.maint.work.SetPointWorkItem) JsonImportExclusion(com.serotonin.m2m2.rt.script.JsonImportExclusion) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptError(com.serotonin.m2m2.rt.script.ScriptError) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) DataType(com.serotonin.m2m2.DataType) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) IntStringPair(com.serotonin.db.pair.IntStringPair) MangoJavaScriptResult(com.infiniteautomation.mango.util.script.MangoJavaScriptResult) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CompiledMangoJavaScript(com.infiniteautomation.mango.util.script.CompiledMangoJavaScript) EventInstanceWrapper(com.serotonin.m2m2.rt.script.EventInstanceWrapper)

Aggregations

ArrayList (java.util.ArrayList)26 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)23 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)17 DataType (com.serotonin.m2m2.DataType)14 HashMap (java.util.HashMap)14 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)13 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)11 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)10 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)10 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)10 DataType (org.osate.aadl2.DataType)10 DataType (ucar.ma2.DataType)10 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)9 IOException (java.io.IOException)9 DataImplementation (org.osate.aadl2.DataImplementation)9 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)8 File (java.io.File)7 ScriptError (com.serotonin.m2m2.rt.script.ScriptError)6 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)6