Search in sources :

Example 1 with AlphanumericValue

use of com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue 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 AlphanumericValue

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

the class PointValueTimeWriter method writeNonNull.

/**
 * Write a Data Value that if null contains an annotation saying there is no data at this point in time
 * - useful for Rollups with gaps in the data
 * @param value
 * @param time
 * @throws ConversionException
 * @throws IOException
 */
public void writeNonNull(DataValue value, Long time, DataPointVO vo) throws ConversionException, IOException {
    if (time == null)
        throw new ShouldNeverHappenException("Time cannot be null");
    if (value == null) {
        if (useRendered) {
            this.writePointValueTime(new AlphanumericValue(""), time, this.noDataMessage, vo);
        } else {
            this.writePointValueTime(0.0D, time, this.noDataMessage, vo);
        }
    } else {
        if (useRendered) {
            // Convert to Alphanumeric Value
            String textValue = Functions.getRenderedText(vo, new PointValueTime(value, time));
            this.writePointValueTime(new AlphanumericValue(textValue), time, null, vo);
        } else if (unitConversion) {
            // Convert Value, must be numeric
            if (value instanceof NumericValue)
                this.writePointValueTime(vo.getUnit().getConverterTo(vo.getRenderedUnit()).convert(value.getDoubleValue()), time, null, vo);
            else
                this.writePointValueTime(value, time, null, vo);
        } else {
            this.writePointValueTime(value, time, null, vo);
        }
    }
}
Also used : AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 3 with AlphanumericValue

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

the class PointValueTimeWriter method writeNonNullDouble.

/**
 * Write a Data Value that if null contains an annotation saying there is no data at this point in time
 * - useful for Rollups with gaps in the data
 * @param value
 * @param time
 * @throws ConversionException
 * @throws IOException
 */
public void writeNonNullDouble(Double value, long time, DataPointVO vo) throws ConversionException, IOException {
    if (value == null) {
        if (useRendered) {
            this.writePointValueTime(new AlphanumericValue(""), time, this.noDataMessage, vo);
        } else {
            this.writePointValueTime(0.0D, time, this.noDataMessage, vo);
        }
    } else {
        if (useRendered) {
            // Convert to Alphanumeric Value
            String textValue = Functions.getRenderedText(vo, new PointValueTime(value, time));
            this.writePointValueTime(new AlphanumericValue(textValue), time, null, vo);
        } else if (unitConversion) {
            // Convert Value, must be numeric
            this.writePointValueTime(vo.getUnit().getConverterTo(vo.getRenderedUnit()).convert(value), time, null, vo);
        } else {
            this.writePointValueTime(value, time, null, vo);
        }
    }
}
Also used : AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 4 with AlphanumericValue

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

the class ReportDao method reportInstanceDataSQL.

public void reportInstanceDataSQL(int instanceId, final ExportDataStreamHandler handler) {
    // Retrieve point information.
    List<ExportPointInfo> pointInfos = query(REPORT_INSTANCE_POINT_SELECT + "where reportInstanceId=?", new Object[] { instanceId }, new RowMapper<ExportPointInfo>() {

        @Override
        public ExportPointInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
            int i = 0;
            ExportPointInfo rp = new ExportPointInfo();
            rp.setReportPointId(rs.getInt(++i));
            rp.setDeviceName(rs.getString(++i));
            rp.setPointName(rs.getString(++i));
            rp.setXid(rs.getString(++i));
            rp.setDataType(rs.getInt(++i));
            String startValue = rs.getString(++i);
            if (startValue != null)
                rp.setStartValue(DataValue.stringToValue(startValue, rp.getDataType()));
            rp.setTextRenderer((TextRenderer) SerializationHelper.readObjectInContext(rs.getBlob(++i).getBinaryStream()));
            rp.setColour(rs.getString(++i));
            rp.setWeight(rs.getFloat(++i));
            rp.setConsolidatedChart(charToBool(rs.getString(++i)));
            rp.setPlotType(rs.getInt(++i));
            return rp;
        }
    });
    final ExportDataValue edv = new ExportDataValue();
    for (final ExportPointInfo point : pointInfos) {
        if (point.getDataType() == DataTypes.IMAGE) {
            DataPointVO vo = DataPointDao.instance.getByXid(point.getXid());
            if (vo != null)
                point.setDataPointId(vo.getId());
            else
                point.setDataPointId(-1);
        }
        handler.startPoint(point);
        edv.setReportPointId(point.getReportPointId());
        final int dataType = point.getDataType();
        ejt.query(REPORT_INSTANCE_DATA_SELECT + "where rd.reportInstancePointId=? order by rd.ts", new Object[] { point.getReportPointId() }, new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                switch(dataType) {
                    case (DataTypes.NUMERIC):
                        edv.setValue(new NumericValue(rs.getDouble(1)));
                        break;
                    case (DataTypes.BINARY):
                        edv.setValue(new BinaryValue(rs.getDouble(1) == 1));
                        break;
                    case (DataTypes.MULTISTATE):
                        edv.setValue(new MultistateValue(rs.getInt(1)));
                        break;
                    case (DataTypes.ALPHANUMERIC):
                        edv.setValue(new AlphanumericValue(rs.getString(2)));
                        if (rs.wasNull())
                            edv.setValue(new AlphanumericValue(rs.getString(3)));
                        break;
                    case (DataTypes.IMAGE):
                        edv.setValue(new ImageValue(Integer.parseInt(rs.getString(2)), rs.getInt(1)));
                        break;
                    default:
                        edv.setValue(null);
                }
                edv.setTime(rs.getLong(4));
                edv.setAnnotation(BaseDao.readTranslatableMessage(rs, 5));
                handler.pointData(edv);
            }
        });
    }
    handler.done();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) SQLException(java.sql.SQLException) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) ExportPointInfo(com.serotonin.m2m2.vo.export.ExportPointInfo) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) TextRenderer(com.serotonin.m2m2.view.text.TextRenderer)

Example 5 with AlphanumericValue

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

the class PointValueTimeCsvStreamCallback method row.

/* (non-Javadoc)
	 * @see com.serotonin.db.MappedRowCallback#row(java.lang.Object, int)
	 */
@Override
public void row(PointValueTime pvt, int index) {
    if (this.limiter.limited())
        return;
    try {
        String annotation = null;
        if (pvt.isAnnotated())
            annotation = ((AnnotatedPointValueTime) pvt).getAnnotation(translations);
        if (useRendered) {
            // Convert to Alphanumeric Value
            String textValue = Functions.getRenderedText(vo, pvt);
            this.writePointValueTime(new AlphanumericValue(textValue), pvt.getTime(), annotation, vo);
        } else if (unitConversion) {
            if (pvt.getValue() instanceof NumericValue)
                this.writePointValueTime(vo.getUnit().getConverterTo(vo.getRenderedUnit()).convert(pvt.getValue().getDoubleValue()), pvt.getTime(), annotation, vo);
            else
                this.writePointValueTime(pvt.getValue(), pvt.getTime(), annotation, vo);
        } else {
            if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
                this.writePointValueTime(imageServletBuilder.buildAndExpand(pvt.getTime(), vo.getId()).toUri().toString(), pvt.getTime(), annotation, vo);
            else
                this.writePointValueTime(pvt.getValue(), pvt.getTime(), annotation, vo);
        }
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) IOException(java.io.IOException) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Aggregations

AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)14 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)11 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)7 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)7 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)6 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)5 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)4 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)3 IOException (java.io.IOException)3 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 ExportDataValue (com.serotonin.m2m2.vo.export.ExportDataValue)2 InvalidArgumentException (com.serotonin.InvalidArgumentException)1 ImageSaveException (com.serotonin.m2m2.ImageSaveException)1 EnhancedPointValueDao (com.serotonin.m2m2.db.dao.EnhancedPointValueDao)1 TextRenderer (com.serotonin.m2m2.view.text.TextRenderer)1 ChangeTypeRT (com.serotonin.m2m2.virtual.rt.ChangeTypeRT)1 VirtualPointLocatorRT (com.serotonin.m2m2.virtual.rt.VirtualPointLocatorRT)1 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)1