Search in sources :

Example 1 with SetPointSource

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

the class BaseDwr method setPointImpl.

protected void setPointImpl(DataPointVO point, String valueStr, SetPointSource source) {
    if (point == null)
        return;
    if (valueStr == null)
        Common.runtimeManager.relinquish(point.getId());
    else {
        // Convert the string value into an object.
        DataValue value = DataValue.stringToValue(valueStr, point.getPointLocator().getDataTypeId());
        // do reverse conversion of renderer
        TextRenderer tr = point.getTextRenderer();
        if (point.getPointLocator().getDataTypeId() == DataTypes.NUMERIC && tr instanceof ConvertingRenderer) {
            ConvertingRenderer cr = (ConvertingRenderer) tr;
            UnitConverter converter = cr.getRenderedUnit().getConverterTo(cr.getUnit());
            double convertedValue = converter.convert(value.getDoubleValue());
            value = new NumericValue(convertedValue);
        }
        Common.runtimeManager.setDataPointValue(point.getId(), value, source);
    }
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) UnitConverter(javax.measure.converter.UnitConverter) ConvertingRenderer(com.serotonin.m2m2.view.text.ConvertingRenderer) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) TextRenderer(com.serotonin.m2m2.view.text.TextRenderer)

Example 2 with SetPointSource

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

the class MockPointValueDao method savePointValueSync.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.db.dao.PointValueDao#savePointValueSync(int, com.serotonin.m2m2.rt.dataImage.PointValueTime, com.serotonin.m2m2.rt.dataImage.SetPointSource)
     */
@Override
public PointValueTime savePointValueSync(int pointId, PointValueTime pointValue, SetPointSource source) {
    List<PointValueTime> pvts = data.get(pointId);
    if (pvts == null) {
        pvts = new ArrayList<>();
        data.put(pointId, pvts);
    }
    PointValueTime newPvt = null;
    if (source != null)
        newPvt = new AnnotatedPointValueTime(pointValue.getValue(), pointValue.getTime(), source.getSetPointSourceMessage());
    else
        newPvt = new PointValueTime(pointValue.getValue(), pointValue.getTime());
    pvts.add(newPvt);
    // Keep in time order?
    Collections.sort(pvts);
    return newPvt;
}
Also used : IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)

Example 3 with SetPointSource

use of com.serotonin.m2m2.rt.dataImage.SetPointSource 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 4 with SetPointSource

use of com.serotonin.m2m2.rt.dataImage.SetPointSource 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 5 with SetPointSource

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

the class PointValueDaoSQL method savePointValueSync.

/**
 * Only the PointValueCache should call this method during runtime. Do not use.
 */
@Override
public PointValueTime savePointValueSync(int pointId, PointValueTime pointValue, SetPointSource source) {
    long id = savePointValueImpl(pointId, pointValue, source, false);
    PointValueTime savedPointValue;
    int retries = 5;
    while (true) {
        try {
            savedPointValue = getPointValue(id);
            break;
        } catch (ConcurrencyFailureException e) {
            if (retries <= 0)
                throw e;
            retries--;
        }
    }
    return savedPointValue;
}
Also used : ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedIdPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedIdPointValueTime)

Aggregations

PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)4 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)4 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)4 ConcurrencyFailureException (org.springframework.dao.ConcurrencyFailureException)4 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)3 IOException (java.io.IOException)3 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 ImageSaveException (com.serotonin.m2m2.ImageSaveException)2 AnnotatedIdPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedIdPointValueTime)2 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 SerialPortException (com.infiniteautomation.mango.io.serial.SerialPortException)1 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)1 LogStopWatch (com.serotonin.log.LogStopWatch)1 RTException (com.serotonin.m2m2.rt.RTException)1