use of com.serotonin.m2m2.rt.dataImage.PointValueFacade in project ma-core-public by infiniteautomation.
the class StatisticsChartRenderer method addDataToModel.
@Override
public void addDataToModel(Map<String, Object> model, DataPointVO point) {
long startTime = getStartTime();
long endTime = startTime + getDuration();
PointValueFacade pointValueFacade = new PointValueFacade(point.getId());
List<PointValueTime> values = pointValueFacade.getPointValuesBetween(startTime, endTime);
PointValueTime startVT = null;
if (!values.isEmpty()) {
startVT = pointValueFacade.getPointValueBefore(startTime);
}
// Generate statistics on the values.
int dataTypeId = point.getPointLocator().getDataTypeId();
if (values.size() > 0) {
if (dataTypeId == DataTypes.BINARY || dataTypeId == DataTypes.MULTISTATE) {
// Runtime stats
StartsAndRuntimeList stats = new StartsAndRuntimeList(startTime, endTime, startVT, values);
model.put("start", startVT != null ? startTime : stats.getFirstTime());
model.put("end", endTime);
model.put("startsAndRuntimes", stats.getData());
} else if (dataTypeId == DataTypes.NUMERIC) {
AnalogStatistics stats = new AnalogStatistics(startTime, endTime, startVT, values);
model.put("start", startVT != null ? startTime : stats.getFirstTime());
model.put("end", endTime);
model.put("minimum", stats.getMinimumValue());
model.put("minTime", stats.getMinimumTime());
model.put("maximum", stats.getMaximumValue());
model.put("maxTime", stats.getMaximumTime());
model.put("average", stats.getAverage());
if (includeSum)
model.put("sum", stats.getSum());
model.put("count", stats.getCount());
model.put("noData", stats.getAverage() == null);
model.put("integral", stats.getIntegral());
} else if (dataTypeId == DataTypes.ALPHANUMERIC) {
ValueChangeCounter stats = new ValueChangeCounter(startTime, endTime, startVT, values);
model.put("changeCount", stats.getChanges());
}
}
model.put("logEntries", values.size());
}
use of com.serotonin.m2m2.rt.dataImage.PointValueFacade in project ma-modules-public by infiniteautomation.
the class XidPointValueTimeLatestPointFacadeStream method streamData.
/*
* (non-Javadoc)
* @see com.serotonin.m2m2.web.mvc.rest.v1.model.ObjectStream#streamData(com.fasterxml.jackson.core.JsonGenerator)
*/
@Override
public void streamData(JsonGenerator jgen) {
Iterator<Integer> it = this.pointMap.keySet().iterator();
while (it.hasNext()) {
try {
DataPointVO vo = this.pointMap.get(it.next());
PointValueFacade pointValueFacade = new PointValueFacade(vo.getId(), useCache);
PointValueTimeJsonStreamCallback callback = new PointValueTimeJsonStreamCallback(jgen, vo, useRendered, unitConversion, null, dateTimeFormat, timezone);
jgen.writeArrayFieldStart(vo.getXid());
List<PointValueTime> pvts = pointValueFacade.getLatestPointValues(limit);
for (int i = 0; i < pvts.size(); i++) callback.row(pvts.get(i), i);
jgen.writeEndArray();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
use of com.serotonin.m2m2.rt.dataImage.PointValueFacade in project ma-modules-public by infiniteautomation.
the class IdPointValueTimeLatestPointValueFacadeStream method streamData.
/* (non-Javadoc)
* @see com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeStream#streamData(com.fasterxml.jackson.core.JsonGenerator)
*/
@Override
public void streamData(JsonGenerator jgen) {
IdPointValueTimeJsonStreamCallback callback = new IdPointValueTimeJsonStreamCallback(jgen, pointMap, useRendered, unitConversion, null, dateTimeFormat, timezone);
// Sadly in this scenario we must collect all the data and then order it
List<IdPointValueTime> ipvts = new ArrayList<IdPointValueTime>();
Iterator<Integer> it = this.pointMap.keySet().iterator();
while (it.hasNext()) {
DataPointVO vo = this.pointMap.get(it.next());
PointValueFacade pointValueFacade = new PointValueFacade(vo.getId(), useCache);
List<PointValueTime> pvts = pointValueFacade.getLatestPointValues(limit);
for (PointValueTime pvt : pvts) ipvts.add(new IdPointValueTime(vo.getId(), pvt.getValue(), pvt.getTime()));
}
// Sort it all
Collections.sort(ipvts, new Comparator<IdPointValueTime>() {
// Compare such that data sets are returned in time descending order
// which turns out is opposite of compare to method for PointValueTime objects
@Override
public int compare(IdPointValueTime o1, IdPointValueTime o2) {
if (o1.getTime() < o2.getTime())
return 1;
if (o1.getTime() > o2.getTime())
return -1;
return 0;
}
});
for (int i = 0; i < ipvts.size(); i++) callback.row(ipvts.get(i), i);
callback.finish();
}
use of com.serotonin.m2m2.rt.dataImage.PointValueFacade in project ma-modules-public by infiniteautomation.
the class IdPointValueTimeLatestPointValueFacadeStream method streamData.
/* (non-Javadoc)
* @see com.serotonin.m2m2.web.mvc.rest.v1.model.QueryArrayStream#streamData(com.serotonin.m2m2.web.mvc.rest.v1.csv.CSVPojoWriter)
*/
@Override
public void streamData(CSVPojoWriter<PointValueTimeModel> writer) throws IOException {
IdPointValueTimeCsvStreamCallback callback = new IdPointValueTimeCsvStreamCallback(writer.getWriter(), pointMap, useRendered, unitConversion, null, dateTimeFormat, timezone);
// Sadly in this scenario we must collect all the data and then order it
List<IdPointValueTime> ipvts = new ArrayList<IdPointValueTime>();
Iterator<Integer> it = this.pointMap.keySet().iterator();
while (it.hasNext()) {
DataPointVO vo = this.pointMap.get(it.next());
PointValueFacade pointValueFacade = new PointValueFacade(vo.getId(), useCache);
List<PointValueTime> pvts = pointValueFacade.getLatestPointValues(limit);
for (PointValueTime pvt : pvts) ipvts.add(new IdPointValueTime(vo.getId(), pvt.getValue(), pvt.getTime()));
}
// Sort it all
Collections.sort(ipvts, new Comparator<IdPointValueTime>() {
@Override
public int compare(IdPointValueTime o1, IdPointValueTime o2) {
return Long.compare(o1.getTime(), o2.getTime());
}
});
for (int i = 0; i < ipvts.size(); i++) callback.row(ipvts.get(i), i);
callback.finish();
}
use of com.serotonin.m2m2.rt.dataImage.PointValueFacade in project ma-modules-public by infiniteautomation.
the class PointValueRestController method firstAndLastPointValues.
@ApiOperation(value = "First and last point values", notes = "Retrieves the first and last point values within a time range, used to read accumulators")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}/first-last", produces = { "application/json", "text/csv" })
public ResponseEntity<List<PointValueTimeModel>> firstAndLastPointValues(HttpServletRequest request, @ApiParam(value = "Point xid", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Return rendered value as String", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean useRendered, @ApiParam(value = "Return converted value using displayed unit", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean unitConversion, @ApiParam(value = "From time", required = false, allowMultiple = false) @RequestParam(value = "from", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) DateTime from, @ApiParam(value = "To time", required = false, allowMultiple = false) @RequestParam(value = "to", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) DateTime to, @ApiParam(value = "Time zone of output, used if formatted times are returned", required = false, allowMultiple = false) @RequestParam(value = "timezone", required = false) String timezone) {
RestProcessResult<List<PointValueTimeModel>> result = new RestProcessResult<List<PointValueTimeModel>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
if (timezone != null) {
try {
ZoneId.of(timezone);
} catch (Exception e) {
RestValidationResult vr = new RestValidationResult();
vr.addError("validate.invalidValue", "timezone");
throw new ValidationFailedRestException(vr);
}
}
DataPointVO vo = DataPointDao.instance.getByXid(xid);
if (vo == null) {
result.addRestMessage(getDoesNotExistMessage());
return result.createResponseEntity();
}
try {
if (Permissions.hasDataPointReadPermission(user, vo)) {
long current = Common.timer.currentTimeMillis();
if (from == null)
from = new DateTime(current);
if (to == null)
to = new DateTime(current);
// better not to for RESTfulness
if (timezone != null) {
DateTimeZone zone = DateTimeZone.forID(timezone);
from = from.withZone(zone);
to = to.withZone(zone);
}
PointValueFacade pointValueFacade = new PointValueFacade(vo.getId(), false);
PointValueTime first = pointValueFacade.getPointValueAfter(from.getMillis());
PointValueTime last = pointValueFacade.getPointValueBefore(to.getMillis());
List<PointValueTimeModel> models = new ArrayList<PointValueTimeModel>(2);
if (useRendered) {
if (first != null) {
PointValueTimeModel model = new PointValueTimeModel();
model.setType(DataTypeEnum.convertTo(first.getValue().getDataType()));
model.setValue(Functions.getRenderedText(vo, first));
model.setTimestamp(first.getTime());
if (first.isAnnotated())
model.setAnnotation(((AnnotatedPointValueTime) first).getAnnotation(Common.getTranslations()));
models.add(model);
}
if (last != null) {
PointValueTimeModel model = new PointValueTimeModel();
model.setType(DataTypeEnum.convertTo(last.getValue().getDataType()));
model.setValue(Functions.getRenderedText(vo, last));
model.setTimestamp(last.getTime());
if (last.isAnnotated())
model.setAnnotation(((AnnotatedPointValueTime) last).getAnnotation(Common.getTranslations()));
models.add(model);
}
} else if (unitConversion) {
if (first != null) {
PointValueTimeModel model = new PointValueTimeModel();
model.setType(DataTypeEnum.convertTo(first.getValue().getDataType()));
model.setValue(vo.getUnit().getConverterTo(vo.getRenderedUnit()).convert(first.getValue().getDoubleValue()));
model.setTimestamp(first.getTime());
if (first.isAnnotated())
model.setAnnotation(((AnnotatedPointValueTime) first).getAnnotation(Common.getTranslations()));
models.add(model);
}
if (last != null) {
PointValueTimeModel model = new PointValueTimeModel();
model.setType(DataTypeEnum.convertTo(last.getValue().getDataType()));
model.setValue(vo.getUnit().getConverterTo(vo.getRenderedUnit()).convert(last.getValue().getDoubleValue()));
model.setTimestamp(last.getTime());
if (last.isAnnotated())
model.setAnnotation(((AnnotatedPointValueTime) last).getAnnotation(Common.getTranslations()));
models.add(model);
}
} else {
models.add(first == null ? null : new PointValueTimeModel(first));
models.add(last == null ? null : new PointValueTimeModel(last));
}
if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE) {
// If we are an image type we should build the URLS
UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/hst{ts}_{id}.jpg");
for (PointValueTimeModel model : models) {
model.setValue(imageServletBuilder.buildAndExpand(model.getTimestamp(), vo.getId()).toUri());
}
}
return result.createResponseEntity(models);
} else {
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
} catch (PermissionException e) {
LOG.error(e.getMessage(), e);
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
} else {
return result.createResponseEntity();
}
}
Aggregations