use of com.serotonin.m2m2.rt.dataImage.PointValueFacade in project ma-core-public by infiniteautomation.
the class DataPointDwr method getMostRecentValue.
/**
* Helper to get the most recent value for a point
*
* @param id
* @return
*/
@DwrPermission(user = true)
public ProcessResult getMostRecentValue(int id) {
ProcessResult result = new ProcessResult();
if (Common.runtimeManager.isDataPointRunning(id)) {
DataPointRT rt = Common.runtimeManager.getDataPoint(id);
// Check to see if the data source is running
if (Common.runtimeManager.isDataSourceRunning(rt.getDataSourceId())) {
PointValueFacade facade = new PointValueFacade(rt.getVO().getId());
PointValueTime value = facade.getPointValue();
if (value != null) {
RenderedPointValueTime rpvt = new RenderedPointValueTime();
rpvt.setValue(Functions.getHtmlText(rt.getVO(), value));
rpvt.setTime(Functions.getTime(value));
// Could return time and value?
result.getData().put("pointValue", rpvt.getValue());
} else
result.getData().put("pointValue", translate("event.setPoint.activePointValue"));
} else {
result.getData().put("pointValue", translate("common.pointWarning"));
}
} else {
result.getData().put("pointValue", translate("common.pointWarning"));
}
return result;
}
use of com.serotonin.m2m2.rt.dataImage.PointValueFacade in project ma-core-public by infiniteautomation.
the class ImageChartServlet method getImageData.
private byte[] getImageData(String imageInfo, HttpServletRequest request) throws IOException {
// Hex colour definitions need to be prefixed with '0x' instead of '#'.
try {
// Remove the / and the .png
imageInfo = imageInfo.substring(1, imageInfo.length() - 4);
// Split by underscore.
String[] imageBits = imageInfo.split("_");
// Get the data.
long from, to;
int pointIdStart;
if (imageBits[0].equals("ft")) {
from = Long.parseLong(imageBits[2]);
to = Long.parseLong(imageBits[3]);
pointIdStart = 4;
} else {
from = Common.timer.currentTimeMillis() - Long.parseLong(imageBits[1]);
to = -1;
pointIdStart = 2;
}
int width = getIntRequestParameter(request, "w", 200);
int height = getIntRequestParameter(request, "h", 100);
String useCacheString = request.getParameter("useCache");
boolean useCache = false;
if (useCacheString != null && Boolean.valueOf(useCacheString))
useCache = true;
TimeZone timeZone = Common.getUserTimeZone(Common.getUser(request));
// Create the datasets
DataPointVO markerPoint = null;
int pointCount = 0;
PointTimeSeriesCollection ptsc = new PointTimeSeriesCollection(timeZone);
for (int i = pointIdStart; i < imageBits.length; i++) {
if (imageBits[i].startsWith("w"))
width = NumberUtils.toInt(imageBits[i].substring(1), width);
else if (imageBits[i].startsWith("h"))
height = NumberUtils.toInt(imageBits[i].substring(1), height);
else {
String dataPointStr = imageBits[i];
Color colour = null;
int dataPointId;
int pipe = dataPointStr.indexOf('|');
if (pipe == -1)
dataPointId = Integer.parseInt(dataPointStr);
else {
try {
String colourStr = dataPointStr.substring(pipe + 1);
if (colourStr.startsWith("0x"))
colourStr = "#" + colourStr.substring(2);
colour = ColorUtils.toColor(colourStr);
} catch (InvalidArgumentException e) {
throw new IOException(e);
}
dataPointId = Integer.parseInt(dataPointStr.substring(0, pipe));
}
// Get the data.
DataPointVO dp = DataPointDao.instance.getDataPoint(dataPointId);
if (dp != null && dp.getName() != null) {
pointCount++;
markerPoint = dp;
// Get the Color if there wasn't one provided
if (colour == null) {
try {
if (dp.getChartColour() != null)
colour = ColorUtils.toColor(dp.getChartColour());
} catch (InvalidArgumentException e) {
// Munch it
}
}
PointValueFacade pointValueFacade = new PointValueFacade(dataPointId, useCache);
List<PointValueTime> data;
if (from == -1 && to == -1)
data = pointValueFacade.getPointValuesBetween(0, Common.timer.currentTimeMillis(), true, true);
else if (from == -1)
data = pointValueFacade.getPointValuesBetween(0, to, true, true);
else if (to == -1)
data = pointValueFacade.getPointValuesBetween(from, Common.timer.currentTimeMillis(), true, true);
else
data = pointValueFacade.getPointValuesBetween(from, to, true, true);
if (dp.getPointLocator().getDataTypeId() == DataTypes.NUMERIC) {
TimeSeries ts;
if (dp.isUseRenderedUnit()) {
// This works because we enforce that all Units default to the ONE Unit if not used
UnitConverter converter = null;
if (dp.getRenderedUnit() != dp.getUnit())
converter = dp.getUnit().getConverterTo(dp.getRenderedUnit());
ts = new TimeSeries(dp.getExtendedName(), null, dp.getTextRenderer().getMetaText());
double value;
for (PointValueTime pv : data) {
if (pv.getValue() != null) {
if (converter != null)
value = converter.convert(pv.getDoubleValue());
else
value = pv.getDoubleValue();
ImageChartUtils.addMillisecond(ts, pv.getTime(), value);
}
}
} else {
// No renderer, don't need it
ts = new TimeSeries(dp.getExtendedName(), null, dp.getTextRenderer().getMetaText());
for (PointValueTime pv : data) {
if (pv.getValue() != null)
ImageChartUtils.addMillisecond(ts, pv.getTime(), pv.getValue().numberValue());
}
}
ptsc.addNumericTimeSeries(new NumericTimeSeries(dp.getPlotType(), ts, colour, null));
} else {
DiscreteTimeSeries ts = new DiscreteTimeSeries(dp.getExtendedName(), dp.getTextRenderer(), colour, null);
for (PointValueTime pv : data) if (pv.getValue() != null)
ts.addValueTime(pv);
ptsc.addDiscreteTimeSeries(ts);
}
}
}
}
if (pointCount == 1) {
// Only one point. Check for limits to draw as markers.
UnitConverter uc;
if (markerPoint.getUnit() != null && markerPoint.getRenderedUnit() != null)
uc = markerPoint.getUnit().getConverterTo(markerPoint.getRenderedUnit());
else
uc = Unit.ONE.getConverterTo(Unit.ONE);
for (AbstractPointEventDetectorVO<?> ped : markerPoint.getEventDetectors()) {
if (ped.getDefinition().getEventDetectorTypeName().equals(AnalogLowLimitEventDetectorDefinition.TYPE_NAME))
ptsc.addRangeMarker(new ValueMarker(uc.convert(((AnalogLowLimitDetectorVO) ped).getLimit()), lowLimitPaint, limitStroke));
else if (ped.getDefinition().getEventDetectorTypeName().equals(AnalogHighLimitEventDetectorDefinition.TYPE_NAME))
ptsc.addRangeMarker(new ValueMarker(uc.convert(((AnalogHighLimitDetectorVO) ped).getLimit()), highLimitPaint, limitStroke));
}
}
return ImageChartUtils.getChartData(ptsc, width, height, from, to);
} catch (StringIndexOutOfBoundsException e) {
// no op
} catch (NumberFormatException e) {
// no op
} catch (ArrayIndexOutOfBoundsException e) {
// no op
}
return null;
}
use of com.serotonin.m2m2.rt.dataImage.PointValueFacade in project ma-core-public by infiniteautomation.
the class ImageValueServlet method doGet.
/**
* @TODO(security): Validate the point access against the user. If anonymous, make sure the view allows public
* access to the point.
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String imageInfo = request.getPathInfo();
try {
// Remove the / and the extension
int dot = imageInfo.indexOf('.');
String extension = imageInfo.substring(dot + 1, imageInfo.length()).toLowerCase();
imageInfo = imageInfo.substring(1, dot);
// Split by underscore.
String[] imageBits = imageInfo.split("_");
// Get the data.
String timestamp = imageBits[0];
int dataPointId = Integer.parseInt(imageBits[1]);
int scalePercent = getIntRequestParameter(request, "p", -1);
int width = getIntRequestParameter(request, "w", -1);
int height = getIntRequestParameter(request, "h", -1);
// DataPointRT dp = Common.ctx.getRuntimeManager().getDataPoint(dataPointId);
// Permissions.ensureDataPointReadPermission(Common.getUser(request), dp.getVO());
PointValueFacade pointValueFacade = new PointValueFacade(dataPointId);
PointValueTime pvt = null;
if (timestamp.startsWith(historyPrefix)) {
// Find the point with the given timestamp
long time = Long.parseLong(timestamp.substring(historyPrefix.length()));
pvt = pointValueFacade.getPointValueAt(time);
} else
// Use the latest value
pvt = pointValueFacade.getPointValue();
if (pvt == null || pvt.getValue() == null || !(pvt.getValue() instanceof ImageValue)) {
LOG.warn("Invalid pvt: " + pvt);
response.sendError(HttpStatus.SC_NOT_FOUND);
} else {
ImageValue imageValue = (ImageValue) pvt.getValue();
byte[] data = imageValue.getImageData();
if (scalePercent != -1) {
// Definitely going to be JPEG
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
// Scale the image
PercentScaledImage scaler = new PercentScaledImage(((float) scalePercent) / 100);
data = ImageUtils.scaleImage(scaler, data, new JpegImageFormat(0.85f));
} else if (width != -1 && height != -1) {
// Definitely going to be JPEG
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
// Scale the image
BoxScaledImage scaler = new BoxScaledImage(width, height);
data = ImageUtils.scaleImage(scaler, data, new JpegImageFormat(0.85f));
} else {
// Use the Image extension to se the Content Type
if ("jpg".equals(extension))
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
else if ("png".equals(extension))
response.setContentType(MediaType.IMAGE_PNG_VALUE);
else if ("gif".equals(extension))
response.setContentType(MediaType.IMAGE_GIF_VALUE);
}
response.getOutputStream().write(data);
}
} catch (FileNotFoundException e) {
LOG.warn("", e);
} catch (InterruptedException e) {
LOG.warn("", e);
} catch (StringIndexOutOfBoundsException e) {
LOG.warn("", e);
} catch (NumberFormatException e) {
LOG.warn("", e);
} catch (ArrayIndexOutOfBoundsException e) {
LOG.warn("", e);
} catch (IllegalArgumentException e) {
LOG.warn("", e);
}
}
Aggregations