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++;
}
}
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);
}
}
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);
}
}
}
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);
}
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();
}
Aggregations