Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
digitalstrom_mqtt_gateway_in_a_docker_container [06.07.2024 00:56] – created Pascal Suter | digitalstrom_mqtt_gateway_in_a_docker_container [06.07.2024 09:47] (current) – Pascal Suter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== DigitalSTROM MQTTG Gateway in a Docker container ====== | ====== DigitalSTROM MQTTG Gateway in a Docker container ====== | ||
- | **this is WIP** | + | this is how i set up Chriss Gross' [[https:// |
- | this is how i set up Chriss Gross' [[https:// | + | |
+ | on a personal note i want to add: kudos to node.js for still being able to run a script that hasn't been touched for 9 years without modifying a single line of code! this actually makes the need to dockerize this quite questionable. however, i still perfer it this way in my current setup :) | ||
+ | |||
+ | so here is how i set it up: | ||
<code yaml docker-compose.yml> | <code yaml docker-compose.yml> | ||
- | version: " | + | # create ./app directory with uid and gid 1000 first |
services: | services: | ||
node: | node: | ||
- | | + | |
- | user: " | + | restart: always |
- | working_dir: | + | user: 1000:1000 |
+ | working_dir: | ||
environment: | environment: | ||
- NODE_ENV=production | - NODE_ENV=production | ||
volumes: | volumes: | ||
- | - ./app:/home/node/app | + | - ./app:/app |
- | # expose: | + | |
- | # - " | + | |
- | # ports: # use if it is necessary to expose the container to the host machine | + | |
- | # - " | + | |
- | # command: "npm start" | + | |
- | command: "/ | + | |
</ | </ | ||
- | | + | |
chown 1000.1000 app | chown 1000.1000 app | ||
- | | + | |
- | npm install --save mqtt-dss-bridge | + | < |
- | | + | # |
- | docker compose | + | USERNAME=" |
+ | PASSWORD=" | ||
+ | MQTT_SERVER=" | ||
+ | |||
+ | if [ -z "$(ls -A /app)" ]; then | ||
+ | echo " | ||
+ | cd /app | ||
+ | npm install --save mqtt-dss-bridge | ||
+ | echo " | ||
+ | echo " | ||
+ | fi | ||
+ | echo " | ||
+ | # start the bridge in the background | ||
+ | cd /app | ||
+ | node / | ||
+ | PID=$! | ||
+ | echo "DSS Bridge started with PID $PID" | ||
+ | # start a loop to monitor the status every second | ||
+ | errcntr=0 | ||
+ | lastUpdate=999 | ||
+ | while true; do | ||
+ | lastUpdate=$(( \ | ||
+ | $(date +%s)\ | ||
+ | -\ | ||
+ | $(date -d "$(\ | ||
+ | mosquitto_sub -h $MQTT_SERVER -u $USERNAME -P $PASSWORD -t dss/ | ||
+ | sed -E -e ' | ||
+ | )" +%s)\ | ||
+ | )) | ||
+ | if [ 10 -lt $lastUpdate ]; then | ||
+ | if [ $errcntr -lt 10 ]; then | ||
+ | let errcntr++ | ||
+ | echo "DSS Bridge state is too old (last updated $lastUpdate seconds ago, max 10 allowed). This was failure $errcntr out of 10 accepted failures" | ||
+ | else | ||
+ | echo "DSS bridge state is still too old ($lastUpdate seconds) after 10 retries. killing mqtt-dss-bridge (pid $PID) and exit with error status 1" | ||
+ | kill $PID | ||
+ | exit 1 | ||
+ | fi | ||
+ | fi | ||
+ | sleep 1 | ||
+ | done | ||
+ | </ | ||
+ | obviously you need to adjust the three variables at the top of the script to match your setup | ||
+ | |||
+ | | ||
+ | |||
+ | <code Dockerfile build/ | ||
+ | FROM node: | ||
+ | ARG DEBIAN_FRONTEND=noninteractive | ||
+ | ENV TZ=Europe/ | ||
+ | # mqtt client | ||
+ | RUN apt-get update && apt-get install -y mosquitto-clients | ||
+ | #RUN npm install --save mqtt-dss-bridge | ||
+ | COPY startup.sh / | ||
+ | CMD / | ||
+ | </ | ||
+ | |||
+ | | ||
+ | docker compose | ||
+ | press '' | ||
nano ./ | nano ./ | ||
+ | |||
+ | now your setup might be different, but i wanted to be able to use a password authentication for mqtt and i wanted to set '' | ||
+ | |||
+ | so in the settings in the '' | ||
+ | <code javascript> | ||
+ | // MQTT-Client | ||
+ | client: { | ||
+ | url: ' | ||
+ | baseTopic: dssTopic, | ||
+ | qos: 2, | ||
+ | username: ' | ||
+ | password: ' | ||
+ | }, | ||
+ | </ | ||
+ | again of course with the correct username and password. | ||
+ | |||
+ | next i had to slightly modify the script in '' | ||
+ | <code javascript> | ||
+ | var clientSettings = { | ||
+ | keepalive: 10, | ||
+ | clientId: config.name, | ||
+ | will: { | ||
+ | topic: config.client.baseTopic + '/ | ||
+ | payload: new Buffer(' | ||
+ | qos: config.client.qos, | ||
+ | retain: true | ||
+ | }, | ||
+ | username: config.client.username, | ||
+ | password: config.client.password | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | with all this done, we can now start the container and hope for it to run ;) | ||
+ | docker compose up -d | ||
+ | |||
+ |