-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt_connector.cpp
48 lines (40 loc) · 1.23 KB
/
mqtt_connector.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
#include "mqtt_connector.h"
#include <mosquittopp.h>
#include <iostream>
mqtt_connector::mqtt_connector(const char *id, const char *host, int port) : mosquittopp(id)
{
int keepalive = 300;
int rc = connect(host, port, keepalive);
std::cout << "Connect: " << mosqpp::strerror(rc) << std::endl;
};
mqtt_connector::~mqtt_connector()
{
}
void mqtt_connector::on_connect(int rc)
{
std::cout << "Connected with code " << rc << std::endl;
if(rc == 0){
/* Only attempt to subscribe on a successful connect. */
//subscribe(NULL, "temperature/celsius");
}
}
void mqtt_connector::on_message(const struct mosquitto_message *message)
{
}
void mqtt_connector::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
std::cout << "Subscription succeeded.\n" << std::endl;
}
int mqtt_connector::send(const char *topic, int size, const void *payload, int qos, bool retain)
{
std::cout << "Mesage sent (length: " << size << " qos: " << qos << " retain: " << retain << ")\r\n";
return publish(NULL, topic, size, payload, qos, retain);
}
void mqtt_connector::on_disconnect(int rc)
{
std::cout << "Disconnected with code: " << mosqpp::strerror(rc) << std::endl;
}
void mqtt_connector::on_error()
{
std::cout << "MQTT error " << std::endl;
}