Skip to content

Commit 656300f

Browse files
committed
Inital addition of some testing of the config
1 parent 3cf6d6f commit 656300f

File tree

3 files changed

+251
-2
lines changed

3 files changed

+251
-2
lines changed

divert_sim/.vscode/launch.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,53 @@
9999
],
100100
"preLaunchTask": "build",
101101
"miDebuggerPath": "/usr/bin/gdb"
102+
},
103+
{
104+
"name": "divert_sim: config-commit",
105+
"type": "cppdbg",
106+
"request": "launch",
107+
"program": "${workspaceFolder}/divert_sim",
108+
"args": [
109+
"--config-commit", "--config-check"
110+
],
111+
"stopAtEntry": false,
112+
"cwd": "${workspaceFolder}",
113+
"environment": [],
114+
"externalConsole": false,
115+
"MIMode": "gdb",
116+
"setupCommands": [
117+
{
118+
"description": "Enable pretty-printing for gdb",
119+
"text": "-enable-pretty-printing",
120+
"ignoreFailures": true
121+
}
122+
],
123+
"preLaunchTask": "build",
124+
"miDebuggerPath": "/usr/bin/gdb"
125+
},
126+
{
127+
"name": "divert_sim: default state setting",
128+
"type": "cppdbg",
129+
"request": "launch",
130+
"program": "${workspaceFolder}/divert_sim",
131+
"args": [
132+
"--config-commit", "--config-check",
133+
"--config", "{\"default_state\": false}"
134+
],
135+
"stopAtEntry": false,
136+
"cwd": "${workspaceFolder}",
137+
"environment": [],
138+
"externalConsole": false,
139+
"MIMode": "gdb",
140+
"setupCommands": [
141+
{
142+
"description": "Enable pretty-printing for gdb",
143+
"text": "-enable-pretty-printing",
144+
"ignoreFailures": true
145+
}
146+
],
147+
"preLaunchTask": "build",
148+
"miDebuggerPath": "/usr/bin/gdb"
102149
}
103150
]
104151
}

divert_sim/divert_sim.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ int main(int argc, char** argv)
115115
("c,config", "Config options, either a file name or JSON", cxxopts::value<std::string>(config))
116116
("v,voltage", "The Voltage column if < 50, else the fixed voltage", cxxopts::value<int>(voltage_arg), "N")
117117
("kw", "values are KW")
118-
("sep", "Field separator", cxxopts::value<std::string>(sep));
118+
("sep", "Field separator", cxxopts::value<std::string>(sep))
119+
("config-check", "Output the config and exit")
120+
("config-load", "Simulate loading config from EEPROM")
121+
("config-commit", "Simulate saving the config to EEPROM");
119122

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

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

128131
fs::EpoxyFS.begin();
129-
config_reset();
132+
if(result.count("config-load") > 0) {
133+
config_load_settings();
134+
} else {
135+
config_reset();
136+
}
130137

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

151+
if(result.count("config-commit") > 0) {
152+
config_commit();
153+
}
154+
155+
if(result.count("config-check"))
156+
{
157+
String config_out;
158+
config_serialize(config_out, true, false, false);
159+
std::cout << config_out.c_str() << std::endl;
160+
return EXIT_SUCCESS;
161+
}
162+
144163
kw = result.count("kw") > 0;
145164

146165
divert_type = grid_ie_col >= 0 ? 1 : 0;

divert_sim/test_config.py

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#!/usr/bin/env python3
2+
"""Test the app config process"""
3+
4+
from subprocess import PIPE, Popen
5+
import json
6+
7+
CONFIG_SERVICE_EMONCMS = 1 << 0
8+
CONFIG_SERVICE_MQTT = 1 << 1
9+
CONFIG_SERVICE_OHM = 1 << 2
10+
CONFIG_SERVICE_SNTP = 1 << 3
11+
CONFIG_MQTT_PROTOCOL = 7 << 4
12+
CONFIG_MQTT_ALLOW_ANY_CERT = 1 << 7
13+
CONFIG_SERVICE_TESLA = 1 << 8
14+
CONFIG_SERVICE_DIVERT = 1 << 9
15+
CONFIG_CHARGE_MODE = 7 << 10
16+
CONFIG_PAUSE_USES_DISABLED = 1 << 13
17+
CONFIG_SERVICE_OCPP = 1 << 14
18+
CONFIG_OCPP_ACCESS_SUSPEND = 1 << 15
19+
CONFIG_OCPP_ACCESS_ENERGIZE = 1 << 16
20+
CONFIG_VEHICLE_RANGE_MILES = 1 << 17
21+
CONFIG_RFID = 1 << 18
22+
CONFIG_SERVICE_CUR_SHAPER = 1 << 19
23+
CONFIG_MQTT_RETAINED = 1 << 20
24+
CONFIG_FACTORY_WRITE_LOCK = 1 << 21
25+
CONFIG_OCPP_AUTO_AUTH = 1 << 22
26+
CONFIG_OCPP_OFFLINE_AUTH = 1 << 23
27+
CONFIG_THREEPHASE = 1 << 24
28+
CONFIG_WIZARD = 1 << 25
29+
CONFIG_DEFAULT_STATE = 1 << 26
30+
31+
def check_config(config: bool = False, load: bool = False, commit: bool = False):
32+
command = ["./divert_sim", "--config-check"]
33+
if config:
34+
command.append("-c")
35+
command.append(json.dumps(config))
36+
if load:
37+
command.append("--config-load")
38+
if commit:
39+
command.append("--config-commit")
40+
41+
print(f"{' '.join(command)}")
42+
43+
divert_process = Popen(command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
44+
output = divert_process.communicate()
45+
46+
return json.loads(output[0])
47+
48+
def test_config_defaults() -> None:
49+
"""Test the default config"""
50+
config = check_config()
51+
assert config["ssid"] == ""
52+
assert config["pass"] == ""
53+
assert config["ap_ssid"] == ""
54+
assert config["ap_pass"] == ""
55+
assert config["lang"] == ""
56+
assert config["www_username"] == ""
57+
assert config["www_password"] == ""
58+
assert config["hostname"] == "openevse-7856"
59+
assert config["sntp_hostname"] == "pool.ntp.org"
60+
assert config["time_zone"] == "Europe/London|GMT0BST,M3.5.0/1,M10.5.0"
61+
assert config["limit_default_type"] == ""
62+
assert config["limit_default_value"] == 0
63+
assert config["emoncms_server"] == "https://data.openevse.com/emoncms"
64+
assert config["emoncms_node"] == ""
65+
assert config["emoncms_apikey"] == ""
66+
assert config["emoncms_fingerprint"] == ""
67+
assert config["mqtt_server"] == "emonpi"
68+
assert config["mqtt_port"] == 1883
69+
assert config["mqtt_topic"] == ""
70+
assert config["mqtt_user"] == "emonpi"
71+
assert config["mqtt_pass"] == "emonpimqtt2016"
72+
assert config["mqtt_solar"] == ""
73+
assert config["mqtt_grid_ie"] == "emon/emonpi/power1"
74+
assert config["mqtt_vrms"] == "emon/emonpi/vrms"
75+
assert config["mqtt_live_pwr"] == ""
76+
assert config["mqtt_vehicle_soc"] == ""
77+
assert config["mqtt_vehicle_range"] == ""
78+
assert config["mqtt_vehicle_eta"] == ""
79+
assert config["mqtt_announce_topic"] == "openevse/announce/7856"
80+
assert config["ocpp_server"] == ""
81+
assert config["ocpp_chargeBoxId"] == ""
82+
assert config["ocpp_authkey"] == ""
83+
assert config["ocpp_idtag"] == "DefaultIdTag"
84+
assert config["ohm"] == ""
85+
assert config["divert_type"] == -1
86+
assert config["divert_PV_ratio"] == 1.1
87+
assert config["divert_attack_smoothing_time"] == 20
88+
assert config["divert_decay_smoothing_time"] == 600
89+
assert config["divert_min_charge_time"] == 600
90+
assert config["current_shaper_max_pwr"] == 0
91+
assert config["current_shaper_smoothing_time"] == 60
92+
assert config["current_shaper_min_pause_time"] == 300
93+
assert config["current_shaper_data_maxinterval"] == 120
94+
assert config["vehicle_data_src"] == 0
95+
assert config["tesla_access_token"] == ""
96+
assert config["tesla_refresh_token"] == ""
97+
#assert config["tesla_created_at"] == 18446744073709552000
98+
#assert config["tesla_expires_in"] == 18446744073709552000
99+
assert config["tesla_vehicle_id"] == ""
100+
assert config["rfid_storage"] == ""
101+
assert config["scheduler_start_window"] == 600
102+
assert config["flags"] == 79691784
103+
assert config["flags_changed"] == 0
104+
assert config["emoncms_enabled"] == False
105+
assert config["mqtt_enabled"] == False
106+
assert config["mqtt_reject_unauthorized"] == True
107+
assert config["mqtt_retained"] == False
108+
assert config["ohm_enabled"] == False
109+
assert config["sntp_enabled"] == True
110+
assert config["tesla_enabled"] == False
111+
assert config["divert_enabled"] == False
112+
assert config["current_shaper_enabled"] == False
113+
assert config["pause_uses_disabled"] == False
114+
assert config["mqtt_vehicle_range_miles"] == False
115+
assert config["ocpp_enabled"] == False
116+
assert config["ocpp_auth_auto"] == True
117+
assert config["ocpp_auth_offline"] == True
118+
assert config["ocpp_suspend_evse"] == False
119+
assert config["ocpp_energize_plug"] == False
120+
assert config["rfid_enabled"] == False
121+
assert config["factory_write_lock"] == False
122+
assert config["is_threephase"] == False
123+
assert config["wizard_passed"] == False
124+
assert config["default_state"] == True
125+
assert config["mqtt_protocol"] == "mqtt"
126+
assert config["charge_mode"] == "fast"
127+
128+
def test_flags_changed_bits() -> None:
129+
"""Test the flags changed bits are set correctly"""
130+
config = check_config({
131+
"mqtt_enabled": "true"
132+
})
133+
assert config["flags_changed"] == CONFIG_SERVICE_MQTT
134+
135+
def test_saving_and_loading() -> None:
136+
"""Test the config is saved and loaded correctly"""
137+
check_config({
138+
"mqtt_enabled": "true"
139+
}, commit=True)
140+
config = check_config(load=True)
141+
assert config["mqtt_enabled"] == True
142+
143+
def test_flags_changed_bits() -> None:
144+
"""Test the flags changed bits are set correctly"""
145+
config = check_config({
146+
"mqtt_enabled": "true"
147+
})
148+
assert config["flags_changed"] == CONFIG_SERVICE_MQTT
149+
150+
def test_default_charging_mode() -> None:
151+
"""Test the default chage mode is set correctly"""
152+
153+
# When initially added the default charge mode was set to false (disabled)
154+
# This test ensures that the default is now set to true (active) and that
155+
# the option is still able to be set to false (disabled)
156+
157+
check_config(commit=True)
158+
config = check_config(load=True)
159+
assert config["default_state"] == True
160+
161+
# Check the previous defaults (that were set to false) result in true
162+
config = check_config({
163+
"flags": CONFIG_SERVICE_SNTP |
164+
CONFIG_OCPP_AUTO_AUTH |
165+
CONFIG_OCPP_OFFLINE_AUTH
166+
}, commit=True)
167+
assert config["default_state"] == False
168+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK
169+
170+
config = check_config(load=True)
171+
assert config["default_state"] == True
172+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK
173+
174+
# Check setting the default state to false works
175+
config = check_config({
176+
"default_state": False
177+
}, commit=True)
178+
assert config["default_state"] == True
179+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE
180+
181+
config = check_config(load=True)
182+
assert config["default_state"] == True
183+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE

0 commit comments

Comments
 (0)