Search in sources :

Example 6 with NumericValue

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

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

the class IdPointValueTimeCsvStreamCallback method row.

/* (non-Javadoc)
	 * @see com.serotonin.db.MappedRowCallback#row(java.lang.Object, int)
	 */
@Override
public void row(IdPointValueTime pvt, int index) {
    try {
        DataPointVO vo = this.voMap.get(pvt.getId());
        long time = pvt.getTime();
        if (dateFormatter == null)
            this.rowData[0] = Long.toString(time);
        else
            this.rowData[0] = dateFormatter.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(time), TimeZone.getDefault().toZoneId()));
        // Ensure we are saving into the correct time entry
        if (this.currentTime != time) {
            if (!wroteHeaders) {
                this.writer.writeNext(headers);
                this.wroteHeaders = true;
            } else {
                // Write the line
                if (this.limiter.limited())
                    return;
                this.writer.writeNext(rowData);
                for (int i = 0; i < this.rowData.length; i++) this.rowData[i] = new String();
            }
            this.currentTime = time;
        }
        if (useRendered) {
            // Convert to Alphanumeric Value
            this.rowData[this.columnMap.get(vo.getId())] = Functions.getRenderedText(vo, pvt);
        } else if (unitConversion) {
            if (pvt.getValue() instanceof NumericValue)
                this.rowData[this.columnMap.get(vo.getId())] = Double.toString(vo.getUnit().getConverterTo(vo.getRenderedUnit()).convert(pvt.getValue().getDoubleValue()));
            else
                this.rowData[this.columnMap.get(vo.getId())] = this.createDataValueString(pvt.getValue());
        } else {
            if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
                this.rowData[this.columnMap.get(vo.getId())] = imageServletBuilder.buildAndExpand(pvt.getTime(), vo.getId()).toUri().toString();
            else
                this.rowData[this.columnMap.get(vo.getId())] = this.createDataValueString(pvt.getValue());
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) IOException(java.io.IOException)

Example 8 with NumericValue

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

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

the class AnalogAttractorChangeRT method change.

@Override
public DataValue change(DataValue currentValue) {
    double current = currentValue.getDoubleValue();
    // Get the value we're attracted to.
    DataPointRT point = Common.runtimeManager.getDataPoint(vo.getAttractionPointId());
    if (point == null) {
        if (log.isDebugEnabled())
            log.debug("Attraction point is not enabled");
        // Point is not currently active.
        return new NumericValue(current);
    }
    DataValue attractorValue = PointValueTime.getValue(point.getPointValue());
    if (attractorValue == null) {
        if (log.isDebugEnabled())
            log.debug("Attraction point has not vaue");
        return new NumericValue(current);
    }
    double attraction = attractorValue.getDoubleValue();
    // Move half the distance toward the attractor...
    double change = (attraction - current) / 2;
    // ... subject to the maximum change allowed...
    if (change < 0 && -change > vo.getMaxChange())
        change = -vo.getMaxChange();
    else if (change > vo.getMaxChange())
        change = vo.getMaxChange();
    // ... and a random fluctuation.
    change += RANDOM.nextDouble() * vo.getVolatility() * 2 - vo.getVolatility();
    if (log.isDebugEnabled())
        log.debug("attraction=" + attraction + ", change=" + change);
    return new NumericValue(current + change);
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 10 with NumericValue

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

Aggregations

NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)23 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)11 IOException (java.io.IOException)11 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)9 AnalogStatistics (com.infiniteautomation.mango.statistics.AnalogStatistics)8 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)8 NextTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster)7 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)7 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)7 ZonedDateTime (java.time.ZonedDateTime)7 MutableInt (org.apache.commons.lang3.mutable.MutableInt)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)5 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)4 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)4 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)4 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 TextRenderer (com.serotonin.m2m2.view.text.TextRenderer)2