-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTTServer.cpp
76 lines (64 loc) · 1.42 KB
/
MQTTServer.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
//
//
//
#include "MQTTServer.h"
MQTTServer::MQTTServer(String host, String port, const String publishChannel, const String subsctibeChannel)
{
this->host = host;
this->port = port;
this->publishChannel = publishChannel;
this->subsctibeChannel = subsctibeChannel;
}
bool MQTTServer::send(String data)
{
return pubSubClient.publish(publishChannel.c_str(), data.c_str());
}
void MQTTServer::addNetworkConnection(NetworkConnection * connection)
{
networkList.add(connection);
}
bool MQTTServer::isConnected(void)
{
if(!pubSubClient.loop())
{
for(int i = 0;i<networkList.size();i++)
{
NetworkConnection *network = networkList.get(i);
if(network->connect())
{
if (!pubSubClient.loop())
{
if (network->getConnectionType() == WIFI)
{
pubSubClient.setClient(wifiClient);
return connect();
}
if (network->getConnectionType() == GPRS)
{
pubSubClient.setClient(gprsClient);
return connect();
}
}else
{
return true ;
}
}else
{
network->disconnect();
}
}
return false;
};
return true;
}
void MQTTServer::setCallback(void(*callback)(char *, uint8_t *, unsigned int))
{
pubSubClient.setCallback(callback);
}
bool MQTTServer::connect(void)
{
pubSubClient.setServer(host.c_str(), port.toInt());
bool connect = pubSubClient.connect("LinkIt One"+random(10000));
pubSubClient.subscribe(subsctibeChannel.c_str());
return connect;
}