Search in sources :

Example 6 with PointValueDao

use of com.serotonin.m2m2.db.dao.PointValueDao 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)

Example 7 with PointValueDao

use of com.serotonin.m2m2.db.dao.PointValueDao in project ma-modules-public by infiniteautomation.

the class StatisticsStream method streamData.

/* (non-Javadoc)
	 * @see com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeStream#streamData(com.fasterxml.jackson.core.JsonGenerator)
	 */
@Override
public void streamData(JsonGenerator jgen) throws IOException {
    // TODO Can't use the Facade as there is no way to perform the callback integrated with the PointValueCache
    // PointValueFacade pointValueFacade = new PointValueFacade(this.dataPointId);
    // First find the start value
    PointValueDao pvd = Common.databaseProxy.newPointValueDao();
    PointValueTime startPvt = pvd.getPointValueBefore(vo.getId(), from);
    DataValue startValue = null;
    if (startPvt != null)
        startValue = startPvt.getValue();
    StatisticsCalculator calculator = new StatisticsCalculator(jgen, vo, useRendered, unitConversion, this.from, this.to, startValue, dateTimeFormat, timezone);
    // Do the main work
    pvd.getPointValuesBetween(vo.getId(), from, to, calculator);
    // Finish
    calculator.done();
}
Also used : PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 8 with PointValueDao

use of com.serotonin.m2m2.db.dao.PointValueDao in project ma-core-public by infiniteautomation.

the class RuntimeManagerImpl method initializeDataSourceStartup.

/**
 * Only to be used at startup as the synchronization has been reduced for performance
 * @param vo
 * @return
 */
@Override
public boolean initializeDataSourceStartup(DataSourceVO<?> vo) {
    long startTime = System.nanoTime();
    // If the data source is already running, just quit.
    if (isDataSourceRunning(vo.getId()))
        return false;
    // Ensure that the data source is enabled.
    Assert.isTrue(vo.isEnabled(), "Data source not enabled.");
    // Create and initialize the runtime version of the data source.
    DataSourceRT<? extends DataSourceVO<?>> dataSource = vo.createDataSourceRT();
    dataSource.initialize();
    // Add it to the list of running data sources.
    synchronized (runningDataSources) {
        runningDataSources.put(dataSource.getId(), dataSource);
    }
    // Add the enabled points to the data source.
    List<DataPointVO> dataSourcePoints = DataPointDao.instance.getDataPointsForDataSourceStart(vo.getId());
    Map<Integer, List<PointValueTime>> latestValuesMap = null;
    PointValueDao pvDao = Common.databaseProxy.newPointValueDao();
    if (pvDao instanceof EnhancedPointValueDao) {
        // Find the maximum cache size for all point in the datasource
        // This number of values will be retrieved for all points in the datasource
        // If even one point has a high cache size this *may* cause issues
        int maxCacheSize = 0;
        for (DataPointVO dataPoint : dataSourcePoints) {
            if (dataPoint.getDefaultCacheSize() > maxCacheSize)
                maxCacheSize = dataPoint.getDefaultCacheSize();
        }
        try {
            latestValuesMap = ((EnhancedPointValueDao) pvDao).getLatestPointValuesForDataSource(vo, maxCacheSize);
        } catch (Exception e) {
            LOG.error("Failed to get latest point values for datasource " + vo.getXid() + ". Mango will try to retrieve latest point values per point which will take longer.", e);
        }
    }
    for (DataPointVO dataPoint : dataSourcePoints) {
        if (dataPoint.isEnabled()) {
            List<PointValueTime> latestValuesForPoint = null;
            if (latestValuesMap != null) {
                latestValuesForPoint = latestValuesMap.get(dataPoint.getId());
                if (latestValuesForPoint == null) {
                    latestValuesForPoint = new ArrayList<>();
                }
            }
            try {
                startDataPointStartup(dataPoint, latestValuesForPoint);
            } catch (Exception e) {
                LOG.error("Failed to start data point " + dataPoint.getXid(), e);
            }
        }
    }
    LOG.info("Data source '" + vo.getName() + "' initialized");
    long endTime = System.nanoTime();
    long duration = endTime - startTime;
    LOG.info("Data source '" + vo.getName() + "' took " + (double) duration / (double) 1000000 + "ms to start");
    return true;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) EnhancedPointValueDao(com.serotonin.m2m2.db.dao.EnhancedPointValueDao) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EnhancedPointValueDao(com.serotonin.m2m2.db.dao.EnhancedPointValueDao)

Example 9 with PointValueDao

use of com.serotonin.m2m2.db.dao.PointValueDao in project ma-core-public by infiniteautomation.

the class ChartExportServlet method exportCsv.

/**
 * Do the export as a CSV File
 * @param response
 * @param from
 * @param to
 * @param def
 * @param user
 * @throws IOException
 */
private void exportCsv(HttpServletRequest request, 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("text/csv");
    final Translations translations = Common.getTranslations();
    final ExportCsvStreamer exportCreator = new ExportCsvStreamer(request.getServerName(), request.getLocalPort(), response.getWriter(), translations);
    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);
            exportCreator.pointData(edv);
        }
    };
    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());
            pointInfo.setDataPointId(pointId);
            exportCreator.startPoint(pointInfo);
            pointValueDao.getPointValuesBetween(pointId, from, to, callback);
        }
    }
    exportCreator.done();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) MappedRowCallback(com.serotonin.db.MappedRowCallback) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) ExportCsvStreamer(com.serotonin.m2m2.vo.export.ExportCsvStreamer) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) ExportPointInfo(com.serotonin.m2m2.vo.export.ExportPointInfo) Translations(com.serotonin.m2m2.i18n.Translations)

Example 10 with PointValueDao

use of com.serotonin.m2m2.db.dao.PointValueDao in project ma-core-public by infiniteautomation.

the class DataPointRTTest method testIntervalOnChangeLogging.

/**
 * Test Interval Logged Values w/ On Change Option
 */
@Test
public void testIntervalOnChangeLogging() {
    PointValueDao dao = Common.databaseProxy.newPointValueDao();
    MockPointLocatorVO plVo = new MockPointLocatorVO(DataTypes.NUMERIC, true);
    DataPointVO dpVo = new DataPointVO();
    dpVo.setId(1);
    // Configure Interval on change logging
    dpVo.setLoggingType(DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL);
    dpVo.setTolerance(0.5);
    dpVo.setIntervalLoggingPeriod(5);
    dpVo.setIntervalLoggingPeriodType(TimePeriods.SECONDS);
    dpVo.setPointLocator(plVo);
    dpVo.setLoggingType(DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL);
    MockDataSourceVO dsVo = new MockDataSourceVO();
    MockPointLocatorRT plRt = new MockPointLocatorRT(plVo);
    // Setup some initial data
    List<PointValueTime> initialCache = new ArrayList<>();
    initialCache.add(new PointValueTime(1.0, 0));
    SimulationTimer timer = new SimulationTimer();
    DataPointRT rt = new DataPointRT(dpVo, plRt, dsVo, initialCache, timer);
    rt.initialize();
    rt.initializeIntervalLogging(0, false);
    // Test no changes
    timer.fastForwardTo(5001);
    PointValueTime value = dao.getLatestPointValue(dpVo.getId());
    // Ensure database has interval logged value
    assertEquals(1.0, value.getDoubleValue(), 0.0001);
    assertEquals(5000, value.getTime());
    // Ensure cache does not have interval logged value
    assertEquals(1.0, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(0, rt.getPointValue().getTime());
    // Next interval
    timer.fastForwardTo(6000);
    rt.setPointValue(new PointValueTime(2.0, 6000), null);
    // Check Log On Change
    value = dao.getLatestPointValue(dpVo.getId());
    assertEquals(2.0, value.getDoubleValue(), 0.0001);
    assertEquals(6000, value.getTime());
    assertEquals(2.0, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(6000, rt.getPointValue().getTime());
    // Interval is reset for 5000ms from now
    timer.fastForwardTo(11001);
    // Check Interval Log
    value = dao.getLatestPointValue(dpVo.getId());
    assertEquals(2.0, value.getDoubleValue(), 0.0001);
    assertEquals(11000, value.getTime());
    assertEquals(2.0, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(6000, rt.getPointValue().getTime());
    // Test Tolerance (Should not get logged)
    timer.fastForwardTo(12000);
    rt.setPointValue(new PointValueTime(2.20, 12000), null);
    // Check Log On Change
    value = dao.getLatestPointValue(dpVo.getId());
    assertEquals(2.0, value.getDoubleValue(), 0.0001);
    assertEquals(11000, value.getTime());
    // Cache will have the set value
    assertEquals(2.2, rt.getPointValue().getDoubleValue(), 0.0001);
    assertEquals(12000, rt.getPointValue().getTime());
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) SimulationTimer(com.serotonin.timer.SimulationTimer) MockPointLocatorRT(com.serotonin.m2m2.rt.dataSource.MockPointLocatorRT) ArrayList(java.util.ArrayList) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) Test(org.junit.Test)

Aggregations

PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)9 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)9 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)8 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)5 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)5 ExportDataValue (com.serotonin.m2m2.vo.export.ExportDataValue)5 ArrayList (java.util.ArrayList)5 ExportPointInfo (com.serotonin.m2m2.vo.export.ExportPointInfo)4 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)3 MappedRowCallback (com.serotonin.db.MappedRowCallback)2 EnhancedPointValueDao (com.serotonin.m2m2.db.dao.EnhancedPointValueDao)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)2 HashMap (java.util.HashMap)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 NoSQLDao (com.serotonin.m2m2.db.dao.nosql.NoSQLDao)1 Translations (com.serotonin.m2m2.i18n.Translations)1 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)1 PointValueEmporter (com.serotonin.m2m2.rt.dataImage.PointValueEmporter)1 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)1