use of com.serotonin.m2m2.rt.dataImage.types.ImageValue in project ma-modules-public by infiniteautomation.
the class PointValueRestController method getLatestPointValues.
/**
* Get the latest point values for a point
*
* @param xid
* @param limit
* @return
*/
@ApiOperation(value = "Get Latest Point Values Directly from the Runtime Manager, this makes Cached and Intra-Interval data available.", notes = "Default limit 100, time descending order, Default to return cached data. For the most efficient use of this endpoint " + " the data point's default cache size should be the size that you will typically query the latest values of.")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}/latest", produces = { "application/json", "text/csv" })
public ResponseEntity<List<RecentPointValueTimeModel>> getLatestPointValues(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 = "Limit results", allowMultiple = false, defaultValue = "100") @RequestParam(value = "limit", defaultValue = "100") int limit, @ApiParam(value = "Return cached data?", allowMultiple = false, defaultValue = "true") @RequestParam(value = "useCache", defaultValue = "true") boolean useCache) {
RestProcessResult<List<RecentPointValueTimeModel>> result = new RestProcessResult<List<RecentPointValueTimeModel>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
DataPointVO vo = DataPointDao.instance.getByXid(xid);
if (vo == null) {
result.addRestMessage(getDoesNotExistMessage());
return result.createResponseEntity();
}
try {
if (Permissions.hasDataPointReadPermission(user, vo)) {
// Check to see if we can convert (Must be a Numeric Value)
if (unitConversion && (vo.getPointLocator().getDataTypeId() != DataTypes.NUMERIC)) {
result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "Can't convert non-numeric types."));
return result.createResponseEntity();
}
// If we are an image type we should build the URLS
UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/hst{ts}_{id}.jpg");
List<RecentPointValueTimeModel> models;
if (useCache) {
// In an effort not to expand the PointValueCache we avoid the
// PointValueFacade
DataPointRT rt = Common.runtimeManager.getDataPoint(vo.getId());
if (rt != null) {
List<PointValueTime> cache = rt.getCacheCopy();
if (limit < cache.size()) {
List<PointValueTime> pvts = cache.subList(0, limit);
models = new ArrayList<>(limit);
for (PointValueTime pvt : pvts) models.add(createRecentPointValueTimeModel(vo, pvt, imageServletBuilder, useRendered, unitConversion, true));
} else {
// We need to merge 2 lists
List<PointValueTime> disk = Common.databaseProxy.newPointValueDao().getLatestPointValues(vo.getId(), limit);
Set<RecentPointValueTimeModel> all = new HashSet<RecentPointValueTimeModel>(limit);
for (PointValueTime pvt : cache) all.add(createRecentPointValueTimeModel(vo, pvt, imageServletBuilder, useRendered, unitConversion, true));
for (PointValueTime pvt : disk) {
if (all.size() >= limit)
break;
else
all.add(createRecentPointValueTimeModel(vo, pvt, imageServletBuilder, useRendered, unitConversion, false));
}
models = new ArrayList<>(all);
// Override the comparison method
Collections.sort(models, new Comparator<RecentPointValueTimeModel>() {
// 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(RecentPointValueTimeModel o1, RecentPointValueTimeModel o2) {
if (o1.getTimestamp() < o2.getTimestamp())
return 1;
if (o1.getTimestamp() > o2.getTimestamp())
return -1;
return 0;
}
});
}
} else {
List<PointValueTime> pvts = Common.databaseProxy.newPointValueDao().getLatestPointValues(vo.getId(), limit);
models = new ArrayList<>(limit);
for (PointValueTime pvt : pvts) models.add(createRecentPointValueTimeModel(vo, pvt, imageServletBuilder, useRendered, unitConversion, false));
}
} else {
models = new ArrayList<>(limit);
List<PointValueTime> pvts = Common.databaseProxy.newPointValueDao().getLatestPointValues(vo.getId(), limit);
for (PointValueTime pvt : pvts) models.add(createRecentPointValueTimeModel(vo, pvt, imageServletBuilder, useRendered, unitConversion, false));
}
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();
}
}
use of com.serotonin.m2m2.rt.dataImage.types.ImageValue in project ma-modules-public by infiniteautomation.
the class RealTimeDataRestController method query.
/**
* Query the User's Real Time Data
* @param request
* @param limit
* @return
*/
@ApiOperation(value = "Query realtime values", notes = "Check the status member to ensure the point is OK not DISABLED or UNRELIABLE")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<List<RealTimeModel>> query(HttpServletRequest request) {
RestProcessResult<List<RealTimeModel>> result = new RestProcessResult<List<RealTimeModel>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
ASTNode model;
try {
model = parseRQLtoAST(request.getQueryString());
if (model == null) {
result.addRestMessage(new RestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "Query Required")));
return result.createResponseEntity();
}
List<RealTimeDataPointValue> values = RealTimeDataPointValueCache.instance.getUserView(user);
values = model.accept(new RQLToObjectListQuery<RealTimeDataPointValue>(), values);
List<RealTimeModel> models = new ArrayList<RealTimeModel>();
UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/{ts}_{id}.jpg");
for (RealTimeDataPointValue value : values) {
if (value.getDataTypeId() == DataTypes.IMAGE) {
models.add(new RealTimeModel(value, imageServletBuilder.buildAndExpand(value.getTimestamp(), value.getDataPointId()).toUri()));
} else {
models.add(new RealTimeModel(value, value.getValue()));
}
}
return result.createResponseEntity(models);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
return result.createResponseEntity();
}
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.rt.dataImage.types.ImageValue in project ma-modules-public by infiniteautomation.
the class RealTimeDataRestController method get.
@ApiOperation(value = "Get realtime value of point based on XID", notes = "Check the status member to ensure the point is OK not DISABLED or UNRELIABLE")
@RequestMapping(method = RequestMethod.GET, value = "/by-xid/{xid}", produces = { "application/json" })
public ResponseEntity<RealTimeModel> get(@PathVariable String xid, HttpServletRequest request) {
RestProcessResult<RealTimeModel> result = new RestProcessResult<RealTimeModel>(HttpStatus.OK);
// Check the user
User user = checkUser(request, result);
// If no messages then go for it
if (result.isOk()) {
List<RealTimeDataPointValue> values = RealTimeDataPointValueCache.instance.getUserView(user);
ASTNode root = new ASTNode("eq", "xid", xid);
values = root.accept(new RQLToObjectListQuery<RealTimeDataPointValue>(), values);
if (values.size() == 0) {
LOG.debug("Attempted access of Real time point that DNE.");
result.addRestMessage(HttpStatus.NOT_FOUND, new TranslatableMessage("common.default", "Point doesn't exist or is not enabled."));
return result.createResponseEntity();
}
RealTimeModel model;
RealTimeDataPointValue value = values.get(0);
if (value.getDataTypeId() != DataTypes.IMAGE)
model = new RealTimeModel(value, value.getValue());
else {
UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/{ts}_{id}.jpg");
model = new RealTimeModel(value, imageServletBuilder.buildAndExpand(value.getTimestamp(), value.getDataPointId()).toUri());
}
return result.createResponseEntity(model);
} else {
return result.createResponseEntity();
}
}
use of com.serotonin.m2m2.rt.dataImage.types.ImageValue in project ma-modules-public by infiniteautomation.
the class RealTimeDataRestController method getAll.
/**
* Get all of the Users Real Time Data
* @param request
* @param limit
* @return
*/
@ApiOperation(value = "List realtime values", notes = "Check the status member to ensure the point is OK not DISABLED or UNRELIABLE")
@RequestMapping(method = RequestMethod.GET, value = "/list", produces = { "application/json" })
public ResponseEntity<List<RealTimeModel>> getAll(HttpServletRequest request, @ApiParam(value = "Limit the number of results", required = false) @RequestParam(value = "limit", required = false, defaultValue = "100") int limit) {
RestProcessResult<List<RealTimeModel>> result = new RestProcessResult<List<RealTimeModel>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
List<RealTimeDataPointValue> values = RealTimeDataPointValueCache.instance.getUserView(user);
ASTNode root = new ASTNode("limit", limit);
values = root.accept(new RQLToObjectListQuery<RealTimeDataPointValue>(), values);
List<RealTimeModel> models = new ArrayList<RealTimeModel>();
UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/{ts}_{id}.jpg");
for (RealTimeDataPointValue value : values) {
if (value.getDataTypeId() == DataTypes.IMAGE) {
models.add(new RealTimeModel(value, imageServletBuilder.buildAndExpand(value.getTimestamp(), value.getDataPointId()).toUri()));
} else {
models.add(new RealTimeModel(value, value.getValue()));
}
}
return result.createResponseEntity(models);
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.rt.dataImage.types.ImageValue in project ma-modules-public by infiniteautomation.
the class ReportPointValueTimeSerializer method getObject.
/* (non-Javadoc)
* @see com.serotonin.m2m2.db.dao.nosql.NoSQLDataSerializer#getObject(byte[], long)
*/
@Override
public ITime getObject(ByteArrayBuilder b, long ts, String seriesId) {
// Get the data type
int dataType = b.getShort();
DataValue dataValue = null;
// Second put in the data value
switch(dataType) {
case DataTypes.ALPHANUMERIC:
String s = b.getString();
dataValue = new AlphanumericValue(s);
break;
case DataTypes.BINARY:
boolean bool = b.getBoolean();
dataValue = new BinaryValue(bool);
break;
case DataTypes.IMAGE:
try {
dataValue = new ImageValue(b.getString());
} catch (InvalidArgumentException e1) {
// Probably no file
}
break;
case DataTypes.MULTISTATE:
int i = b.getInt();
dataValue = new MultistateValue(i);
break;
case DataTypes.NUMERIC:
double d = b.getDouble();
dataValue = new NumericValue(d);
break;
default:
throw new ShouldNeverHappenException("Data type of " + dataType + " is not supported");
}
// Get the annotation
String annotation = b.getString();
if (annotation != null) {
try {
return new AnnotatedPointValueTime(dataValue, ts, TranslatableMessage.deserialize(annotation));
} catch (Exception e) {
throw new ShouldNeverHappenException(e);
}
} else {
return new PointValueTime(dataValue, ts);
}
}
Aggregations