-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1784 from yankee42/manual-soc-python
convert SoC "manual+calculation" to python
- Loading branch information
Showing
10 changed files
with
222 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import argparse | ||
import logging | ||
import sys | ||
|
||
from helpermodules.log import setup_logging_stdout | ||
from modules.common.store import ramdisk_write, ramdisk_read_float | ||
|
||
setup_logging_stdout() | ||
log = logging.getLogger("SoC Manual") | ||
|
||
|
||
def run(charge_point: int, battery_size: float, efficiency: float): | ||
""" | ||
:param charge_point: charge point number >= 1 | ||
:param battery_size: battery size in kWh | ||
:param efficiency: efficiency as fraction (usually between 0 and 1) | ||
:return: | ||
""" | ||
file_soc_start = "manual_soc_lp{}".format(charge_point) | ||
file_meter_start = "manual_soc_meter_lp{}".format(charge_point) | ||
# SoC-file contains float with SoC 0..100: | ||
file_soc = "soc{}".format(charge_point - 1) | ||
# meter file contains float with total energy ever charged at this charge point in kWh: | ||
file_meter = "llkwhs{}".format(charge_point - 1) | ||
if charge_point == 1: | ||
# For historic reasons some file names for charge point one do not fit the usual pattern: | ||
file_soc = "soc" | ||
file_meter = "llkwh" | ||
|
||
meter_now = ramdisk_read_float(file_meter) | ||
try: | ||
meter_start = ramdisk_read_float(file_meter_start) | ||
soc_start = ramdisk_read_float(file_soc_start) / 100 | ||
except FileNotFoundError: | ||
soc_now = ramdisk_read_float(file_soc) / 100 | ||
log.warning("Not initialized. Begin with meter=%g kWh, soc=%g%%", meter_now, soc_now * 100) | ||
ramdisk_write(file_meter_start, meter_now) | ||
ramdisk_write(file_soc_start, soc_now * 100, 1) | ||
return | ||
|
||
energy_counted = meter_now - meter_start | ||
energy_battery_gain = energy_counted * efficiency | ||
battery_soc_gain = energy_battery_gain / battery_size | ||
soc_new = soc_start + battery_soc_gain | ||
log.debug("Charged: %g kWh -> %g kWh = %g kWh", meter_start, meter_now, energy_counted) | ||
log.debug( | ||
"SoC-Gain: Charged (%g kWh) * efficiency (%g%%) / battery-size (%g kWh) = %.1f%%", | ||
energy_counted, efficiency * 100, battery_size, battery_soc_gain * 100 | ||
) | ||
log.info("%g%% + %g kWh = %g%%", soc_start * 100, energy_battery_gain, soc_new * 100) | ||
ramdisk_write(file_soc, soc_new * 100, digits=0) | ||
|
||
|
||
def run_command_line(): | ||
parser = argparse.ArgumentParser(description='Calculate SoC based on kWh charged') | ||
parser.add_argument("charge_point", type=int) | ||
parser.add_argument("efficiency", type=float) | ||
parser.add_argument("battery_size", type=float) | ||
args = parser.parse_args() | ||
run(charge_point=args.charge_point, efficiency=args.efficiency / 100, battery_size=args.battery_size) | ||
|
||
|
||
if __name__ == '__main__': | ||
run_command_line() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pytest | ||
|
||
import soc_manual | ||
from test_utils.mock_ramdisk import MockRamdisk | ||
|
||
|
||
@pytest.fixture(scope='function') | ||
def mock_ramdisk(monkeypatch): | ||
return MockRamdisk(monkeypatch) | ||
|
||
|
||
def test_charge_point_1(mock_ramdisk): | ||
# setup | ||
mock_ramdisk["manual_soc_lp1"] = "12.6" | ||
mock_ramdisk["manual_soc_meter_lp1"] = "42.123" | ||
mock_ramdisk["soc"] = "42" | ||
mock_ramdisk["llkwh"] = "52.123" | ||
|
||
# execution | ||
soc_manual.run(charge_point=1, battery_size=100, efficiency=.9) | ||
|
||
# evaluation | ||
assert mock_ramdisk["soc"] == "21" | ||
|
||
|
||
def test_charge_point_2(mock_ramdisk): | ||
# setup | ||
mock_ramdisk["manual_soc_lp2"] = "12.6" | ||
mock_ramdisk["manual_soc_meter_lp2"] = "42.123" | ||
mock_ramdisk["soc1"] = "42" | ||
mock_ramdisk["llkwhs1"] = "52.123" | ||
|
||
# execution | ||
soc_manual.run(charge_point=2, battery_size=100, efficiency=.9) | ||
|
||
# evaluation | ||
assert mock_ramdisk["soc1"] == "21" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"manual_files", | ||
[ | ||
{"manual_soc_meter_lp2": "42.123"}, | ||
{"manual_soc_lp2": "15"}, | ||
{} | ||
] | ||
) | ||
def test_reinitializes_if_state_is_missing(mock_ramdisk, manual_files: dict): | ||
# setup | ||
mock_ramdisk["soc1"] = "42" | ||
mock_ramdisk["llkwhs1"] = "52.123" | ||
mock_ramdisk.files.update(manual_files) | ||
|
||
# execution | ||
soc_manual.run(charge_point=2, battery_size=100, efficiency=.9) | ||
|
||
# evaluation | ||
assert mock_ramdisk["soc1"] == "42" # Expect value to be unchanged | ||
assert mock_ramdisk["manual_soc_lp2"] == "42.0" | ||
assert mock_ramdisk["manual_soc_meter_lp2"] == "52.123" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from pathlib import Path | ||
|
||
from modules.common.store import RAMDISK_PATH | ||
|
||
|
||
class MockRamdisk: | ||
def __init__(self, monkeypatch): | ||
self.files = {} | ||
original_read_text = Path.read_text | ||
original_write_text = Path.write_text | ||
|
||
def mock_read_text(file: Path): | ||
try: | ||
relative = file.relative_to(RAMDISK_PATH) | ||
except ValueError: | ||
return original_read_text(file) | ||
return self[str(relative)] | ||
|
||
def mock_write_text(file: Path, content: str): | ||
try: | ||
relative = file.relative_to(RAMDISK_PATH) | ||
except ValueError: | ||
original_write_text(file, content) | ||
return | ||
self[str(relative)] = content | ||
|
||
monkeypatch.setattr(Path, 'read_text', mock_read_text) | ||
monkeypatch.setattr(Path, 'write_text', mock_write_text) | ||
|
||
def __setitem__(self, key, value): | ||
if not isinstance(value, str): | ||
raise TypeError("only strings are allowed") | ||
self.files[key] = value | ||
|
||
def __getitem__(self, item): | ||
try: | ||
return self.files.__getitem__(item) | ||
except KeyError: | ||
raise FileNotFoundError() | ||
|
||
def __str__(self): | ||
return "Mock ramdisk: " + str(self.files) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.