Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Live chart updates

Christian Bauerd25d8fb78039
diff --git a/container/src/main/resources/org/openremote/container/web/jsapi/client.js b/container/src/main/resources/org/openremote/container/web/jsapi/client.js
index 888b1fe3b..35a798027 100644
--- a/container/src/main/resources/org/openremote/container/web/jsapi/client.js
+++ b/container/src/main/resources/org/openremote/container/web/jsapi/client.js
@@ -456,11 +456,9 @@ REST.Encoding.encodePathParamValue = function (val) {
REST.Encoding.encodeValue = function (val, allowed, form) {
if (typeof val != "string") {
- REST.log("val is not a string");
return val;
}
if (val.length == 0) {
- REST.log("empty string");
return val;
}
var ret = '';
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java b/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java
index 534237f3d..782f06d31 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java
@@ -46,6 +46,7 @@ import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
+import static com.google.gwt.dom.client.Style.Unit.EM;
import static com.google.gwt.dom.client.Style.Unit.PX;
public abstract class AttributesBrowser
@@ -77,7 +78,7 @@ public abstract class AttributesBrowser
for (AssetAttribute assetAttribute : attributeGroups.keySet()) {
if (assetAttribute.getReference().equals(event.getAttributeRef())) {
assetAttribute.setValueUnchecked(event.getValue(), event.getTimestamp());
- rebuildEditor(assetAttribute);
+ refreshAttribute(assetAttribute);
break;
}
}
@@ -216,7 +217,9 @@ public abstract class AttributesBrowser
}
}
- protected void rebuildEditor(AssetAttribute attribute) {
+ protected void refreshAttribute(AssetAttribute attribute) {
+
+ // Rebuild editor
FormGroup formGroup = attributeGroups.get(attribute);
if (formGroup == null)
return;
@@ -232,168 +235,195 @@ public abstract class AttributesBrowser
formField.add(editor);
- // "Blink" the editor so users know there is a new value
+ // "Blink" the editor so users know there might be a new value
editor.asWidget().addStyleName(environment.getWidgetStyle().HighlightBackground());
Browser.getWindow().setTimeout(() -> {
editor.asWidget().removeStyleName(environment.getWidgetStyle().HighlightBackground());
}, 250);
+
+ // Refresh data point browser
+ FormGroup attributeFormGroup = attributeGroups.get(attribute);
+ if (attributeFormGroup != null) {
+ DatapointBrowser datapointBrowser = attributeFormGroup.getExtensionOfType(DatapointBrowser.class);
+ if (datapointBrowser != null) {
+ datapointBrowser.updateChart(System.currentTimeMillis());
+ }
+ }
}
- protected Widget createDatapointBrowser(AssetAttribute attribute) {
+ protected DatapointBrowser createDatapointBrowser(AssetAttribute attribute) {
if (!attribute.isStoreDatapoints())
return null;
+ if (!(attribute.getType() == AttributeType.DECIMAL || attribute.getType() == AttributeType.INTEGER))
+ return null;
+ return new DatapointBrowser(attribute);
+ }
- class ChartPanel extends FlowPanel {
-
- Chart chart;
- FormValueListBox<DatapointInterval> intervalListBox;
- long lastUsedTimestamp;
- FormOutputText timeOutput;
- FormButton nextButton;
-
- public ChartPanel() {
- setStyleName("layout vertical");
- addAttachHandler(event -> {
- if (event.isAttached()) {
- createChart();
- } else {
- destroyChart();
- }
- });
- }
-
- void createChart() {
- FormSectionLabel sectionLabel = new FormSectionLabel(environment.getMessages().historicalData());
- add(sectionLabel);
-
- Canvas canvas = Canvas.createIfSupported();
- if (canvas == null) {
- add(new Label(environment.getMessages().canvasNotSupported()));
- return;
- }
+ /* ####################################################################### */
- FlowPanel charFormPanel = new FlowPanel();
- charFormPanel.setStyleName("layout vertical center or-FormGroup");
- add(charFormPanel);
-
- canvas.getCanvasElement().setWidth(900);
- canvas.getCanvasElement().setHeight(250);
- FlowPanel canvasContainer = new FlowPanel();
- canvasContainer.setWidth("900px");
- canvasContainer.setHeight("250px");
- canvasContainer.add(canvas);
-
- charFormPanel.add(canvasContainer);
-
- Form controlForm = new Form();
- controlForm.getElement().getStyle().setWidth(850, PX);
- charFormPanel.add(controlForm);
-
- FormGroup controlFormGroup = new FormGroup();
- controlForm.add(controlFormGroup);
-
- FormLabel controlFormLabel = new FormLabel(environment.getMessages().showChartAggregatedFor());
- controlFormGroup.addFormLabel(controlFormLabel);
-
- FormField controlFormField = new FormField();
- controlFormGroup.addFormField(controlFormField);
- intervalListBox = new FormValueListBox<>(
- new AbstractRenderer<DatapointInterval>() {
- @Override
- public String render(DatapointInterval interval) {
- return environment.getMessages().datapointInterval(interval.name());
- }
- }
- );
- intervalListBox.addValueChangeHandler(event -> {
- updateChart(System.currentTimeMillis());
- });
- intervalListBox.setValue(DatapointInterval.HOUR);
- intervalListBox.setAcceptableValues(Arrays.asList(DatapointInterval.values()));
- intervalListBox.setEnabled(true);
- controlFormField.add(intervalListBox);
+ class DatapointBrowser extends FlowPanel {
- FormGroupActions controlFormActions = new FormGroupActions();
- controlFormGroup.addFormGroupActions(controlFormActions);
+ final AssetAttribute attribute;
+ Chart chart;
+ DatapointInterval interval;
+ long timestamp;
+ FormValueListBox<DatapointInterval> intervalListBox;
+ FormOutputText timeOutput;
+ FormButton nextButton;
- timeOutput = new FormOutputText();
- timeOutput.addStyleName("flex");
- controlFormActions.add(timeOutput);
+ public DatapointBrowser(AssetAttribute attribute) {
+ this.attribute = attribute;
+ this.interval = DatapointInterval.HOUR;
+ this.timestamp = System.currentTimeMillis();
- FormButton previousButton = new FormButton();
- previousButton.setIcon("arrow-circle-left");
- previousButton.setText(environment.getMessages().previous());
- previousButton.addClickHandler(event -> updateChart(calculateTimestamp(true)));
- controlFormActions.add(previousButton);
+ setStyleName("layout vertical");
- nextButton = new FormButton();
- nextButton.setIcon("arrow-circle-right");
- nextButton.setText(environment.getMessages().next());
- nextButton.addClickHandler(event -> updateChart(calculateTimestamp(false)));
- controlFormActions.add(nextButton);
+ addAttachHandler(event -> {
+ if (event.isAttached()) {
+ createChart();
+ } else {
+ destroyChart();
+ }
+ });
+ }
- chart = ChartUtil.createLineChart(canvas.getContext2d());
+ void createChart() {
+ FormSectionLabel sectionLabel = new FormSectionLabel(environment.getMessages().historicalData());
+ add(sectionLabel);
- updateChart(System.currentTimeMillis());
+ Canvas canvas = Canvas.createIfSupported();
+ if (canvas == null) {
+ add(new Label(environment.getMessages().canvasNotSupported()));
+ return;
}
- long calculateTimestamp(boolean subtract) {
- long hour = 1000 * 60 * 60;
- long day = hour * 24;
- long week = day * 7;
- long month = day * 30;
- long year = day * 365;
- switch (intervalListBox.getValue()) {
- case HOUR:
- return subtract ? (lastUsedTimestamp - hour) : (lastUsedTimestamp + hour);
- case DAY:
- return subtract ? (lastUsedTimestamp - day) : (lastUsedTimestamp + day);
- case WEEK:
- return subtract ? (lastUsedTimestamp - week) : (lastUsedTimestamp + week);
- case MONTH:
- return subtract ? (lastUsedTimestamp - month) : (lastUsedTimestamp + month);
- case YEAR:
- return subtract ? (lastUsedTimestamp - year) : (lastUsedTimestamp + year);
- default:
- throw new IllegalArgumentException("Unsupported time period: " + intervalListBox.getValue());
+ FlowPanel chartFormPanel = new FlowPanel();
+ chartFormPanel.setStyleName("layout vertical center or-FormGroup");
+ add(chartFormPanel);
+
+ canvas.getCanvasElement().setWidth(850);
+ canvas.getCanvasElement().setHeight(250);
+ FlowPanel canvasContainer = new FlowPanel();
+ canvasContainer.setWidth("850px");
+ canvasContainer.setHeight("250px");
+ canvasContainer.add(canvas);
+
+ chartFormPanel.add(canvasContainer);
+
+ Form controlForm = new Form();
+ controlForm.getElement().getStyle().setWidth(850, PX);
+ controlForm.getElement().getStyle().setMarginTop(0.4, EM);
+ controlForm.getElement().getStyle().setMarginBottom(0.4, EM);
+ chartFormPanel.add(controlForm);
+
+ FormGroup controlFormGroup = new FormGroup();
+ controlForm.add(controlFormGroup);
+
+ FormLabel controlFormLabel = new FormLabel(environment.getMessages().showChartAggregatedFor());
+ controlFormGroup.addFormLabel(controlFormLabel);
+
+ FormField controlFormField = new FormField();
+ controlFormGroup.addFormField(controlFormField);
+ intervalListBox = new FormValueListBox<>(
+ new AbstractRenderer<DatapointInterval>() {
+ @Override
+ public String render(DatapointInterval interval) {
+ return environment.getMessages().datapointInterval(interval.name());
+ }
}
- }
+ );
+ intervalListBox.addValueChangeHandler(event -> {
+ updateChart(timestamp);
+ });
+ intervalListBox.setValue(interval);
+ intervalListBox.setAcceptableValues(Arrays.asList(DatapointInterval.values()));
+ intervalListBox.setEnabled(true);
+ controlFormField.add(intervalListBox);
+
+ FormGroupActions controlFormActions = new FormGroupActions();
+ controlFormGroup.addFormGroupActions(controlFormActions);
+
+ timeOutput = new FormOutputText();
+ timeOutput.addStyleName("flex");
+ timeOutput.getElement().getStyle().setFontSize(0.8, EM);
+ controlFormActions.add(timeOutput);
+
+ FormButton previousButton = new FormButton();
+ previousButton.setIcon("arrow-circle-left");
+ previousButton.setText(environment.getMessages().previous());
+ previousButton.addClickHandler(event -> updateChart(calculateTimestamp(true)));
+ controlFormActions.add(previousButton);
+
+ nextButton = new FormButton();
+ nextButton.setIcon("arrow-circle-right");
+ nextButton.setText(environment.getMessages().next());
+ nextButton.addClickHandler(event -> updateChart(calculateTimestamp(false)));
+ controlFormActions.add(nextButton);
+
+ chart = ChartUtil.createLineChart(canvas.getContext2d());
+
+ updateChart(timestamp);
+ }
- void updateChart(long timestamp) {
- this.lastUsedTimestamp = timestamp;
-
- timeOutput.setText(
- DateTimeFormat.getFormat(Constants.DEFAULT_DATETIME_FORMAT).format(new Date(timestamp))
- );
-
- nextButton.setEnabled(timestamp < System.currentTimeMillis());
-
- getNumberDatapoints(
- attribute,
- intervalListBox.getValue(),
- timestamp,
- numberDatapoints -> {
- ChartUtil.update(
- chart,
- ChartUtil.convertLabels(numberDatapoints),
- ChartUtil.convertData(numberDatapoints)
- );
- });
+ long calculateTimestamp(boolean subtract) {
+ long hour = 1000 * 60 * 60;
+ long day = hour * 24;
+ long week = day * 7;
+ long month = day * 30;
+ long year = day * 365;
+ switch (intervalListBox.getValue()) {
+ case HOUR:
+ return subtract ? (timestamp - hour) : (timestamp + hour);
+ case DAY:
+ return subtract ? (timestamp - day) : (timestamp + day);
+ case WEEK:
+ return subtract ? (timestamp - week) : (timestamp + week);
+ case MONTH:
+ return subtract ? (timestamp - month) : (timestamp + month);
+ case YEAR:
+ return subtract ? (timestamp - year) : (timestamp + year);
+ default:
+ throw new IllegalArgumentException("Unsupported time period: " + intervalListBox.getValue());
}
+ }
- void destroyChart() {
- if (chart != null) {
- chart.destroy();
- chart = null;
- }
- clear();
+ void updateChart(long timestamp) {
+ if (chart == null) {
+ return;
}
+
+ this.timestamp = timestamp;
+ this.interval = intervalListBox.getValue();
+
+ timeOutput.setText(
+ DateTimeFormat.getFormat(Constants.DEFAULT_DATETIME_FORMAT).format(new Date(timestamp))
+ );
+
+ getNumberDatapoints(
+ attribute,
+ interval,
+ timestamp,
+ numberDatapoints -> {
+ ChartUtil.update(
+ chart,
+ ChartUtil.convertLabels(numberDatapoints),
+ ChartUtil.convertData(numberDatapoints)
+ );
+ });
}
- return new ChartPanel();
+ void destroyChart() {
+ if (chart != null) {
+ chart.destroy();
+ }
+ chart = null;
+ intervalListBox = null;
+ timeOutput = null;
+ nextButton = null;
+ clear();
+ }
}
-
/* ####################################################################### */
abstract protected void writeAttributeValue(AssetAttribute attribute);
diff --git a/manager/client/src/main/java/org/openremote/manager/client/interop/chartjs/ChartUtil.java b/manager/client/src/main/java/org/openremote/manager/client/interop/chartjs/ChartUtil.java
index 9b5d82da6..a250db9e0 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/interop/chartjs/ChartUtil.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/interop/chartjs/ChartUtil.java
@@ -20,7 +20,6 @@
package org.openremote.manager.client.interop.chartjs;
import com.google.gwt.canvas.dom.client.Context2d;
-import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayNumber;
import com.google.gwt.core.client.JsArrayString;
import org.openremote.model.datapoint.NumberDatapoint;
@@ -30,14 +29,11 @@ import org.openremote.model.datapoint.NumberDatapoint;
*/
public class ChartUtil {
- static public JsArray<JsArrayString> convertLabels(NumberDatapoint[] numberDatapoints) {
- JsArray<JsArrayString> array = (JsArray<JsArrayString>) JsArray.createArray();
+ static public JsArrayString convertLabels(NumberDatapoint[] numberDatapoints) {
+ JsArrayString array = (JsArrayString) JsArrayString.createArray();
for (int i = 0; i < numberDatapoints.length; i++) {
NumberDatapoint numberDatapoint = numberDatapoints[i];
- JsArrayString labelArray = (JsArrayString) JsArrayString.createArray();
- labelArray.set(0, numberDatapoint.getLabel1());
- labelArray.set(1, numberDatapoint.getLabel2());
- array.set(i, labelArray);
+ array.set(i, numberDatapoint.getLabel());
}
return array;
}
@@ -52,7 +48,7 @@ public class ChartUtil {
}
- public native static void update(Chart chart, JsArray<JsArrayString> labels, JsArrayNumber data) /*-{
+ public native static void update(Chart chart, JsArrayString labels, JsArrayNumber data) /*-{
chart.data.labels = labels;
chart.data.datasets[0].data = data;
chart.update();
@@ -71,9 +67,9 @@ public class ChartUtil {
pointHoverBackgroundColor: "#374c4f",
pointHoverBorderColor: "#c1d72f",
pointBorderWidth: 2,
- pointHoverRadius: 10,
+ pointHoverRadius: 4,
pointHoverBorderWidth: 4,
- pointRadius: 5,
+ pointRadius: 4,
pointHitRadius: 20
}
]
diff --git a/manager/client/src/main/java/org/openremote/manager/client/widget/FormGroup.java b/manager/client/src/main/java/org/openremote/manager/client/widget/FormGroup.java
index 397209c77..97438b5fa 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/widget/FormGroup.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/widget/FormGroup.java
@@ -131,6 +131,7 @@ public class FormGroup extends FlowPanel implements HasWidgets {
toggleExtensionButton.setEnabled(true);
extensionPanel.insert(widget, beforeIndex);
}
+
public boolean removeExtension(Widget widget) {
boolean removed = extensionPanel.remove(widget);
if (removed && extensionPanel.getWidgetCount() == 0)
@@ -138,6 +139,16 @@ public class FormGroup extends FlowPanel implements HasWidgets {
return removed;
}
+ @SuppressWarnings("unchecked")
+ public <W extends Widget> W getExtensionOfType(Class<W> widgetType) {
+ for (int i = 0; i < extensionPanel.getWidgetCount(); i++) {
+ Widget w = extensionPanel.getWidget(i);
+ if (widgetType.equals(w.getClass()))
+ return (W) w;
+ }
+ return null;
+ }
+
public FormLabel getFormLabel() {
return formLabel;
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java b/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java
index 86550930e..62d0b153b 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java
@@ -89,7 +89,7 @@ public class AssetDatapointService implements ContainerService, Consumer<AssetSt
String truncateX;
String step;
String interval;
- Function<Timestamp, Pair<String, String>> labelFunction;
+ Function<Timestamp, String> labelFunction;
SimpleDateFormat dayFormat = new SimpleDateFormat("dd. MMM yyyy");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
@@ -98,31 +98,31 @@ public class AssetDatapointService implements ContainerService, Consumer<AssetSt
truncateX = "minute";
step = "1 minute";
interval = "1 hour";
- labelFunction = ts -> new Pair<>(timeFormat.format(ts), "");
+ labelFunction = timeFormat::format;
break;
case DAY:
truncateX = "hour";
step = "1 hour";
interval = "1 day";
- labelFunction = ts -> new Pair<>(timeFormat.format(ts), "");
+ labelFunction = timeFormat::format;
break;
case WEEK:
truncateX = "day";
step = "1 day";
interval = "7 day";
- labelFunction = ts -> new Pair<>(dayFormat.format(ts), "");
+ labelFunction = dayFormat::format;
break;
case MONTH:
truncateX = "day";
step = "1 day";
interval = "1 month";
- labelFunction = ts -> new Pair<>(dayFormat.format(ts), "");
+ labelFunction = dayFormat::format;
break;
case YEAR:
truncateX = "month";
step = "1 month";
interval = "1 year";
- labelFunction = ts -> new Pair<>(dayFormat.format(ts), "");
+ labelFunction = dayFormat::format;
break;
default:
throw new IllegalArgumentException("Can't handle interval: " + datapointInterval);
@@ -166,10 +166,8 @@ public class AssetDatapointService implements ContainerService, Consumer<AssetSt
try (ResultSet rs = st.executeQuery()) {
List<NumberDatapoint> result = new ArrayList<>();
while (rs.next()) {
- Pair<String, String> labelPair = labelFunction.apply(rs.getTimestamp(1));
result.add(new NumberDatapoint(
- labelPair.key,
- labelPair.value,
+ labelFunction.apply(rs.getTimestamp(1)),
rs.getBigDecimal(2)
)
);
diff --git a/model/src/main/java/org/openremote/model/datapoint/NumberDatapoint.java b/model/src/main/java/org/openremote/model/datapoint/NumberDatapoint.java
index e0ebbf99b..8c6d953e7 100644
--- a/model/src/main/java/org/openremote/model/datapoint/NumberDatapoint.java
+++ b/model/src/main/java/org/openremote/model/datapoint/NumberDatapoint.java
@@ -21,33 +21,23 @@ package org.openremote.model.datapoint;
public class NumberDatapoint {
- protected String label1;
- protected String label2;
+ protected String label;
protected Number number;
protected NumberDatapoint() {
}
- public NumberDatapoint(String label1, String label2, Number number) {
- this.label1 = label1;
- this.label2 = label2;
+ public NumberDatapoint(String label, Number number) {
+ this.label = label;
this.number = number;
}
- public String getLabel1() {
- return label1;
+ public String getLabel() {
+ return label;
}
- public void setLabel1(String label1) {
- this.label1 = label1;
- }
-
- public String getLabel2() {
- return label2;
- }
-
- public void setLabel2(String label2) {
- this.label2 = label2;
+ public void setLabel(String label) {
+ this.label = label;
}
public Number getNumber() {
@@ -61,8 +51,7 @@ public class NumberDatapoint {
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
- "label1='" + label1 + '\'' +
- "label2='" + label2 + '\'' +
+ "label='" + label + '\'' +
", number=" + number +
'}';
}