Skip to content

Commit

Permalink
Update README and dockerfile (#6)
Browse files Browse the repository at this point in the history
* Auto discovery of arylic devices on the network

* Upstep quarkus to 3.9

Replace kotlin serialization by jackson

* Make dockerfile compatible with zeroconf

* Update README
  • Loading branch information
mouse256 authored Apr 21, 2024
1 parent 66571ed commit d5eb824
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 109 deletions.
75 changes: 56 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,83 @@ Pre-built docker images are available on [github](https://github.com/mouse256/ar

A sample systemd service is provided in [systemd-example.service](src/main/docker/systemd-example.service)

## Auto-discovery

Arylic devices can be auto-discovered, or statically configured.

Auto-discovery required additional permission to docker to be able to work:
* network mode = host to be able to send/receive discovery messages
* connection to dbus/avahi to resolve the discovered hostnames

## Usage
To run it locally, use the following command to start it

### With auto-discovery enabled
```
docker run \
--net host \
-e MQTT_HOST=192.168.1.152 \
-e MQTT_PORT=1883 \
-v /var/run/dbus:/var/run/dbus \
-v /var/run/avahi-daemon/socket:/var/run/avahi-daemon/socket \
ghcr.io/mouse256/arylic-mqtt:latest
```

### With auto-discovery disabled
```
docker run \
--name arylic-mqtt \
-e MQTT_HOST=192.168.1.152 \
-e MQTT_PORT=1883 \
-e ARYLIC_AUTO-DISCOVERY_ENABLED=false \
-e ARYLIC_DEVICES_0__IP=192.168.1.73 \
-p 8080:8080 \
ghcr.io/mouse256/arylic-mqtt:latest
```
The following environment variables are supported:

| Variable | Description |
|----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| MQTT_HOST | IP address of the MQTT server |
| MQTT_HOST | Port of the MQTT server |
| MQTT_USERNAME | MQTT username (optional) |
| MQTT_PASSWORD | MQTT password (optional) |
| MQTT_SSL_... | MQTT SSL options. See [smallrye mqtt configuration page](https://smallrye.io/smallrye-reactive-messaging/smallrye-reactive-messaging/3.4/mqtt/mqtt.html#_configuration_reference) for options |
| ARYLIC_DEVICES_0__IP | IP address of an Arylic device |
## Configuration
The project is based on [Quarkus](https://quarkus.io). Out-of-the box Quarkus configuration options can be used.

The following environment variables are important:

| Variable | Default | Description |
|-------------------------------|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| QUARKUS_HTTP_PORT | 8080 | Listening port for REST endpoint |
| MQTT_HOST | 127.0.0.1 | IP address of the MQTT server |
| MQTT_HOST | 1883 | Port of the MQTT server |
| MQTT_USERNAME | N/A | MQTT username (optional) |
| MQTT_PASSWORD | N/A | MQTT password (optional) |
| MQTT_SSL_... | N/A | MQTT SSL options. See [smallrye mqtt configuration page](https://smallrye.io/smallrye-reactive-messaging/smallrye-reactive-messaging/3.4/mqtt/mqtt.html#_configuration_reference) for options |
| ARYLIC_AUTO-DISCOVERY_ENABLED | true | Enable/disable auto-discovery |
| ARYLIC_DEVICES_0__IP | N/A | IP address of an Arylic device (if auto-discovery is disabled) |

The ARYLIC_DEVICES variable can be repeated multiple times with a different index for multiple devices

All configuration options on the [smallrye mqtt configuration page](https://smallrye.io/smallrye-reactive-messaging/smallrye-reactive-messaging/3.4/mqtt/mqtt.html#_configuration_reference) can be used.
They need to be prefixed with `MP_MESSAGING_CONNECTOR_SMALLRYE_MQTT_` (eg `MP_MESSAGING_CONNECTOR_SMALLRYE_MQTT_SSL=true`)

## MQTT

MQTT is the main interface to interact with this tool. It can be used to get real-time updates of the status and can be used to control the device.

State is published on `arylic/state/#`

Commands are to be sent to `arylic/cmd/...`

## REST API
There is a (basic) REST api available for debugging purposes.

| Endpoint | Description |
|-------------------------------|----------------------|
| /arylic/{device}/device-info | get device-info |
| /arylic/{device}/mute | mute |
| /arylic/{device}/unmute | unmute |
| /arylic/{device}/metadata | get metadata |
| /arylic/{device}/status | get playback status |
| /arylic/{device}/play | play |
| /arylic/{device}/playpause | toggle play/pause |
| /arylic/{device}/pause | pause |
| Endpoint | Description |
|------------------------------|---------------------------|
| /arylic | List of devices connected |
| /arylic/{device}/device-info | get device-info |
| /arylic/{device}/mute | mute |
| /arylic/{device}/unmute | unmute |
| /arylic/{device}/metadata | get metadata |
| /arylic/{device}/status | get playback status |
| /arylic/{device}/play | play |
| /arylic/{device}/playpause | toggle play/pause |
| /arylic/{device}/pause | pause |

## OpenHab

Expand Down
17 changes: 17 additions & 0 deletions buildSnapshot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

set -e

TAG=$(git tag | sort -n -r | head -n 1)
echo "Last git tag: ${TAG}"
TAG1=$(echo "${TAG}" | cut -d "." -f 1 | sed "s/v//")
TAG2=$(echo "${TAG}" | cut -d "." -f 2)
TAG3=$(echo "${TAG}" | cut -d "." -f 3)
TAG2=$((TAG2+1))

SNAP="${TAG1}.${TAG2}.${TAG3}-SNAPSHOT"
echo "Next SNAPSHOT version: $SNAP"

./gradlew clean assemble
docker buildx build --pull --push --platform linux/arm64,linux/amd64 --tag ghcr.io/mouse256/arylic-mqtt:${SNAP} -f src/main/docker/Dockerfile.jvm .

6 changes: 5 additions & 1 deletion runInDocker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
./gradlew assemble && \
docker build -f src/main/docker/Dockerfile.jvm -t quarkus/arylic-mqtt-jvm . && \
docker run -i --rm \
--net host \
-e MQTT_HOST=192.168.1.152 \
-e MQTT_PORT=1883 \
-e arylic_devices_0__ip=192.168.1.73 \
-e MQTT_CLIENT_ID=arylic-mqtt-docker \
-v /var/run/dbus:/var/run/dbus \
-v /var/run/avahi-daemon/socket:/var/run/avahi-daemon/socket \
-p 8080:8080 quarkus/arylic-mqtt-jvm
# -e arylic_devices_0__ip=192.168.1.73 \
90 changes: 10 additions & 80 deletions src/main/docker/Dockerfile.jvm
Original file line number Diff line number Diff line change
@@ -1,86 +1,9 @@
####
# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
#
# Before building the container image run:
#
# ./gradlew build
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/arylic-mqtt-jvm .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/arylic-mqtt-jvm
#
# If you want to include the debug port into your docker image
# you will have to expose the debug port (default 5005 being the default) like this : EXPOSE 8080 5005.
# Additionally you will have to set -e JAVA_DEBUG=true and -e JAVA_DEBUG_PORT=*:5005
# when running the container
#
# Then run the container using :
#
# docker run -i --rm -p 8080:8080 quarkus/arylic-mqtt-jvm
#
# This image uses the `run-java.sh` script to run the application.
# This scripts computes the command line to execute your Java application, and
# includes memory/GC tuning.
# You can configure the behavior using the following environment properties:
# - JAVA_OPTS: JVM options passed to the `java` command (example: "-verbose:class")
# - JAVA_OPTS_APPEND: User specified Java options to be appended to generated options
# in JAVA_OPTS (example: "-Dsome.property=foo")
# - JAVA_MAX_MEM_RATIO: Is used when no `-Xmx` option is given in JAVA_OPTS. This is
# used to calculate a default maximal heap memory based on a containers restriction.
# If used in a container without any memory constraints for the container then this
# option has no effect. If there is a memory constraint then `-Xmx` is set to a ratio
# of the container available memory as set here. The default is `50` which means 50%
# of the available memory is used as an upper boundary. You can skip this mechanism by
# setting this value to `0` in which case no `-Xmx` option is added.
# - JAVA_INITIAL_MEM_RATIO: Is used when no `-Xms` option is given in JAVA_OPTS. This
# is used to calculate a default initial heap memory based on the maximum heap memory.
# If used in a container without any memory constraints for the container then this
# option has no effect. If there is a memory constraint then `-Xms` is set to a ratio
# of the `-Xmx` memory as set here. The default is `25` which means 25% of the `-Xmx`
# is used as the initial heap size. You can skip this mechanism by setting this value
# to `0` in which case no `-Xms` option is added (example: "25")
# - JAVA_MAX_INITIAL_MEM: Is used when no `-Xms` option is given in JAVA_OPTS.
# This is used to calculate the maximum value of the initial heap memory. If used in
# a container without any memory constraints for the container then this option has
# no effect. If there is a memory constraint then `-Xms` is limited to the value set
# here. The default is 4096MB which means the calculated value of `-Xms` never will
# be greater than 4096MB. The value of this variable is expressed in MB (example: "4096")
# - JAVA_DIAGNOSTICS: Set this to get some diagnostics information to standard output
# when things are happening. This option, if set to true, will set
# `-XX:+UnlockDiagnosticVMOptions`. Disabled by default (example: "true").
# - JAVA_DEBUG: If set remote debugging will be switched on. Disabled by default (example:
# true").
# - JAVA_DEBUG_PORT: Port used for remote debugging. Defaults to 5005 (example: "8787").
# - CONTAINER_CORE_LIMIT: A calculated core limit as described in
# https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt. (example: "2")
# - CONTAINER_MAX_MEMORY: Memory limit given to the container (example: "1024").
# - GC_MIN_HEAP_FREE_RATIO: Minimum percentage of heap free after GC to avoid expansion.
# (example: "20")
# - GC_MAX_HEAP_FREE_RATIO: Maximum percentage of heap free after GC to avoid shrinking.
# (example: "40")
# - GC_TIME_RATIO: Specifies the ratio of the time spent outside the garbage collection.
# (example: "4")
# - GC_ADAPTIVE_SIZE_POLICY_WEIGHT: The weighting given to the current GC time versus
# previous GC times. (example: "90")
# - GC_METASPACE_SIZE: The initial metaspace size. (example: "20")
# - GC_MAX_METASPACE_SIZE: The maximum metaspace size. (example: "100")
# - GC_CONTAINER_OPTIONS: Specify Java GC to use. The value of this variable should
# contain the necessary JRE command-line options to specify the required GC, which
# will override the default of `-XX:+UseParallelGC` (example: -XX:+UseG1GC).
# - HTTPS_PROXY: The location of the https proxy. (example: "[email protected]:8080")
# - HTTP_PROXY: The location of the http proxy. (example: "[email protected]:8080")
# - NO_PROXY: A comma separated lists of hosts, IP addresses or domains that can be
# accessed directly. (example: "foo.example.com,bar.example.com")
#
###
FROM registry.access.redhat.com/ubi9/openjdk-21:1.17-2
FROM eclipse-temurin:21-jre-jammy

ENV LANGUAGE='en_US:en'

RUN apt update && apt install -y avahi-utils && apt clean

# We make four distinct layers so if there are application changes the library layers can be re-used
COPY --chown=185 build/quarkus-app/lib/ /deployments/lib/
Expand All @@ -93,5 +16,12 @@ USER 185
ENV JAVA_OPTS_APPEND="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager -Xmx16m"
ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"

ENTRYPOINT [ "/opt/jboss/container/java/run/run-java.sh" ]
ENTRYPOINT [\
"java",\
"-Dquarkus.http.host=0.0.0.0",\
"-Djava.util.logging.manager=org.jboss.logmanager.LogManager",\
"-Xmx16m",\
"-jar",\
"/deployments/quarkus-run.jar"\
]

14 changes: 8 additions & 6 deletions src/main/docker/systemd-example.service
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill arylic-mqtt
ExecStartPre=-/usr/bin/docker rm arylic-mqtt
ExecStart=/usr/bin/docker run \
--name arylic-mqtt \
-e MQTT_HOST=192.168.1.152 \
-e MQTT_PORT=1883 \
-e arylic_devices_0__ip=192.168.1.73 \
-p 8085:8080 \
ghcr.io/mouse256/arylic-mqtt:latest
--name arylic-mqtt \
--net host \
-e MQTT_HOST=192.168.1.152 \
-e MQTT_PORT=1883 \
-e QUARKUS_HTTP_PORT=8080 \
-v /var/run/dbus:/var/run/dbus \
-v /var/run/avahi-daemon/socket:/var/run/avahi-daemon/socket \
ghcr.io/mouse256/arylic-mqtt:latest
Restart=always
RestartSec=5

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/acme/Controller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Controller : ArylicConnection.Callbacks {
fun onStart(@Observes ev: StartupEvent) {
log.info { "onStart. Discovery time: ${arylicConfig.discoveryTimer()}" }
vertx.setPeriodic(0, arylicConfig.discoveryTimer().toMillis()) { _ ->
log.info { "re-connecting" }
log.debug { "re-connecting" }
arylicConfig.devices()
.map { d -> Device(d.ip(), d.port().orElse(Device.DEFAULT_PORT)) }
.forEach { tryConnect(it) }
Expand Down Expand Up @@ -90,7 +90,7 @@ class Controller : ArylicConnection.Callbacks {


private fun tryConnect(cfg: Device) {
log.info { "tyyConnect" }
log.debug { "tryConnect" }
synchronized(connections) {
for (conn in connections) {
if (conn.value.device == cfg) {
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ arylic.ping-timer=PT5m
quarkus.log.level=INFO
quarkus.log.category."org.acme".level=INFO

#
mqtt.host=127.0.0.1
mqtt.port=1883
mqtt.client-id=arylic-mqtt
Expand Down

0 comments on commit d5eb824

Please sign in to comment.