-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt_subscriber.hpp
157 lines (128 loc) · 4.69 KB
/
mqtt_subscriber.hpp
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
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cctype>
#include <thread>
#include <chrono>
#include <vector>
#include <functional>
#include "mqtt/client.h"
#include "include/nlohmann/json.hpp"
using namespace std::chrono;
const std::string TOPIC = "LED";
class MqttSubscriber
{
public:
MqttSubscriber()
{
}
~MqttSubscriber() = default;
static void publishToThirdParty(std::string message)
{
const std::string address = "localhost";
const std::string clientId = "smartLed";
mqtt::client client(address, clientId);
mqtt::connect_options options;
options.set_keep_alive_interval(20);
options.set_clean_session(true);
try
{
client.connect(options);
const std::string TOPIC = "outputStream";
const std::string PAYLOAD = message;
auto msg = mqtt::make_message(TOPIC, PAYLOAD);
client.publish(msg);
client.disconnect();
}
catch (const mqtt::exception &exc)
{
std::cerr << exc.what() << " [" << exc.get_reason_code() << "]" << std::endl;
}
}
static void Subscribe(std::string message)
{
const std::set<std::string> InputType = {
"UserManualInput",
"DisplayInput",
"MusicInput",
"WeatherInput",
"BrightnessInput",
"RandomInput"};
using json = nlohmann::json;
std::cout << "Started Subscribe" << std::endl;
const std::string address = "localhost";
const std::string clientId = "subscriber";
mqtt::client client(address, clientId, mqtt::create_options(MQTTVERSION_5));
mqtt::connect_options options;
options.set_clean_session(false);
options.set_automatic_reconnect(seconds(2), seconds(30));
options.set_mqtt_version(MQTTVERSION_5);
try
{
auto &ctx = AppContext::getInstance();
ctx->StartProcessingInputs();
std::cout << "Connecting to the MQTT server..." << std::flush;
client.connect(options);
std::cout << "Connected!" << std::endl;
std::cout << "Subscribing to topic: " << TOPIC << std::endl;
client.subscribe(TOPIC);
std::cout << "Subscribed to topic: " << TOPIC << std::endl;
while (true)
{
auto msg = client.consume_message();
if (msg)
{
json requestJson = json::parse(msg->to_string());
if (requestJson.contains("stop"))
{
ctx->Stop();
}
if (requestJson.contains("start"))
{
ctx->StartProcessingInputs();
}
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 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));
publishToThirdParty("A functionat");
}
else
{
throw std::runtime_error(INVALID_REQUEST_BODY);
}
}
}
else if (!client.is_connected())
{
std::cout << "Lost connection" << std::endl;
while (!client.is_connected())
{
std::this_thread::sleep_for(milliseconds(250));
}
std::cout << "Re-established connection" << std::endl;
}
}
std::cout << "Disconnecting from MQTT server..." << std::flush;
client.disconnect();
std::cout << "Disconnected from MQTT server" << std::endl;
}
catch (const mqtt::exception &exc)
{
std::cerr << exc.what() << " [" << exc.get_reason_code() << "]" << std::endl;
}
}
void StartProcessingInputs()
{
std::thread processInput(Subscribe, "PROCESS_MQTT");
processInput.detach();
}
};