Skip to content

Commit bbad42f

Browse files
authored
Merge pull request #796 from OpenEVSE/jeremypoulter/issue753
Set the default mode to active
2 parents 02d9048 + 31f79b0 commit bbad42f

File tree

8 files changed

+429
-93
lines changed

8 files changed

+429
-93
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/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ CPPFLAGS := \
3333
-D ARDUINOJSON_ENABLE_PROGMEM=0 \
3434
-D ENABLE_CONFIG_V1_IMPORT=0 \
3535
-D ENABLE_CONFIG_CHANGE_NOTIFICATION=0
36-
# \
36+
# \
3737
# -D ENABLE_DEBUG \
38+
# -D SERIAL_OUTFD=STDERR_FILENO
3839
# -D ENABLE_DEBUG_DIVERT \
3940
# -D ENABLE_DEBUG_INPUT_FILTER \
4041
# -D ENABLE_DEBUG_EVSE_MAN \

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: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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+
if output[1]:
47+
print(output[1])
48+
49+
return json.loads(output[0])
50+
51+
def test_config_defaults() -> None:
52+
"""Test the default config"""
53+
config = check_config()
54+
assert config["ssid"] == ""
55+
assert config["pass"] == ""
56+
assert config["ap_ssid"] == ""
57+
assert config["ap_pass"] == ""
58+
assert config["lang"] == ""
59+
assert config["www_username"] == ""
60+
assert config["www_password"] == ""
61+
assert config["hostname"] == "openevse-7856"
62+
assert config["sntp_hostname"] == "pool.ntp.org"
63+
assert config["time_zone"] == "Europe/London|GMT0BST,M3.5.0/1,M10.5.0"
64+
assert config["limit_default_type"] == ""
65+
assert config["limit_default_value"] == 0
66+
assert config["emoncms_server"] == "https://data.openevse.com/emoncms"
67+
assert config["emoncms_node"] == ""
68+
assert config["emoncms_apikey"] == ""
69+
assert config["emoncms_fingerprint"] == ""
70+
assert config["mqtt_server"] == "emonpi"
71+
assert config["mqtt_port"] == 1883
72+
assert config["mqtt_topic"] == ""
73+
assert config["mqtt_user"] == "emonpi"
74+
assert config["mqtt_pass"] == "emonpimqtt2016"
75+
assert config["mqtt_solar"] == ""
76+
assert config["mqtt_grid_ie"] == "emon/emonpi/power1"
77+
assert config["mqtt_vrms"] == "emon/emonpi/vrms"
78+
assert config["mqtt_live_pwr"] == ""
79+
assert config["mqtt_vehicle_soc"] == ""
80+
assert config["mqtt_vehicle_range"] == ""
81+
assert config["mqtt_vehicle_eta"] == ""
82+
assert config["mqtt_announce_topic"] == "openevse/announce/7856"
83+
assert config["ocpp_server"] == ""
84+
assert config["ocpp_chargeBoxId"] == ""
85+
assert config["ocpp_authkey"] == ""
86+
assert config["ocpp_idtag"] == "DefaultIdTag"
87+
assert config["ohm"] == ""
88+
assert config["divert_type"] == -1
89+
assert config["divert_PV_ratio"] == 1.1
90+
assert config["divert_attack_smoothing_time"] == 20
91+
assert config["divert_decay_smoothing_time"] == 600
92+
assert config["divert_min_charge_time"] == 600
93+
assert config["current_shaper_max_pwr"] == 0
94+
assert config["current_shaper_smoothing_time"] == 60
95+
assert config["current_shaper_min_pause_time"] == 300
96+
assert config["current_shaper_data_maxinterval"] == 120
97+
assert config["vehicle_data_src"] == 0
98+
assert config["tesla_access_token"] == ""
99+
assert config["tesla_refresh_token"] == ""
100+
#assert config["tesla_created_at"] == 18446744073709552000
101+
#assert config["tesla_expires_in"] == 18446744073709552000
102+
assert config["tesla_vehicle_id"] == ""
103+
assert config["rfid_storage"] == ""
104+
assert config["scheduler_start_window"] == 600
105+
assert config["flags"] == 79691784
106+
assert config["flags_changed"] == 0
107+
assert config["emoncms_enabled"] == False
108+
assert config["mqtt_enabled"] == False
109+
assert config["mqtt_reject_unauthorized"] == True
110+
assert config["mqtt_retained"] == False
111+
assert config["ohm_enabled"] == False
112+
assert config["sntp_enabled"] == True
113+
assert config["tesla_enabled"] == False
114+
assert config["divert_enabled"] == False
115+
assert config["current_shaper_enabled"] == False
116+
assert config["pause_uses_disabled"] == False
117+
assert config["mqtt_vehicle_range_miles"] == False
118+
assert config["ocpp_enabled"] == False
119+
assert config["ocpp_auth_auto"] == True
120+
assert config["ocpp_auth_offline"] == True
121+
assert config["ocpp_suspend_evse"] == False
122+
assert config["ocpp_energize_plug"] == False
123+
assert config["rfid_enabled"] == False
124+
assert config["factory_write_lock"] == False
125+
assert config["is_threephase"] == False
126+
assert config["wizard_passed"] == False
127+
assert config["default_state"] == True
128+
assert config["mqtt_protocol"] == "mqtt"
129+
assert config["charge_mode"] == "fast"
130+
131+
def test_flags_changed_bits() -> None:
132+
"""Test the flags changed bits are set correctly"""
133+
config = check_config({
134+
"mqtt_enabled": "true"
135+
})
136+
assert config["flags_changed"] == CONFIG_SERVICE_MQTT
137+
138+
def test_saving_and_loading() -> None:
139+
"""Test the config is saved and loaded correctly"""
140+
check_config({
141+
"mqtt_enabled": "true"
142+
}, commit=True)
143+
config = check_config(load=True)
144+
assert config["mqtt_enabled"] == True
145+
146+
def test_flags_changed_bits() -> None:
147+
"""Test the flags changed bits are set correctly"""
148+
config = check_config({
149+
"mqtt_enabled": "true"
150+
})
151+
assert config["flags_changed"] == CONFIG_SERVICE_MQTT
152+
153+
def test_default_charging_mode() -> None:
154+
"""Test the default chage mode is set correctly"""
155+
156+
# When initially added the default charge mode was set to false (disabled)
157+
# This test ensures that the default is now set to true (active) and that
158+
# the option is still able to be set to false (disabled)
159+
160+
check_config(commit=True)
161+
config = check_config(load=True)
162+
assert config["default_state"] == True
163+
164+
# Check the previous defaults (that were set to false) result in true
165+
config = check_config({
166+
"flags": CONFIG_SERVICE_SNTP |
167+
CONFIG_OCPP_AUTO_AUTH |
168+
CONFIG_OCPP_OFFLINE_AUTH
169+
}, commit=True)
170+
assert config["default_state"] == False
171+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK
172+
173+
config = check_config(load=True)
174+
assert config["default_state"] == True
175+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK
176+
177+
# Check setting the default state to false works
178+
config = check_config({
179+
"default_state": False
180+
}, commit=True)
181+
assert config["default_state"] == False
182+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE
183+
184+
config = check_config(load=True)
185+
assert config["default_state"] == False
186+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE
187+
188+
# Check setting the default state to true works
189+
config = check_config({
190+
"default_state": True
191+
}, commit=True, load=True)
192+
assert config["default_state"] == True
193+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE
194+
195+
config = check_config(load=True)
196+
assert config["default_state"] == True
197+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE
198+
199+
200+
def test_wizard_flag():
201+
"""Test the wizard flag is set correctly"""
202+
203+
config = check_config({
204+
"wizard_passed": True
205+
}, commit=True)
206+
assert config["wizard_passed"] == True
207+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_WIZARD
208+
209+
def test_three_phase_flag():
210+
"""Test the three phase flag is set correctly"""
211+
212+
config = check_config({
213+
"is_threephase": True
214+
})
215+
assert config["is_threephase"] == True
216+
assert config["flags_changed"] == CONFIG_THREEPHASE
217+
218+
config = check_config({
219+
"is_threephase": True
220+
}, commit=True)
221+
assert config["is_threephase"] == True
222+
assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_THREEPHASE

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ lib_deps =
3333
3434
jeremypoulter/[email protected]
3535
jeremypoulter/Micro [email protected]
36-
jeremypoulter/[email protected].5
36+
jeremypoulter/[email protected].6
3737
jeremypoulter/[email protected]
3838
jeremypoulter/[email protected]
3939
jeremypoulter/[email protected]

0 commit comments

Comments
 (0)