-
Notifications
You must be signed in to change notification settings - Fork 13
/
MQTTConnection.cc
84 lines (67 loc) · 1.67 KB
/
MQTTConnection.cc
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
#include <iostream>
#include <sstream>
#include "MQTTConnection.h"
#include <unistd.h>
using namespace std;
MQTTConnection::MQTTConnection( const std::string hostname, const std::string topic, const int port)
{
_hostname = hostname;
_topic = topic;
_port=port;
_status = 0;
mosquitto_lib_init();
mosq = mosquitto_new(NULL, true, NULL);
if (mosq == NULL)
{
cerr << "Failed to create Mosquitto object" << endl;
_status =-3;
}
if ( OpenConnection())
{
cerr << "Failed to connect to server" << endl;
}
CloseConnection();
}
int MQTTConnection::send(const std::string message)
{
//cout << __FILE__ << " " << __LINE__ << " sending message " << message;
if ( OpenConnection())
{
return _status;
}
int s = mosquitto_publish(mosq, NULL, _topic.c_str(), message.size(), message.c_str(), 0, false);
if (s != MOSQ_ERR_SUCCESS)
{
cerr << "Failed to publish message: " << mosquitto_strerror(s) << endl;
mosquitto_destroy(mosq);
_status = -2;
return _status;
}
CloseConnection();
return 0;
}
MQTTConnection::~MQTTConnection()
{
CloseConnection();
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
}
int MQTTConnection::OpenConnection()
{
int rc = mosquitto_connect(mosq, _hostname.c_str(), _port, 60);
if (rc != MOSQ_ERR_SUCCESS)
{
cerr << "Failed to connect to MQTT broker: " << mosquitto_strerror(rc) << " on port " << _port << endl;
mosquitto_destroy(mosq);
_status = -2;
return _status;
}
_status = 0;
return 0;
}
int MQTTConnection::CloseConnection()
{
mosquitto_disconnect(mosq);
// mosquitto_destroy(mosq);
return 0;
}