Skip to content

Commit 8b9f017

Browse files
committed
Added support for modifying the config options generically
This allows for comparisons of #590 against the mainline without having to change the divert_sim to change the different options
1 parent d9215b7 commit 8b9f017

File tree

10 files changed

+78
-25
lines changed

10 files changed

+78
-25
lines changed

.github/workflows/divert_sim.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
with:
4545
repository: bblanchon/ArduinoJson
4646
path: ArduinoJson
47+
ref: v6.20.1
4748

4849
- uses: actions/checkout@v3
4950
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@ lib/MicroTasks
9191

9292
*.bin
9393
divert_sim/epoxyfsdata
94+
divert_sim/epoxyeepromdata

divert_sim/.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"request": "launch",
1111
"program": "${workspaceFolder}/divert_sim",
1212
"args": [
13+
"-c", "data/test_config.json",
1314
"<", "${workspaceFolder}/data/day1.csv"
1415
],
1516
"stopAtEntry": false,
@@ -100,4 +101,4 @@
100101
"miDebuggerPath": "/usr/bin/gdb"
101102
}
102103
]
103-
}
104+
}

divert_sim/EEPROM.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include <EpoxyEepromEsp.h>
2+
#define EEPROM EpoxyEepromEspInstance

divert_sim/Makefile

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ CPPFLAGS := \
2020
-I $(ARDUINO_LIB_DIR)/ConfigJson/src \
2121
-I $(ARDUINO_LIB_DIR)/ArduinoJson/src \
2222
-I $(ARDUINO_LIB_DIR)/OpenEVSE_Lib/src \
23+
-I $(ARDUINO_LIB_DIR)/ESPAL/src \
2324
-I $(EPOXY_LIB_DIR)/EpoxyFS/src \
25+
-I $(EPOXY_LIB_DIR)/EpoxyEepromEsp/src \
2426
-ggdb \
2527
-D DIVERT_SIM \
2628
-D ARDUINO=100 \
2729
-D UNIX_HOST_DUINO \
2830
-D EPOXY_DUINO \
2931
-D EPOXY_CORE_ESP8266 \
3032
-D ENERGY_METER_STORE_STATE=0 \
31-
-D ARDUINOJSON_ENABLE_PROGMEM=0
33+
-D ARDUINOJSON_ENABLE_PROGMEM=0 \
34+
-D ENABLE_CONFIG_V1_IMPORT=0 \
35+
-D ENABLE_CONFIG_CHANGE_NOTIFICATION=0
3236
# \
3337
# -D ENABLE_DEBUG \
3438
# -D ENABLE_DEBUG_DIVERT \
@@ -54,8 +58,8 @@ OPENEVSE_LIB_OBJ := \
5458
openevse.o \
5559
RapiSender.o
5660

57-
#CONFIG_JSON_OBJ := \
58-
# ConfigJson.o
61+
CONFIG_JSON_OBJ := \
62+
ConfigJson.o
5963

6064
STREAM_SPY_OBJ := \
6165
StreamSpy.o \
@@ -68,11 +72,13 @@ OPENEVSE_WIFI_OBJ := \
6872
energy_meter.o \
6973
event_log.o \
7074
debug.o \
71-
manual.o
75+
manual.o \
76+
app_config.o
7277

7378
ARDUINO_OBJ := \
7479
avr_stdlib.o \
7580
Arduino.o \
81+
base64.o \
7682
Esp.o \
7783
IPAddress.o \
7884
Print.o \
@@ -88,13 +94,21 @@ EPOXY_FS_OBJ := \
8894
FS.o \
8995
FSImpl.o
9096

97+
EPOXY_EEPROM_OBJ := \
98+
EpoxyEepromEsp.o
99+
100+
ESPAL_OBJ := \
101+
espal.o
102+
91103
OBJ := \
92104
$(MICRO_TASKS_OBJ) \
93105
$(CONFIG_JSON_OBJ) \
94106
$(STREAM_SPY_OBJ) \
107+
$(ESPAL_OBJ) \
95108
$(OPENEVSE_LIB_OBJ) \
96109
$(OPENEVSE_WIFI_OBJ) \
97110
$(EPOXY_FS_OBJ) \
111+
$(EPOXY_EEPROM_OBJ) \
98112
$(ARDUINO_OBJ) \
99113
$(MAINS)
100114
DEPS :=
@@ -106,7 +120,9 @@ VPATH := \
106120
$(ARDUINO_LIB_DIR)/MicroTasks/src \
107121
$(ARDUINO_LIB_DIR)/ConfigJson/src \
108122
$(ARDUINO_LIB_DIR)/StreamSpy/src \
109-
$(EPOXY_LIB_DIR)/EpoxyFS/src
123+
$(ARDUINO_LIB_DIR)/ESPAL/src \
124+
$(EPOXY_LIB_DIR)/EpoxyFS/src \
125+
$(EPOXY_LIB_DIR)/EpoxyEepromEsp/src
110126

111127
.PHONY: all clean
112128

divert_sim/data/test_config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"divert_PV_ratio": 0.5,
3+
"divert_attack_smoothing_factor": 0.4,
4+
"divert_decay_smoothing_factor": 0.05,
5+
"divert_min_charge_time": 600
6+
}

divert_sim/divert_sim.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ ManualOverride manual(evse);
2525

2626
long pilot = 32; // OpenEVSE Pilot Setting
2727
long state = OPENEVSE_STATE_CONNECTED; // OpenEVSE State
28-
String mqtt_solar = "";
29-
String mqtt_grid_ie = "";
30-
uint32_t flags;
28+
double voltage = 240; // Voltage from OpenEVSE or MQTT
29+
30+
extern double smoothed_available_current;
3131

3232
int date_col = 0;
3333
int grid_ie_col = -1;
@@ -38,15 +38,6 @@ time_t simulated_time = 0;
3838

3939
bool kw = false;
4040

41-
extern double smoothed_available_current;
42-
double divert_attack_smoothing_factor = 0.4;
43-
double divert_decay_smoothing_factor = 0.05;
44-
double divert_PV_ratio = 0.5;
45-
uint32_t divert_min_charge_time = (10 * 60);
46-
double voltage = 240; // Voltage from OpenEVSE or MQTT
47-
48-
uint32_t current_shaper_max_pwr = 0;
49-
5041
time_t parse_date(const char *dateStr)
5142
{
5243
int y = 2020, M = 1, d = 1, h = 0, m = 0, s = 0;
@@ -105,6 +96,7 @@ int main(int argc, char** argv)
10596
{
10697
int voltage_arg = -1;
10798
std::string sep = ",";
99+
std::string config;
108100

109101
cxxopts::Options options(argv[0], " - example command line options");
110102
options
@@ -117,8 +109,7 @@ int main(int argc, char** argv)
117109
("d,date", "The date column", cxxopts::value<int>(date_col), "N")
118110
("s,solar", "The solar column", cxxopts::value<int>(solar_col), "N")
119111
("g,gridie", "The Grid IE column", cxxopts::value<int>(grid_ie_col), "N")
120-
("attack", "The attack factor for the smoothing", cxxopts::value<double>(divert_attack_smoothing_factor))
121-
("decay", "The decay factor for the smoothing", cxxopts::value<double>(divert_decay_smoothing_factor))
112+
("c,config", "Config options, either a file name or JSON", cxxopts::value<std::string>(config))
122113
("v,voltage", "The Voltage column if < 50, else the fixed voltage", cxxopts::value<int>(voltage_arg), "N")
123114
("kw", "values are KW")
124115
("sep", "Field separator", cxxopts::value<std::string>(sep));
@@ -131,6 +122,22 @@ int main(int argc, char** argv)
131122
exit(0);
132123
}
133124

125+
fs::EpoxyFS.begin();
126+
config_reset();
127+
128+
// If config is set and not a JSON string, assume it is a file name
129+
if(config.length() > 0 && config[0] != '{')
130+
{
131+
std::ifstream t(config);
132+
std::stringstream buffer;
133+
buffer << t.rdbuf();
134+
config = buffer.str();
135+
}
136+
// If we have some JSON load it
137+
if(config.length() > 0 && config[0] == '{') {
138+
config_deserialize(config.c_str());
139+
}
140+
134141
kw = result.count("kw") > 0;
135142

136143
mqtt_solar = grid_ie_col >= 0 ? "" : "yes";
@@ -147,7 +154,6 @@ int main(int argc, char** argv)
147154
solar = 0;
148155
grid_ie = 0;
149156

150-
fs::EpoxyFS.begin();
151157
evse.begin();
152158
divert.begin();
153159

divert_sim/runall.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ mkdir -p output
1111
./divert_sim -g 2 < data/day2_grid_ie.csv > output/day2_grid_ie.csv
1212
./divert_sim -g 2 < data/day3_grid_ie.csv > output/day3_grid_ie.csv
1313
./divert_sim -v 2 < data/solar-vrms.csv > output/solar-vrms.csv
14-
./divert_sim --sep \; --kw --decay 0.4 < data/Energy_and_Power_Day_2020-03-22.csv > output/Energy_and_Power_Day_2020-03-22.csv
15-
./divert_sim --sep \; --kw --decay 0.4 < data/Energy_and_Power_Day_2020-03-31.csv > output/Energy_and_Power_Day_2020-03-31.csv
16-
./divert_sim --sep \; --kw --decay 0.4 < data/Energy_and_Power_Day_2020-04-01.csv > output/Energy_and_Power_Day_2020-04-01.csv
14+
./divert_sim --sep \; --kw --config '{"divert_decay_smoothing_factor":0.4}' < data/Energy_and_Power_Day_2020-03-22.csv > output/Energy_and_Power_Day_2020-03-22.csv
15+
./divert_sim --sep \; --kw --config '{"divert_decay_smoothing_factor":0.4}' < data/Energy_and_Power_Day_2020-03-31.csv > output/Energy_and_Power_Day_2020-03-31.csv
16+
./divert_sim --sep \; --kw --config '{"divert_decay_smoothing_factor":0.4}' < data/Energy_and_Power_Day_2020-04-01.csv > output/Energy_and_Power_Day_2020-04-01.csv

src/app_config.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "emonesp.h"
22
#include "espal.h"
3+
4+
#if ENABLE_CONFIG_CHANGE_NOTIFICATION
35
#include "divert.h"
46
#include "net_manager.h"
57
#include "mqtt.h"
@@ -10,6 +12,7 @@
1012
#include "LedManagerTask.h"
1113
#include "current_shaper.h"
1214
#include "limit.h"
15+
#endif
1316

1417
#include "app_config.h"
1518
#include "app_config_mqtt.h"
@@ -253,16 +256,22 @@ config_load_settings()
253256
user_config.onChanged(config_changed);
254257

255258
factory_config.load(false);
256-
if(!user_config.load(true)) {
259+
if(!user_config.load(true))
260+
{
261+
#if ENABLE_CONFIG_V1_IMPORT
257262
DBUGF("No JSON config found, trying v1 settings");
258263
config_load_v1_settings();
264+
#else
265+
DBUGF("No JSON config found, using defaults");
266+
#endif
259267
}
260268
}
261269

262270
void config_changed(String name)
263271
{
264272
DBUGF("%s changed", name.c_str());
265273

274+
#if ENABLE_CONFIG_CHANGE_NOTIFICATION
266275
if(name == "time_zone") {
267276
timeManager.setTimeZone(time_zone);
268277
} else if(name == "flags") {
@@ -319,6 +328,7 @@ void config_changed(String name)
319328
} else if(name == "sntp_enabled") {
320329
timeManager.setSntpEnabled(config_sntp_enabled());
321330
}
331+
#endif
322332
}
323333

324334
void config_commit(bool factory)

src/app_config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
#include <Arduino.h>
55
#include <ArduinoJson.h>
66

7+
#ifndef ENABLE_CONFIG_V1_IMPORT
8+
#define ENABLE_CONFIG_V1_IMPORT 1
9+
#endif
10+
11+
#ifndef ENABLE_CONFIG_CHANGE_NOTIFICATION
12+
#define ENABLE_CONFIG_CHANGE_NOTIFICATION 1
13+
#endif
14+
715
// -------------------------------------------------------------------
816
// Load and save the OpenEVSE WiFi config.
917
//
@@ -194,7 +202,9 @@ extern String ohm;
194202
// Load saved settings
195203
// -------------------------------------------------------------------
196204
extern void config_load_settings();
205+
#if ENABLE_CONFIG_V1_IMPORT
197206
extern void config_load_v1_settings();
207+
#endif
198208

199209
// -------------------------------------------------------------------
200210
// Reset the config back to defaults

0 commit comments

Comments
 (0)