forked from hjelev/rpi-mqtt-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·135 lines (116 loc) · 3.1 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
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
welcome(){
echo " This script will install if not installed: python-pip and python module paho-mqtt,"
echo " configure Raspberry Pi MQTT monitor and create a cronjob to run it."
read -r -p " Do you want to proceed? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
printf ""
else
exit
fi
}
find_python(){
if [[ $(python --version) ]]; then
python=$(which python)
pip="python-pip"
pip_run='pip'
else
python=$(which python3)
pip="python3-pip"
pip_run='pip3'
fi
if [[ "$python" == *"python"* ]]; then
print_green "+ Found: $python"
else
print_yellow "Python not found!\n Exiting\n"
exit
fi
}
printm(){
length=$(expr length "$1")
length=$(($length + 4))
printf "\n"
#printf -- '-%.0s' $(seq $length); echo ""
printf ":: $1 \n\n"
#printf -- '-%.0s' $(seq $length); echo ""
}
print_green(){
tput setaf 2; echo "$1"
tput sgr 0
}
print_yellow(){
tput setaf 3; printf "$1"
tput sgr 0
}
check_and_install_pip(){
pip_ver=$(${python} -m pip --version 2>&1);
if [[ "$pip_ver" == *"No"* ]]; then
echo "- Pip is not installed, installing it."
sudo apt install $pip
else
print_green "+ Found: $pip"
fi
}
install_requirements(){
printm "Installing requirements"
sudo $pip_run install -r requirements.txt
}
update_config(){
print_green "+ Copy config.py.example to config.py"
cp src/config.py.example src/config.py
printm "MQTT settings"
printf "Enter mqtt_host: "
read HOST
sed -i "s/ip address or host/${HOST}/" src/config.py
printf "Enter mqtt_user: "
read USER
sed -i "s/username/${USER}/" src/config.py
printf "Enter mqtt_password: "
read PASS
sed -i "s/\"password/\"${PASS}/" src/config.py
printf "Enter mqtt_port (default is 1883): "
read PORT
if [ -z "$PORT" ]; then
PORT=1883
fi
sed -i "s/1883/${PORT}/" src/config.py
printf "Enter mqtt_topic_prefix (default is rpi-MQTT-monitor): "
read TOPIC
if [ -z "$TOPIC" ]; then
TOPIC=rpi-MQTT-monitor
fi
sed -i "s/rpi-MQTT-monitor/${TOPIC}/" src/config.py
print_green "+ config.py is updated with provided settings"
}
set_cron(){
printm "Setting Cronjob"
cwd=$(pwd)
crontab -l > tempcron
if grep -q rpi-cpu2mqtt.py tempcron; then
cronfound=$(grep rpi-cpu2mqtt.py tempcron)
print_yellow " There is already a cronjob running rpi-cpu2mqtt.py - skipping cronjob creation.\n"
print_yellow " If you want the cronjob to be automatically created remove the line below from your\n cronjobs list and run the installer again.\n\n"
echo " ${cronfound}"
else
printf "How often do you want the script to run in minutes? (default is 2): "
read MIN
if [ -z "$MIN" ]; then
MIN=2
fi
echo "Adding the line below to your crontab"
echo "*/${MIN} * * * * ${python} ${cwd}/src/rpi-cpu2mqtt.py"
echo "*/${MIN} * * * * ${python} ${cwd}/src/rpi-cpu2mqtt.py" >> tempcron
crontab tempcron
fi
rm tempcron
}
main(){
printm "Raspberry Pi MQTT Monitor installer"
welcome
find_python
check_and_install_pip
install_requirements
update_config
set_cron
printm "Done"
}
main