forked from ct-Open-Source/ct-Smart-Home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
executable file
·178 lines (149 loc) · 3.99 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
168
169
170
171
172
173
174
175
176
177
178
#!/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 detect_arch {
uname_arch=$(uname -m)
if [[ $uname_arch == *"x86_64"* ]]; then
echo "amd64"
elif [[ $uname_arch == *"arm"* ]]; then
echo "arm"
else
echo "unknown"
fi
}
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"
architecture=$(detect_arch)
echo "CPU architecture is: "$architecture
if [ $architecture == "unknown" ]; then
echo 'Error: Only amd64 and arm32v7 are supported'
exit 1
fi
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
echo "Pulling current version via git."
git pull
echo "Pulling current 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 "setup.sh update – to update to this copy of the repo"
echo "setup.sh start – run all containers"
echo "setup.sh stop – stop all containers"
echo "setup.sh data – set up the data folder needed for the containers, but run none of them. Useful for personalized setups."
;;
esac