Search in sources :

Example 6 with AnnotatedPointValueTime

use of com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime in project ma-modules-public by infiniteautomation.

the class PointValueImportResult method saveValue.

public void saveValue(XidPointValueTimeModel model) {
    if (valid) {
        // Validate the model against our point
        long timestamp = model.getTimestamp();
        if (timestamp == 0)
            timestamp = Common.timer.currentTimeMillis();
        int dataTypeId = DataTypeEnum.convertFrom(model.getType());
        if (dataTypeId != vo.getPointLocator().getDataTypeId()) {
            result.addContextualMessage("dataType", "event.ds.dataType");
            return;
        }
        DataValue value;
        switch(model.getType()) {
            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(), dataTypeId);
                    } 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;
            case IMAGE:
            default:
                result.addContextualMessage("dataType", "common.default", model.getType() + " 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.getId(), pvt, null);
        } else {
            rt.savePointValueDirectToCache(pvt, null, true, true);
        }
        total++;
    }
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) 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)

Example 7 with AnnotatedPointValueTime

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

the class ChartExportServlet method exportExcel.

/**
 * Do the export as Excel XLSX File
 * @param response
 * @param from
 * @param to
 * @param def
 * @param user
 * @throws IOException
 */
private void exportExcel(HttpServletResponse response, long from, long to, DataExportDefinition def, User user) throws IOException {
    DataPointDao dataPointDao = DataPointDao.instance;
    PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
    // Stream the content.
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    final List<PointValueEmporter> sheetEmporters = new ArrayList<PointValueEmporter>();
    final AtomicInteger sheetIndex = new AtomicInteger();
    sheetEmporters.add(new PointValueEmporter(Common.translate("emport.pointValues") + " " + sheetIndex.get()));
    final SpreadsheetEmporter emporter = new SpreadsheetEmporter(FileType.XLSX);
    BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
    emporter.prepareExport(bos);
    emporter.prepareSheetExport(sheetEmporters.get(0));
    final ExportDataValue edv = new ExportDataValue();
    MappedRowCallback<PointValueTime> callback = new MappedRowCallback<PointValueTime>() {

        @Override
        public void row(PointValueTime pvt, int rowIndex) {
            edv.setValue(pvt.getValue());
            edv.setTime(pvt.getTime());
            if (pvt instanceof AnnotatedPointValueTime)
                edv.setAnnotation(((AnnotatedPointValueTime) pvt).getSourceMessage());
            else
                edv.setAnnotation(null);
            sheetEmporters.get(sheetIndex.get()).exportRow(edv);
            if (sheetEmporters.get(sheetIndex.get()).getRowsAdded() >= emporter.getMaxRowsPerSheet()) {
                ExportPointInfo info = sheetEmporters.get(sheetIndex.get()).getPointInfo();
                sheetIndex.incrementAndGet();
                PointValueEmporter sheetEmporter = new PointValueEmporter(Common.translate("emport.pointValues") + " " + sheetIndex.get());
                sheetEmporter.setPointInfo(info);
                sheetEmporters.add(sheetEmporter);
                emporter.prepareSheetExport(sheetEmporters.get(sheetIndex.get()));
            }
        }
    };
    for (int pointId : def.getPointIds()) {
        DataPointVO dp = dataPointDao.getDataPoint(pointId, false);
        if (Permissions.hasDataPointReadPermission(user, dp)) {
            ExportPointInfo pointInfo = new ExportPointInfo();
            pointInfo.setXid(dp.getXid());
            pointInfo.setPointName(dp.getName());
            pointInfo.setDeviceName(dp.getDeviceName());
            pointInfo.setTextRenderer(dp.getTextRenderer());
            sheetEmporters.get(sheetIndex.get()).setPointInfo(pointInfo);
            pointValueDao.getPointValuesBetween(pointId, from, to, callback);
        }
    }
    emporter.finishExport();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) ArrayList(java.util.ArrayList) ExportPointInfo(com.serotonin.m2m2.vo.export.ExportPointInfo) SpreadsheetEmporter(com.serotonin.m2m2.vo.emport.SpreadsheetEmporter) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MappedRowCallback(com.serotonin.db.MappedRowCallback) PointValueEmporter(com.serotonin.m2m2.rt.dataImage.PointValueEmporter) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with AnnotatedPointValueTime

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

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

the class DataPointRT method savePointValue.

private void savePointValue(PointValueTime newValue, SetPointSource source, boolean async, boolean saveToDatabase) {
    // Null values are not very nice, and since they don't have a specific meaning they are hereby ignored.
    if (newValue == null)
        return;
    // Check the data type of the value against that of the locator, just for fun.
    int valueDataType = DataTypes.getDataType(newValue.getValue());
    if (valueDataType != DataTypes.UNKNOWN && valueDataType != vo.getPointLocator().getDataTypeId())
        // to know how it happened, and the stack trace here provides the best information.
        throw new ShouldNeverHappenException("Data type mismatch between new value and point locator: newValue=" + DataTypes.getDataType(newValue.getValue()) + ", locator=" + vo.getPointLocator().getDataTypeId());
    // Check if this value qualifies for discardation.
    if (vo.isDiscardExtremeValues() && DataTypes.getDataType(newValue.getValue()) == DataTypes.NUMERIC) {
        double newd = newValue.getDoubleValue();
        // Discard if NaN
        if (Double.isNaN(newd))
            return;
        if (newd < vo.getDiscardLowLimit() || newd > vo.getDiscardHighLimit())
            // Discard the value
            return;
    }
    if (newValue.getTime() > Common.timer.currentTimeMillis() + SystemSettingsDao.getFutureDateLimit()) {
        // Too far future dated. Toss it. But log a message first.
        LOG.warn("Future dated value detected: pointId=" + vo.getId() + ", value=" + newValue.getValue().toString() + ", type=" + vo.getPointLocator().getDataTypeId() + ", ts=" + newValue.getTime(), new Exception());
        return;
    }
    boolean backdated = pointValue != null && newValue.getTime() < pointValue.getTime();
    // Determine whether the new value qualifies for logging.
    boolean logValue;
    // ... or even saving in the cache.
    boolean saveValue = true;
    switch(vo.getLoggingType()) {
        case DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL:
        case DataPointVO.LoggingTypes.ON_CHANGE:
            if (pointValue == null)
                logValue = true;
            else if (backdated)
                // Backdated. Ignore it
                logValue = false;
            else {
                if (newValue.getValue() instanceof NumericValue) {
                    // Get the new double
                    double newd = newValue.getDoubleValue();
                    // See if the new value is outside of the tolerance.
                    double diff = toleranceOrigin - newd;
                    if (diff < 0)
                        diff = -diff;
                    if (diff > vo.getTolerance()) {
                        toleranceOrigin = newd;
                        logValue = true;
                    } else
                        logValue = false;
                } else if (newValue.getValue() instanceof ImageValue) {
                    logValue = !((ImageValue) newValue.getValue()).equalDigests(((ImageValue) pointValue.getValue()).getDigest());
                } else
                    logValue = !Objects.equals(newValue.getValue(), pointValue.getValue());
            }
            saveValue = logValue;
            break;
        case DataPointVO.LoggingTypes.ALL:
            logValue = true;
            break;
        case DataPointVO.LoggingTypes.ON_TS_CHANGE:
            if (pointValue == null)
                logValue = true;
            else if (backdated)
                // Backdated. Ignore it
                logValue = false;
            else
                logValue = newValue.getTime() != pointValue.getTime();
            saveValue = logValue;
            break;
        case DataPointVO.LoggingTypes.INTERVAL:
            if (!backdated)
                intervalSave(newValue);
        default:
            logValue = false;
    }
    if (!saveToDatabase)
        logValue = false;
    if (saveValue) {
        valueCache.savePointValue(newValue, source, logValue, async);
        if (vo.getLoggingType() == DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL)
            rescheduleChangeInterval(Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod()));
    }
    // fetch the annotation
    if (source != null) {
        newValue = new AnnotatedPointValueTime(newValue.getValue(), newValue.getTime(), source.getSetPointSourceMessage());
    }
    // Ignore historical values.
    if (pointValue == null || newValue.getTime() >= pointValue.getTime()) {
        PointValueTime oldValue = pointValue;
        pointValue = newValue;
        fireEvents(oldValue, newValue, null, source != null, false, logValue, true, false);
    } else
        fireEvents(null, newValue, null, false, true, logValue, false, false);
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException)

Example 10 with AnnotatedPointValueTime

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

the class PointValueEmporter method importRow.

/*
     * (non-Javadoc)
     * @see com.serotonin.m2m2.vo.emport.AbstractSheetEmporter#importRow(org.apache.poi.ss.usermodel.Row)
     */
@Override
protected void importRow(Row rowData) throws SpreadsheetException {
    int cellNum = 0;
    // Data Point XID
    Cell xidCell = rowData.getCell(cellNum++);
    if (xidCell == null)
        throw new SpreadsheetException(rowData.getRowNum(), "emport.error.xidRequired");
    if ((xidCell.getStringCellValue() == null) || (xidCell.getStringCellValue().isEmpty()))
        throw new SpreadsheetException("emport.error.xidRequired");
    // First Check to see if we already have a point
    String xid = xidCell.getStringCellValue();
    DataPointVO dp = voMap.get(xid);
    DataPointRT dpRt = rtMap.get(xid);
    // We will always have the vo in the map but the RT may be null if the point isn't running
    if (dp == null) {
        dp = dataPointDao.getDataPoint(xid);
        if (dp == null)
            throw new SpreadsheetException(rowData.getRowNum(), "emport.error.missingPoint", xid);
        dpRt = Common.runtimeManager.getDataPoint(dp.getId());
        rtMap.put(xid, dpRt);
        voMap.put(xid, dp);
    }
    PointValueTime pvt;
    // Cell Device name (Not using Here)
    cellNum++;
    // Cell Point name (Not using Here)
    cellNum++;
    // Cell Time
    Date time = rowData.getCell(cellNum++).getDateCellValue();
    // delete/add column
    Cell modifyCell = rowData.getCell(7);
    boolean add = false;
    boolean delete = false;
    if (modifyCell != null) {
        String modification = (String) modifyCell.getStringCellValue();
        if (modification.equalsIgnoreCase("delete")) {
            delete = true;
        } else if (modification.equalsIgnoreCase("add")) {
            add = true;
        } else {
            throw new SpreadsheetException(rowData.getRowNum(), "emport.spreadsheet.modifyCellUnknown");
        }
    }
    // What do we do with the row
    if (delete) {
        if (time == null) {
            throw new SpreadsheetException(rowData.getRowNum(), "emport.error.deleteNew", "no timestamp, unable to delete");
        } else {
            try {
                this.rowsDeleted += Common.runtimeManager.purgeDataPointValue(dp.getId(), time.getTime());
            } catch (Exception e) {
                if (e instanceof DataIntegrityViolationException)
                    throw new SpreadsheetException(rowData.getRowNum(), "emport.error.unableToDeleteDueToConstraints");
                else
                    throw new SpreadsheetException(rowData.getRowNum(), "emport.error.unableToDelete", e.getMessage());
            }
        }
        // Done now
        return;
    } else if (add) {
        // Cell Value
        Cell cell;
        cell = rowData.getCell(cellNum++);
        // Create a data value
        DataValue dataValue;
        switch(dp.getPointLocator().getDataTypeId()) {
            case DataTypes.ALPHANUMERIC:
                dataValue = new AlphanumericValue(cell.getStringCellValue());
                break;
            case DataTypes.BINARY:
                switch(cell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        dataValue = new BinaryValue(new Boolean(cell.getBooleanCellValue()));
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        if (cell.getNumericCellValue() == 0)
                            dataValue = new BinaryValue(new Boolean(false));
                        else
                            dataValue = new BinaryValue(new Boolean(true));
                        break;
                    case Cell.CELL_TYPE_STRING:
                        if (cell.getStringCellValue().equalsIgnoreCase("false"))
                            dataValue = new BinaryValue(new Boolean(false));
                        else
                            dataValue = new BinaryValue(new Boolean(true));
                        break;
                    default:
                        throw new SpreadsheetException(rowData.getRowNum(), "common.default", "Invalid cell type for extracting boolean");
                }
                break;
            case DataTypes.MULTISTATE:
                dataValue = new MultistateValue((int) cell.getNumericCellValue());
                break;
            case DataTypes.NUMERIC:
                dataValue = new NumericValue(cell.getNumericCellValue());
                break;
            default:
                throw new SpreadsheetException(rowData.getRowNum(), "emport.spreadsheet.unsupportedDataType", dp.getPointLocator().getDataTypeId());
        }
        // Cell Rendered Value (Not using yet)
        cellNum++;
        // Cell Annotation
        Cell annotationRow = rowData.getCell(cellNum++);
        if (annotationRow != null) {
            String annotation = annotationRow.getStringCellValue();
            // TODO These methods here do not allow updating the Annotation. We need to be a set point source for that to work
            TranslatableMessage sourceMessage = new TranslatableMessage("common.default", annotation);
            pvt = new AnnotatedPointValueTime(dataValue, time.getTime(), sourceMessage);
        } else {
            pvt = new PointValueTime(dataValue, time.getTime());
        }
        // Save to cache if running
        if (dpRt != null)
            dpRt.savePointValueDirectToCache(pvt, null, true, true);
        else {
            if (pointValueDao instanceof EnhancedPointValueDao) {
                DataSourceVO<?> ds = getDataSource(dp.getDataSourceId());
                ((EnhancedPointValueDao) pointValueDao).savePointValueAsync(dp, ds, pvt, null);
            } else {
                pointValueDao.savePointValueAsync(dp.getId(), pvt, null);
            }
        }
        // Increment our counter
        this.rowsAdded++;
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) SpreadsheetException(com.serotonin.m2m2.vo.emport.SpreadsheetException) Date(java.util.Date) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) SpreadsheetException(com.serotonin.m2m2.vo.emport.SpreadsheetException) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EnhancedPointValueDao(com.serotonin.m2m2.db.dao.EnhancedPointValueDao) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)15 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)12 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)8 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)6 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)5 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 ExportDataValue (com.serotonin.m2m2.vo.export.ExportDataValue)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)3 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)3 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)3 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)3 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)3 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)3 ExportPointInfo (com.serotonin.m2m2.vo.export.ExportPointInfo)3 RecentPointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel)3 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)2