Search in sources :

Example 11 with DataType

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

the class PointValueDaoSQL method updatePointValueImpl.

long updatePointValueImpl(final int pointId, final PointValueTime pvt, final SetPointSource source, boolean async) {
    DataValue value = pvt.getValue();
    final int dataType = DataTypes.getDataType(value);
    double dvalue = 0;
    String svalue = null;
    if (dataType == DataTypes.IMAGE) {
        ImageValue imageValue = (ImageValue) value;
        dvalue = imageValue.getType();
        if (imageValue.isSaved())
            svalue = Long.toString(imageValue.getId());
    } else if (value.hasDoubleRepresentation())
        dvalue = value.getDoubleValue();
    else
        svalue = value.getStringValue();
    // Check if we need to create an annotation.
    long id;
    try {
        if (svalue != null || source != null || dataType == DataTypes.IMAGE)
            async = false;
        id = updatePointValue(pointId, dataType, dvalue, pvt.getTime(), svalue, source, async);
    } catch (ConcurrencyFailureException e) {
        // Still failed to insert after all of the retries. Store the data
        synchronized (UNSAVED_POINT_UPDATES) {
            UNSAVED_POINT_UPDATES.add(new UnsavedPointUpdate(pointId, pvt, source));
        }
        return -1;
    }
    // Check if we need to save an image
    if (dataType == DataTypes.IMAGE) {
        ImageValue imageValue = (ImageValue) value;
        if (!imageValue.isSaved()) {
            imageValue.setId(id);
            File file = new File(Common.getFiledataPath(), imageValue.getFilename());
            // Write the file.
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(file);
                StreamUtils.transfer(new ByteArrayInputStream(imageValue.getData()), out);
            } catch (IOException e) {
                // Rethrow as an RTE
                throw new ImageSaveException(e);
            } finally {
                try {
                    if (out != null)
                        out.close();
                } catch (IOException e) {
                // no op
                }
            }
            // Allow the data to be GC'ed
            imageValue.setData(null);
        }
    }
    clearUnsavedPointUpdates();
    return id;
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) FileOutputStream(java.io.FileOutputStream) ImageSaveException(com.serotonin.m2m2.ImageSaveException) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) File(java.io.File)

Example 12 with DataType

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

the class PointValueDaoSQL method updatePointValueAnnotation.

private void updatePointValueAnnotation(long id, int dataType, String svalue, SetPointSource source) {
    if (svalue == null && dataType == DataTypes.IMAGE)
        svalue = Long.toString(id);
    // Check if we need to create an annotation.
    TranslatableMessage sourceMessage = null;
    if (source != null)
        sourceMessage = source.getSetPointSourceMessage();
    if (svalue != null || sourceMessage != null) {
        String shortString = null;
        String longString = null;
        if (svalue != null) {
            if (svalue.length() > 128)
                longString = svalue;
            else
                shortString = svalue;
        }
        // 
        ejt.update(// 
        POINT_VALUE_ANNOTATION_UPDATE + "WHERE pointValueId = ?", // 
        new Object[] { shortString, longString, writeTranslatableMessage(sourceMessage), id }, new int[] { Types.VARCHAR, Types.CLOB, Types.CLOB, Types.INTEGER });
    }
}
Also used : TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 13 with DataType

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

the class DeltamationCommon method validatePoint.

public static DataPointVO validatePoint(int pointId, String name, ProcessResult response, Integer dataType, boolean requireSettable) {
    DataPointDao points = DataPointDao.instance;
    DataPointVO point = points.getDataPoint(pointId);
    if (point == null) {
        response.addContextualMessage(name, "validate.noPoint");
        return null;
    }
    if (requireSettable && !point.getPointLocator().isSettable()) {
        response.addContextualMessage(name, "validate.pointNotSettable", point.getName());
    }
    if (dataType != null && point.getPointLocator().getDataTypeId() != dataType) {
        response.addContextualMessage(name, "validate.pointWrongType", point.getName());
    }
    return point;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao)

Example 14 with DataType

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

the class MultiPointSeriesIdStatisticsStreamTest method createDataPoint.

@Override
protected DataPointVO createDataPoint(int dataSourceId, DataType dataType, int defaultCacheSize) {
    DataPointVO vo = super.createDataPoint(dataSourceId, dataType, defaultCacheSize);
    // Change series id
    int seriesId = DataPointDao.getInstance().insertNewTimeSeries();
    while (vo.getId() == seriesId) {
        seriesId = DataPointDao.getInstance().insertNewTimeSeries();
    }
    vo.setSeriesId(seriesId);
    DataPointDao.getInstance().update(vo.getId(), vo);
    return vo;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO)

Example 15 with DataType

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

the class PointValueImportResult method saveValue.

public void saveValue(LegacyXidPointValueTimeModel model) {
    if (valid) {
        // Validate the model against our point
        long timestamp = model.getTimestamp();
        if (timestamp == 0)
            timestamp = Common.timer.currentTimeMillis();
        try {
            DataValue value;
            switch(vo.getPointLocator().getDataType()) {
                case ALPHANUMERIC:
                    value = new AlphanumericValue((String) model.getValue());
                    break;
                case BINARY:
                    value = new BinaryValue((Boolean) model.getValue());
                    break;
                case MULTISTATE:
                    if (model.getValue() instanceof String) {
                        try {
                            value = vo.getTextRenderer().parseText((String) model.getValue(), vo.getPointLocator().getDataType());
                        } catch (Exception e) {
                            // Lots can go wrong here so let the user know
                            result.addContextualMessage("value", "event.valueParse.textParse", e.getMessage());
                            return;
                        }
                    } else {
                        value = new MultistateValue(((Number) model.getValue()).intValue());
                    }
                    break;
                case NUMERIC:
                    value = new NumericValue(((Number) model.getValue()).doubleValue());
                    break;
                default:
                    result.addContextualMessage("dataType", "common.default", vo.getPointLocator().getDataType() + " data type not supported yet");
                    return;
            }
            PointValueTime pvt;
            if (model.getAnnotation() == null) {
                pvt = new PointValueTime(value, timestamp);
            } else {
                pvt = new AnnotatedPointValueTime(value, timestamp, new TranslatableMessage("common.default", model.getAnnotation()));
            }
            if (rt == null) {
                dao.savePointValueAsync(vo, pvt);
            } else {
                rt.savePointValueDirectToCache(pvt, null, true, true, fireEvents);
            }
            total++;
        } catch (Exception e) {
            if (e instanceof ClassCastException) {
                result.addContextualMessage("dataType", "event.ds.dataType");
            } else {
                result.addContextualMessage("value", "common.default", e.getMessage());
            }
        }
    }
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Aggregations

ArrayList (java.util.ArrayList)24 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)23 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)17 DataType (com.serotonin.m2m2.DataType)14 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)13 HashMap (java.util.HashMap)12 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 DataImplementation (org.osate.aadl2.DataImplementation)9 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)8 IOException (java.io.IOException)8 File (java.io.File)7 ScriptError (com.serotonin.m2m2.rt.script.ScriptError)6 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)6