Search in sources :

Example 46 with DataType

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

the class MangoJavaScriptService method execute.

/**
 * Reset the result and execute for PointValueTime result
 */
public void execute(CompiledMangoJavaScript script, long runtime, long timestamp, DataType resultDataType) throws ScriptError, ResultTypeException, ScriptPermissionsException {
    try {
        runAs.runAsCallable(script.getPermissionHolder(), () -> {
            execute(script, runtime, timestamp);
            Object ts = script.getEngine().getBindings(ScriptContext.ENGINE_SCOPE).get(MangoJavaScriptService.TIMESTAMP_CONTEXT_KEY);
            long scriptRuntime;
            if (ts != null) {
                // Check the type of the object.
                if (ts instanceof Number) {
                    // Convert to long
                    scriptRuntime = ((Number) ts).longValue();
                } else {
                    scriptRuntime = timestamp;
                }
            } else {
                scriptRuntime = timestamp;
            }
            Object resultObject = script.getResult().getResult();
            DataValue value = coerce(resultObject, resultDataType);
            script.getResult().setResult(new PointValueTime(value, scriptRuntime));
            return null;
        });
    } catch (ScriptException e) {
        throw ScriptError.create(e, script.isWrapInFunction());
    } catch (RuntimeException e) {
        // Nashorn seems to like to wrap exceptions in RuntimeException
        if (e.getCause() instanceof ScriptPermissionsException)
            throw (ScriptPermissionsException) e.getCause();
        else
            throw new ShouldNeverHappenException(e);
    } catch (Exception e) {
        if (e instanceof ResultTypeException) {
            throw (ResultTypeException) e;
        }
        throw new ShouldNeverHappenException(e);
    }
}
Also used : ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) DataPointStateException(com.serotonin.m2m2.rt.script.DataPointStateException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Example 47 with DataType

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

the class MangoJavaScriptService method coerce.

/**
 * Coerce an object into a DataValue
 */
public DataValue coerce(Object input, DataType toDataType) throws ResultTypeException {
    DataValue value;
    if (input instanceof DataValue)
        return (DataValue) input;
    else if (input instanceof PointValueTime)
        return ((PointValueTime) input).getValue();
    if (input == null) {
        if (toDataType == DataType.BINARY)
            value = new BinaryValue(false);
        else if (toDataType == DataType.MULTISTATE)
            value = new MultistateValue(0);
        else if (toDataType == DataType.NUMERIC)
            value = new NumericValue(0);
        else if (toDataType == DataType.ALPHANUMERIC)
            value = new AlphanumericValue("");
        else
            value = null;
    } else if (input instanceof AbstractPointWrapper) {
        value = ((AbstractPointWrapper) input).getValueImpl();
        if ((value != null) && (value.getDataType() != toDataType))
            throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
    } else // See if the type matches.
    if (toDataType == DataType.BINARY && input instanceof Boolean)
        value = new BinaryValue((Boolean) input);
    else if (toDataType == DataType.MULTISTATE) {
        if (input instanceof Number)
            value = new MultistateValue(((Number) input).intValue());
        else if (input instanceof String) {
            try {
                value = new MultistateValue(Integer.parseInt((String) input));
            } catch (NumberFormatException e) {
                throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
            }
        } else
            throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
    } else if (toDataType == DataType.NUMERIC) {
        if (input instanceof Number)
            value = new NumericValue(((Number) input).doubleValue());
        else if (input instanceof String) {
            try {
                value = new NumericValue(Double.parseDouble((String) input));
            } catch (NumberFormatException e) {
                throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
            }
        } else
            throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
    } else if (toDataType == DataType.ALPHANUMERIC)
        value = new AlphanumericValue(input.toString());
    else
        // If not, ditch it.
        throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
    return value;
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AbstractPointWrapper(com.serotonin.m2m2.rt.script.AbstractPointWrapper) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 48 with DataType

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

the class SetPointEventHandlerDefinition method commonValidation.

private void commonValidation(ProcessResult response, SetPointEventHandlerVO vo) {
    DataPointVO dp = DataPointDao.getInstance().get(vo.getTargetPointId());
    DataType dataType = null;
    if (dp == null)
        response.addContextualMessage("targetPointId", "eventHandlers.noTargetPoint");
    else {
        dataType = dp.getPointLocator().getDataType();
        if (!dp.getPointLocator().isSettable())
            response.addContextualMessage("targetPointId", "event.setPoint.targetNotSettable");
    }
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE && vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE) {
        response.addContextualMessage("activeAction", "eventHandlers.noSetPointAction");
        response.addContextualMessage("inactiveAction", "eventHandlers.noSetPointAction");
    }
    MangoJavaScriptService javaScriptService = Common.getBean(MangoJavaScriptService.class);
    // Active
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.MULTISTATE) {
        try {
            Integer.parseInt(vo.getActiveValueToSet());
        } catch (NumberFormatException e) {
            response.addContextualMessage("activeValueToSet", "eventHandlers.invalidActiveValue");
        }
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.NUMERIC) {
        try {
            Double.parseDouble(vo.getActiveValueToSet());
        } catch (NumberFormatException e) {
            response.addContextualMessage("activeValueToSet", "eventHandlers.invalidActiveValue");
        }
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
        DataPointVO dpActive = DataPointDao.getInstance().get(vo.getActivePointId());
        if (dpActive == null)
            response.addContextualMessage("activePointId", "eventHandlers.invalidActiveSource");
        else if (dataType != dpActive.getPointLocator().getDataType())
            response.addContextualMessage("activeDataPointId", "eventHandlers.invalidActiveSourceType");
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
        if (StringUtils.isEmpty(vo.getActiveScript())) {
            response.addContextualMessage("activeScript", "eventHandlers.invalidActiveScript");
        } else {
            try {
                javaScriptService.compile(vo.getActiveScript(), true);
            } catch (ScriptError e) {
                response.addContextualMessage("activeScript", "eventHandlers.invalidActiveScriptError", e.getTranslatableMessage());
            }
        }
    }
    // Inactive
    if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.MULTISTATE) {
        try {
            Integer.parseInt(vo.getInactiveValueToSet());
        } catch (NumberFormatException e) {
            response.addContextualMessage("inactiveValueToSet", "eventHandlers.invalidInactiveValue");
        }
    } else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.NUMERIC) {
        try {
            Double.parseDouble(vo.getInactiveValueToSet());
        } catch (NumberFormatException e) {
            response.addContextualMessage("inactiveValueToSet", "eventHandlers.invalidInactiveValue");
        }
    } else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
        DataPointVO dpInactive = DataPointDao.getInstance().get(vo.getInactivePointId());
        if (dpInactive == null)
            response.addContextualMessage("inactivePointId", "eventHandlers.invalidInactiveSource");
        else if (dataType != dpInactive.getPointLocator().getDataType())
            response.addContextualMessage("inactivePointId", "eventHandlers.invalidInactiveSourceType");
    } else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
        if (StringUtils.isEmpty(vo.getInactiveScript())) {
            response.addContextualMessage("inactiveScript", "eventHandlers.invalidInactiveScript");
        } else {
            try {
                javaScriptService.compile(vo.getInactiveScript(), true);
            } catch (ScriptError e) {
                response.addContextualMessage("inactiveScript", "eventHandlers.invalidActiveScriptError", e.getTranslatableMessage());
            }
        }
    }
    if (vo.getAdditionalContext() != null)
        validateScriptContext(vo.getAdditionalContext(), response);
    else
        vo.setAdditionalContext(new ArrayList<>());
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ScriptError(com.serotonin.m2m2.rt.script.ScriptError) ArrayList(java.util.ArrayList) DataType(com.serotonin.m2m2.DataType) MangoJavaScriptService(com.infiniteautomation.mango.spring.service.MangoJavaScriptService)

Example 49 with DataType

use of com.serotonin.m2m2.DataType in project openj9 by eclipse-openj9.

the class StructurePointer method getStructureFields.

public StructureField[] getStructureFields() {
    List<StructureField> fields = new LinkedList<StructureField>();
    Class<?> working = this.getClass();
    while (working != null) {
        GeneratedPointerClass classAnnotation = working.getAnnotation(GeneratedPointerClass.class);
        if (null == classAnnotation) {
            break;
        }
        for (Method thisMethod : working.getMethods()) {
            if (thisMethod.isAnnotationPresent(GeneratedFieldAccessor.class)) {
                GeneratedFieldAccessor fieldAnnotation = thisMethod.getAnnotation(GeneratedFieldAccessor.class);
                Field offsetField = null;
                try {
                    offsetField = classAnnotation.structureClass().getField(fieldAnnotation.offsetFieldName());
                } catch (SecurityException e) {
                    throw new Error("Unexpected security exception", e);
                } catch (NoSuchFieldException e) {
                    // This will happen if we reach for a field that doesn't exist on this level
                    continue;
                }
                int offset = -1;
                try {
                    offset = offsetField.getInt(null);
                } catch (Exception e) {
                    throw new Error(e);
                }
                DataType result = null;
                CorruptDataException cde = null;
                try {
                    result = (DataType) thisMethod.invoke(this);
                } catch (IllegalArgumentException e) {
                    throw new RuntimeException(e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (InvocationTargetException e) {
                    Throwable cause = e.getCause();
                    if (cause instanceof CorruptDataException) {
                        cde = (CorruptDataException) cause;
                    } else {
                        throw new RuntimeException(e);
                    }
                }
                fields.add(new StructureField(thisMethod.getName(), fieldAnnotation.declaredType(), offset, result, cde));
            }
        }
        working = working.getSuperclass();
    }
    Collections.sort(fields);
    StructureField[] result = new StructureField[fields.size()];
    fields.toArray(result);
    return result;
}
Also used : GeneratedPointerClass(com.ibm.j9ddr.GeneratedPointerClass) Method(java.lang.reflect.Method) CorruptDataException(com.ibm.j9ddr.CorruptDataException) GeneratedFieldAccessor(com.ibm.j9ddr.GeneratedFieldAccessor) LinkedList(java.util.LinkedList) CorruptDataException(com.ibm.j9ddr.CorruptDataException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) DataType(com.ibm.j9ddr.vm29.j9.DataType)

Example 50 with DataType

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

the class SetPointEventHandlerVO method validate.

public void validate(ProcessResult response) {
    super.validate(response);
    DataPointVO dp = DataPointDao.instance.getDataPoint(targetPointId, false);
    if (dp == null)
        response.addGenericMessage("eventHandlers.noTargetPoint");
    else {
        int dataType = dp.getPointLocator().getDataTypeId();
        if (activeAction == SET_ACTION_NONE && inactiveAction == SET_ACTION_NONE)
            response.addGenericMessage("eventHandlers.noSetPointAction");
        // Active
        if (activeAction == SET_ACTION_STATIC_VALUE && dataType == DataTypes.MULTISTATE) {
            try {
                Integer.parseInt(activeValueToSet);
            } catch (NumberFormatException e) {
                response.addGenericMessage("eventHandlers.invalidActiveValue");
            }
        } else if (activeAction == SET_ACTION_STATIC_VALUE && dataType == DataTypes.NUMERIC) {
            try {
                Double.parseDouble(activeValueToSet);
            } catch (NumberFormatException e) {
                response.addGenericMessage("eventHandlers.invalidActiveValue");
            }
        } else if (activeAction == SET_ACTION_POINT_VALUE) {
            DataPointVO dpActive = DataPointDao.instance.getDataPoint(activePointId, false);
            if (dpActive == null)
                response.addGenericMessage("eventHandlers.invalidActiveSource");
            else if (dataType != dpActive.getPointLocator().getDataTypeId())
                response.addGenericMessage("eventHandlers.invalidActiveSourceType");
        } else if (activeAction == SET_ACTION_SCRIPT_VALUE) {
            if (StringUtils.isEmpty(activeScript))
                response.addGenericMessage("eventHandlers.invalidActiveScript");
            try {
                CompiledScriptExecutor.compile(activeScript);
            } catch (ScriptException e) {
                response.addGenericMessage("eventHandlers.invalidActiveScriptError", e.getMessage() == null ? e.getCause().getMessage() : e.getMessage());
            }
        }
        // Inactive
        if (inactiveAction == SET_ACTION_STATIC_VALUE && dataType == DataTypes.MULTISTATE) {
            try {
                Integer.parseInt(inactiveValueToSet);
            } catch (NumberFormatException e) {
                response.addGenericMessage("eventHandlers.invalidInactiveValue");
            }
        } else if (inactiveAction == SET_ACTION_STATIC_VALUE && dataType == DataTypes.NUMERIC) {
            try {
                Double.parseDouble(inactiveValueToSet);
            } catch (NumberFormatException e) {
                response.addGenericMessage("eventHandlers.invalidInactiveValue");
            }
        } else if (inactiveAction == SET_ACTION_POINT_VALUE) {
            DataPointVO dpInactive = DataPointDao.instance.getDataPoint(inactivePointId, false);
            if (dpInactive == null)
                response.addGenericMessage("eventHandlers.invalidInactiveSource");
            else if (dataType != dpInactive.getPointLocator().getDataTypeId())
                response.addGenericMessage("eventHandlers.invalidInactiveSourceType");
        } else if (inactiveAction == SET_ACTION_SCRIPT_VALUE) {
            if (StringUtils.isEmpty(inactiveScript))
                response.addGenericMessage("eventHandlers.invalidInactiveScript");
            try {
                CompiledScriptExecutor.compile(inactiveScript);
            } catch (ScriptException e) {
                response.addGenericMessage("eventHandlers.invalidInactiveScriptError", e.getMessage() == null ? e.getCause().getMessage() : e.getMessage());
            }
        }
        List<String> varNameSpace = new ArrayList<String>();
        varNameSpace.add(TARGET_CONTEXT_KEY);
        for (IntStringPair cxt : additionalContext) {
            if (DataPointDao.instance.getDataPoint(cxt.getKey(), false) == null)
                response.addGenericMessage("event.script.contextPointMissing", cxt.getKey(), cxt.getValue());
            String varName = cxt.getValue();
            if (StringUtils.isBlank(varName)) {
                response.addGenericMessage("validate.allVarNames");
                break;
            }
            if (!VarNames.validateVarName(varName)) {
                response.addGenericMessage("validate.invalidVarName", varName);
                break;
            }
            if (varNameSpace.contains(varName)) {
                response.addGenericMessage("validate.duplicateVarName", varName);
                break;
            }
            varNameSpace.add(varName);
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ScriptException(javax.script.ScriptException) IntStringPair(com.serotonin.db.pair.IntStringPair) ArrayList(java.util.ArrayList)

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