forked from matth-x/MicroOcppSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
148 lines (110 loc) · 3.62 KB
/
main.cpp
File metadata and controls
148 lines (110 loc) · 3.62 KB
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
// matth-x/MicroOcppSimulator
// Copyright Matthias Akstaller 2022 - 2024
// GPL-3.0 License
#include <iostream>
#include <MicroOcpp.h>
#include "evse.h"
#include "api.h"
#if MO_NUMCONNECTORS == 3
std::array<Evse, MO_NUMCONNECTORS - 1> connectors {{1,2}};
#else
std::array<Evse, MO_NUMCONNECTORS - 1> connectors {{1}};
#endif
bool g_isOcpp201 = false;
#define MO_NETLIB_MONGOOSE 1
#define MO_NETLIB_WASM 2
#if MO_NETLIB == MO_NETLIB_MONGOOSE
#include "mongoose.h"
#include <MicroOcppMongooseClient.h>
#include "net_mongoose.h"
struct mg_mgr mgr;
MicroOcpp::MOcppMongooseClient *osock;
#elif MO_NETLIB == MO_NETLIB_WASM
#include <emscripten.h>
#include <MicroOcpp/Core/Connection.h>
#include "net_wasm.h"
MicroOcpp::Connection *conn = nullptr;
#else
#error Please ensure that build flag MO_NETLIB is set as MO_NETLIB_MONGOOSE or MO_NETLIB_WASM
#endif
/*
* Setup MicroOcpp and API
*/
void load_ocpp_version(std::shared_ptr<MicroOcpp::FilesystemAdapter> filesystem) {
MicroOcpp::configuration_init(filesystem);
#if MO_ENABLE_V201
{
auto protocolVersion_stored = MicroOcpp::declareConfiguration<const char*>("OcppVersion", "1.6", SIMULATOR_FN, false, false, false);
MicroOcpp::configuration_load(SIMULATOR_FN);
if (!strcmp(protocolVersion_stored->getString(), "2.0.1")) {
//select OCPP 2.0.1
g_isOcpp201 = true;
return;
}
}
#endif //MO_ENABLE_V201
g_isOcpp201 = false;
}
void app_setup(MicroOcpp::Connection& connection, std::shared_ptr<MicroOcpp::FilesystemAdapter> filesystem) {
mocpp_initialize(connection,
g_isOcpp201 ?
ChargerCredentials::v201("MicroOcpp Simulator", "MicroOcpp") :
ChargerCredentials("MicroOcpp Simulator", "MicroOcpp"),
filesystem,
false,
g_isOcpp201 ?
MicroOcpp::ProtocolVersion{2,0,1} :
MicroOcpp::ProtocolVersion{1,6}
);
for (unsigned int i = 0; i < connectors.size(); i++) {
connectors[i].setup();
}
}
/*
* Execute one loop iteration
*/
void app_loop() {
mocpp_loop();
for (unsigned int i = 0; i < connectors.size(); i++) {
connectors[i].loop();
}
}
#if MO_NETLIB == MO_NETLIB_MONGOOSE
int main() {
mg_log_set(MG_LL_INFO);
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "0.0.0.0:8000", http_serve, NULL); // Create listening connection
auto filesystem = MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Use_Mount_FormatOnFail);
load_ocpp_version(filesystem);
osock = new MicroOcpp::MOcppMongooseClient(&mgr,
"ws://echo.websocket.events",
"charger-01",
"",
"",
filesystem,
g_isOcpp201 ?
MicroOcpp::ProtocolVersion{2,0,1} :
MicroOcpp::ProtocolVersion{1,6}
);
server_initialize(osock);
app_setup(*osock, filesystem);
for (;;) { // Block forever
mg_mgr_poll(&mgr, 100);
app_loop();
}
delete osock;
mg_mgr_free(&mgr);
return 0;
}
#elif MO_NETLIB == MO_NETLIB_WASM
int main() {
printf("[WASM] start\n");
auto filesystem = MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Deactivate);
conn = wasm_ocpp_connection_init(nullptr, nullptr, nullptr);
app_setup(*conn, filesystem);
const int LOOP_FREQ = 10; //called 10 times per second
const int BLOCK_INFINITELY = 0; //0 for non-blocking execution, 1 for blocking infinitely
emscripten_set_main_loop(app_loop, LOOP_FREQ, BLOCK_INFINITELY);
printf("[WASM] setup complete\n");
}
#endif