Search in sources :

Example 11 with ResultTypeException

use of com.serotonin.m2m2.rt.script.ResultTypeException 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

PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)10 ScriptException (javax.script.ScriptException)9 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)8 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)8 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)8 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)7 ScriptLog (com.serotonin.m2m2.rt.script.ScriptLog)7 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)6 HashMap (java.util.HashMap)6 PrintWriter (java.io.PrintWriter)5 CompiledScript (javax.script.CompiledScript)5 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)4 StringWriter (java.io.StringWriter)4 IntStringPair (com.serotonin.db.pair.IntStringPair)3 ScriptPointValueSetter (com.serotonin.m2m2.rt.script.ScriptPointValueSetter)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2