Skip to content

Commit

Permalink
Inital addition of some testing of the config
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremypoulter committed Jan 23, 2024
1 parent 3cf6d6f commit 656300f
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 2 deletions.
47 changes: 47 additions & 0 deletions divert_sim/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,53 @@
],
"preLaunchTask": "build",
"miDebuggerPath": "/usr/bin/gdb"
},
{
"name": "divert_sim: config-commit",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/divert_sim",
"args": [
"--config-commit", "--config-check"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build",
"miDebuggerPath": "/usr/bin/gdb"
},
{
"name": "divert_sim: default state setting",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/divert_sim",
"args": [
"--config-commit", "--config-check",
"--config", "{\"default_state\": false}"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
23 changes: 21 additions & 2 deletions divert_sim/divert_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ int main(int argc, char** argv)
("c,config", "Config options, either a file name or JSON", cxxopts::value<std::string>(config))
("v,voltage", "The Voltage column if < 50, else the fixed voltage", cxxopts::value<int>(voltage_arg), "N")
("kw", "values are KW")
("sep", "Field separator", cxxopts::value<std::string>(sep));
("sep", "Field separator", cxxopts::value<std::string>(sep))
("config-check", "Output the config and exit")
("config-load", "Simulate loading config from EEPROM")
("config-commit", "Simulate saving the config to EEPROM");

auto result = options.parse(argc, argv);

Expand All @@ -126,7 +129,11 @@ int main(int argc, char** argv)
}

fs::EpoxyFS.begin();
config_reset();
if(result.count("config-load") > 0) {
config_load_settings();
} else {
config_reset();
}

// If config is set and not a JSON string, assume it is a file name
if(config.length() > 0 && config[0] != '{')
Expand All @@ -141,6 +148,18 @@ int main(int argc, char** argv)
config_deserialize(config.c_str());
}

if(result.count("config-commit") > 0) {
config_commit();
}

if(result.count("config-check"))
{
String config_out;
config_serialize(config_out, true, false, false);
std::cout << config_out.c_str() << std::endl;
return EXIT_SUCCESS;
}

kw = result.count("kw") > 0;

divert_type = grid_ie_col >= 0 ? 1 : 0;
Expand Down
183 changes: 183 additions & 0 deletions divert_sim/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#!/usr/bin/env python3
"""Test the app config process"""

from subprocess import PIPE, Popen
import json

CONFIG_SERVICE_EMONCMS = 1 << 0
CONFIG_SERVICE_MQTT = 1 << 1
CONFIG_SERVICE_OHM = 1 << 2
CONFIG_SERVICE_SNTP = 1 << 3
CONFIG_MQTT_PROTOCOL = 7 << 4
CONFIG_MQTT_ALLOW_ANY_CERT = 1 << 7
CONFIG_SERVICE_TESLA = 1 << 8
CONFIG_SERVICE_DIVERT = 1 << 9
CONFIG_CHARGE_MODE = 7 << 10
CONFIG_PAUSE_USES_DISABLED = 1 << 13
CONFIG_SERVICE_OCPP = 1 << 14
CONFIG_OCPP_ACCESS_SUSPEND = 1 << 15
CONFIG_OCPP_ACCESS_ENERGIZE = 1 << 16
CONFIG_VEHICLE_RANGE_MILES = 1 << 17
CONFIG_RFID = 1 << 18
CONFIG_SERVICE_CUR_SHAPER = 1 << 19
CONFIG_MQTT_RETAINED = 1 << 20
CONFIG_FACTORY_WRITE_LOCK = 1 << 21
CONFIG_OCPP_AUTO_AUTH = 1 << 22
CONFIG_OCPP_OFFLINE_AUTH = 1 << 23
CONFIG_THREEPHASE = 1 << 24
CONFIG_WIZARD = 1 << 25
CONFIG_DEFAULT_STATE = 1 << 26

def check_config(config: bool = False, load: bool = False, commit: bool = False):
command = ["./divert_sim", "--config-check"]
if config:
command.append("-c")
command.append(json.dumps(config))
if load:
command.append("--config-load")
if commit:
command.append("--config-commit")

print(f"{' '.join(command)}")

divert_process = Popen(command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
output = divert_process.communicate()

return json.loads(output[0])

def test_config_defaults() -> None:
"""Test the default config"""
config = check_config()
assert config["ssid"] == ""
assert config["pass"] == ""
assert config["ap_ssid"] == ""
assert config["ap_pass"] == ""
assert config["lang"] == ""
assert config["www_username"] == ""
assert config["www_password"] == ""
assert config["hostname"] == "openevse-7856"
assert config["sntp_hostname"] == "pool.ntp.org"
assert config["time_zone"] == "Europe/London|GMT0BST,M3.5.0/1,M10.5.0"
assert config["limit_default_type"] == ""
assert config["limit_default_value"] == 0
assert config["emoncms_server"] == "https://data.openevse.com/emoncms"
assert config["emoncms_node"] == ""
assert config["emoncms_apikey"] == ""
assert config["emoncms_fingerprint"] == ""
assert config["mqtt_server"] == "emonpi"
assert config["mqtt_port"] == 1883
assert config["mqtt_topic"] == ""
assert config["mqtt_user"] == "emonpi"
assert config["mqtt_pass"] == "emonpimqtt2016"
assert config["mqtt_solar"] == ""
assert config["mqtt_grid_ie"] == "emon/emonpi/power1"
assert config["mqtt_vrms"] == "emon/emonpi/vrms"
assert config["mqtt_live_pwr"] == ""
assert config["mqtt_vehicle_soc"] == ""
assert config["mqtt_vehicle_range"] == ""
assert config["mqtt_vehicle_eta"] == ""
assert config["mqtt_announce_topic"] == "openevse/announce/7856"
assert config["ocpp_server"] == ""
assert config["ocpp_chargeBoxId"] == ""
assert config["ocpp_authkey"] == ""
assert config["ocpp_idtag"] == "DefaultIdTag"
assert config["ohm"] == ""
assert config["divert_type"] == -1
assert config["divert_PV_ratio"] == 1.1
assert config["divert_attack_smoothing_time"] == 20
assert config["divert_decay_smoothing_time"] == 600
assert config["divert_min_charge_time"] == 600
assert config["current_shaper_max_pwr"] == 0
assert config["current_shaper_smoothing_time"] == 60
assert config["current_shaper_min_pause_time"] == 300
assert config["current_shaper_data_maxinterval"] == 120
assert config["vehicle_data_src"] == 0
assert config["tesla_access_token"] == ""
assert config["tesla_refresh_token"] == ""
#assert config["tesla_created_at"] == 18446744073709552000
#assert config["tesla_expires_in"] == 18446744073709552000
assert config["tesla_vehicle_id"] == ""
assert config["rfid_storage"] == ""
assert config["scheduler_start_window"] == 600
assert config["flags"] == 79691784
assert config["flags_changed"] == 0
assert config["emoncms_enabled"] == False
assert config["mqtt_enabled"] == False
assert config["mqtt_reject_unauthorized"] == True
assert config["mqtt_retained"] == False
assert config["ohm_enabled"] == False
assert config["sntp_enabled"] == True
assert config["tesla_enabled"] == False
assert config["divert_enabled"] == False
assert config["current_shaper_enabled"] == False
assert config["pause_uses_disabled"] == False
assert config["mqtt_vehicle_range_miles"] == False
assert config["ocpp_enabled"] == False
assert config["ocpp_auth_auto"] == True
assert config["ocpp_auth_offline"] == True
assert config["ocpp_suspend_evse"] == False
assert config["ocpp_energize_plug"] == False
assert config["rfid_enabled"] == False
assert config["factory_write_lock"] == False
assert config["is_threephase"] == False
assert config["wizard_passed"] == False
assert config["default_state"] == True
assert config["mqtt_protocol"] == "mqtt"
assert config["charge_mode"] == "fast"

def test_flags_changed_bits() -> None:
"""Test the flags changed bits are set correctly"""
config = check_config({
"mqtt_enabled": "true"
})
assert config["flags_changed"] == CONFIG_SERVICE_MQTT

def test_saving_and_loading() -> None:
"""Test the config is saved and loaded correctly"""
check_config({
"mqtt_enabled": "true"
}, commit=True)
config = check_config(load=True)
assert config["mqtt_enabled"] == True

def test_flags_changed_bits() -> None:
"""Test the flags changed bits are set correctly"""
config = check_config({
"mqtt_enabled": "true"
})
assert config["flags_changed"] == CONFIG_SERVICE_MQTT

def test_default_charging_mode() -> None:
"""Test the default chage mode is set correctly"""

# When initially added the default charge mode was set to false (disabled)
# This test ensures that the default is now set to true (active) and that
# the option is still able to be set to false (disabled)

check_config(commit=True)
config = check_config(load=True)
assert config["default_state"] == True

# Check the previous defaults (that were set to false) result in true
config = check_config({
"flags": CONFIG_SERVICE_SNTP |
CONFIG_OCPP_AUTO_AUTH |
CONFIG_OCPP_OFFLINE_AUTH
}, commit=True)
assert config["default_state"] == False
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK

config = check_config(load=True)
assert config["default_state"] == True
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK

# Check setting the default state to false works
config = check_config({
"default_state": False
}, commit=True)
assert config["default_state"] == True
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE

config = check_config(load=True)
assert config["default_state"] == True
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE

0 comments on commit 656300f

Please sign in to comment.