use of com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime in project ma-core-public by infiniteautomation.
the class ChartExportServlet method exportExcel.
/**
* Do the export as Excel XLSX File
* @param response
* @param from
* @param to
* @param def
* @param user
* @throws IOException
*/
private void exportExcel(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("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
final List<PointValueEmporter> sheetEmporters = new ArrayList<PointValueEmporter>();
final AtomicInteger sheetIndex = new AtomicInteger();
sheetEmporters.add(new PointValueEmporter(Common.translate("emport.pointValues") + " " + sheetIndex.get()));
final SpreadsheetEmporter emporter = new SpreadsheetEmporter(FileType.XLSX);
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
emporter.prepareExport(bos);
emporter.prepareSheetExport(sheetEmporters.get(0));
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);
sheetEmporters.get(sheetIndex.get()).exportRow(edv);
if (sheetEmporters.get(sheetIndex.get()).getRowsAdded() >= emporter.getMaxRowsPerSheet()) {
ExportPointInfo info = sheetEmporters.get(sheetIndex.get()).getPointInfo();
sheetIndex.incrementAndGet();
PointValueEmporter sheetEmporter = new PointValueEmporter(Common.translate("emport.pointValues") + " " + sheetIndex.get());
sheetEmporter.setPointInfo(info);
sheetEmporters.add(sheetEmporter);
emporter.prepareSheetExport(sheetEmporters.get(sheetIndex.get()));
}
}
};
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());
sheetEmporters.get(sheetIndex.get()).setPointInfo(pointInfo);
pointValueDao.getPointValuesBetween(pointId, from, to, callback);
}
}
emporter.finishExport();
}
use of com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime in project ma-core-public by infiniteautomation.
the class MockPointValueDao method savePointValueSync.
/* (non-Javadoc)
* @see com.serotonin.m2m2.db.dao.PointValueDao#savePointValueSync(int, com.serotonin.m2m2.rt.dataImage.PointValueTime, com.serotonin.m2m2.rt.dataImage.SetPointSource)
*/
@Override
public PointValueTime savePointValueSync(int pointId, PointValueTime pointValue, SetPointSource source) {
List<PointValueTime> pvts = data.get(pointId);
if (pvts == null) {
pvts = new ArrayList<>();
data.put(pointId, pvts);
}
PointValueTime newPvt = null;
if (source != null)
newPvt = new AnnotatedPointValueTime(pointValue.getValue(), pointValue.getTime(), source.getSetPointSourceMessage());
else
newPvt = new PointValueTime(pointValue.getValue(), pointValue.getTime());
pvts.add(newPvt);
// Keep in time order?
Collections.sort(pvts);
return newPvt;
}
use of com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime in project ma-core-public by infiniteautomation.
the class DataPointRT method savePointValue.
private void savePointValue(PointValueTime newValue, SetPointSource source, boolean async, boolean saveToDatabase) {
// Null values are not very nice, and since they don't have a specific meaning they are hereby ignored.
if (newValue == null)
return;
// Check the data type of the value against that of the locator, just for fun.
int valueDataType = DataTypes.getDataType(newValue.getValue());
if (valueDataType != DataTypes.UNKNOWN && valueDataType != vo.getPointLocator().getDataTypeId())
// to know how it happened, and the stack trace here provides the best information.
throw new ShouldNeverHappenException("Data type mismatch between new value and point locator: newValue=" + DataTypes.getDataType(newValue.getValue()) + ", locator=" + vo.getPointLocator().getDataTypeId());
// Check if this value qualifies for discardation.
if (vo.isDiscardExtremeValues() && DataTypes.getDataType(newValue.getValue()) == DataTypes.NUMERIC) {
double newd = newValue.getDoubleValue();
// Discard if NaN
if (Double.isNaN(newd))
return;
if (newd < vo.getDiscardLowLimit() || newd > vo.getDiscardHighLimit())
// Discard the value
return;
}
if (newValue.getTime() > Common.timer.currentTimeMillis() + SystemSettingsDao.getFutureDateLimit()) {
// Too far future dated. Toss it. But log a message first.
LOG.warn("Future dated value detected: pointId=" + vo.getId() + ", value=" + newValue.getValue().toString() + ", type=" + vo.getPointLocator().getDataTypeId() + ", ts=" + newValue.getTime(), new Exception());
return;
}
boolean backdated = pointValue != null && newValue.getTime() < pointValue.getTime();
// Determine whether the new value qualifies for logging.
boolean logValue;
// ... or even saving in the cache.
boolean saveValue = true;
switch(vo.getLoggingType()) {
case DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL:
case DataPointVO.LoggingTypes.ON_CHANGE:
if (pointValue == null)
logValue = true;
else if (backdated)
// Backdated. Ignore it
logValue = false;
else {
if (newValue.getValue() instanceof NumericValue) {
// Get the new double
double newd = newValue.getDoubleValue();
// See if the new value is outside of the tolerance.
double diff = toleranceOrigin - newd;
if (diff < 0)
diff = -diff;
if (diff > vo.getTolerance()) {
toleranceOrigin = newd;
logValue = true;
} else
logValue = false;
} else if (newValue.getValue() instanceof ImageValue) {
logValue = !((ImageValue) newValue.getValue()).equalDigests(((ImageValue) pointValue.getValue()).getDigest());
} else
logValue = !Objects.equals(newValue.getValue(), pointValue.getValue());
}
saveValue = logValue;
break;
case DataPointVO.LoggingTypes.ALL:
logValue = true;
break;
case DataPointVO.LoggingTypes.ON_TS_CHANGE:
if (pointValue == null)
logValue = true;
else if (backdated)
// Backdated. Ignore it
logValue = false;
else
logValue = newValue.getTime() != pointValue.getTime();
saveValue = logValue;
break;
case DataPointVO.LoggingTypes.INTERVAL:
if (!backdated)
intervalSave(newValue);
default:
logValue = false;
}
if (!saveToDatabase)
logValue = false;
if (saveValue) {
valueCache.savePointValue(newValue, source, logValue, async);
if (vo.getLoggingType() == DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL)
rescheduleChangeInterval(Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod()));
}
// fetch the annotation
if (source != null) {
newValue = new AnnotatedPointValueTime(newValue.getValue(), newValue.getTime(), source.getSetPointSourceMessage());
}
// Ignore historical values.
if (pointValue == null || newValue.getTime() >= pointValue.getTime()) {
PointValueTime oldValue = pointValue;
pointValue = newValue;
fireEvents(oldValue, newValue, null, source != null, false, logValue, true, false);
} else
fireEvents(null, newValue, null, false, true, logValue, false, false);
}
use of com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime 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.AnnotatedPointValueTime in project ma-modules-public by infiniteautomation.
the class ReportPointValueTimeSerializer method putBytes.
/* (non-Javadoc)
* @see com.serotonin.m2m2.db.dao.nosql.NoSQLDataSerializer#getBytes(com.serotonin.m2m2.db.dao.nosql.NoSQLDataEntry)
*/
@Override
public void putBytes(ByteArrayBuilder b, ITime obj, long timestamp, String seriesId) {
PointValueTime value = (PointValueTime) obj;
// First put in the data type
b.putShort((short) value.getValue().getDataType());
// Second put in the data value
switch(value.getValue().getDataType()) {
case DataTypes.ALPHANUMERIC:
b.putString(value.getStringValue());
break;
case DataTypes.BINARY:
b.putBoolean(value.getBooleanValue());
break;
case DataTypes.IMAGE:
ImageValue imageValue = (ImageValue) value.getValue();
if (!imageValue.isSaved()) {
throw new ImageSaveException(new IOException("Image not saved."));
}
b.putString(imageValue.getFilename());
break;
case DataTypes.MULTISTATE:
b.putInt(value.getIntegerValue());
break;
case DataTypes.NUMERIC:
b.putDouble(value.getDoubleValue());
break;
default:
throw new ShouldNeverHappenException("Data type of " + value.getValue().getDataType() + " is not supported");
}
// Put in annotation
if (value.isAnnotated()) {
AnnotatedPointValueTime apv = (AnnotatedPointValueTime) value;
b.putString(apv.getSourceMessage().serialize());
} else
b.putString(null);
}
Aggregations