Search in sources :

Example 31 with DataValue

use of com.serotonin.m2m2.rt.dataImage.types.DataValue in project ma-core-public by infiniteautomation.

the class ValueChangeCounterTest method main.

public static void main(String[] args) {
    AlphanumericValue startValue = new AlphanumericValue("asdf");
    List<PointValueTime> values = new ArrayList<PointValueTime>();
    values.add(new PointValueTime("asdf", 2000));
    values.add(new PointValueTime("zxcv", 3000));
    values.add(new PointValueTime("qwer", 4000));
    values.add(new PointValueTime("wert", 5000));
    values.add(new PointValueTime("wert", 6000));
    values.add(new PointValueTime("erty", 8000));
    validate(new ValueChangeCounter(0, 10000, startValue, values), 6, 4);
    validate(new ValueChangeCounter(0, 10000, startValue, values), 6, 4);
    validate(new ValueChangeCounter(0, 10000, (DataValue) null, values), 6, 5);
    validate(new ValueChangeCounter(0, 10000, (DataValue) null, values), 6, 5);
    validate(new ValueChangeCounter(0, 10000, (DataValue) null, new ArrayList<PointValueTime>()), 0, 0);
    validate(new ValueChangeCounter(0, 10000, startValue, new ArrayList<PointValueTime>()), 0, 0);
}
Also used : AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList)

Example 32 with DataValue

use of com.serotonin.m2m2.rt.dataImage.types.DataValue in project ma-core-public by infiniteautomation.

the class SetPointHandlerRT method eventRaised.

@Override
public void eventRaised(EventInstance evt) {
    if (vo.getActiveAction() == 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;
    }
    int targetDataType = targetPoint.getVO().getPointLocator().getDataTypeId();
    DataValue value;
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
        // Get the source data point.
        DataPointRT sourcePoint = Common.runtimeManager.getDataPoint(vo.getActivePointId());
        if (sourcePoint == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointMissing"), evt.getEventType());
            return;
        }
        PointValueTime valueTime = sourcePoint.getPointValue();
        if (valueTime == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointValue"), evt.getEventType());
            return;
        }
        if (DataTypes.getDataType(valueTime.getValue()) != targetDataType) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointDataType"), evt.getEventType());
            return;
        }
        value = valueTime.getValue();
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE) {
        value = DataValue.stringToValue(vo.getActiveValueToSet(), targetDataType);
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
        if (activeScript == null) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScript"), evt.getEventType());
            return;
        }
        Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
        context.put(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, targetPoint);
        Map<String, Object> additionalContext = new HashMap<String, Object>();
        additionalContext.put(SetPointEventHandlerVO.EVENT_CONTEXT_KEY, new EventInstanceWrapper(evt));
        try {
            for (IntStringPair cxt : vo.getAdditionalContext()) {
                DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
                if (dprt != null)
                    context.put(cxt.getValue(), dprt);
            }
            PointValueTime pvt = CompiledScriptExecutor.execute(activeScript, context, additionalContext, evt.getActiveTimestamp(), targetPoint.getDataTypeId(), evt.getActiveTimestamp(), vo.getScriptPermissions(), NULL_WRITER, new ScriptLog(NULL_WRITER, LogLevel.FATAL), setCallback, importExclusions, false);
            value = pvt.getValue();
        } catch (ScriptPermissionsException e) {
            raiseFailureEvent(e.getTranslatableMessage(), evt.getEventType());
            return;
        } catch (ScriptException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getCause().getMessage()), evt.getEventType());
            return;
        } catch (ResultTypeException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getMessage()), evt.getEventType());
            return;
        }
    } else
        throw new ShouldNeverHappenException("Unknown active action: " + vo.getActiveAction());
    // Queue a work item to perform the set point.
    if (CompiledScriptExecutor.UNCHANGED != value)
        Common.backgroundProcessing.addWorkItem(new SetPointWorkItem(vo.getTargetPointId(), new PointValueTime(value, evt.getActiveTimestamp()), this));
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) HashMap(java.util.HashMap) IntStringPair(com.serotonin.db.pair.IntStringPair) SetPointWorkItem(com.serotonin.m2m2.rt.maint.work.SetPointWorkItem) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EventInstanceWrapper(com.serotonin.m2m2.rt.script.EventInstanceWrapper)

Example 33 with DataValue

use of com.serotonin.m2m2.rt.dataImage.types.DataValue in project ma-core-public by infiniteautomation.

the class ScriptExecutor method getResult.

/**
 * Common method to extract the result
 * @param engine
 * @param result
 * @param dataTypeId
 * @param timestamp
 * @return
 * @throws ResultTypeException
 */
protected static PointValueTime getResult(ScriptEngine engine, Object result, int dataTypeId, long timestamp) throws ResultTypeException {
    // Check if a timestamp was set
    Object ts = engine.getBindings(ScriptContext.ENGINE_SCOPE).get(ScriptUtils.TIMESTAMP_CONTEXT_KEY);
    if (ts != null) {
        // Check the type of the object.
        if (ts instanceof Number)
            // Convert to long
            timestamp = ((Number) ts).longValue();
    // else if (ts instanceof ScriptableObject && "Date".equals(((ScriptableObject)ts).getClassName())) {
    // // A native date
    // // It turns out to be a crazy hack to try and get the value from a native date, and the Rhino source
    // // code FTP server is not responding, so, going to have to leave this for now.
    // }
    }
    DataValue value = ScriptUtils.coerce(result, dataTypeId);
    return new PointValueTime(value, timestamp);
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 34 with DataValue

use of com.serotonin.m2m2.rt.dataImage.types.DataValue in project ma-core-public by infiniteautomation.

the class ScriptUtils method coerce.

/**
 * Coerce an object into a DataValue
 * @param input
 * @param toDataTypeId
 * @return
 * @throws ResultTypeException
 */
public static DataValue coerce(Object input, int toDataTypeId) throws ResultTypeException {
    DataValue value;
    if (input instanceof DataValue)
        return (DataValue) input;
    if (input == null) {
        if (toDataTypeId == DataTypes.BINARY)
            value = new BinaryValue(false);
        else if (toDataTypeId == DataTypes.MULTISTATE)
            value = new MultistateValue(0);
        else if (toDataTypeId == DataTypes.NUMERIC)
            value = new NumericValue(0);
        else if (toDataTypeId == DataTypes.ALPHANUMERIC)
            value = new AlphanumericValue("");
        else
            value = null;
    } else if (input instanceof AbstractPointWrapper) {
        value = ((AbstractPointWrapper) input).getValueImpl();
        if ((value != null) && (value.getDataType() != toDataTypeId))
            return throwResultTypeException(value, toDataTypeId);
    } else // See if the type matches.
    if (toDataTypeId == DataTypes.BINARY && input instanceof Boolean)
        value = new BinaryValue((Boolean) input);
    else if (toDataTypeId == DataTypes.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) {
                return throwResultTypeException(input, toDataTypeId);
            }
        } else
            return throwResultTypeException(input, toDataTypeId);
    } else if (toDataTypeId == DataTypes.NUMERIC) {
        if (input instanceof Number)
            value = new NumericValue(((Number) input).doubleValue());
        else if (input instanceof NumericValue)
            value = (NumericValue) input;
        else if (input instanceof String) {
            try {
                value = new NumericValue(Double.parseDouble((String) input));
            } catch (NumberFormatException e) {
                return throwResultTypeException(input, toDataTypeId);
            }
        } else
            return throwResultTypeException(input, toDataTypeId);
    } else if (toDataTypeId == DataTypes.ALPHANUMERIC)
        value = new AlphanumericValue(input.toString());
    else
        // If not, ditch it.
        return throwResultTypeException(input, toDataTypeId);
    return value;
}
Also used : AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue)

Aggregations

DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)30 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)13 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)10 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)10 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)8 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)8 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)6 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)6 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)6 BucketCalculator (com.serotonin.m2m2.view.quantize2.BucketCalculator)6 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)5 HashMap (java.util.HashMap)5 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)4 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)4 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)4 AbstractDataQuantizer (com.serotonin.m2m2.view.quantize2.AbstractDataQuantizer)4 ExportDataValue (com.serotonin.m2m2.vo.export.ExportDataValue)4 ImageSaveException (com.serotonin.m2m2.ImageSaveException)3 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)2 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)2