Skip to content

Commit

Permalink
[sitemap] Extend chart periods to cover past and future
Browse files Browse the repository at this point in the history
Closes openhab/openhab-webui#2518

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed Apr 5, 2024
1 parent 875ebaa commit 7ade1e1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public class RESTConstants {
* Version 4: OH3, refactored extensions to addons (#1560)
* Version 5: transparent charts (#2502)
* Version 6: extended chart period parameter format (#3863)
* Version 7: extended chart period parameter format to cover past and future
*/
public static final String API_VERSION = "6";
public static final String API_VERSION = "7";

public static final CacheControl CACHE_CONTROL = new CacheControl();
static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Chart:
(('icon=' icon=Icon) |
('icon=[' (IconRules+=IconRule (',' IconRules+=IconRule)*) ']') |
('staticIcon=' staticIcon=Icon))? &
('service=' service=STRING)? & ('refresh=' refresh=INT)? & ('period=' period=ID) &
('service=' service=STRING)? & ('refresh=' refresh=INT)? & ('period=' period=Period) &
('legend=' legend=BOOLEAN_OBJECT)? & ('forceasitem=' forceAsItem=BOOLEAN_OBJECT)? &
('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)*) ']')? &
('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)*) ']')? &
Expand Down Expand Up @@ -222,6 +222,9 @@ Icon returns ecore::EString:
IconName:
(ID '-')* ID;

Period:
(ID '-')? ID;

ColorArray:
((conditions+=Condition ('AND' conditions+=Condition)*) '=')? (arg=STRING);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
* @author Chris Jackson - Initial contribution
* @author Holger Reichert - Support for themes, DPI, legend hiding
* @author Laurent Garnier - Extend support to ISO8601 format for chart period parameter
* @author Laurent Garnier - Extend support to past and future for chart period parameter
*/
@Component(immediate = true, service = { ChartServlet.class, Servlet.class }, configurationPid = "org.openhab.chart", //
property = Constants.SERVICE_PID + "=org.openhab.chart")
Expand Down Expand Up @@ -223,8 +224,22 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
return;
}

String periodParamPast = null;
String periodParamFuture = null;
if (periodParam != null) {
int idx = periodParam.indexOf("-");
if (idx < 0) {
periodParamPast = periodParam;
} else {
if (idx > 0) {
periodParamPast = periodParam.substring(0, idx);
}
periodParamFuture = periodParam.substring(idx + 1);
}
}
// Read out the parameter period, begin and end and save them.
TemporalAmount period = convertToTemporalAmount(periodParam, DEFAULT_PERIOD);
TemporalAmount periodPast = convertToTemporalAmount(periodParamPast, DEFAULT_PERIOD);
TemporalAmount periodFuture = convertToTemporalAmount(periodParamFuture, DEFAULT_PERIOD);
ZonedDateTime timeBegin = null;
ZonedDateTime timeEnd = null;

Expand All @@ -250,14 +265,31 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser

// Set begin and end time and check legality.
if (timeBegin == null && timeEnd == null) {
timeEnd = ZonedDateTime.now(timeZoneProvider.getTimeZone());
timeBegin = timeEnd.minus(period);
timeBegin = timeEnd = ZonedDateTime.now(timeZoneProvider.getTimeZone());
if (periodPast != null) {
timeBegin = timeBegin.minus(periodPast);
}
if (periodFuture != null) {
timeEnd = timeEnd.plus(periodFuture);
}
logger.debug("No begin or end is specified, use now as end and now-period as begin.");
} else if (timeEnd == null) {
timeEnd = timeBegin.plus(period);
timeEnd = timeBegin;
if (periodPast != null) {
timeEnd = timeEnd.plus(periodPast);
}
if (periodFuture != null) {
timeEnd = timeEnd.plus(periodFuture);
}
logger.debug("No end is specified, use begin + period as end.");
} else if (timeBegin == null) {
timeBegin = timeEnd.minus(period);
timeBegin = timeEnd;
if (periodFuture != null) {
timeBegin = timeBegin.minus(periodFuture);
}
if (periodPast != null) {
timeBegin = timeBegin.minus(periodPast);
}
logger.debug("No begin is specified, use end-period as begin");
} else if (timeEnd.isBefore(timeBegin)) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "The end is before the begin.");
Expand Down Expand Up @@ -308,9 +340,9 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser

logger.debug("chart building with width {} height {} dpi {}", width, height, dpi);
try {
BufferedImage chart = provider.createChart(serviceName, req.getParameter("theme"), timeBegin, timeEnd,
height, width, req.getParameter("items"), req.getParameter("groups"), dpi, yAxisDecimalPattern,
legend);
BufferedImage chart = provider.createChart(serviceName, req.getParameter("theme"),
Objects.requireNonNull(timeBegin), timeEnd, height, width, req.getParameter("items"),
req.getParameter("groups"), dpi, yAxisDecimalPattern, legend);
// Set the content type to that provided by the chart provider
res.setContentType("image/" + provider.getChartType());
try (ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(res.getOutputStream())) {
Expand Down

0 comments on commit 7ade1e1

Please sign in to comment.