Search in sources :

Example 26 with DataValue

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

the class EventHandlersDwr method createSetValueContent.

@DwrPermission(user = true)
public String createSetValueContent(int pointId, String valueStr, String idSuffix) {
    DataPointVO pointVO = DataPointDao.instance.getDataPoint(pointId);
    Permissions.ensureDataSourcePermission(Common.getHttpUser(), pointVO.getDataSourceId());
    DataValue value = DataValue.stringToValue(valueStr, pointVO.getPointLocator().getDataTypeId());
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("point", pointVO);
    model.put("idSuffix", idSuffix);
    model.put("text", pointVO.getTextRenderer().getText(value, TextRenderer.HINT_FULL));
    model.put("rawText", pointVO.getTextRenderer().getText(value, TextRenderer.HINT_RAW));
    model.put("valueStr", valueStr);
    String snippet = pointVO.getTextRenderer().getSetPointSnippetFilename();
    return generateContent(WebContextFactory.get().getHttpServletRequest(), snippet, model);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 27 with DataValue

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

the class DataPointRT method scheduleTimeoutImpl.

public void scheduleTimeoutImpl(long fireTime) {
    synchronized (intervalLoggingLock) {
        DataValue value;
        if (vo.getLoggingType() == DataPointVO.LoggingTypes.INTERVAL) {
            if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.INSTANT)
                value = PointValueTime.getValue(pointValue);
            else if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.MAXIMUM || vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.MINIMUM) {
                value = PointValueTime.getValue(intervalValue);
                intervalValue = pointValue;
            } else if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.AVERAGE) {
                // If we don't have enough averaging values then we will bail and wait for more
                if (vo.isOverrideIntervalLoggingSamples() && (averagingValues.size() != vo.getIntervalLoggingSampleWindowSize()))
                    return;
                if (vo.getPointLocator().getDataTypeId() == DataTypes.MULTISTATE) {
                    StartsAndRuntimeList stats = new StartsAndRuntimeList(intervalStartTime, fireTime, intervalValue, averagingValues);
                    double maxProportion = -1;
                    Object valueAtMax = null;
                    for (StartsAndRuntime sar : stats.getData()) {
                        if (sar.getProportion() > maxProportion) {
                            maxProportion = sar.getProportion();
                            valueAtMax = sar.getValue();
                        }
                    }
                    if (valueAtMax != null)
                        value = new MultistateValue(DataValue.objectToValue(valueAtMax).getIntegerValue());
                    else
                        value = null;
                } else {
                    AnalogStatistics stats = new AnalogStatistics(intervalStartTime, fireTime, intervalValue, averagingValues);
                    if (stats.getAverage() == null || (stats.getAverage() == Double.NaN && stats.getCount() == 0))
                        value = null;
                    else if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC)
                        value = new NumericValue(stats.getAverage());
                    else if (vo.getPointLocator().getDataTypeId() == DataTypes.BINARY)
                        value = new BinaryValue(stats.getAverage() >= 0.5);
                    else
                        throw new ShouldNeverHappenException("Unsupported average interval logging data type.");
                }
                // Compute the center point of our average data, starting by finding where our period started
                long sampleWindowStartTime;
                if (vo.isOverrideIntervalLoggingSamples())
                    sampleWindowStartTime = averagingValues.get(0).getTime();
                else
                    sampleWindowStartTime = intervalStartTime;
                intervalStartTime = fireTime;
                // Fix to simulate center tapped filter (un-shift the average)
                fireTime = sampleWindowStartTime + (fireTime - sampleWindowStartTime) / 2L;
                intervalValue = pointValue;
                if (!vo.isOverrideIntervalLoggingSamples())
                    averagingValues.clear();
            } else
                throw new ShouldNeverHappenException("Unknown interval logging type: " + vo.getIntervalLoggingType());
        } else if (vo.getLoggingType() == DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL) {
            // Okay, no changes rescheduled the timer. Get a value, reschedule
            if (pointValue != null) {
                value = pointValue.getValue();
                if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC)
                    toleranceOrigin = pointValue.getDoubleValue();
            } else
                value = null;
            rescheduleChangeInterval(Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod()));
        } else
            value = null;
        if (value != null) {
            PointValueTime newValue = new PointValueTime(value, fireTime);
            valueCache.logPointValueAsync(newValue, null);
            // Fire logged Events
            fireEvents(null, newValue, null, false, false, true, false, false);
        }
    }
}
Also used : StartsAndRuntime(com.infiniteautomation.mango.statistics.StartsAndRuntime) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) StartsAndRuntimeList(com.infiniteautomation.mango.statistics.StartsAndRuntimeList) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) 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 28 with DataValue

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

the class PointValueEmporter method exportRow.

public void exportRow(ExportDataValue edv) {
    int cellNum = 0;
    Cell cell;
    Row row;
    row = sheet.createRow(this.rowNum++);
    // Set Point XID
    cell = row.createCell(cellNum++);
    cell.setCellValue(this.pointInfo.getXid());
    // Set the Device Name
    cell = row.createCell(cellNum++);
    cell.setCellValue(this.pointInfo.getDeviceName());
    // Set the Point Name
    cell = row.createCell(cellNum++);
    cell.setCellValue(this.pointInfo.getPointName());
    // Time
    cell = row.createCell(cellNum++);
    cell.setCellValue(new DateTime(edv.getTime()).toDate());
    cell.setCellStyle(dateStyle);
    if (edv.getValue() != null) {
        cell = row.createCell(cellNum++);
        DataValue value = edv.getValue();
        switch(value.getDataType()) {
            case DataTypes.ALPHANUMERIC:
                cell.setCellValue(value.getStringValue());
                break;
            case DataTypes.BINARY:
                cell.setCellValue(value.getBooleanValue());
                break;
            case DataTypes.MULTISTATE:
                cell.setCellValue(value.getIntegerValue());
                break;
            case DataTypes.NUMERIC:
                cell.setCellValue(value.getDoubleValue());
                break;
            default:
        }
        // Set the text renderer value here
        cell = row.createCell(cellNum++);
        cell.setCellValue(this.pointInfo.getTextRenderer().getText(edv.getValue(), TextRenderer.HINT_FULL));
    } else {
        row.createCell(cellNum++);
        // Do we need an empty cell?
        row.createCell(cellNum++);
    }
    // Do we have an annotation
    if (edv.getAnnotation() != null) {
        cell = row.createCell(cellNum++);
        cell.setCellValue(edv.getAnnotation().translate(Common.getTranslations()));
    } else {
        cell = row.createCell(cellNum++);
    }
    this.rowsAdded++;
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell) DateTime(org.joda.time.DateTime)

Example 29 with DataValue

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

the class PointValueDaoSQL method savePointValueImpl.

long savePointValueImpl(final int pointId, final PointValueTime pointValue, final SetPointSource source, boolean async) {
    DataValue value = pointValue.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 = savePointValue(pointId, dataType, dvalue, pointValue.getTime(), svalue, source, async);
    } catch (ConcurrencyFailureException e) {
        // Still failed to insert after all of the retries. Store the data
        synchronized (UNSAVED_POINT_VALUES) {
            UNSAVED_POINT_VALUES.add(new UnsavedPointValue(pointId, pointValue, 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);
        }
    }
    clearUnsavedPointValues();
    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 30 with DataValue

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

the class PointValueDaoSQL method createDataValue.

DataValue createDataValue(ResultSet rs, int firstParameter) throws SQLException {
    int dataType = rs.getInt(firstParameter);
    DataValue value;
    switch(dataType) {
        case (DataTypes.NUMERIC):
            value = new NumericValue(rs.getDouble(firstParameter + 1));
            break;
        case (DataTypes.BINARY):
            value = new BinaryValue(rs.getDouble(firstParameter + 1) == 1);
            break;
        case (DataTypes.MULTISTATE):
            value = new MultistateValue(rs.getInt(firstParameter + 1));
            break;
        case (DataTypes.ALPHANUMERIC):
            String s = rs.getString(firstParameter + 2);
            if (s == null)
                s = rs.getString(firstParameter + 3);
            value = new AlphanumericValue(s);
            break;
        case (DataTypes.IMAGE):
            value = new ImageValue(Integer.parseInt(rs.getString(firstParameter + 2)), rs.getInt(firstParameter + 1));
            break;
        default:
            value = null;
    }
    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) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) 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