-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
188 lines (153 loc) · 5.72 KB
/
main.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
/**
* @file main.cpp
* @brief Driver program for the project, exposes an HTTP server on port 8080 to handle API requests.
*/
#define INPUT_KEY "input"
#define INPUT_TYPE_KEY "input_type"
#define INPUT_SETTINGS_KEY "input_settings"
#define OUTPUT_ERROR_KEY "error"
#define OUTPUT_VALID_KEY "valid_request"
#define INVALID_REQUEST_BODY "invalid_request"
#define BAD_REQUEST_JSON "request format isn't correct"
#include <set>
#include <cstdio>
#include <string>
#include <fstream>
#include <stdexcept>
#include "device.hpp"
#include "mqtt_subscriber.hpp"
const std::set<std::string> InputType = {
"UserManualInput",
"DisplayInput",
"MusicInput",
"WeatherInput",
"BrightnessInput",
"RandomInput"};
nlohmann::json sendRequestToContext(nlohmann::json requestJson, AppContextPtr& ctx) {
nlohmann::json response;
try
{
/* Validating all mandatory keys from the JSON */
if (requestJson.contains(INPUT_KEY) &&
requestJson.contains(INPUT_TYPE_KEY) &&
requestJson.contains(INPUT_SETTINGS_KEY))
{
/* Parsing the values from each key */
const std::string requestType = requestJson[INPUT_TYPE_KEY];
const nlohmann::json requestSettings = requestJson[INPUT_SETTINGS_KEY];
if (InputType.find(requestType) != InputType.end())
{
// Pushing json to the context instance
ctx->AddInput(buildContext(requestJson[INPUT_KEY], requestType));
response[OUTPUT_VALID_KEY] = requestJson[INPUT_KEY];
}
else
{
throw std::runtime_error(INVALID_REQUEST_BODY);
}
}
}
catch (std::runtime_error &error)
{
response[OUTPUT_ERROR_KEY] = BAD_REQUEST_JSON;
}
return response;
}
/**
* @brief Opens a stream and parses the json from the parameter file
*/
void parseInputFromFile(std::string fileName, AppContextPtr& ctx)
{
using json = nlohmann::json;
std::ifstream inputJson(fileName);
json requestJson;
inputJson >> requestJson;
sendRequestToContext(requestJson, ctx);
}
/**
* @brief Main driver function of the program, runs an HTTP
* server on port 8080 to process incoming requests (blocking!)
* @return int - status code
*/
int main(void)
{
using namespace httplib;
using json = nlohmann::json;
Server svr;
/* Application context instate */
auto &ctx = AppContext::getInstance();
ctx->StartProcessingInputs();
/* Create MQTT Subscriber */
MqttSubscriber mqttSubscriber;
mqttSubscriber.StartProcessingInputs();
/* Default input stream */
parseInputFromFile("fileinput.json", ctx);
/* Static folder handling */
svr.set_mount_point("/static", "./static");
/* Route to get reached by the IoT device */
svr.Post("/start", [&](const Request &req, Response &res) {
ctx->StartProcessingInputs();
});
svr.Post("/stop", [&](const Request &req, Response &res) {
ctx->Stop();
});
svr.Post("/previous-setting", [&](const Request &req, Response &res) {
ctx->PopInput();
});
svr.Post("/iot", [&](const Request &req, Response &res) {
json requestJson = json::parse(req.body);
json response = sendRequestToContext(requestJson, ctx);
/* Return response to user */
res.set_content(response.dump(), "text/json");
});
svr.Get("/hi", [](const Request &req, Response &res) {
res.set_content("Hello World!", "text/plain");
});
printf("Listening on port 8080...\n");
svr.listen("0.0.0.0", 8080);
}
/*
Example request:
Example /iot
curl --header "Content-Type: application/json" \
--request POST \
--data '{"input_type":"UserManualInput","input":{"r":1,"g":1,"b":1,"intensity":1},"input_settings":"{}"}' \
http://localhost:8080/iot
curl --header "Content-Type: application/json" \
--request POST \
--data '{"input_type":"DisplayInput","input":[{"r":1,"g":1,"b":1,"intensity":1},{"r":2,"g":2,"b":2,"intensity":1},{"r":3,"g":3,"b":3,"intensity":1},{"r":4,"g":4,"b":4,"intensity":1}],"input_settings":"{}"}' \
http://localhost:8080/iot
curl --header "Content-Type: application/json" \
--request POST \
--data '{"input_type":"MusicInput","input":[{"frequency": 1},{"frequency": 1.25},{"frequency": 1.5},{"frequency": 2}],"input_settings":"{}"}' \
http://localhost:8080/iot
curl --header "Content-Type: application/json" \
--request POST \
--data '{"input_type":"WeatherInput","input":{"temperature":12.5},"input_settings":"{}"}' \
http://localhost:8080/iot
curl --header "Content-Type: application/json" \
--request POST \
--data '{"input_type":"BrightnessInput","input":[{"intensity": 1},{"intensity": 2},{"intensity": 3},{"intensity": 4}],"input_settings":"{}"}' \
http://localhost:8080/iot
curl --header "Content-Type: application/json" \
--request POST \
--data '{"input_type":"RandomInput","input":[],"input_settings":"{}"}' \
http://localhost:8080/iot
Example /start
Pentru MQTT daca trimiti in JSON cheia "start", atunci o sa dea start
curl --header "Content-Type: application/json" \
--request POST \
--data '{}' \
http://localhost:8080/start
Example /stop
Pentru MQTT daca trimiti in JSON cheia "stop", atunci o sa dea stop
curl --header "Content-Type: application/json" \
--request POST \
--data '{}' \
http://localhost:8080/stop
Example /previous-setting
curl --header "Content-Type: application/json" \
--request POST \
--data '{}' \
http://localhost:8080/previous-setting
*/