forked from ct-Open-Source/ct-Smart-Home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
executable file
·167 lines (141 loc) · 3.92 KB
/
start.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
function detect_zigbee_device {
if usb_dev=$(lsusb -d 0451:); then
usb_dev_count=$(ls -1 /dev/ttyACM* 2>/dev/null | wc -l)
if [ "$usb_dev_count" -gt 1 ]; then
>&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."
echo "/dev/ttyACM0"
fi
if [ -c /dev/ttyACM0 ]; then
echo "/dev/ttyACM0"
else
>&2 echo "I could not find /dev/ttyACM0. Please check your hardware."
fi
else
>&2 echo No Texas Instruments USB device found.
echo "False"
fi
}
function create_zigbee2mqtt_config {
key=$(dd if=/dev/urandom bs=1 count=16 2>/dev/null | od -A n -t x1 | awk '{printf "["} {for(i = 1; i< NF; i++) {printf "0x%s, ", $i}} {printf "0x%s]\n", $NF}')
echo "Zigbee2Mqtt configuration is missing. creating it."
echo
echo
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "This is your random Zigbee encryption key:"
echo
echo $key
echo
echo "Store it safely or you will have to repair all of your devices."
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo
echo
cat > data/zigbee/configuration.yaml <<EOF
# Home Assistant integration (MQTT discovery)
homeassistant: false
# allow new devices to join
permit_join: true
# MQTT settings
mqtt:
# MQTT base topic for zigbee2mqtt MQTT messages
base_topic: zigbee2mqtt
# MQTT server URL
server: 'mqtt://mqtt'
# MQTT server authentication, uncomment if required:
# user: my_user
# password: my_password
# Serial settings
serial:
# Location of CC2531 USB sniffer
port: /dev/ttyACM0
disable_led: false
advanced:
channel: 25
network_key: $key
EOF
echo "Disable permit_join in data/zigbee/configuration.yaml after you have paired all of your devices!"
}
function build_data_structure {
echo "data folder is missing. creating it"
mkdir -p data/mqtt/config
mkdir -p data/zigbee/
mkdir -p data/nodered/
touch data/mqtt/config/mosquitto.conf
if [ ! -f data/zigbee/configuration.yaml ]; then
create_zigbee2mqtt_config
fi
sudo chown 1883:1883 data/mqtt
sudo chown -R 1883:1883 data/mqtt/*
sudo chown 1001:1001 data/nodered
sudo chown -Rf 1001:1001 data/nodered/*
}
function check_dependencies {
if ! [ -x "$(command -v docker-compose)" ]; then
echo 'Error: docker-compose is not installed.' >&2
exit 1
fi
if ! [ -x "$(command -v git)" ]; then
echo 'Error: git is not installed.' >&2
exit 1
fi
}
function start {
device=$(detect_zigbee_device)
if [ $device == "False" ]; then
echo "No Zigbee adaptor found. Not starting Zigbee2MQTT."
container="nodered mqtt"
fi
if [ ! -d data ]; then
build_data_structure
fi
echo "Starting the containers"
docker-compose up -d $container
}
function stop {
echo "Stopping all containers"
docker-compose stop
}
function update {
echo "Shutting down all running containers and removing them."
docker-compose down
if [ ! $? -eq 0 ]; then
echo "Updating failed. Please check the repository on GitHub."
fi
echo "Pulling latest release via git."
git fetch --tags
latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)
git checkout $latestTag
if [ ! $? -eq 0 ]; then
echo "Updating failed. Please check the repository on GitHub."
fi
echo "Pulling docker images."
docker-compose pull
if [ ! $? -eq 0 ]; then
echo "Updating failed. Please check the repository on GitHub."
fi
start
}
check_dependencies
case "$1" in
"start")
start
;;
"stop")
stop
;;
"update")
update
;;
"data")
build_data_structure
;;
* )
echo "c't-Smart-Home – setup script"
echo "============================="
echo "Usage:"
echo "start.sh update – to update this copy of the repo"
echo "start.sh start – run all containers"
echo "start.sh stop – stop all containers"
echo "start.sh data – set up the data folder needed for the containers, but run none of them. Useful for personalized setups."
;;
esac