Skip to content

Commit

Permalink
Fix "No target SoC found."
Browse files Browse the repository at this point in the history
  • Loading branch information
christian1980nrw authored Jan 7, 2024
1 parent d66c304 commit 434438e
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions scripts/controller.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

VERSION="2.4.9"
VERSION="2.4.10"

set -e

Expand Down Expand Up @@ -659,38 +659,39 @@ is_charging_economical() {
get_target_soc() {
local megajoule=$1
local result=""

IFS=' ' read -ra first_line <<< "${config_matrix_target_soc_weather[0]}"
if awk -v megajoule="$megajoule" -v lower="${first_line[0]}" 'BEGIN {exit !(megajoule < lower)}'; then
echo "${first_line[1]}"
return
fi

for ((i = 0; i < ${#config_matrix_target_soc_weather[@]} - 1; i++)); do
IFS=' ' read -ra line <<< "${config_matrix_target_soc_weather[$i]}"
if (( i < ${#config_matrix_target_soc_weather[@]} - 1 )); then
next_line="${config_matrix_target_soc_weather[$((i + 1))]}"
IFS=' ' read -ra next_line <<< "$next_line"

if awk -v megajoule="$megajoule" -v lower="${line[0]}" -v upper="${next_line[0]}" \
'BEGIN {exit !(megajoule >= lower && megajoule < upper)}'; then
result=$(awk -v megajoule="$megajoule" -v lower="${line[0]}" \
-v upper="${next_line[0]}" -v lower_soc="${line[1]}" -v upper_soc="${next_line[1]}" \
'BEGIN {printf "%.0f", lower_soc + (megajoule - lower) * (upper_soc - lower_soc) / (upper - lower)}')
break
fi
fi

if [ $i -eq 0 ] && \
awk -v megajoule="$megajoule" -v lower="${line[0]}" 'BEGIN {exit !(megajoule < lower)}'; then
result="${line[1]}"
break
fi

if [ $i -eq $((${#config_matrix_target_soc_weather[@]} - 1)) ] && \
awk -v megajoule="$megajoule" -v upper="${line[0]}" 'BEGIN {exit !(megajoule >= upper)}'; then
result="${line[1]}"
break
next_line="${config_matrix_target_soc_weather[$((i + 1))]}"
IFS=' ' read -ra next_line <<< "$next_line"

if awk -v megajoule="$megajoule" -v lower="${line[0]}" -v upper="${next_line[0]}" \
'BEGIN {exit !(megajoule >= lower && megajoule < upper)}'; then
result=$(awk -v megajoule="$megajoule" -v lower="${line[0]}" \
-v upper="${next_line[0]}" -v lower_soc="${line[1]}" -v upper_soc="${next_line[1]}" \
'BEGIN {printf "%.0f", lower_soc + (megajoule - lower) * (upper_soc - lower_soc) / (upper - lower)}')
echo "$result"
return
fi
done

echo "${result:-"No target SoC found."}"
IFS=' ' read -ra last_line <<< "${config_matrix_target_soc_weather[-1]}"
if awk -v megajoule="$megajoule" -v upper="${last_line[0]}" 'BEGIN {exit !(megajoule >= upper)}'; then
echo "${last_line[1]}"
return
fi

echo "No target SoC found."
}



# Function to manage charging
manage_charging() {
local action=$1
Expand Down

0 comments on commit 434438e

Please sign in to comment.