Skip to content

Commit

Permalink
Changed driver location, changed to overlay fs, use modules shipped w…
Browse files Browse the repository at this point in the history
…ith driver
  • Loading branch information
mr-manuel committed Nov 4, 2024
1 parent 6bd7d7a commit 4d1c71d
Show file tree
Hide file tree
Showing 14 changed files with 571 additions and 414 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
* Changed: Calculate Time-to-Go until ESS -> Minimum SOC (unless grid fails), Active SOC limit or `SOC_LOW_WARNING` from `config.ini` by @mr-manuel
* Changed: HLPDATABMS4S BMS - improved driver with https://github.com/mr-manuel/venus-os_dbus-serialbattery/pull/96 by @peterohman
* Changed: Rewritten code for external current sensor and fixed https://github.com/mr-manuel/venus-os_dbus-serialbattery/issues/60 by @mr-manuel
* Changed: Ship third party modules with driver to avoid installing them to the system by @mr-manuel
* Changed: The driver was moved from `/data/etc` to `/data/apps` by @mr-manuel
* Changed: The installation process was completely rewritten in order that the root fs does not have to be mounted as read-write anymore by @mr-manuel

## v1.4.20240928

Expand Down
6 changes: 5 additions & 1 deletion etc/dbus-serialbattery/bms/daly_can.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
MIN_CELL_VOLTAGE,
)
from struct import unpack_from
import can
import sys
import os

# import external libraries
sys.path.insert(1, os.path.join(os.path.dirname(os.path.dirname(__file__)), "ext"))
import can # noqa: E402


class Daly_Can(Battery):
Expand Down
6 changes: 5 additions & 1 deletion etc/dbus-serialbattery/bms/jkbms_brn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
# Updated by https://github.com/mr-manuel

from struct import unpack_from, calcsize
from bleak import BleakScanner, BleakClient, exc
from time import sleep, time
import asyncio
import threading
import sys
import os

# if used as standalone script then use custom logger
# else import logger from utils
Expand All @@ -25,6 +25,10 @@ def bytearray_to_string(data):
else:
from utils import bytearray_to_string, logger

# import external libraries
sys.path.insert(1, os.path.join(os.path.dirname(os.path.dirname(__file__)), "ext"))
from bleak import BleakScanner, BleakClient, exc # noqa: E402

# zero means parse all incoming data (every second)
CELL_INFO_REFRESH_S = 0
CHAR_HANDLE = "0000ffe1-0000-1000-8000-00805f9b34fb"
Expand Down
9 changes: 6 additions & 3 deletions etc/dbus-serialbattery/bms/lltjbd_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
from time import sleep
from typing import Union, Optional
from utils import logger
from bleak import BleakClient, BleakScanner, BLEDevice
from bleak.exc import BleakDBusError
from bms.lltjbd import LltJbdProtection, LltJbd

# import external libraries
sys.path.insert(1, os.path.join(os.path.dirname(os.path.dirname(__file__)), "ext"))
from bleak import BleakClient, BleakScanner, BLEDevice # noqa: E402
from bleak.exc import BleakDBusError # noqa: E402
from bms.lltjbd import LltJbdProtection, LltJbd # noqa: E402

BLE_SERVICE_UUID = "0000ff00-0000-1000-8000-00805f9b34fb"
BLE_CHARACTERISTICS_TX_UUID = "0000ff02-0000-1000-8000-00805f9b34fb"
Expand Down
47 changes: 37 additions & 10 deletions etc/dbus-serialbattery/disable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,50 @@
# remove comment for easier troubleshooting
#set -x

# handle read only mounts
bash /opt/victronenergy/swupdate-scripts/remount-rw.sh

# remove driver from serial starter
rm -f /data/conf/serial-starter.d/dbus-serialbattery.conf
# remove serial-starter.d if empty
rmdir /data/conf/serial-starter.d >/dev/null 2>&1
# kill serial starter, to reload changes
pkill -f "/opt/victronenergy/serial-starter/serial-starter.sh"
# restart serial starter
svc -t /service/serial-starter


# restore GUI changes
bash /data/apps/dbus-serialbattery/restore-gui.sh


# get a list of all dbus-serialbattery services and stop them
for service in /service/dbus-serialbattery.*; do
if [ -e "$service" ]; then
echo "Stopping $service..."
svc -d "$service"
fi
done
for service in /service/dbus-blebattery.*; do
if [ -e "$service" ]; then
echo "Stopping $service..."
svc -d "$service"
fi
done
for service in /service/dbus-canbattery.*; do
if [ -e "$service" ]; then
echo "Stopping $service..."
svc -d "$service"
fi
done


# wait shortly
sleep 1


# remove services
rm -rf /service/dbus-serialbattery.*
rm -rf /service/dbus-blebattery.*
rm -rf /service/dbus-canbattery.*

# kill driver, if running

# kill driver, if still running
# serial
pkill -f "supervise dbus-serialbattery.*"
pkill -f "multilog .* /var/log/dbus-serialbattery.*"
Expand All @@ -32,11 +60,9 @@ pkill -f "supervise dbus-canbattery.*"
pkill -f "multilog .* /var/log/dbus-canbattery.*"
pkill -f "python .*/dbus-serialbattery.py can.*"

# remove install script from rc.local
sed -i "/bash \/data\/etc\/dbus-serialbattery\/reinstall-local.sh/d" /data/rc.local

# remove cronjob
sed -i "/5 0,12 \* \* \* \/etc\/init.d\/bluetooth restart/d" /var/spool/cron/root >/dev/null 2>&1
# remove install script from rc.local
sed -i "/bash \/data\/apps\/dbus-serialbattery\/reinstall-local.sh &/d" /data/rc.local


### needed for upgrading from older versions | start ###
Expand All @@ -45,6 +71,7 @@ rm -rf /service/dbus-blebattery-*
# remove old install script from rc.local
sed -i "/sh \/data\/etc\/dbus-serialbattery\/reinstalllocal.sh/d" /data/rc.local
sed -i "/sh \/data\/etc\/dbus-serialbattery\/reinstall-local.sh/d" /data/rc.local
sed -i "/bash \/data\/etc\/dbus-serialbattery\/reinstall-local.sh/d" /data/rc.local
# remove old entry from rc.local
sed -i "/sh \/data\/etc\/dbus-serialbattery\/installble.sh/d" /data/rc.local
### needed for upgrading from older versions | end ###
Expand Down
82 changes: 82 additions & 0 deletions etc/dbus-serialbattery/functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

# elaborate version string for better comparing
# https://github.com/kwindrem/SetupHelper/blob/ebaa65fcf23e2bea6797f99c1c41174143c1153c/updateFileSets#L56-L81
function versionStringToNumber ()
{
local p4="" ; local p5="" ; local p5=""
local major=""; local minor=""

# first character should be 'v' so first awk parameter will be empty and is not prited into the read command
#
# version number formats: v2.40, v2.40~6, v2.40-large-7, v2.40~6-large-7
# so we must adjust how we use paramters read from the version string
# and parsed by awk
# if no beta make sure release is greater than any beta (i.e., a beta portion of 999)

read major minor p4 p5 p6 <<< $(echo $1 | awk -v FS='[v.~-]' '{print $2, $3, $4, $5, $6}')
((versionNumber = major * 1000000000 + minor * 1000000))
if [ -z $p4 ] || [ $p4 = "large" ]; then
((versionNumber += 999))
else
((versionNumber += p4))
fi
if [ ! -z $p4 ] && [ $p4 = "large" ]; then
((versionNumber += p5 * 1000))
large=$p5
elif [ ! -z $p6 ]; then
((versionNumber += p6 * 1000))
fi
}


# create overlay-fs and mount it, if not already mounted
function mountOverlayFs ()
{
path="/data/overlay-fs"
lowerDir=$1

if [ -z $2 ]; then
# replace / with _
overlayName=${1//\//_}
else
overlayName=$2
fi


if [ ! -d "${path}/${overlayName}/upper" ]; then
mkdir -p "${path}/${overlayName}/upper"
fi
if [ ! -d "${path}/${overlayName}/work" ]; then
mkdir -p "${path}/${overlayName}/work"
fi
if [ ! -d "${path}/${overlayName}/merged" ]; then
mkdir -p "${path}/${overlayName}/merged"
fi


# check if overlay is already mounted
if ! mountpoint -q "${path}/${overlayName}/merged"; then
# Mount the overlay
mount -t overlay overlay_${overlayName} -o lowerdir=${lowerDir},upperdir=${path}/${overlayName}/upper,workdir=${path}/${overlayName}/work ${path}/${overlayName}/merged

# Check if the mount was successful
if [ $? -ne 0 ]; then
echo "ERROR: Could not mount overlay for ${lowerDir}"
return 1
fi
fi


# check if overlay is already mounted
if ! mountpoint -q "${lowerDir}"; then
# Bind mount to the lower directory path
mount --bind ${path}/${overlayName}/merged ${lowerDir}

# Check if the bind mount was successful
if [ $? -ne 0 ]; then
echo "ERROR: Could not bind mount overlay for ${lowerDir}"
return 1
fi
fi
}
Loading

0 comments on commit 4d1c71d

Please sign in to comment.