use of com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime in project ma-modules-public by infiniteautomation.
the class PointValueImportResult method saveValue.
public void saveValue(XidPointValueTimeModel model) {
if (valid) {
// Validate the model against our point
long timestamp = model.getTimestamp();
if (timestamp == 0)
timestamp = Common.timer.currentTimeMillis();
int dataTypeId = DataTypeEnum.convertFrom(model.getType());
if (dataTypeId != vo.getPointLocator().getDataTypeId()) {
result.addContextualMessage("dataType", "event.ds.dataType");
return;
}
DataValue value;
switch(model.getType()) {
case ALPHANUMERIC:
value = new AlphanumericValue((String) model.getValue());
break;
case BINARY:
value = new BinaryValue((Boolean) model.getValue());
break;
case MULTISTATE:
if (model.getValue() instanceof String) {
try {
value = vo.getTextRenderer().parseText((String) model.getValue(), dataTypeId);
} catch (Exception e) {
// Lots can go wrong here so let the user know
result.addContextualMessage("value", "event.valueParse.textParse", e.getMessage());
return;
}
} else {
value = new MultistateValue(((Number) model.getValue()).intValue());
}
break;
case NUMERIC:
value = new NumericValue(((Number) model.getValue()).doubleValue());
break;
case IMAGE:
default:
result.addContextualMessage("dataType", "common.default", model.getType() + " data type not supported yet");
return;
}
PointValueTime pvt;
if (model.getAnnotation() == null) {
pvt = new PointValueTime(value, timestamp);
} else {
pvt = new AnnotatedPointValueTime(value, timestamp, new TranslatableMessage("common.default", model.getAnnotation()));
}
if (rt == null) {
dao.savePointValueAsync(vo.getId(), pvt, null);
} else {
rt.savePointValueDirectToCache(pvt, null, true, true);
}
total++;
}
}
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++;
}
}
Aggregations