Skip to content

Commit

Permalink
Made install and upgrade easier
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Dec 20, 2024
1 parent f7c0d12 commit 331177a
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v1.0.9-dev
* Changed: Broker port missing on reconnect
* Changed: Default device instance is now `100`
* Changed: Fixed service not starting sometimes

## v0.1.8
Expand Down
119 changes: 64 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
1. [Config](#config)
1. [JSON structure](#json-structure)
1. [Home Assistant](#home-assistant)
1. [Install](#install)
1. [Install / Update](#install--update)
1. [Uninstall](#uninstall)
1. [Restart](#restart)
1. [Debugging](#debugging)
1. [Multiple instances](#multiple-instances)
1. [Compatibility](#compatibility)
1. [Screenshots](#screenshots)

Expand Down Expand Up @@ -176,98 +175,108 @@ action:
In the `config.ini` of `dbus-mqtt-pv` set the MQTT broker to the Home Assistant hostname/IP and the topic to the same as in your Home Assistant config (like above).


## Install
## Install / Update

1. Login to your Venus OS device via SSH. See [Venus OS:Root Access](https://www.victronenergy.com/live/ccgx:root_access#root_access) for more details.

2. Execute this commands to download and extract the files:
2. Execute this commands to download and copy the files:

```bash
# change to temp folder
cd /tmp
wget -O /tmp/download_dbus-mqtt-pv.sh https://raw.githubusercontent.com/mr-manuel/venus-os_dbus-mqtt-pv/master/download.sh
# download driver
wget -O /tmp/venus-os_dbus-mqtt-pv.zip https://github.com/mr-manuel/venus-os_dbus-mqtt-pv/archive/refs/heads/master.zip
# If updating: cleanup old folder
rm -rf /tmp/venus-os_dbus-mqtt-pv-master
bash /tmp/download_dbus-mqtt-pv.sh
```

# unzip folder
unzip venus-os_dbus-mqtt-pv.zip
3. Select the version you want to install.

# If updating: backup existing config file
mv /data/etc/dbus-mqtt-pv/config.ini /data/etc/dbus-mqtt-pv_config.ini
4. Press enter for a single instance. For multiple instances, enter a number and press enter.

# If updating: cleanup existing driver
rm -rf /data/etc/dbus-mqtt-pv
Example:

# copy files
cp -R /tmp/venus-os_dbus-mqtt-pv-master/dbus-mqtt-pv/ /data/etc/
- Pressing enter or entering `1` will install the driver to `/data/etc/dbus-mqtt-pv`.
- Entering `2` will install the driver to `/data/etc/dbus-mqtt-pv-2`.

# If updating: restore existing config file
mv /data/etc/dbus-mqtt-pv_config.ini /data/etc/dbus-mqtt-pv/config.ini
```
### Extra steps for your first installation

3. Copy the sample config file, if you are installing the driver for the first time and edit it to your needs.
5. Edit the config file to fit your needs. The correct command for your installation is shown after the installation.

- If you pressed enter or entered `1` during installation:
```bash
# copy default config file
cp /data/etc/dbus-mqtt-pv/config.sample.ini /data/etc/dbus-mqtt-pv/config.ini
# edit the config file with nano
nano /data/etc/dbus-mqtt-pv/config.ini
```

4. Run `bash /data/etc/dbus-mqtt-pv/install.sh` to install the driver as service.

The daemon-tools should start this service automatically within seconds.

## Uninstall
- If you entered `2` during installation:
```bash
nano /data/etc/dbus-mqtt-pv-2/config.ini
```

Run `/data/etc/dbus-mqtt-pv/uninstall.sh`
6. Install the driver as a service. The correct command for your installation is shown after the installation.

## Restart
- If you pressed enter or entered `1` during installation:
```bash
bash /data/etc/dbus-mqtt-pv/install.sh
```

Run `/data/etc/dbus-mqtt-pv/restart.sh`
- If you entered `2` during installation:
```bash
bash /data/etc/dbus-mqtt-pv-2/install.sh
```

## Debugging
The daemon-tools should start this service automatically within seconds.

The logs can be checked with `tail -n 100 -f /data/log/dbus-mqtt-pv/current | tai64nlocal`
## Uninstall

The service status can be checked with svstat `svstat /service/dbus-mqtt-pv`
⚠️ If you have multiple instances, ensure you choose the correct one. For example:

This will output somethink like `/service/dbus-mqtt-pv: up (pid 5845) 185 seconds`
- To uninstall the default instance:
```bash
bash /data/etc/dbus-mqtt-pv/uninstall.sh
```

If the seconds are under 5 then the service crashes and gets restarted all the time. If you do not see anything in the logs you can increase the log level in `/data/etc/dbus-mqtt-pv/dbus-mqtt-pv.py` by changing `level=logging.WARNING` to `level=logging.INFO` or `level=logging.DEBUG`
- To uninstall the second instance:
```bash
bash /data/etc/dbus-mqtt-pv-2/uninstall.sh
```

If the script stops with the message `dbus.exceptions.NameExistsException: Bus name already exists: com.victronenergy.pvinverter.mqtt_pv"` it means that the service is still running or another service is using that bus name.
## Restart

## Multiple instances
⚠️ If you have multiple instances, ensure you choose the correct one. For example:

It's possible to have multiple instances, but it's not automated. Follow these steps to achieve this:
- To restart the default instance:
```bash
bash /data/etc/dbus-mqtt-pv/restart.sh
```

1. Save the new name to a variable `driverclone=dbus-mqtt-pv-2`
- To restart the second instance:
```bash
bash /data/etc/dbus-mqtt-pv-2/restart.sh
```

2. Copy current folder `cp -r /data/etc/dbus-mqtt-pv/ /data/etc/$driverclone/`
## Debugging

3. Rename the main script `mv /data/etc/$driverclone/dbus-mqtt-pv.py /data/etc/$driverclone/$driverclone.py`
⚠️ If you have multiple instances, ensure you choose the correct one.

4. Fix the script references for service and log
- To check the logs of the default instance:
```bash
tail -n 100 -F /data/log/dbus-mqtt-pv/current | tai64nlocal
```
sed -i 's:dbus-mqtt-pv:'$driverclone':g' /data/etc/$driverclone/service/run
sed -i 's:dbus-mqtt-pv:'$driverclone':g' /data/etc/$driverclone/service/log/run

- To check the logs of the second instance:
```bash
tail -n 100 -F /data/log/dbus-mqtt-pv-2/current | tai64nlocal
```

5. Change the `device_name`, increase the `device_instance` and update the `topic` in the `config.ini`
The service status can be checked with svstat `svstat /service/dbus-mqtt-pv`

This will output somethink like `/service/dbus-mqtt-pv: up (pid 5845) 185 seconds`

Now you can install and run the cloned driver. Should you need another instance just increase the number in step 1 and repeat all steps.
If the seconds are under 5 then the service crashes and gets restarted all the time. If you do not see anything in the logs you can increase the log level in `/data/etc/dbus-mqtt-pv/dbus-mqtt-pv.py` by changing `level=logging.WARNING` to `level=logging.INFO` or `level=logging.DEBUG`

## Compatibility
If the script stops with the message `dbus.exceptions.NameExistsException: Bus name already exists: com.victronenergy.pvinverter.mqtt_pv"` it means that the service is still running or another service is using that bus name.

It was tested on Venus OS Large `v2.92` on the following devices:
## Compatibility

* RaspberryPi 4b
* MultiPlus II (GX Version)
This software supports the latest three stable versions of Venus OS. It may also work on older versions, but this is not guaranteed.

## Screenshots

Expand Down
4 changes: 2 additions & 2 deletions dbus-mqtt-pv/config.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ logging = WARNING
device_name = MQTT PV

; Device VRM instance
; default: 51
device_instance = 51
; default: 100
device_instance = 100

; Specify after how many seconds the driver should exit (disconnect), if no new MQTT message was received
; default: 60
Expand Down
22 changes: 16 additions & 6 deletions dbus-mqtt-pv/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SERVICE_NAME=$(basename $SCRIPT_DIR)

echo
echo "Installing $SERVICE_NAME..."

# set permissions for script files
chmod 744 $SCRIPT_DIR/$SERVICE_NAME.py
chmod 744 $SCRIPT_DIR/install.sh
chmod 744 $SCRIPT_DIR/restart.sh
chmod 744 $SCRIPT_DIR/uninstall.sh
echo "Setting permissions..."
chmod 755 $SCRIPT_DIR/*.py
chmod 755 $SCRIPT_DIR/*.sh
chmod 755 $SCRIPT_DIR/service/run
chmod 755 $SCRIPT_DIR/service/log/run

# check dependencies
python -c "import paho.mqtt.client"
if [ $? -gt 0 ]
then
echo "Installing paho.mqtt.client..."
# install paho.mqtt.client
python -m pip install paho-mqtt
if [ $? -gt 0 ]
Expand All @@ -25,17 +28,24 @@ then
fi

# create sym-link to run script in deamon
ln -s $SCRIPT_DIR/service /service/$SERVICE_NAME
if [ ! -L /service/$SERVICE_NAME ]; then
echo "Creating service..."
ln -s $SCRIPT_DIR/service /service/$SERVICE_NAME
else
echo "Service already exists."
fi

# add install-script to rc.local to be ready for firmware update
filename=/data/rc.local
if [ ! -f $filename ]
then
touch $filename
chmod 777 $filename
chmod 755 $filename
echo "#!/bin/bash" >> $filename
echo >> $filename
fi

# if not alreay added, then add to rc.local
grep -qxF "bash $SCRIPT_DIR/install.sh" $filename || echo "bash $SCRIPT_DIR/install.sh" >> $filename

echo
13 changes: 12 additions & 1 deletion dbus-mqtt-pv/restart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SERVICE_NAME=$(basename $SCRIPT_DIR)

kill $(pgrep -f "python $SCRIPT_DIR/$SERVICE_NAME.py")
echo
echo "Restarting $SERVICE_NAME..."

pid=$(pgrep -f "python $SCRIPT_DIR/$SERVICE_NAME.py")
if [ -n "$pid" ]; then
svc -t /service/$SERVICE_NAME
echo "done."
else
echo "driver is not running!"
fi

echo
45 changes: 43 additions & 2 deletions dbus-mqtt-pv/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,49 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SERVICE_NAME=$(basename $SCRIPT_DIR)

# make sure SERVICE_NAME is set and not empty
if [ -z "$SERVICE_NAME" ]; then
echo "Error: SERVICE_NAME is not set."
exit 1
fi

echo
echo "Uninstalling $SERVICE_NAME..."

# Remove driver from rc.local
echo "Removing driver from rc.local..."
sed -i "/$SERVICE_NAME/d" /data/rc.local

# Stop the service
echo "Stopping service..."
svc -d /service/$SERVICE_NAME

sleep 1

# Remove service driver
echo "Removing driver from services..."
rm /service/$SERVICE_NAME
kill $(pgrep -f "supervise $SERVICE_NAME")

$SCRIPT_DIR/restart.sh
# kill
pkill -f "supervise .*$SERVICE_NAME"
pkill -f "multilog .*$SERVICE_NAME"
pkill -f "python .*$SERVICE_NAME"

echo "done."
echo

# Ask the user if they want to delete the service folder
echo "Do you also want to delete all driver files including the config? [y/N]"
read -r DELETE_FILES

if [[ "$DELETE_FILES" == "y" || "$DELETE_FILES" == "Y" ]]; then
echo "Deleting all driver files..."
rm -rf "$SCRIPT_DIR"
echo "done."
else
echo "Driver files not deleted."
fi

echo
echo "*** Please reboot your device to complete the uninstallation. ***"
echo
Loading

0 comments on commit 331177a

Please sign in to comment.