use of com.serotonin.m2m2.rt.dataImage.PointValueTime 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.PointValueTime in project ma-core-public by infiniteautomation.
the class StateChangeCountDetectorRT method scheduleTimeoutImpl.
@Override
public void scheduleTimeoutImpl(long fireTime) {
synchronized (pointValues) {
// This call was scheduled to occur at the eventInactiveTime.
// Strictly speaking, the fact this method was called implies that the detector is going from active to
// inactive. However, it really doesn't hurt to do a bit of cleanup and checking, so what the heck...
removeOldPointValues(fireTime);
if (pointValues.size() >= vo.getChangeCount()) {
// Something has gone wrong.
StringBuilder sb = new StringBuilder();
sb.append("I was supposed to go inactive, but there are still too many state changes in my list: ");
sb.append("fireTime=").append(fireTime);
sb.append(", list=[");
for (PointValueTime pvt : pointValues) sb.append(pvt.getTime()).append(", ");
sb.append("], durationMS=").append(getDurationMS());
sb.append(", changeCount=").append(vo.getChangeCount());
log.error(sb.toString(), new Exception());
}
}
// Deactive the event.
eventActive = false;
returnToNormal(fireTime);
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class AnalogChangeDetectorRT method pruneValueList.
private void pruneValueList(long time) {
long cutoff = time - durationMillis;
boolean recomputeMinimum = false, recomputeMaximum = false;
if (dirty) {
Collections.sort(periodValues);
latestTime = periodValues.get(periodValues.size() - 1).getTime();
dirty = false;
}
while (periodValues.size() > 1) {
PointValueTime pvt1 = periodValues.get(0);
PointValueTime pvt2 = periodValues.get(1);
if (pvt2.getTime() <= cutoff) {
if (pvt1.getDoubleValue() >= max)
recomputeMaximum = true;
if (pvt1.getDoubleValue() <= min)
recomputeMinimum = true;
periodValues.remove(0);
} else {
break;
}
}
recomputeMaximum |= periodValues.size() <= 1;
recomputeMinimum |= periodValues.size() <= 1;
if (recomputeMaximum || recomputeMinimum) {
double newMax = Double.NEGATIVE_INFINITY;
double newMin = Double.POSITIVE_INFINITY;
Iterator<PointValueTime> iter = periodValues.iterator();
while (iter.hasNext()) {
PointValueTime pvt = iter.next();
if (pvt.getDoubleValue() > newMax)
newMax = pvt.getDoubleValue();
if (pvt.getDoubleValue() < newMin)
newMin = pvt.getDoubleValue();
}
if (recomputeMaximum)
max = newMax;
if (recomputeMinimum)
min = newMin;
}
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-modules-public by infiniteautomation.
the class EnvCanDataSourceRT method doPollImpl.
private void doPollImpl(long runtime) {
DateTime dt = new DateTime(nextValueTime);
StringBuilder url = new StringBuilder();
url.append("http://climate.weather.gc.ca/climate_data/bulk_data_e.html?stationID=").append(vo.getStationId());
url.append("&Year=").append(dt.getYear());
url.append("&Month=").append(dt.getMonthOfYear() + 1);
url.append("&format=xml&timeframe=1");
String data;
try {
data = getData(url.toString(), 30, 2);
} catch (Exception e) {
TranslatableMessage lm;
if (e instanceof TranslatableException)
lm = ((TranslatableException) e).getTranslatableMessage();
else
lm = new TranslatableMessage("envcands.retrievalError", e.getMessage());
raiseEvent(DATA_RETRIEVAL_FAILURE_EVENT, runtime, true, lm);
return;
}
// If we made it this far, everything is good.
returnToNormal(DATA_RETRIEVAL_FAILURE_EVENT, runtime);
try {
Document xml = XmlUtilsTS.parse(data);
Element climateDataElement = xml.getDocumentElement();
for (Element stationDataElement : XmlUtilsTS.getChildElements(climateDataElement, "stationdata")) {
int year = XmlUtilsTS.getIntAttribute(stationDataElement, "year", -1);
int month = XmlUtilsTS.getIntAttribute(stationDataElement, "month", -1);
int day = XmlUtilsTS.getIntAttribute(stationDataElement, "day", -1);
int hour = XmlUtilsTS.getIntAttribute(stationDataElement, "hour", -1);
long time = new DateTime(year, month, day, hour, 0, 0, 0, DateTimeZone.UTC).getMillis();
time -= tzOffset;
double temp = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "temp", Double.NaN);
double dptemp = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "dptemp", Double.NaN);
double visibility = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "visibility", Double.NaN);
double relhum = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "relhum", Double.NaN);
double winddir = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "winddir", Double.NaN);
double windspd = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "windspd", Double.NaN);
double stnpress = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "stnpress", Double.NaN);
double humidex = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "humidex", Double.NaN);
double windchill = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "windchill", Double.NaN);
String weather = XmlUtilsTS.getChildElementText(stationDataElement, "weather");
if (Double.isNaN(temp))
// If there is no temp value, ignore the record
continue;
// Update the next value time.
nextValueTime = time + 1000 * 60 * 60;
for (DataPointRT dp : dataPoints) {
PointValueTime pvt = dp.getPointValue();
if (pvt != null && pvt.getTime() >= time)
// This value is in the past. Ignore it.
continue;
EnvCanPointLocatorVO plvo = dp.getVO().getPointLocator();
if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.TEMP && !Double.isNaN(temp))
pvt = new PointValueTime(temp, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.DEW_POINT_TEMP && !Double.isNaN(dptemp))
pvt = new PointValueTime(dptemp, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.REL_HUM && !Double.isNaN(relhum))
pvt = new PointValueTime(relhum, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.WIND_DIR && !Double.isNaN(winddir))
pvt = new PointValueTime(winddir, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.WIND_SPEED && !Double.isNaN(windspd))
pvt = new PointValueTime(windspd, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.VISIBILITY && !Double.isNaN(visibility))
pvt = new PointValueTime(visibility, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.STN_PRESS && !Double.isNaN(stnpress))
pvt = new PointValueTime(stnpress, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.HUMIDEX && !Double.isNaN(humidex))
pvt = new PointValueTime(humidex, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.WIND_CHILL && !Double.isNaN(windchill))
pvt = new PointValueTime(windchill, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.WEATHER && weather != null)
pvt = new PointValueTime(weather, time);
else
continue;
dp.updatePointValue(pvt);
}
}
returnToNormal(PARSE_EXCEPTION_EVENT, runtime);
} catch (SAXException e) {
raiseEvent(PARSE_EXCEPTION_EVENT, runtime, true, DataSourceRT.getExceptionMessage(e));
}
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-modules-public by infiniteautomation.
the class EnvCanDataSourceRT method doPoll.
@Override
protected void doPoll(long time) {
if (nextValueTime == -1) {
// Determine when we should start from
nextValueTime = System.currentTimeMillis();
for (DataPointRT dp : dataPoints) {
PointValueTime pvt = dp.getPointValue();
if (pvt == null) {
nextValueTime = 0;
break;
}
if (nextValueTime > pvt.getTime())
nextValueTime = pvt.getTime();
}
if (nextValueTime == 0)
nextValueTime = vo.getDataStartTime();
else
nextValueTime += 1000 * 60 * 60;
}
long previousValueTime = nextValueTime;
doPollImpl(time);
if (nextValueTime == previousValueTime)
raiseEvent(NO_DATA_RETRIEVED_EVENT, time, true, new TranslatableMessage("envcands.event.noTemperatureData"));
else
returnToNormal(NO_DATA_RETRIEVED_EVENT, time);
while (nextValueTime != previousValueTime) {
// Something was changed.
DateTime prev = new DateTime(previousValueTime);
DateTime now = new DateTime(System.currentTimeMillis());
if (prev.getYear() < now.getYear() || prev.getMonthOfYear() < now.getMonthOfYear()) {
previousValueTime = nextValueTime;
doPollImpl(time);
} else
break;
}
}
Aggregations