-
Notifications
You must be signed in to change notification settings - Fork 117
/
divert_sim.cpp
258 lines (210 loc) · 5.96 KB
/
divert_sim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <iostream>
#include <sys/time.h>
#include <string>
#include "StdioSerial.h"
#include "RapiSender.h"
#include "openevse.h"
#include "divert.h"
#include "event.h"
#include "event_log.h"
#include "manual.h"
#include "parser.hpp"
#include "cxxopts.hpp"
#include <MicroTasks.h>
#include <EpoxyFS.h>
#include <epoxy_test/ArduinoTest.h>
using namespace aria::csv;
EventLog eventLog;
EvseManager evse(RAPI_PORT, eventLog);
DivertTask divert(evse);
ManualOverride manual(evse);
long pilot = 32; // OpenEVSE Pilot Setting
long state = OPENEVSE_STATE_CONNECTED; // OpenEVSE State
double voltage = 240; // Voltage from OpenEVSE or MQTT
extern double smoothed_available_current;
int date_col = 0;
int grid_ie_col = -1;
int solar_col = 1;
int voltage_col = 1;
time_t simulated_time = 0;
time_t last_time = 0;
bool kw = false;
time_t parse_date(const char *dateStr)
{
int y = 2020, M = 1, d = 1, h = 0, m = 0, s = 0;
char ampm[5];
if(6 != sscanf(dateStr, "%d-%d-%dT%d:%d:%dZ", &y, &M, &d, &h, &m, &s)) {
if(6 != sscanf(dateStr, "%d-%d-%dT%d:%d:%d+00:00", &y, &M, &d, &h, &m, &s)) {
if(6 != sscanf(dateStr, "%d-%d-%d %d:%d:%d", &y, &M, &d, &h, &m, &s)) {
if(3 != sscanf(dateStr, "%d:%d %s", &h, &m, ampm)) {
if(1 == sscanf(dateStr, "%d", &s)) {
return s;
}
} else {
y = 2020; M = 1; d = 1; s = 0;
if(12 == h) {
h -= 12;
}
if('P' == ampm[0]) {
h += 12;
}
}
}
}
}
tm time = {0};
time.tm_year = y - 1900; // Year since 1900
time.tm_mon = M - 1; // 0-11
time.tm_mday = d; // 1-31
time.tm_hour = h; // 0-23
time.tm_min = m; // 0-59
time.tm_sec = s; // 0-61 (0-60 in C++11)
return timegm(&time);
}
int get_watt(const char *val)
{
float number = 0.0;
if(1 != sscanf(val, "%f", &number)) {
throw std::invalid_argument("Not a number");
}
if(kw) {
number *= 1000;
}
return (int)round(number);
}
time_t divertmode_get_time()
{
return simulated_time;
}
int main(int argc, char** argv)
{
int voltage_arg = -1;
std::string sep = ",";
std::string config;
cxxopts::Options options(argv[0], " - example command line options");
options
.positional_help("[optional args]")
.show_positional_help();
options
.add_options()
("help", "Print help")
("d,date", "The date column", cxxopts::value<int>(date_col), "N")
("s,solar", "The solar column", cxxopts::value<int>(solar_col), "N")
("g,gridie", "The Grid IE column", cxxopts::value<int>(grid_ie_col), "N")
("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))
("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);
if (result.count("help"))
{
std::cout << options.help({"", "Group"}) << std::endl;
exit(0);
}
fs::EpoxyFS.begin();
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] != '{')
{
std::ifstream t(config);
std::stringstream buffer;
buffer << t.rdbuf();
config = buffer.str();
}
// If we have some JSON load it
if(config.length() > 0 && config[0] == '{') {
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;
if(voltage_arg >= 0) {
if(voltage_arg < 50) {
voltage_col = voltage_arg;
} else {
voltage = voltage_arg;
}
}
solar = 0;
grid_ie = 0;
evse.begin();
divert.begin();
// Initialise the EVSE Manager
while (!evse.isConnected()) {
MicroTask.update();
}
divert.setMode(DivertMode::Eco);
CsvParser parser(std::cin);
parser.delimiter(sep.c_str()[0]);
int row_number = 0;
std::cout << "Date,Solar,Grid IE,Pilot,Charge Power,Min Charge Power,State,Smoothed Available" << std::endl;
for (auto& row : parser)
{
try
{
int col = 0;
std::string val;
for (auto& field : row)
{
val = field;
if(date_col == col) {
simulated_time = parse_date(val.c_str());
} else if (grid_ie_col == col) {
grid_ie = get_watt(val.c_str());
} else if (solar_col == col) {
solar = get_watt(val.c_str());
} else if (voltage_col == col) {
voltage = stoi(field);
}
col++;
}
if(last_time != 0)
{
int delta = simulated_time - last_time;
if(delta > 0) {
EpoxyTest::add_millis(delta * 1000);
}
}
last_time = simulated_time;
divert.update_state();
MicroTask.update();
tm tm;
gmtime_r(&simulated_time, &tm);
char buffer[32];
std::strftime(buffer, 32, "%d/%m/%Y %H:%M:%S", &tm);
int ev_pilot = (OPENEVSE_STATE_CHARGING == state ? pilot : 0);
int ev_watt = ev_pilot * voltage;
int min_ev_watt = 6 * voltage;
double smoothed = divert.smoothedAvailableCurrent() * voltage;
std::cout << buffer << "," << solar << "," << grid_ie << "," << ev_pilot << "," << ev_watt << "," << min_ev_watt << "," << state << "," << smoothed << std::endl;
}
catch(const std::invalid_argument& e)
{
}
}
}
void event_send(String event)
{
}
void event_send(JsonDocument &event)
{
}
void emoncms_publish(JsonDocument &data)
{
}