|
|
Here, I'll walk you through the java source code, responsible for starting the [ESP32 Controller](What-am-I-looking-at-here#esp32-controller). Additional startup procedures are explained in the documentation for the `start_component.sh` and the `run_demo.sh` scripts.
|
|
|
|
|
|
The discussed file can be found [here](https://gitlab.hrz.tu-chemnitz.de/vws-demo/vws-spielwiese/-/blob/6d65d24412bda2e555e70a4f9581eb142096bd49/basyx.lichterkette/src/main/java/de/olipar/basyx/lichterkette/ProprietaryLightstrand.java).
|
|
|
|
|
|
We'll skip the license headers, imports and comments, as they are either pretty much self explanatory or not the product of conscious development effort.
|
|
|
This class uses the [Eclipse Paho MQTT Client](https://www.eclipse.org/paho/index.php?page=clients/java/index.php).
|
|
|
|
|
|
This has quite a bit of overlap with the startup of [ordinary lights](Light-host-walkthrough). The first method `getLightAmount` is infact the same one, so please check out the documentation for ordinary lights for this method.
|
|
|
|
|
|
```java
|
|
|
public static void main(String[] args) {
|
|
|
```
|
|
|
Differences become more apparent in the main method, mostly consisting of creation of an MQTT Client and establishing a connection.
|
|
|
|
|
|
```java
|
|
|
redLights = getLightAmount("red");
|
|
|
greenLights = getLightAmount("green");
|
|
|
|
|
|
final int HOSTPORT = Common.getHostPort();
|
|
|
logger.info("Local host port is set to " + HOSTPORT);
|
|
|
logger.info("Ports " + HOSTPORT + " through including " + (HOSTPORT + redLights + greenLights - 1) + " will be used for lights.");
|
|
|
```
|
|
|
We start of again by establishing, how many Proprietary lights we should connect to, jut like with the ordinary lights.
|
|
|
|
|
|
```java
|
|
|
MqttClient mqttClient = null;
|
|
|
try {
|
|
|
mqttClient = new MqttClient("tcp://esp-controller.local:1883", "ProprietaryLightstrand", new MemoryPersistence());
|
|
|
} catch (MqttException e) {
|
|
|
logger.warn("MQTT client could not be created: " + e.getMessage());
|
|
|
logger.warn("No lights will be switched in the real world.");
|
|
|
}
|
|
|
```
|
|
|
Then, we initialize a MQTT Client with `null` and try to connect to the MQTT broker hosted on the ESP32 Controller. If a client can't be created, a warning is issued and lights will only be created as virtual lights on the machine.
|
|
|
|
|
|
```java
|
|
|
MqttConnectOptions connOpts = new MqttConnectOptions();
|
|
|
connOpts.setCleanSession(true);
|
|
|
```
|
|
|
Then, we create connection options to pass on to the client on connection. We instruct the client to discard all stale sessions, should they exist.
|
|
|
|
|
|
```java
|
|
|
try {
|
|
|
mqttClient.connect(connOpts);
|
|
|
} catch (NullPointerException | MqttException e) {
|
|
|
logger.warn("MQTT client could not connect to broker: " + e.getMessage());
|
|
|
logger.warn("No lights will be switched in the real world.");
|
|
|
}
|
|
|
```
|
|
|
We then try to establish a connection. If a connection could not be established, a warning is issued.
|
|
|
|
|
|
Then we create the lights just like for ordinary lights. Only difference being, that we instantiate [proprietaryLight](Walkthrough-for-the-general-light-classes#redproprietarylight)s, to which we pass the MQTT client if established and the word "Proprietary" as part of the IDs.
|
|
|
Please see the documentation for ordinary lights for a walkthrough. |
|
|
\ No newline at end of file |