forked from Beetle-II/lumi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
102 lines (93 loc) · 3.13 KB
/
install.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
#!/bin/busybox sh
# shellcheck shell=dash
print_style () {
if [ "$2" = "info" ] ; then
COLOR="96m";
elif [ "$2" = "success" ] ; then
COLOR="92m";
elif [ "$2" = "warning" ] ; then
COLOR="93m";
elif [ "$2" = "danger" ] ; then
COLOR="91m";
else #default color
COLOR="0m";
fi
STARTCOLOR="\e[$COLOR";
ENDCOLOR="\e[0m";
printf "$STARTCOLOR%b$ENDCOLOR\n" "$1";
}
: "${USE_MAC_IN_MQTT_TOPIC:=true}"
: "${MQTT_SERVER:=localhost}"
: "${MQTT_USERNAME:=mqtt}"
: "${MQTT_PASSWORD:=password}"
: "${MQTT_HOSTNAME:=$(uname -n)}"
: "${GIT_REPO_URL:="https://github.com/Hy3n4/lumi.git"}"
GIT_REPO_PATH="/opt/lumi"
LOCALREPO_VC_DIR=$GIT_REPO_PATH/.git
if [ "${USE_MAC_IN_MQTT_TOPIC}" = "true" ]; then
MQTT_TOPIC=$(printf "%s_%s" "${MQTT_HOSTNAME}" "$(ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | sed 's/\://g')")
else
MQTT_TOPIC="${MQTT_HOSTNAME}"
fi
print_style "MQTT_HOSTNAME: ${MQTT_HOSTNAME}" "info"
print_style "MQTT_USERNAME: ${MQTT_USERNAME}" "info"
print_style "MQTT_PASSWORD: ${MQTT_PASSWORD}" "info"
print_style "MQTT_TOPIC: ${MQTT_TOPIC}" "info"
print_style "Checking if dependencies are installed." "info"
INSTALLED_DEPS=$(opkg list-installed | cut -f 1 -d " " | grep -Ec "^node|^git-http|^mpg123|^mpc|^mpd-full")
if [ "${INSTALLED_DEPS}" = 5 ]; then
print_style "Dependencies already installed." "info"
else
print_style "Installing dependencies." "info"
print_style "Running opkg update." "info"; print_style "(This might take a while...)" "warning"
opkg update >/dev/null 2>&1
print_style "Installing packages." "info"
opkg install node git-http mpg123 mpc mpd-full >/dev/null 2>&1
fi
mkdir -p /opt
cd /opt || { print_style "Cannot cd to /opt" "danger"; exit 1; }
[ -d $LOCALREPO_VC_DIR ] || git clone $GIT_REPO_URL $GIT_REPO_PATH
(cd $GIT_REPO_PATH; git pull $GIT_REPO_URL)
cd $GIT_REPO_PATH || { printf '%b\n' "cannot cd to /opt/lumi"; exit 1; }
print_style "Generating lumi config file." "info"
cat <<EOL > /opt/lumi/config.json
{
"sensor_debounce_period": 300,
"sensor_treshhold": 50,
"button_click_duration": 300,
"homeassistant": true,
"tts_cache": true,
"sound_channel": "Master",
"sound_volume": 50,
"mqtt_url": "mqtt://${MQTT_SERVER}",
"mqtt_topic": "lumi/${MQTT_HOSTNAME}",
"use_mac_in_mqtt_topic": ${USE_MAC_IN_MQTT_TOPIC},
"mqtt_options": {
"port": 1883,
"username": "${MQTT_USERNAME}",
"password": "${MQTT_PASSWORD}",
"keepalive": 60,
"reconnectPeriod": 1000,
"clean": true,
"encoding": "utf8",
"will": {
"topic": "lumi/${MQTT_TOPIC}/state",
"payload": "offline",
"qos": 1,
"retain": true
}
}
}
EOL
print_style "Installing service." "info"
chmod +x $GIT_REPO_PATH/lumi
cp $GIT_REPO_PATH/lumi /etc/init.d/lumi
/etc/init.d/lumi enable
/etc/init.d/lumi restart
LUMI_SERVICE_STATUS=$(/etc/init.d/lumi status)
if [ "${LUMI_SERVICE_STATUS}" = "running" ]; then
print_style "lumi service is ${LUMI_SERVICE_STATUS}." "success"
else
print_style "lumi service failed to start and is now ${LUMI_SERVICE_STATUS}." "danger"
fi
print_style "All done!" "success"