22
33# # Install the Grott proxy server on the current system
44# #
5- # # install_grott(String install|remove, String username, String openhab-ip-address )
5+ # # install_grott(String install|remove)
66# #
77install_grott () {
8+ # Fail on errors
9+ set -e
10+
811 # Validate install type argument
912 if [ " $# " -lt 1 ]; then
1013 echo " Error: Please supply install type."
11- echo " Usage: $0 <install|remove> <username> (<openhab-ip>) "
14+ echo " Usage: $0 <install|remove>"
1215 exit 1
1316 elif [[ " $1 " != " install" && " $1 " != " remove" ]]; then
1417 echo " Error: Invalid install type '$1 '. Use 'install' or 'remove'."
1518 exit 1
1619 fi
17- INSTALL_TYPE=" $1 "
1820
19- # Validate user name argument
20- if [ " $# " -lt 2 ]; then
21- echo " Error: Please supply user name."
22- echo " Usage: $0 <install|remove> <username> (<openhab-ip>)"
23- exit 1
24- elif ! id " $2 " & > /dev/null; then
25- echo " Error: User '$2 ' does not exist."
26- exit 1
27- fi
28- USERNAME=" $2 "
29- INSTALL_DIR=" /home/${2} /grott"
30-
31- SERVICE_FILE=" /etc/systemd/system/grott.service"
21+ local INSTALL_TYPE=" $1 "
22+ local USERNAME=${username:- openhabian}
23+ local INSTALL_DIR=" /home/${USERNAME} /grott"
24+ local SERVICE_FILE=" /etc/systemd/system/grott.service"
3225
3326 if [[ $INSTALL_TYPE == " install" ]]; then
34- # Validate openhab ip address argument
35- if [ " $# " -lt 3 ]; then
36- echo " Error: Please supply openHAB IP address."
37- echo " Usage: $0 <install|remove> <username> <openhab-ip>"
38- exit 1
39- elif ! [[ " $3 " =~ ^[0-9]+\. [0-9]+\. [0-9]+\. [0-9]+$ ]]; then
40- echo " Error: Invalid IP address format: $3 "
27+ # Get default IPv4 address
28+ local ipAddress=" $( ip route get 8.8.8.8 | awk ' {print $7}' | xargs) "
29+ if ! [[ " $ipAddress " =~ ^[0-9]+\. [0-9]+\. [0-9]+\. [0-9]+$ ]]; then
30+ echo " Error: Invalid IP address format: $ipAddress "
4131 exit 1
4232 fi
43- EXT_URL=" http://${3 } :8080/growatt"
33+ local EXT_URL=" http://${ipAddress } :8080/growatt"
4434
4535 echo -n " [openHABian] Installing Grott Proxy with extension URL: $EXT_URL "
4636
@@ -49,16 +39,17 @@ install_grott() {
4939 sudo apt install -y python3 python3-pip
5040 sudo pip3 install paho-mqtt requests # paho-mqtt is a required dependency (even if disabled)
5141
52- # Prepare installation directory and populate it with Grott files
53- sudo -u " $USERNAME " mkdir -p " $INSTALL_DIR "
42+ # Prepare installation directory
43+ sudo -u " $USERNAME " mkdir -p -m 755 " $INSTALL_DIR "
5444 if [ ! -d " $INSTALL_DIR " ]; then
5545 echo " Error: Installation directory $INSTALL_DIR does not exist."
5646 exit 1
5747 fi
5848
59- download_grott_files || {
60- echo " Failed to download Grott files. Exiting."
61- return 1
49+ # Download Grott Python files to installation directory
50+ download_grott_files " $INSTALL_DIR " || {
51+ echo " Failed to download Grott files. Exiting."
52+ return 1
6253 }
6354
6455 # Create grott.service systemd file from template
@@ -121,34 +112,40 @@ install_grott() {
121112 fi
122113}
123114
115+ # # Download the Grott Proxy Python files
116+ # #
117+ # # download_grott_files(String install_directory)
118+ # #
124119download_grott_files () {
125- GROTT_FILE_URL=" https://raw.githubusercontent.com/johanmeijer/grott/master"
126- GROTT_PY_FILES=(
127- " grott.py"
128- " grottconf.py"
129- " grottdata.py"
130- " grottproxy.py"
131- " grottserver.py"
132- " grottsniffer.py"
120+ local INSTALL_DIR=" $1 "
121+ local GROTT_FILE_URL=" https://raw.githubusercontent.com/johanmeijer/grott/master"
122+ local GROTT_PY_FILES=(
123+ " grott.py"
124+ " grottconf.py"
125+ " grottdata.py"
126+ " grottproxy.py"
127+ " grottserver.py"
128+ " grottsniffer.py"
133129 )
134- GROTT_EXT_PY_FILE=" grottext.py"
135-
130+ local GROTT_EXT_PY_FILE=" grottext.py"
131+ local file url
132+
136133 # Download Grott main program Python files
137134 for file in " ${GROTT_PY_FILES[@]} " ; do
138135 url=" ${GROTT_FILE_URL} /${file} "
139136 echo " Downloading $file "
140137 curl -fsSL " $url " -o " ${INSTALL_DIR} /${file} " || {
141- echo " Failed to download $file "
142- return 1
138+ echo " Failed to download $file "
139+ return 1
143140 }
144141 done
145-
142+
146143 # Download Grott extension Python file
147- file=$GROTT_EXT_PY_FILE
144+ file=" $GROTT_EXT_PY_FILE "
148145 url=" ${GROTT_FILE_URL} /examples/Extensions/${file} "
149146 echo " Downloading $file "
150147 curl -fsSL " $url " -o " ${INSTALL_DIR} /${file} " || {
151- echo " Failed to download $file "
152- return 1
148+ echo " Failed to download $file "
149+ return 1
153150 }
154151}
0 commit comments