1
+ #include < ApiModule.h>
2
+ #include < ArduinoJson.h>
3
+ #include < LogStore.h>
4
+ #include < System.h>
5
+
6
+ ApiModule::ApiModule (std::string msgType) {
7
+ LogStore::info (" [ApiModule::constructor] handle for " + msgType +
8
+ " message type" );
9
+ ApiModule::registeredApiModules.insert ({msgType, this });
10
+ }
11
+
12
+ // STATIC DEF
13
+ std::map<std::string, ApiModule *> ApiModule::registeredApiModules;
14
+
15
+ std::string ApiModule::dispatchApiRequest (std::string apiRequestMsg) {
16
+ JsonDocument apiRequest;
17
+ // convert to a json object
18
+ DeserializationError error = deserializeJson (apiRequest, apiRequestMsg);
19
+ std::string interfaceType = " <type>" ;
20
+ LogStore::dbg (" [MessageInterface::onMessage] received message on " +
21
+ interfaceType + " interface: " + apiRequestMsg);
22
+ // root msg level props
23
+ // std::string msgContext = msgRoot["context"];
24
+ // API module, submodule, command
25
+ JsonObject apiData = apiRequest[" api" ];
26
+ std::string apiModuleName = apiData[" module" ];
27
+ std::string apiSubmodule = apiData[" submod" ];
28
+ std::string apiCmd = apiData[" cmd" ];
29
+ std::string apiInput = apiData[" input" ]; // API module/submodule input data
30
+
31
+ // TODO
32
+ // std::string msgSrc = type; // interface type (LORA,WS)
33
+ // std::string msgCtx = msgRoot["ctx"];
34
+ // std::string clientKey = msgCtx["clientKey"];
35
+ // int msgTime = msgRoot["time"];
36
+ // if (msgTime) {
37
+ // System::time.sync(msgTime);
38
+ // }
39
+ // std::string msgMode = msgRoot["mode"]; // sync or async
40
+ // bool bypassEvtQueue(msgMode == "sync");
41
+ // EventQueue::pushEvent(event, bypassEvtQueue);
42
+ // find corresponding sub service
43
+ auto apiModule = ApiModule::registeredApiModules.find (apiModuleName);
44
+ if (apiModule != ApiModule::registeredApiModules.end ()) {
45
+ LogStore::info (
46
+ " [ApiModule::dispatchApiRequest] API request dispatched to module " +
47
+ apiModuleName);
48
+ // std::string dataOut = apiModule->second->onApiCall(incomingMsg,
49
+ // clientKey);
50
+ // // default empty reply
51
+ // std::string outgoingMsg("");
52
+ // bool expectedReply = timestamp > 0 || dataOut.length() > 0;
53
+ // // fill with data from service if any
54
+ // if (expectedReply) {
55
+ // JsonDocument replyData;
56
+ // deserializeJson(replyData, dataOut);
57
+ // // Build the JSON reply including service output
58
+ // // and additional fields (msgType, timestamp)
59
+ // JsonDocument replyRoot;
60
+ // replyRoot["handle"] = handle;
61
+ // if (timestamp > 0) {
62
+ // LogStore::info("[ApiModule::extractMsg] timestamp
63
+ // provided " +
64
+ // std::to_string(timestamp) +
65
+ // " => expected reply confirmation");
66
+ // replyRoot["timestamp"] = timestamp;
67
+ // }
68
+ // if (dataOut.length() > 0) {
69
+ // replyRoot["data"] = replyData;
70
+ // }
71
+ // serializeJsonPretty(replyRoot, outgoingMsg);
72
+ // }
73
+ // return outgoingMsg;
74
+ } else {
75
+ LogStore::info (" [ApiModule::dispatchMsg] unregistered API module: " +
76
+ apiModuleName);
77
+ }
78
+ return " " ;
79
+ }
80
+
81
+ // std::string ApiModule::dispatchMsg(std::string incomingMsg,
82
+ // std::string clientKey) {
83
+ // JsonDocument msgRoot;
84
+ // // convert to a json object
85
+ // DeserializationError error = deserializeJson(msgRoot, incomingMsg);
86
+
87
+ // // root level props common to all services
88
+ // std::string handle = msgRoot["handle"];
89
+ // int timestamp = msgRoot["timestamp"];
90
+ // if (timestamp) {
91
+ // System::time.sync(timestamp);
92
+ // }
93
+ // // find corresponding sub service
94
+ // auto msgHandler = ApiModule::registeredApiModules.find(handle);
95
+ // if (msgHandler != ApiModule::registeredApiModules.end()) {
96
+ // // LogStore::info("[ApiModule::dispatchMsg] dispatch to message
97
+ // // handler " +msgType); forward message to subservice handler
98
+ // std::string dataOut = msgHandler->second->onCall(incomingMsg,
99
+ // clientKey);
100
+ // // default empty reply
101
+ // std::string outgoingMsg("");
102
+ // bool expectedReply = timestamp > 0 || dataOut.length() > 0;
103
+ // // fill with data from service if any
104
+ // if (expectedReply) {
105
+ // JsonDocument replyData;
106
+ // deserializeJson(replyData, dataOut);
107
+ // // Build the JSON reply including service output
108
+ // // and additional fields (msgType, timestamp)
109
+ // JsonDocument replyRoot;
110
+ // replyRoot["handle"] = handle;
111
+ // if (timestamp > 0) {
112
+ // LogStore::info("[ApiModule::extractMsg] timestamp provided
113
+ // " +
114
+ // std::to_string(timestamp) +
115
+ // " => expected reply confirmation");
116
+ // replyRoot["timestamp"] = timestamp;
117
+ // }
118
+ // if (dataOut.length() > 0) {
119
+ // replyRoot["data"] = replyData;
120
+ // }
121
+ // serializeJsonPretty(replyRoot, outgoingMsg);
122
+ // }
123
+ // return outgoingMsg;
124
+ // } else {
125
+ // LogStore::info("[ApiModule::dispatchMsg] no registered message
126
+ // "
127
+ // "handler matching " +
128
+ // handle);
129
+ // }
130
+ // return "";
131
+ // }
0 commit comments