Skip to content

Commit 2ac2f69

Browse files
committed
Update to env file
see ct-Open-Source#48
1 parent 1cfed90 commit 2ac2f69

File tree

3 files changed

+105
-31
lines changed

3 files changed

+105
-31
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
data/*
1+
data/*
2+
.env

docker-compose.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3.6'
22

33
services:
44
nodered:
5-
image: ghcr.io/ct-open-source/ctnodered:latest
5+
image: ghcr.io/ct-open-source/ctnodered:${CONTAINER_TAG:-latest}
66
volumes:
77
- ./data/nodered:/data
88
- /etc/localtime:/etc/localtime
@@ -16,8 +16,9 @@ services:
1616
mqtt:
1717
image: "eclipse-mosquitto"
1818
ports:
19-
- "1883:1883"
20-
- "9001:9001"
19+
- "${MQTT_PORT:-1883}:1883"
20+
- "${MQTT_WEBSOCKET_PORT:-9001}:9001"
21+
- "${MQTT_SECURE_PORT:-8883}:8883"
2122
volumes:
2223
- ./data/mqtt:/mosquitto
2324
- /etc/localtime:/etc/localtime
@@ -32,11 +33,10 @@ services:
3233
- /run/udev:/run/udev:ro
3334
- /etc/localtime:/etc/localtime
3435
devices:
35-
- "/dev/ttyACM0:/dev/ttyACM0"
36-
- "/dev/ttyACM1:/dev/ttyACM1"
36+
- "${ZIGBEE_DEVICE:-/dev/ttyACM0}:${ZIGBEE_DEVICE:-/dev/ttyACM0}"
3737
restart: always
3838
privileged: true
3939
ports:
40-
- 1881:1881
40+
- "${ZIGBEE_FRONTEND_PORT:-1881}:1881"
4141
environment:
42-
- TZ=Europe/Berlin
42+
- TZ=Europe/Berlin

start.sh

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
#!/bin/bash
22

33
function detect_zigbee_device {
4-
if usb_dev=$(lsusb -d 0451:); then
5-
usb_dev_count=$(ls -1 /dev/ttyACM* 2>/dev/null | wc -l)
6-
if [ "$usb_dev_count" -gt 1 ]; then
7-
>&2 echo "There are multiple devices connected, that could be Zigbee USB adaptors. Please check data/zigbee/configuration.yml, if the device is wrong. /dev/ttyACM0 is used as the default."
8-
9-
echo "/dev/ttyACM0"
4+
usb_dev_count=0
5+
usb_dev_found="FALSE"
6+
for device in /dev/ttyUSB* /dev/ttyACM*
7+
do
8+
if [ ! -c $device ]; then
9+
continue
1010
fi
11-
12-
if [ -c /dev/ttyACM0 ]; then
13-
echo "/dev/ttyACM0"
14-
else
15-
>&2 echo "I could not find /dev/ttyACM0. Please check your hardware."
11+
12+
VENDOR_PRODUCT=$(udevadm info --name=$device | egrep -i "ID_VENDOR_ID|ID_MODEL_ID" | cut -d'=' -f2 | tr '\n' ':')
13+
# Texas Instruments USB device - Vendor: 0451
14+
# slaesh’s CC2652RB stick - Vendor: 10c4
15+
if [ "$(echo ${VENDOR_PRODUCT} | egrep '^0451:|^10c4:')" != "" ]
16+
then
17+
((usb_dev_count=usb_dev_count+1))
18+
usb_dev_found="$device"
19+
>&2 echo "📄 Found Device #$usb_dev_count $device (vendor:product=${VENDOR_PRODUCT}) that could be Zigbee USB adaptor"
1620
fi
17-
else
18-
>&2 echo No Texas Instruments USB device found.
21+
done
1922

20-
echo "False"
23+
if [ "$usb_dev_count" -gt 1 ]; then
24+
>&2 echo "⚠️ There are multiple devices connected, that could be Zigbee USB adaptors. Please check data/zigbee/configuration.yml, if the device is wrong. $usb_dev_found is used as the default."
2125
fi
26+
27+
if [ "$usb_dev_count" -eq 0 ]; then
28+
>&2 echo "⚠️ No Texas Instruments USB device nor slaesh’s CC2652RB stick found for zigbee2mqtt"
29+
fi
30+
echo "$usb_dev_found"
2231
}
2332

2433
function create_mosquitto_config {
@@ -41,13 +50,19 @@ touch data/mqtt/config/passwd
4150
}
4251

4352
function create_zigbee2mqtt_config {
53+
# zigbee2mqtt device
54+
device="$1"
55+
4456
cat > data/zigbee/configuration.yaml <<EOF
4557
# Home Assistant integration (MQTT discovery)
4658
homeassistant: true
4759
4860
# allow new devices to join
4961
permit_join: true
5062
63+
serial:
64+
port: $device
65+
5166
# enable frontend
5267
frontend:
5368
port: 1881
@@ -74,6 +89,48 @@ echo '⚠️ Disable permit_join in data/zigbee/configuration.yaml or the Zigbe
7489

7590
}
7691

92+
function create_compose_env {
93+
# zigbee2mqtt device
94+
device="$1"
95+
96+
cat > .env <<EOF
97+
# Container-Tag listed in README.md (e.g.: latest-14, devel, devel-14, ...)
98+
# Default = latest
99+
CONTAINER_TAG=latest
100+
101+
#
102+
# MQTT Ports for mosquitto
103+
# Default:
104+
# - 1883 insecure
105+
# - 9001 websocket
106+
# - 8883 secure (must be configured)
107+
MQTT_PORT=1883
108+
MQTT_WEBSOCKET_PORT=9001
109+
MQTT_SECURE_PORT=8883
110+
111+
# Port for access to zigbee2mqtt Frontend
112+
ZIGBEE_FRONTEND_PORT=1881
113+
114+
EOF
115+
if [ "$device" != "FALSE" ] ; then
116+
cat >> .env <<EOF
117+
# Device mounted into zigbee2mqtt container
118+
ZIGBEE_DEVICE=$device
119+
EOF
120+
else
121+
cat >> .env <<EOF
122+
# Device mounted into zigbee2mqtt container
123+
# ZIGBEE_DEVICE=$device
124+
# Uncomment line ZIGBEE_DEVICE and replace $device with device path like /dev/ttyXXX
125+
# also edit data/zigbee/configuration.yaml to set the same device!
126+
EOF
127+
128+
fi
129+
echo '⚠️ Check .env for correct versions, ports and zigbee2mqtt-device'
130+
}
131+
132+
133+
77134
function fix_permissions {
78135
echo '📄 Setting the permissions of the configurations in the data folder.'
79136
sudo chown 1883:1883 data/mqtt
@@ -84,17 +141,26 @@ function fix_permissions {
84141

85142

86143
function build_data_structure {
87-
echo '📄 Configuration folder ./data is missing. Creating it from scratch.'
88144
mkdir -p data/mqtt/config
89145
mkdir -p data/zigbee/
90146
mkdir -p data/nodered/
91147

148+
# zigbee2mqtt device
149+
device="$1"
150+
92151
if [ ! -f data/mqtt/config/mosquitto.conf ]; then
152+
echo '📄 Configuration file data/mqtt/config/mosquitto.conf is missing. Creating it from scratch.'
93153
create_mosquitto_config
94154
fi
95155

96-
if [ ! -f data/zigbee/configuration.yaml ]; then
97-
create_zigbee2mqtt_config
156+
if [[ ! -f data/zigbee/configuration.yaml && "$device" != "FALSE" ]]; then
157+
echo '📄 Configuration file data/zigbee/configuration.yaml is missing. Creating it from scratch.'
158+
create_zigbee2mqtt_config "$device"
159+
fi
160+
161+
if [ ! -f .env ]; then
162+
echo '📄 Configuration file .env is missing. Creating it from scratch.'
163+
create_compose_env "$device"
98164
fi
99165

100166
fix_permissions
@@ -110,6 +176,13 @@ function check_dependencies {
110176
echo '⚠️ Error: git is not installed.' >&2
111177
exit 1
112178
fi
179+
180+
if ! [ -x "$(command -v udevadm)" ]; then
181+
echo '⚠️ Error: udevadm is not installed.' >&2
182+
exit 1
183+
fi
184+
185+
113186
}
114187

115188
function start {
@@ -120,10 +193,9 @@ function start {
120193
container="nodered mqtt"
121194
fi
122195

123-
if [ ! -d data ]; then
124-
build_data_structure
125-
fi
126-
196+
# Build data structure with default file if not existing
197+
build_data_structure "$device"
198+
127199
echo '🏃 Starting the containers'
128200
docker-compose up -d $container
129201
echo '⚠️ After you made yourself familiar with the setup, it'"'"'s strongly suggested to secure the services. Read the "Security" section in the README!'
@@ -188,7 +260,8 @@ case "$1" in
188260
fix_permissions
189261
;;
190262
"data")
191-
build_data_structure
263+
device=$(detect_zigbee_device)
264+
build_data_structure "$device"
192265
;;
193266
* )
194267
cat << EOF
@@ -204,4 +277,4 @@ start.sh data – set up the data folder needed for the containers, but run none
204277
Check https://github.com/ct-Open-Source/ct-Smart-Home/ for updates.
205278
EOF
206279
;;
207-
esac
280+
esac

0 commit comments

Comments
 (0)