Skip to content

armgit / core

Public Imported from GitHub
0

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

Implement KNX link used for tests to verify correct data arrived at KNX level

Marcus Redeker215c06b7a035
diff --git a/manager/test/src/main/groovy/org/openremote/test/KNXTestingNetworkLink.java b/manager/test/src/main/groovy/org/openremote/test/KNXTestingNetworkLink.java
new file mode 100644
index 000000000..c3e95cb2f
--- /dev/null
+++ b/manager/test/src/main/groovy/org/openremote/test/KNXTestingNetworkLink.java
@@ -0,0 +1,56 @@
+package org.openremote.test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import tuwien.auto.calimero.FrameEvent;
+import tuwien.auto.calimero.IndividualAddress;
+import tuwien.auto.calimero.KNXAddress;
+import tuwien.auto.calimero.KNXFormatException;
+import tuwien.auto.calimero.KNXTimeoutException;
+import tuwien.auto.calimero.cemi.CEMI;
+import tuwien.auto.calimero.cemi.CEMILData;
+import tuwien.auto.calimero.link.AbstractLink;
+import tuwien.auto.calimero.link.KNXLinkClosedException;
+import tuwien.auto.calimero.link.medium.KNXMediumSettings;
+
+public class KNXTestingNetworkLink extends AbstractLink {
+
+ private static KNXTestingNetworkLink instance;
+ private String lastDataReceived;
+
+ public KNXTestingNetworkLink(Object... args) throws KNXFormatException {
+ super("KNXTestingLink", KNXMediumSettings.create(KNXMediumSettings.MEDIUM_TP1, new IndividualAddress("1.1.1")));
+ KNXTestingNetworkLink.instance = this;
+ }
+
+
+ @Override
+ protected void onSend(KNXAddress dst, byte[] msg, boolean waitForCon) throws KNXTimeoutException, KNXLinkClosedException {
+ }
+
+ @Override
+ protected void onSend(CEMILData msg, boolean waitForCon) throws KNXTimeoutException, KNXLinkClosedException {
+ this.lastDataReceived = byteArrayToHex(msg.getPayload());
+ }
+
+ @Override
+ protected CEMI onReceive(FrameEvent e) throws KNXFormatException {
+ return super.onReceive(e);
+ }
+
+ public static KNXTestingNetworkLink getInstance() {
+ return KNXTestingNetworkLink.instance;
+ }
+
+ public static String byteArrayToHex(byte[] a) {
+ StringBuilder sb = new StringBuilder(a.length * 2);
+ for(byte b: a)
+ sb.append(String.format("%02x", b));
+ return sb.toString();
+ }
+
+ public String getLastDataReceived() {
+ return this.lastDataReceived;
+ }
+}
diff --git a/manager/test/src/test/groovy/org/openremote/test/protocol/KNXProtocolTest.groovy b/manager/test/src/test/groovy/org/openremote/test/protocol/KNXProtocolTest.groovy
index 9fedf2b19..793eaba4f 100644
--- a/manager/test/src/test/groovy/org/openremote/test/protocol/KNXProtocolTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/protocol/KNXProtocolTest.groovy
@@ -37,6 +37,7 @@ import org.openremote.model.asset.AssetType
import org.openremote.model.asset.agent.ProtocolConfiguration
import org.openremote.model.attribute.*
import org.openremote.model.value.Values
+import org.openremote.test.KNXTestingNetworkLink
import org.openremote.test.ManagerContainerTrait
import spock.lang.Ignore
import spock.lang.Specification
@@ -46,18 +47,20 @@ import tuwien.auto.calimero.server.Launcher
/**
* This tests the KNX protocol and protocol implementation.
*/
-@Ignore // TODO Test fails
+@Ignore // TODO Verify test on other machines
class KNXProtocolTest extends Specification implements ManagerContainerTrait {
def "Check KNX protocol"() {
given: "expected conditions"
- def conditions = new PollingConditions(timeout: 10, initialDelay: 1.5, delay: 1)
+ def conditions = new PollingConditions(timeout: 10, initialDelay: 1, delay: 1)
and: "the KNX emulation server is started"
def knxEmulationServer = new Launcher("manager/test/src/test/resources/knx-server-config.xml")
def knxServerThread = new Thread(knxEmulationServer);
knxServerThread.start();
+
+ def knxTestingNetwork = KNXTestingNetworkLink.getInstance();
and: "the container is started"
def serverPort = findEphemeralPort()
@@ -89,23 +92,6 @@ class KNXProtocolTest extends Specification implements ManagerContainerTrait {
assert agentService.getProtocolDeploymentStatus(knxAgent.getAttribute("knxConfigError").get().getReferenceOrThrow()) == ConnectionStatus.ERROR
}
-// when: "the connection to the KNX IP gateway is lost"
-// knxEmulationServer.quit()
-//
-// then: "the protocol status should switch to closed, waiting, connecting, connected"
-// conditions.eventually {
-// assert agentService.getProtocolDeploymentStatus(knxAgent.getAttribute("knxConfig").get().getReferenceOrThrow()) == ConnectionStatus.CLOSED
-// }
-// conditions.eventually {
-// assert agentService.getProtocolDeploymentStatus(knxAgent.getAttribute("knxConfig").get().getReferenceOrThrow()) == ConnectionStatus.WAITING
-// }
-// conditions.eventually {
-// assert agentService.getProtocolDeploymentStatus(knxAgent.getAttribute("knxConfig").get().getReferenceOrThrow()) == ConnectionStatus.CONNECTING
-// }
-// conditions.eventually {
-// assert agentService.getProtocolDeploymentStatus(knxAgent.getAttribute("knxConfig").get().getReferenceOrThrow()) == ConnectionStatus.CONNECTED
-// }
-
when: "a thing asset is created that links it's attributes to the knx protocol configuration"
def knxThing = new ServerAsset("Living Room Assset", AssetType.THING, knxAgent)
@@ -122,21 +108,29 @@ class KNXProtocolTest extends Specification implements ManagerContainerTrait {
)
knxThing = assetStorageService.merge(knxThing)
- then: "the mock thing to be fully deployed"
+ then: "the living room thing to be fully deployed"
conditions.eventually {
((KNXProtocol) agentService.getProtocol(knxAgent.getAttribute("knxConfig").get())).getAttributeActionMap().get(knxThing.getAttribute("light1ToggleOnOff")) != null
}
- when: "change light1ToggleOnOff value to 'true'"
+ when: "change light1ToggleOnOff value to 'false'"
def switchChange = new AttributeEvent(knxThing.getId(), "light1ToggleOnOff", Values.create(false))
assetProcessingService.sendAttributeEvent(switchChange)
- then: ""
+ then: "the correct data should arrive on KNX bus"
conditions.eventually {
- knxThing.getAttribute("light1ToggleOnOff") != null
+ assert knxTestingNetwork.getLastDataReceived().equals("0080") == true
+ }
+
+ when: "change light1ToggleOnOff value to 'true'"
+ switchChange = new AttributeEvent(knxThing.getId(), "light1ToggleOnOff", Values.create(true))
+ assetProcessingService.sendAttributeEvent(switchChange)
+
+ then: "the correct data should arrive on KNX bus"
+ conditions.eventually {
+ assert knxTestingNetwork.getLastDataReceived().equals("0081") == true
}
-
cleanup: "the server should be stopped"
stopContainer(container)
diff --git a/manager/test/src/test/resources/knx-server-config.xml b/manager/test/src/test/resources/knx-server-config.xml
index 62731cd86..72b270166 100644
--- a/manager/test/src/test/resources/knx-server-config.xml
+++ b/manager/test/src/test/resources/knx-server-config.xml
@@ -3,20 +3,13 @@
<knxServer name="knx-server" friendlyName="Calimero KNXnet/IP Test Server">
<!-- KNXnet/IP search & discovery -->
- <discovery listenNetIf="all" outgoingNetIf="all" activate="true" />
+ <discovery listenNetIf="lo0" outgoingNetIf="lo0" activate="false" />
<!-- Provides the KNXnet/IP-side configuration for access to one KNX subnet -->
- <serviceContainer activate="true" routing="false" networkMonitoring="true" udpPort="3671" listenNetIf="any" reuseCtrlEP="false">
+ <serviceContainer activate="true" routing="false" networkMonitoring="false" udpPort="3671" listenNetIf="lo0" reuseCtrlEP="false">
<knxAddress type="individual">1.1.1</knxAddress>
- <knxSubnet type="emulate">1.1.1</knxSubnet>
+ <knxSubnet type="user-supplied" class="org.openremote.test.KNXTestingNetworkLink">1.1.1</knxSubnet>
- <!-- KNX group address filter applied for this service container (optional) -->
- <groupAddressFilter>
- <knxAddress type="group">0/0/0</knxAddress>
- <knxAddress type="group">2/1/1</knxAddress>
- <knxAddress type="group">2/1/0</knxAddress>
- <knxAddress type="group">2/1/20</knxAddress>
- </groupAddressFilter>
<!-- Additional KNX individual addresses assigned to KNXnet/IP connections (optional) -->
<additionalAddresses>
<knxAddress type="individual">1.1.10</knxAddress>
@@ -25,5 +18,5 @@
<knxAddress type="individual">1.1.13</knxAddress>
</additionalAddresses>
</serviceContainer>
- <!-- Next service container (optional) -->
+
</knxServer>