Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and cleanup #114

Merged
merged 30 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fc7f467
Create main_versioning.yml
ckvsoft Oct 23, 2023
e08056a
Update main_versioning.yml
ckvsoft Oct 23, 2023
b9abf28
Update main_versioning.yml
ckvsoft Oct 23, 2023
746c8f8
Update main_versioning.yml
ckvsoft Oct 23, 2023
d438b92
Merge pull request #112 from christian1980nrw/dev
christian1980nrw Oct 23, 2023
4e7abab
Update controller.sh
ckvsoft Oct 23, 2023
0cd1023
Update run
ckvsoft Oct 23, 2023
4315d74
Update victron-venus-os-install.sh
ckvsoft Oct 23, 2023
e20cb32
Update venus.yml
ckvsoft Oct 23, 2023
9be9b54
Update victron-venus-os-install.sh
ckvsoft Oct 23, 2023
44464e6
Create dev_versioning.yml
ckvsoft Oct 23, 2023
a2f6ac6
Update version to 2.3.1-DEV
actions-user Oct 23, 2023
81b0559
Update venus.yml
ckvsoft Oct 23, 2023
ea3599c
Merge branch 'main' into dev
ckvsoft Oct 23, 2023
aba3e26
Update version to 2.3.2-DEV
actions-user Oct 23, 2023
0546b2c
Merge pull request #15 from ckvsoft/dev
ckvsoft Oct 23, 2023
fc75936
Update version to 2.3.2
actions-user Oct 23, 2023
3c4b382
Merge branch 'christian1980nrw:main' into main
ckvsoft Oct 23, 2023
407ab64
remove set -x
ckvsoft Oct 23, 2023
6a9fe6a
Update version to 2.3.3-DEV
actions-user Oct 23, 2023
1b779dd
Merge branch 'main' into dev
ckvsoft Oct 23, 2023
347db3c
Update version to ..1-DEV
actions-user Oct 23, 2023
a3a4283
Delete .github/workflows/dev_versioning.yml
ckvsoft Oct 23, 2023
778aeab
Update controller.sh
ckvsoft Oct 23, 2023
a974e17
Update version to ..1-DEV
actions-user Oct 23, 2023
d15aecb
Delete scripts/controller.sh
ckvsoft Oct 23, 2023
f2fbfd7
Create controller.sh
ckvsoft Oct 23, 2023
0e934ec
Update version to 2.3.4-DEV
actions-user Oct 23, 2023
ed12732
Merge pull request #17 from ckvsoft/dev
ckvsoft Oct 23, 2023
5716b13
Update version to 2.3.4
actions-user Oct 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/main_versioning.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Main Versioning

# This workflow triggers when a pull request to the 'main' branch is closed
on:
pull_request:
types: [closed]
branches:
- main

jobs:
update_version:
# Ensure this job only runs if the PR was merged, not just closed without merging
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Update VERSION in main
run: |
# Remove the -DEV suffix from the version number
NEW_VERSION=$(sed -n 's/^VERSION=\"\(.*\)-DEV\"$/\1/p' scripts/controller.sh)
sed -i "s/^VERSION=\".*\"$/VERSION=\"${NEW_VERSION}\"/" scripts/controller.sh
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git commit -am "Update version to ${NEW_VERSION}"
git push origin main

- name: Merge changes back to dev and append -DEV
run: |
git checkout dev
git merge main --no-ff -m "Merge main changes back to dev"
# Append -DEV back to the version number for the dev branch
NEW_DEV_VERSION="${NEW_VERSION}-DEV"
sed -i "s/^VERSION=\".*\"$/VERSION=\"${NEW_DEV_VERSION}\"/" scripts/controller.sh
git commit -am "Update version to ${NEW_DEV_VERSION} in dev"
git push origin dev
11 changes: 8 additions & 3 deletions .github/workflows/venus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,32 @@ jobs:
if which apt > /dev/null
then
apt update
apt -y install wget curl
apt -y install wget curl cron
elif which opkg > /dev/null
then
opkg install wget
opkg install curl
else
echo "W: Tests limited because of non-avail of wget and curl"
fi
- name: Set script permissions
run: chmod +x ./victron-venus-os-install.sh
env:
DEBUG: 1
- name: Execute Installation under Venus OS
run: |
echo pwd
pwd
echo ls
ls
echo victron-venus-install.sh
echo victron-venus-os-install.sh
if ./victron-venus-os-install.sh ; then
echo "[OK]"
else
echo "[FAIL]"
pwd
find . | head -n 30
HEAD_PATH=$(which head)
find . | $HEAD_PATH -n 30
exit 1
fi
env:
Expand Down
96 changes: 56 additions & 40 deletions scripts/controller.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ License=$(
EOLICENSE
)

VERSION="2.3.4"

set -e

if [ -z "$LANG" ]; then
Expand Down Expand Up @@ -132,7 +134,7 @@ if [ -f "$DIR/config.txt" ]; then
# Include the configuration file
source "$DIR/config.txt"
else
log_info "E: The file $DIR/config.txt was not found! Configure the existing sample.config.txt file and then save it as config.txt in the same directory." false
echo "E: The file $DIR/config.txt was not found! Configure the existing sample.config.txt file and then save it as config.txt in the same directory." false
exit 127
fi

Expand Down Expand Up @@ -237,7 +239,6 @@ fi

unset num_tools_missing


#######################################
### Begin of the functions... ###
#######################################
Expand Down Expand Up @@ -301,7 +302,7 @@ parse_and_validate_config() {
local file="$1"
local errors=""

rotating_spinner & # Start the spinner in the background
rotating_spinner & # Start the spinner in the background
local spinner_pid=$! # Get the PID of the spinner

# Step 1: Parse
Expand All @@ -315,7 +316,7 @@ parse_and_validate_config() {

# Set the value in the associative array
config_values["$key"]="$value"
done < "$file"
done <"$file"

# Step 2: Validation
for var_name in "${!valid_vars[@]}"; do
Expand Down Expand Up @@ -479,14 +480,14 @@ download_entsoe_prices() {
exit_with_cleanup 1
fi

if [ -n "$DEBUG" ]; then log_info "D: No delay of download of entsoe data since DEBUG variable set." >&2 "D: Entsoe file '$file' with price data downloaded" >&2; fi
if [ -n "$DEBUG" ]; then log_info "D: No delay of download of entsoe data since DEBUG variable set." "D: Entsoe file '$file' with price data downloaded" >&2 >&2; fi

if [ ! -s "$file" ]; then
log_info "E: Entsoe file '$file' is empty, please check your entsoe API Key."
exit_with_cleanup 1
fi

if [ -n "$DEBUG" ]; then log_info "D: No delay of download of entsoe data since DEBUG variable set." >&2 "D: Entsoe file '$file' with price data downloaded"; fi
if [ -n "$DEBUG" ]; then log_info "D: No delay of download of entsoe data since DEBUG variable set." "D: Entsoe file '$file' with price data downloaded" >&2; fi

awk '
# Capture content inside the <Period> tag
Expand Down Expand Up @@ -545,13 +546,13 @@ download_entsoe_prices() {
log_info "E: No prices found in the tomorrow XML data."
}
' "$file"

if [ -f "$output_file" ]; then
sort -g "$output_file" > "${output_file%.*}_sorted.${output_file##*.}"
sort -g "$output_file" >"${output_file%.*}_sorted.${output_file##*.}"
timestamp=$(TZ=$TZ date +%d)
echo "date_now_day: $timestamp" >> "$output_file"
echo "date_now_day: $timestamp" >>"$output_file"
fi

# Check if tomorrow file contains next day prices
if [ "$include_second_day" = 1 ] && grep -q "PT60M" "$file" && [ "$(wc -l <"$output_file")" -gt 3 ]; then
cat $file10 >$file8
Expand Down Expand Up @@ -616,15 +617,15 @@ get_awattar_prices() {
}

get_tibber_prices() {
current_price=$(sed -n "${now_linenumber}s/.*\"${tibber_prices}\":\([^,]*\),.*/\1/p" "$file15")
lowest_price=$(sed -n "1s/.*\"${tibber_prices}\":\([^,]*\),.*/\1/p" "$file12")
second_lowest_price=$(sed -n "2s/.*\"${tibber_prices}\":\([^,]*\),.*/\1/p" "$file12")
third_lowest_price=$(sed -n "3s/.*\"${tibber_prices}\":\([^,]*\),.*/\1/p" "$file12")
fourth_lowest_price=$(sed -n "4s/.*\"${tibber_prices}\":\([^,]*\),.*/\1/p" "$file12")
fifth_lowest_price=$(sed -n "5s/.*\"${tibber_prices}\":\([^,]*\),.*/\1/p" "$file12")
sixth_lowest_price=$(sed -n "6s/.*\"${tibber_prices}\":\([^,]*\),.*/\1/p" "$file12")
highest_price=$(sed -n "s/.*\"${tibber_prices}\":\([^,]*\),.*/\1/p" "$file12" | awk 'BEGIN {max = 0} {if ($1 > max) max = $1} END {print max}')
average_price=$(sed -n "s/.*\"${tibber_prices}\":\([^,]*\),.*/\1/p" "$file12" | awk '{sum += $1} END {print sum/NR}')
current_price=$(sed -n "${now_linenumber}s/.*\"${tibber_prices}\":([^,]*),.*/\1/p" "$file15")
lowest_price=$(sed -n "1s/.*\"${tibber_prices}\":([^,]*),.*/\1/p" "$file12")
second_lowest_price=$(sed -n "2s/.*\"${tibber_prices}\":([^,]*),.*/\1/p" "$file12")
third_lowest_price=$(sed -n "3s/.*\"${tibber_prices}\":([^,]*),.*/\1/p" "$file12")
fourth_lowest_price=$(sed -n "4s/.*\"${tibber_prices}\":([^,]*),.*/\1/p" "$file12")
fifth_lowest_price=$(sed -n "5s/.*\"${tibber_prices}\":([^,]*),.*/\1/p" "$file12")
sixth_lowest_price=$(sed -n "6s/.*\"${tibber_prices}\":([^,]*),.*/\1/p" "$file12")
highest_price=$(sed -n "s/.*\"${tibber_prices}\":([^,]*),.*/\1/p" "$file12" | awk 'BEGIN {max = 0} {if ($1 > max) max = $1} END {print max}')
average_price=$(sed -n "s/.*\"${tibber_prices}\":([^,]*),.*/\1/p" "$file12" | awk '{sum += $1} END {print sum/NR}')
}

get_current_entsoe_day() { current_entsoe_day=$(sed -n 25p "$file10" | grep -Eo '[0-9]+'); }
Expand Down Expand Up @@ -712,10 +713,12 @@ evaluate_conditions() {

for condition in "${!conditions_ref[@]}"; do
if [ -n "$DEBUG" ]; then
result="( ${descriptions_ref[$condition]} ) evaluates to $([ "${conditions_ref[$condition]}" -eq 1 ] && echo true || echo false)"
description_value="${descriptions_ref[$condition]}"
condition_evaluation=$([ "${conditions_ref[$condition]}" -eq 1 ] && echo true || echo false)
result="($description_value) evaluates to $condition_evaluation"
log_info "D: condition_evaluation [ $result ]." >&2
fi

if ((conditions_ref[$condition])) && [[ $condition_met -eq 0 ]]; then
execute_ref=1
condition_met_ref="$condition"
Expand Down Expand Up @@ -747,7 +750,10 @@ is_charging_economical() {

if [ -n "$DEBUG" ]; then
log_info "D: is_charging_economical [ $is_economical - $([ "$is_economical" -eq 1 ] && echo "false" || echo "true") ]." >&2
log_info "D: if [ reference_price ($(millicentToEuro $reference_price)) > total_cost ($(millicentToEuro $total_cost)) ] result is $([ "$is_economical" -eq 1 ] && echo "false" || echo "true")." >&2
reference_price_euro=$(millicentToEuro $reference_price)
total_cost_euro=$(millicentToEuro $total_cost)
is_economical_str=$([ "$is_economical" -eq 1 ] && echo "false" || echo "true")
log_info "D: if [ reference_price $reference_price_euro > total_cost $total_cost_euro ] result is $is_economical_str." >&2
fi

return $is_economical
Expand Down Expand Up @@ -872,12 +878,8 @@ euroToMillicent() {
# Replace each comma with a period, fixme if this is wrong
euro=$(echo "$euro" | sed 's/,/./g')

if which bc >/dev/null 2>&1; then
# Using bc to multiply the euro number and convert it to an integer
v=$(echo "scale=0; $euro * 10^$potency / 1" | bc)
else
v=$(awk "BEGIN {print int($euro * (10 ^ $potency))}")
fi
# v=$(awk "BEGIN {print int($euro * (10 ^ $potency))}")
v=$(awk -v euro="$euro" -v potency="$potency" 'BEGIN {printf "%.0f", euro * (10 ^ potency)}')

if [ -z "$v" ]; then
log_info "E: Could not translate '$euro' to an integer."
Expand All @@ -898,26 +900,28 @@ euroToMillicent_test() {

log_info() {
local msg="$1"
local prefix=$(echo "$msg" | head -n 1 | cut -d' ' -f1) # Extract the first word from the first line
local color="\033[1m" # Default color
local writeToLog=true # Default is true
local prefix=$(echo "$msg" | head -n 1 | cut -d' ' -f1) # Extract the first word from the first line
local color="\033[1m" # Default color
local writeToLog=true # Default is true

case "$prefix" in
"E:") color="\033[1;31m" ;; # Bright Red
"D:") color="\033[1;34m" # Bright Blue
writeToLog=false ;; # Default to not log debug messages
"W:") color="\033[1;33m" ;; # Bright Yellow
"I:") color="\033[1;32m" ;; # Bright Green
"E:") color="\033[1;31m" ;; # Bright Red
"D:")
color="\033[1;34m" # Bright Blue
writeToLog=false
;; # Default to not log debug messages
"W:") color="\033[1;33m" ;; # Bright Yellow
"I:") color="\033[1;32m" ;; # Bright Green
esac

writeToLog="${2:-$writeToLog}" # Override default if second parameter is provided
writeToLog="${2:-$writeToLog}" # Override default if second parameter is provided

# Print to console with color codes
printf "${color}%b\033[0m\n" "$msg"

# If we should write to the log, write without color codes
if [ "$writeToLog" == "true" ]; then
echo -e "$msg" | sed 's/\x1b\[[0-9;]*m//g' >> "$LOG_FILE"
echo -e "$msg" | sed 's/\x1b\[[0-9;]*m//g' >>"$LOG_FILE"
fi
}

Expand All @@ -935,9 +939,12 @@ exit_with_cleanup() {

echo >>"$LOG_FILE"

log_info "I: Bash Version: $(bash --version | head -n 1)"
log_info "I: Spotmarket-Switcher - Version $VERSION"

parse_and_validate_config "$DIR/config.txt"
# if [ $? -eq 1 ]; then
# Handle error
# Handle error
# fi

# An independent segment to test the conversion of floats to integers
Expand Down Expand Up @@ -1087,6 +1094,8 @@ if ((use_solarweather_api_to_abort == 1)); then
log_info "E: File '$file3' is empty, please check your API Key if download is still not possible tomorrow."
fi
find "$file3" -size 0 -delete # FIXME - looks wrong and complicated - simple RM included in prior if clause?
else
log_info "W: skip Solarweather. not activated"
fi

charging_condition_met=""
Expand Down Expand Up @@ -1173,22 +1182,29 @@ if ((execute_charging == 1 && use_victron_charger == 1)); then
fi
elif ((execute_charging != 1 && use_victron_charger == 1)); then
manage_charging "off" "Charging was not executed."
else
log_info "W: skip Victron Charger. not activated"
fi

# Execute Fritz DECT on command
if ((use_fritz_dect_sockets == 1)); then
manage_fritz_sockets
else
log_info "W: skip Fritz DECT. not activated"
fi

if ((use_shelly_wlan_sockets == 1)); then
manage_shelly_sockets
else
log_info "W: skip Shelly Api. not activated"
fi

echo >>"$LOG_FILE"

# Rotating log files
if [ -f "$LOG_FILE" ]; then
if [ "$(du -k "$LOG_FILE" | awk '{print $1}')" -gt "$LOG_MAX_SIZE" ]; then
log_info "I: Rotating log files"
mv "$LOG_FILE" "${LOG_FILE}.$(date +%Y%m%d%H%M%S)"
touch "$LOG_FILE"
find . -maxdepth 1 -name "${LOG_FILE}*" -type f -exec ls -1t {} + |
Expand All @@ -1199,5 +1215,5 @@ if [ -f "$LOG_FILE" ]; then
fi

if [ -n "$DEBUG" ]; then
log_info "D: [ OK ]" >&2
log_info "D: \[ OK \]" >&2
fi
20 changes: 11 additions & 9 deletions scripts/run
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh
#!/bin/bash

License=$(cat <<EOLICENSE
License=$(
cat <<EOLICENSE
MIT License

Copyright (c) 2023 christian1980nrw
Expand Down Expand Up @@ -39,20 +40,21 @@ fi
# Define the rc.local file path
rc_local_file="/data/rc.local"

# Define the code to be added to rc.local and crontab
code='(crontab -l | grep -Fxq "0 * * * * /data/etc/Spotmarket-Switcher/controller.sh") || (crontab -l; echo "0 * * * * /data/etc/Spotmarket-Switcher/controller.sh") | crontab -'

# Check if rc.local file exists, if not, create it
if [ ! -e "$rc_local_file" ]; then
echo "#!/bin/sh" > "$rc_local_file"
echo "#!/bin/sh" >"$rc_local_file"
chmod +x "$rc_local_file"
$code
fi

# Check if the code is already in rc.local, if not, add it
if ! grep -qF "$code" "$rc_local_file"; then
echo "$code" >> "$rc_local_file"
$code
{
echo '(crontab -l | grep -Fxq "0 * * * * /data/etc/Spotmarket-Switcher/controller.sh") || (crontab -l; echo "0 * * * * /data/etc/Spotmarket-Switcher/controller.sh")'
(crontab -l | grep -Fxq "0 * * * * /data/etc/Spotmarket-Switcher/controller.sh") || (
crontab -l
echo "0 * * * * /data/etc/Spotmarket-Switcher/controller.sh"
) | crontab -
} | tee -a "$rc_local_file"
fi

# Display a success message in DEBUG mode
Expand Down
Loading