Skip to content

armgit / core

Public Imported from GitHub
0

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

Implemented device resource read/write UI, better Keycloak boot

Christian Bauer920bafe29ac4
diff --git a/keycloak/Dockerfile-demo b/keycloak/Dockerfile-demo
index 3bcc444a6..cb1d422c2 100644
--- a/keycloak/Dockerfile-demo
+++ b/keycloak/Dockerfile-demo
@@ -10,3 +10,11 @@ ADD setProxyForwarding.xsl /opt/jboss/keycloak/
RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone.xml -xsl:/opt/jboss/keycloak/setProxyForwarding.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone.xml
ADD theme /opt/jboss/keycloak/themes/openremote
+
+USER root
+ADD wait-for-it.sh /opt/wait-for-it.sh
+RUN chmod +x /opt/wait-for-it.sh
+USER jboss
+
+ENTRYPOINT ["/opt/wait-for-it.sh", "postgresql:5432", "-t", "60", "--strict", "--", "/opt/jboss/docker-entrypoint.sh"]
+CMD ["-b", "0.0.0.0"]
\ No newline at end of file
diff --git a/keycloak/Dockerfile-dev b/keycloak/Dockerfile-dev
index 61615c279..f593e7afc 100644
--- a/keycloak/Dockerfile-dev
+++ b/keycloak/Dockerfile-dev
@@ -11,3 +11,11 @@ RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/config
ADD keycloak-server-json-dev.jq /opt/jboss/keycloak/config.jq
RUN INPUT=`cat /opt/jboss/keycloak/standalone/configuration/keycloak-server.json` && echo $INPUT | jq -f /opt/jboss/keycloak/config.jq > /opt/jboss/keycloak/standalone/configuration/keycloak-server.json
+
+USER root
+ADD wait-for-it.sh /opt/wait-for-it.sh
+RUN chmod +x /opt/wait-for-it.sh
+USER jboss
+
+ENTRYPOINT ["/opt/wait-for-it.sh", "postgresql:5432", "-t", "60", "--strict", "--", "/opt/jboss/docker-entrypoint.sh"]
+CMD ["-b", "0.0.0.0"]
\ No newline at end of file
diff --git a/keycloak/wait-for-it.sh b/keycloak/wait-for-it.sh
new file mode 100644
index 000000000..dd1d87e0b
--- /dev/null
+++ b/keycloak/wait-for-it.sh
@@ -0,0 +1,183 @@
+#!/usr/bin/env bash
+
+# The MIT License (MIT)
+# Copyright (c) 2016 Giles Hall
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy of
+# this software and associated documentation files (the "Software"), to deal in
+# the Software without restriction, including without limitation the rights to
+# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+# of the Software, and to permit persons to whom the Software is furnished to do
+# so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+# Use this script to test if a given TCP host/port are available
+
+cmdname=$(basename $0)
+
+echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
+
+usage()
+{
+ cat << USAGE >&2
+Usage:
+ $cmdname host:port [-s] [-t timeout] [-- command args]
+ -h HOST | --host=HOST Host or IP under test
+ -p PORT | --port=PORT TCP port under test
+ Alternatively, you specify the host and port as host:port
+ -s | --strict Only execute subcommand if the test succeeds
+ -q | --quiet Don't output any status messages
+ -t TIMEOUT | --timeout=TIMEOUT
+ Timeout in seconds, zero for no timeout
+ -- COMMAND ARGS Execute command with args after the test finishes
+USAGE
+ exit 1
+}
+
+wait_for()
+{
+ if [[ $TIMEOUT -gt 0 ]]; then
+ echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
+ else
+ echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
+ fi
+ start_ts=$(date +%s)
+ while :
+ do
+ (echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
+ result=$?
+ if [[ $result -eq 0 ]]; then
+ end_ts=$(date +%s)
+ echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
+ break
+ fi
+ sleep 1
+ done
+ return $result
+}
+
+wait_for_wrapper()
+{
+ # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
+ if [[ $QUIET -eq 1 ]]; then
+ timeout $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
+ else
+ timeout $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
+ fi
+ PID=$!
+ trap "kill -INT -$PID" INT
+ wait $PID
+ RESULT=$?
+ if [[ $RESULT -ne 0 ]]; then
+ echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
+ fi
+ return $RESULT
+}
+
+# process arguments
+while [[ $# -gt 0 ]]
+do
+ case "$1" in
+ *:* )
+ hostport=(${1//:/ })
+ HOST=${hostport[0]}
+ PORT=${hostport[1]}
+ shift 1
+ ;;
+ --child)
+ CHILD=1
+ shift 1
+ ;;
+ -q | --quiet)
+ QUIET=1
+ shift 1
+ ;;
+ -s | --strict)
+ STRICT=1
+ shift 1
+ ;;
+ -h)
+ HOST="$2"
+ if [[ $HOST == "" ]]; then break; fi
+ shift 2
+ ;;
+ --host=*)
+ HOST="${1#*=}"
+ shift 1
+ ;;
+ -p)
+ PORT="$2"
+ if [[ $PORT == "" ]]; then break; fi
+ shift 2
+ ;;
+ --port=*)
+ PORT="${1#*=}"
+ shift 1
+ ;;
+ -t)
+ TIMEOUT="$2"
+ if [[ $TIMEOUT == "" ]]; then break; fi
+ shift 2
+ ;;
+ --timeout=*)
+ TIMEOUT="${1#*=}"
+ shift 1
+ ;;
+ --)
+ shift
+ CLI="$@"
+ break
+ ;;
+ --help)
+ usage
+ ;;
+ *)
+ echoerr "Unknown argument: $1"
+ usage
+ ;;
+ esac
+done
+
+if [[ "$HOST" == "" || "$PORT" == "" ]]; then
+ echoerr "Error: you need to provide a host and port to test."
+ usage
+fi
+
+TIMEOUT=${TIMEOUT:-15}
+STRICT=${STRICT:-0}
+CHILD=${CHILD:-0}
+QUIET=${QUIET:-0}
+
+if [[ $CHILD -gt 0 ]]; then
+ wait_for
+ RESULT=$?
+ exit $RESULT
+else
+ if [[ $TIMEOUT -gt 0 ]]; then
+ wait_for_wrapper
+ RESULT=$?
+ else
+ wait_for
+ RESULT=$?
+ fi
+fi
+
+if [[ $CLI != "" ]]; then
+ if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
+ echoerr "$cmdname: strict mode, refusing to execute subprocess"
+ exit $RESULT
+ fi
+ exec $CLI
+else
+ exit $RESULT
+fi
\ No newline at end of file
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetActivity.java b/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetActivity.java
index 563536611..b7ebc587a 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetActivity.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetActivity.java
@@ -133,15 +133,16 @@ public class AssetActivity
view.setTypeSelectionEnabled(false);
view.setEditable(asset.getWellKnownType().isEditable());
writeTypeToView();
- writeAttributesEditorToView();
if (asset.getParentId() != null) {
loadAsset(asset.getParentId(), loadedParentAsset -> {
this.parentAsset = loadedParentAsset;
writeParentToView();
+ writeAttributesEditorToView();
view.setFormBusy(false);
});
} else {
writeParentToView();
+ writeAttributesEditorToView();
view.setFormBusy(false);
}
}
@@ -188,6 +189,9 @@ public class AssetActivity
@Override
public void onStop() {
super.onStop();
+ if (attributesEditor != null) {
+ attributesEditor.close();
+ }
clearViewFieldErrors();
view.setPresenter(null);
@@ -363,11 +367,14 @@ public class AssetActivity
protected void writeAttributesEditorToView() {
switch(asset.getWellKnownType()) {
case DEVICE:
- attributesEditor = new DeviceAttributesEditor(
- environment,
- view.getDeviceAttributesEditorContainer(),
- new Attributes(asset.getAttributes())
- );
+ if (parentAsset != null) {
+ attributesEditor = new DeviceAttributesEditor(
+ environment,
+ view.getDeviceAttributesEditorContainer(),
+ new Attributes(asset.getAttributes()),
+ parentAsset.getId()
+ );
+ }
break;
case AGENT:
attributesEditor = new AgentAttributesEditor(
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetView.java b/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetView.java
index a446363d6..3f009af51 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetView.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetView.java
@@ -19,22 +19,20 @@
*/
package org.openremote.manager.client.assets.asset;
-import com.google.gwt.user.client.ui.InsertPanel;
import elemental.json.JsonObject;
import org.openremote.manager.client.assets.browser.AssetBrowsingView;
import org.openremote.manager.client.assets.device.DeviceAttributesEditor;
import org.openremote.manager.client.widget.AttributesEditor;
-import org.openremote.manager.client.widget.Form;
import org.openremote.manager.client.widget.FormView;
import org.openremote.manager.shared.asset.AssetType;
-import org.openremote.manager.shared.attribute.Attributes;
import org.openremote.manager.shared.map.GeoJSON;
import java.util.Date;
public interface AssetView extends AssetBrowsingView<AssetView.Presenter>, FormView {
- interface Presenter extends AssetBrowsingView.Presenter {
+ interface
+ Presenter extends AssetBrowsingView.Presenter {
void beginParentSelection();
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/device/DeviceAttributesEditor.java b/manager/client/src/main/java/org/openremote/manager/client/assets/device/DeviceAttributesEditor.java
index 844477f50..889c261ce 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/device/DeviceAttributesEditor.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/device/DeviceAttributesEditor.java
@@ -21,13 +21,21 @@ package org.openremote.manager.client.assets.device;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
+import org.eclipse.jetty.server.Server;
import org.openremote.manager.client.Environment;
+import org.openremote.manager.client.event.CancelRepeatingServerSendEvent;
+import org.openremote.manager.client.event.RepeatingServerSendEvent;
+import org.openremote.manager.client.event.ServerSendEvent;
+import org.openremote.manager.client.event.bus.EventRegistration;
import org.openremote.manager.client.widget.*;
+import org.openremote.manager.shared.agent.*;
import org.openremote.manager.shared.attribute.Attribute;
import org.openremote.manager.shared.attribute.Attributes;
import org.openremote.manager.shared.device.DeviceAttributes;
import org.openremote.manager.shared.device.DeviceResource;
+import java.util.HashMap;
+import java.util.Map;
import java.util.logging.Logger;
public class DeviceAttributesEditor extends AttributesEditor<DeviceAttributesEditor.Style> {
@@ -43,12 +51,20 @@ public class DeviceAttributesEditor extends AttributesEditor<DeviceAttributesEdi
String writeButton();
}
+ final String agentAssetId;
final protected FormGroup deviceActionsGroup = new FormGroup();
final protected FormCheckBox enableLiveUpdatesCheckBox = new FormCheckBox();
+ final protected DeviceAttributes deviceAttributes;
+ final protected Map<String, FormInputText> deviceResourceInputs = new HashMap<>();
- public DeviceAttributesEditor(Environment environment, Container<DeviceAttributesEditor.Style> container, Attributes attributes) {
+ protected EventRegistration<DeviceResourceValueEvent> deviceResourceValueRegistration;
+
+ public DeviceAttributesEditor(Environment environment, Container<DeviceAttributesEditor.Style> container, Attributes attributes, String agentAssetId) {
super(environment, container, attributes);
+ this.agentAssetId = agentAssetId;
+ this.deviceAttributes = new DeviceAttributes(attributes.getJsonObject());
+
FormLabel enableLiveUpdates = new FormLabel();
enableLiveUpdates.addStyleName("larger");
enableLiveUpdates.setText(environment.getMessages().enableLiveUpdates());
@@ -58,8 +74,27 @@ public class DeviceAttributesEditor extends AttributesEditor<DeviceAttributesEdi
deviceActionsGroup.addFormField(deviceActionsFormField);
enableLiveUpdatesCheckBox.addValueChangeHandler(event -> {
- LOG.info("### LIVE UPDATES: " + event.getValue());
+ enableLiveUpdates(event.getValue());
});
+
+ deviceResourceValueRegistration = environment.getEventBus().register(
+ DeviceResourceValueEvent.class, event -> {
+ if (deviceAttributes.getKey().equals(event.getDeviceKey()) &&
+ deviceResourceInputs.containsKey(event.getDeviceResourceKey())) {
+ deviceResourceInputs.get(event.getDeviceResourceKey())
+ .setText(event.getValue());
+ }
+ }
+ );
+ }
+
+ @Override
+ public void close() {
+ super.close();
+ enableLiveUpdates(false);
+ if (deviceResourceValueRegistration != null) {
+ environment.getEventBus().remove(deviceResourceValueRegistration);
+ }
}
@Override
@@ -104,14 +139,34 @@ public class DeviceAttributesEditor extends AttributesEditor<DeviceAttributesEdi
actionPanel.setStyleName("flex layout horizontal center");
FormButton readButton = new FormButton();
+ readButton.addClickHandler(event -> {
+ environment.getEventBus().dispatch(new ServerSendEvent(
+ new DeviceResourceRead(
+ agentAssetId,
+ deviceAttributes.getKey(),
+ deviceResource.getValueAsString()
+ )
+ ));
+ });
readButton.setPrimary(true);
readButton.addStyleName(container.getStyle().readButton());
readButton.setText(environment.getMessages().read());
FormInputText readWriteInput = new FormInputText();
+ deviceResourceInputs.put(deviceResource.getValueAsString(), readWriteInput);
readWriteInput.addStyleName(container.getStyle().readWriteInput());
FormButton writeButton = new FormButton();
+ writeButton.addClickHandler(event -> {
+ environment.getEventBus().dispatch(new ServerSendEvent(
+ new DeviceResourceWrite(
+ agentAssetId,
+ deviceAttributes.getKey(),
+ deviceResource.getValueAsString(),
+ readWriteInput.getText()
+ )
+ ));
+ });
writeButton.setDanger(true);
writeButton.addStyleName(container.getStyle().writeButton());
writeButton.setText(environment.getMessages().write());
@@ -164,4 +219,20 @@ public class DeviceAttributesEditor extends AttributesEditor<DeviceAttributesEdi
protected FormCheckBox createBooleanEditor(Style style, Attribute attribute, boolean readOnly) {
return super.createBooleanEditor(style, attribute, readOnly || DeviceResource.isDeviceResource(attribute));
}
+
+ protected void enableLiveUpdates(boolean enable) {
+ if (enable) {
+ environment.getEventBus().dispatch(new RepeatingServerSendEvent(
+ new SubscribeDeviceResourceUpdates(agentAssetId, deviceAttributes.getKey()),
+ "deviceAttributes-liveUpdate",
+ 30000
+ ));
+ } else {
+ environment.getEventBus().dispatch(new CancelRepeatingServerSendEvent(
+ new UnsubscribeDeviceResourceUpdates(agentAssetId, deviceAttributes.getKey()),
+ "deviceAttributes-liveUpdate"
+ ));
+ }
+
+ }
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/widget/AttributesEditor.java b/manager/client/src/main/java/org/openremote/manager/client/widget/AttributesEditor.java
index a0e7c39f3..18c61dc12 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/widget/AttributesEditor.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/widget/AttributesEditor.java
@@ -69,8 +69,6 @@ public class AttributesEditor<S extends AttributesEditor.Style> {
this.environment = environment;
this.container = container;
this.attributes = attributes;
-
- buildAttributeFormGroups();
}
public Attributes getAttributes() {
@@ -115,6 +113,7 @@ public class AttributesEditor<S extends AttributesEditor.Style> {
*/
public void render() {
+ buildAttributeFormGroups();
for (FormGroup formGroup : formGroups.values()) {
container.getPanel().add(formGroup);
}
@@ -133,6 +132,10 @@ public class AttributesEditor<S extends AttributesEditor.Style> {
}
}
+ public void close() {
+ // Subclasses can manage lifecycle
+ }
+
/* ####################################################################### */
protected void clearContainer() {