Skip to content

Commit 00062fa

Browse files
committed
EventNotifications: send/handle foreign events. Objects serialization: ApiCall and Event
1 parent 8d43ce0 commit 00062fa

13 files changed

+548
-130
lines changed

ApiModule.cpp

+7-23
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,7 @@ ApiModule::ApiModule(std::string msgType) {
1212
// STATIC DEF
1313
std::map<std::string, ApiModule *> ApiModule::registeredApiModules;
1414

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-
15+
std::string ApiModule::dispatchApiCall(ApiCall &apiCall) {
3116
// TODO
3217
// std::string msgSrc = type; // interface type (LORA,WS)
3318
// std::string msgCtx = msgRoot["ctx"];
@@ -40,13 +25,12 @@ std::string ApiModule::dispatchApiRequest(std::string apiRequestMsg) {
4025
// bool bypassEvtQueue(msgMode == "sync");
4126
// EventQueue::pushEvent(event, bypassEvtQueue);
4227
// find corresponding sub service
43-
auto apiModule = ApiModule::registeredApiModules.find(apiModuleName);
28+
auto apiModule = ApiModule::registeredApiModules.find(apiCall.apiModule);
4429
if (apiModule != ApiModule::registeredApiModules.end()) {
4530
LogStore::info(
46-
"[ApiModule::dispatchApiRequest] API request dispatched to module " +
47-
apiModuleName);
48-
// std::string dataOut = apiModule->second->onApiCall(incomingMsg,
49-
// clientKey);
31+
"[ApiModule::dispatchApiCall] API request dispatched to module " +
32+
apiCall.apiModule);
33+
std::string dataOut = apiModule->second->onApiCall(apiCall);
5034
// // default empty reply
5135
// std::string outgoingMsg("");
5236
// bool expectedReply = timestamp > 0 || dataOut.length() > 0;
@@ -72,8 +56,8 @@ std::string ApiModule::dispatchApiRequest(std::string apiRequestMsg) {
7256
// }
7357
// return outgoingMsg;
7458
} else {
75-
LogStore::info("[ApiModule::dispatchMsg] unregistered API module: " +
76-
apiModuleName);
59+
LogStore::info("[ApiModule::dispatchApiCall] unregistered API module: " +
60+
apiCall.apiModule);
7761
}
7862
return "";
7963
}

ApiModule.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#pragma once
22
#include <map>
3+
#include <CommonObjects.h>
34

45
/*
6+
* API modules reachable through MessageInterface
57
* Each inheriting class will be registered as service and must be singleton
68
* When api call or request is received, it will be dispatched to matching
79
declared service instance
@@ -15,9 +17,16 @@
1517
which is identified depends on interface type
1618
* - LORA: deviceId
1719
* - WS: opened socket (clientKey)
18-
*
20+
*
21+
* Modules examples: Test, Monitor, Benchmark,
22+
* API usescases:
23+
- device capabilities: API modules available on device,
24+
- device monitoring: report ESP32 internal state like EventQueue, registered EventListeners
25+
- benchmark device: latency by sending multiple requests,
26+
- configure runtime config like frequency of lora notifications, LORA dutycyle
1927
*/
2028
// class ApiRequestHandler {
29+
2130
class ApiModule {
2231
static std::map<std::string, ApiModule *> registeredApiModules;
2332
// static std::map<std::string, ApiModule *> serviceHandlers;
@@ -27,8 +36,8 @@ class ApiModule {
2736
ApiModule(std::string serviceId);
2837

2938
public:
30-
static std::string dispatchApiRequest(std::string apiRequestMsg);
39+
static std::string dispatchApiCall(ApiCall &apiCall);
3140
// TO BE IMPLEMENTED IN CHILD CLASS
3241

33-
virtual std::string onApiCall(std::string rawMsg) = 0;
42+
virtual std::string onApiCall(ApiCall &apiCall) = 0;
3443
};

CommonObjects.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <ArduinoJson.h>
2+
#include <CommonObjects.h>
3+
#include <LogStore.h>
4+
#include <iostream>
5+
#include <utils.h>
6+
7+
// ApiCall::ApiCall(std::string apiModule, std::string call, std::string data)
8+
// : apiModule(apiModule), call(call), data(data){};
9+
10+
/*
11+
Parse API call from MSG
12+
*/
13+
void ApiCall::fromMsg(std::string msg) {
14+
JsonDocument msgData;
15+
// convert to a json object
16+
DeserializationError error = deserializeJson(msgData, msg);
17+
std::string apiModuleCall = msgData["apiCall"];
18+
std::string sData = msgData["data"];
19+
std::vector<std::string> items = splitFromCharDelim(apiModuleCall, ':');
20+
apiModule = items.at(0);
21+
call = items.at(1);
22+
data = sData;
23+
}
24+
25+
std::string ApiCall::toMsg() {
26+
// convert object to JSON
27+
JsonDocument jsonObj;
28+
jsonObj["apiCall"] = apiModule + ":" + call;
29+
jsonObj["data"] = data;
30+
std::cout << std::hex << apiModule << std::endl;
31+
std::string jsonMsg("");
32+
serializeJson(jsonObj, jsonMsg);
33+
return jsonMsg;
34+
}
35+
36+
// /*
37+
// Transport API call over MSG
38+
// */
39+
// std::string ApiCall::toMsg() {
40+
// JsonDocument apiCall;
41+
// apiCall["module"] = apiModule;
42+
// apiCall["call"] = call;
43+
// apiCall["data"] = data;
44+
// std::string apiCallMsg;
45+
// serializeJson(req, apiCallMsg);
46+
// return apiCallMsg;
47+
// }
48+
49+
ForeignEvent::ForeignEvent() {}
50+
51+
ForeignEvent::ForeignEvent(Event evt, std::string source) : Event(evt) {
52+
context.source = source;
53+
}
54+
55+
// create event object from JSON
56+
void ForeignEvent::fromObjContent(std::string objContent) {
57+
JsonDocument objData;
58+
// convert content to a json object
59+
DeserializationError error = deserializeJson(objData, objContent);
60+
// parse JSON and fill object fields
61+
std::string evtType = objData["evtType"];
62+
std::string evtData = objData["evtData"];
63+
std::string evtSrc = objData["evtSender"];
64+
EventContext evtContext; // use default, customise any required fields
65+
evtContext.source = evtSrc;
66+
// evtCtx.timestamp = msgTime ? msgTime : evtCtx.time;
67+
// evtCtx.priority = 0;
68+
type = evtType;
69+
data = evtData;
70+
context = evtContext;
71+
}
72+
73+
// convert event object to JSON
74+
std::string Event::toObjContent() {
75+
JsonDocument jsonObj;
76+
jsonObj["evtType"] = type;
77+
jsonObj["evtData"] = data;
78+
jsonObj["evtSender"] = context.source;
79+
std::string jsonMsg("");
80+
serializeJson(jsonObj, jsonMsg);
81+
return jsonMsg;
82+
}

CommonObjects.h

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#pragma once
2+
#include <string>
3+
/*
4+
Object container to be put in message
5+
Typical objects to be carried over messages are:
6+
- API calls
7+
- Foreign events
8+
*/
9+
10+
// class MsgObj {
11+
// template <class T> static T &fromString(std::string msgObj);
12+
// template <class T> static T &fromMinifiedString(std::string msgObj);
13+
// // export foreign event content to send over MSG
14+
// virtual std::string toString() = 0;
15+
// virtual std::string toMinifiedString() = 0;
16+
// };
17+
18+
// template <T>
19+
// MsgObj::fromMsgObj(std::string msgObj){
20+
// return T::fromMsgObj(msgObj);
21+
// }
22+
23+
// class ApiCall : public MsgObj {
24+
class ApiCall {
25+
public:
26+
std::string apiModule;
27+
std::string call;
28+
std::string data;
29+
30+
// ApiCall(std::string apiModule, std::string call, std::string data);
31+
32+
void fromMsg(std::string msg);
33+
void fromMinifiedString(std::string msg);
34+
std::string toMsg();
35+
std::string toMinifiedString();
36+
};
37+
38+
/*
39+
40+
Event -- (export to EVTOBJ) --> MSG -- (parse from EVTOBJ) --> ForeignEvent
41+
*/
42+
// additional information about event
43+
// events can be filtered based on context
44+
typedef struct {
45+
// EventOrigin origin = EventOrigin::LOCAL;
46+
std::string source = "local"; // original source from where event comes from
47+
int timestamp = 0;
48+
int priority = 0;
49+
} EventContext;
50+
51+
class Event {
52+
public:
53+
std::string type;
54+
std::string data;
55+
EventContext context;
56+
// serialize event content to send over MSG
57+
std::string toObjContent();
58+
};
59+
/*
60+
apiCall: "EvtMsgNot:onForeignEvt"
61+
orig:
62+
*/
63+
class ForeignEvent : public Event {
64+
public:
65+
// std::string source;
66+
ForeignEvent();
67+
ForeignEvent(Event evt, std::string source);
68+
void fromObjContent(std::string objContent);
69+
};

0 commit comments

Comments
 (0)