Search in sources :

Example 1 with EnhancedPointValueDao

use of com.serotonin.m2m2.db.dao.EnhancedPointValueDao 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 2 with EnhancedPointValueDao

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

Aggregations

EnhancedPointValueDao (com.serotonin.m2m2.db.dao.EnhancedPointValueDao)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)1 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)1 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)1 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)1 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)1 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)1 SpreadsheetException (com.serotonin.m2m2.vo.emport.SpreadsheetException)1 ExportDataValue (com.serotonin.m2m2.vo.export.ExportDataValue)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Cell (org.apache.poi.ss.usermodel.Cell)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1