forked from openenergymonitor/emonhub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·166 lines (137 loc) · 5.91 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
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
#!/bin/bash
# -------------------------------------------------------------
# emonHub install and update script
# -------------------------------------------------------------
# Assumes emonhub repository installed via git:
# git clone https://github.com/openenergymonitor/emonhub.git
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
echo "EmonHub directory: $script_dir"
# User input: is this a raspberrypi environment that requires serial configuration
emonSD_pi_env=0
if [ -z "$1" ]; then
read -p 'Apply raspberrypi serial configuration? (y/n): ' input
if [ "$input" == "y" ] || [ "$input" == "Y" ]; then
emonSD_pi_env=1
fi
else
openenergymonitor_dir=$1
cd $openenergymonitor_dir/EmonScripts/update
source load_config.sh
echo "emonSD_pi_env provided in arg = $emonSD_pi_env"
fi
# User input: check username to install emonhub with
if [ -z "$2" ]; then
user=$USER
read -p "Would you like to install emonhub under the $USER user? (y/n): " input
if [ "$input" != "y" ] && [ "$input" != "Y" ]; then
echo "Please switch to the user that you wish emonhub to be installed under"
exit 0
fi
echo "Running apt update"
sudo apt update
else
user=$2
echo "user provided as arg = $user"
fi
echo "installing or updating emonhub dependencies"
sudo apt-get install -y python3-serial python3-configobj python3-pip python3-pymodbus bluetooth libbluetooth-dev python3-spidev
# FIXME paho-mqtt V2 has new API. stick to V1.x for now
pip install --upgrade paho-mqtt==1.6.1
pip install requests py-sds011 sdm_modbus minimalmodbus
# Custom rpi-rfm69 library used for SPI RFM69 Low Power Labs interfacer
pip3 install https://github.com/openenergymonitor/rpi-rfm69/archive/refs/tags/v0.3.0-oem-4.zip
if [ "$emonSD_pi_env" = 1 ]; then
boot_config=/boot/config.txt
if [ -f /boot/firmware/config.txt ]; then
boot_config=/boot/firmware/config.txt
fi
echo "installing or updating raspberry pi related dependencies"
# Only install the GPIO library if on a Pi. Used by Pulse interfacer
pip3 install RPi.GPIO
# RaspberryPi Serial configuration
# disable Pi3 Bluetooth and restore UART0/ttyAMA0 over GPIOs 14 & 15;
# Review should this be: dtoverlay=miniuart-bt?
echo "Disabling Bluetooth"
sudo sed -i -n '/dtoverlay=disable-bt/!p;$a dtoverlay=disable-bt' $boot_config
# Enable SPI
sudo sed -i 's/#dtparam=spi=on/dtparam=spi=on/' $boot_config
# We also need to stop the Bluetooth modem trying to use UART
echo "Stop Bluetooth modem"
sudo systemctl disable hciuart
boot_cmdline=/boot/cmdline.txt
if [ -f /boot/firmware/cmdline.txt ]; then
boot_cmdline=/boot/firmware/cmdline.txt
fi
# Remove console from /boot/cmdline.txt
echo "Remove console from /boot/cmdline.txt"
sudo sed -i "s/console=serial0,115200 //" $boot_cmdline
# stop and disable serial service??
echo "Stop and disable serial service"
sudo systemctl stop [email protected]
sudo systemctl disable [email protected]
sudo systemctl mask [email protected]
fi
# this should not be needed on main user but could be re-enabled
# sudo useradd -M -r -G dialout,tty -c "emonHub user" emonhub
# ---------------------------------------------------------
# EmonHub config file
# ---------------------------------------------------------
if [ ! -d /etc/emonhub ]; then
echo "Creating /etc/emonhub directory"
sudo mkdir /etc/emonhub
sudo chown $user:root /var/log/emonhub
else
echo "/etc/emonhub directory already exists"
sudo chown $user:root /var/log/emonhub
fi
if [ ! -f /etc/emonhub/emonhub.conf ]; then
sudo cp $script_dir/conf/emonpi2.default.emonhub.conf /etc/emonhub/emonhub.conf
echo "No existing emonhub.conf configuration file found, installing default"
# requires write permission for configuration from emoncms:config module
sudo chmod 666 /etc/emonhub/emonhub.conf
echo "emonhub.conf permissions adjusted to 666"
# Temporary: replace with update to default settings file
sudo sed -i "s/loglevel = DEBUG/loglevel = WARNING/" /etc/emonhub/emonhub.conf
echo "Default emonhub.conf log level set to WARNING"
fi
# Fix emonhub log file permissions
if [ -d /var/log/emonhub ]; then
echo "Setting ownership of /var/log/emonhub to $user"
sudo chown $user:root /var/log/emonhub
fi
if [ -f /var/log/emonhub/emonhub.log ]; then
echo "Setting ownership of /var/log/emonhub/emonhub.log to $user and permissions to 644"
sudo chown $user:$user /var/log/emonhub/emonhub.log
sudo chmod 644 /var/log/emonhub/emonhub.log
fi
# ---------------------------------------------------------
# Symlink emonhub source to /usr/local/bin/emonhub
# ---------------------------------------------------------
echo "Installing /usr/local/bin/emonhub symlink"
sudo ln -sf $script_dir/src /usr/local/bin/emonhub
# ---------------------------------------------------------
# Install service
# ---------------------------------------------------------
if [ -d /lib/systemd/system ]; then
if [ ! -f /lib/systemd/system/emonhub.service ]; then
echo "Installing emonhub.service in /lib/systemd/system (creating symlink)"
sudo ln -sf $script_dir/service/emonhub.service /lib/systemd/system
sudo systemctl enable emonhub.service
sudo systemctl restart emonhub.service
else
echo "emonhub.service already installed"
fi
fi
if [ "$user" != "pi" ]; then
echo "installing emonhub drop-in User=$user"
if [ ! -d /lib/systemd/system/emonhub.service.d ]; then
sudo mkdir /lib/systemd/system/emonhub.service.d
fi
echo $'[Service]\nUser='$user$'\nEnvironment="USER='$user'"' > emonhub.service.conf
sudo mv emonhub.service.conf /lib/systemd/system/emonhub.service.d/emonhub.conf
fi
sudo systemctl daemon-reload
sudo systemctl restart emonhub.service
state=$(systemctl show emonhub | grep ActiveState)
echo "- Service $state"
# ---------------------------------------------------------