Search in sources :

Example 11 with AbstractPointWrapper

use of com.serotonin.m2m2.rt.script.AbstractPointWrapper in project ma-core-public by infiniteautomation.

the class DataSourceQuery method getPointsForSource.

/**
 * Helper to extract points for a source
 * @param ds
 * @return
 */
private List<DataPointWrapper> getPointsForSource(DataSourceVO<?> ds) {
    List<DataPointWrapper> points = new ArrayList<DataPointWrapper>();
    List<DataPointVO> dataPoints = DataPointDao.instance.getDataPoints(ds.getId(), null, false);
    for (DataPointVO vo : dataPoints) {
        DataPointRT rt = Common.runtimeManager.getDataPoint(vo.getId());
        AbstractPointWrapper wrapper = null;
        if (rt != null)
            wrapper = ScriptUtils.wrapPoint(engine, rt, setter);
        points.add(new DataPointWrapper(vo, wrapper));
    }
    return points;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) ArrayList(java.util.ArrayList)

Example 12 with AbstractPointWrapper

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

Example 13 with AbstractPointWrapper

use of com.serotonin.m2m2.rt.script.AbstractPointWrapper in project ma-core-public by infiniteautomation.

the class DataPointQuery method byXid.

public DataPointWrapper byXid(String xid) {
    DataPointVO dp = DataPointDao.getInstance().getByXid(xid);
    if (dp == null)
        return null;
    if (permissionService.hasPermission(permissions, dp.getReadPermission())) {
        DataPointRT rt = null;
        AbstractPointWrapper wrapper = null;
        rt = Common.runtimeManager.getDataPoint(dp.getId());
        if (rt != null)
            wrapper = service.wrapPoint(engine, rt, setter);
        else
            wrapper = null;
        return new DataPointWrapper(dp, wrapper);
    } else
        return null;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT)

Example 14 with AbstractPointWrapper

use of com.serotonin.m2m2.rt.script.AbstractPointWrapper in project ma-core-public by infiniteautomation.

the class DataPointQuery method query.

/**
 * Perform a query on the data points table
 */
public List<DataPointWrapper> query(String query) {
    ASTNode root = RQLUtils.parseRQLtoAST(query);
    List<DataPointVO> dataPoints = new ArrayList<>();
    ConditionSortLimitWithTagKeys conditions = (ConditionSortLimitWithTagKeys) DataPointDao.getInstance().rqlToCondition(root, null, null, null);
    DataPointDao.getInstance().customizedQuery(conditions, permissions, dataPoints::add);
    List<DataPointWrapper> results = new ArrayList<DataPointWrapper>();
    // Filter on permissions
    DataPointRT rt = null;
    AbstractPointWrapper wrapper = null;
    for (DataPointVO dp : dataPoints) {
        // Can we read or write to this point?
        if (permissionService.hasPermission(permissions, dp.getReadPermission())) {
            rt = Common.runtimeManager.getDataPoint(dp.getId());
            if (rt != null)
                wrapper = service.wrapPoint(engine, rt, setter);
            else
                wrapper = null;
            results.add(new DataPointWrapper(dp, wrapper));
        }
    }
    return results;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) ASTNode(net.jazdw.rql.parser.ASTNode) ArrayList(java.util.ArrayList) ConditionSortLimitWithTagKeys(com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys)

Example 15 with AbstractPointWrapper

use of com.serotonin.m2m2.rt.script.AbstractPointWrapper in project ma-core-public by MangoAutomation.

the class DataPointQuery method query.

/**
 * Perform a query on the data points table
 */
public List<DataPointWrapper> query(String query) {
    ASTNode root = RQLUtils.parseRQLtoAST(query);
    List<DataPointVO> dataPoints = new ArrayList<>();
    ConditionSortLimitWithTagKeys conditions = (ConditionSortLimitWithTagKeys) DataPointDao.getInstance().rqlToCondition(root, null, null, null);
    DataPointDao.getInstance().customizedQuery(conditions, permissions, dataPoints::add);
    List<DataPointWrapper> results = new ArrayList<DataPointWrapper>();
    // Filter on permissions
    DataPointRT rt = null;
    AbstractPointWrapper wrapper = null;
    for (DataPointVO dp : dataPoints) {
        // Can we read or write to this point?
        if (permissionService.hasPermission(permissions, dp.getReadPermission())) {
            rt = Common.runtimeManager.getDataPoint(dp.getId());
            if (rt != null)
                wrapper = service.wrapPoint(engine, rt, setter);
            else
                wrapper = null;
            results.add(new DataPointWrapper(dp, wrapper));
        }
    }
    return results;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) ASTNode(net.jazdw.rql.parser.ASTNode) ArrayList(java.util.ArrayList) ConditionSortLimitWithTagKeys(com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys)

Aggregations

AbstractPointWrapper (com.serotonin.m2m2.rt.script.AbstractPointWrapper)8 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)7 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)7 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Bindings (javax.script.Bindings)6 SimpleBindings (javax.script.SimpleBindings)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 Set (java.util.Set)4 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)3 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)3 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)3 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)3 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)3 ConditionSortLimitWithTagKeys (com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys)2 ScriptUtility (com.infiniteautomation.mango.util.script.ScriptUtility)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2