-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiFraSDK.cpp
156 lines (117 loc) · 5.38 KB
/
iFraSDK.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
#include "iFraSDK.h"
DynamicJsonDocument _doc(_capacity);
iFraSDK::iFraSDK(){
}
iFraSDK::iFraSDK(HardWarePlatform* hardWarePlatform, char* channel, char* username, char* password) {
//Assign to global valuable.
this->hardWarePlatform_ = hardWarePlatform;
this->channel_ = channel;
this->username_ = username;
this->password_ = password;
//Get client from strategy sdk
this->client_ = this->hardWarePlatform_->GetClient();
//Set Client to MQTT client
this->mqtt_client_.setClient(*this->client_);
//Setup server and port number.
this->mqtt_client_.setServer(IFRA_SERVER, MQTT_PORT);
}
iFraSDK::iFraSDK(HardWarePlatform* hardWarePlatform ,char* channel, char* username, char* password, char* server){
this->channel_ = channel;
this->username_ = username;
this->password_ = password;
this->hardWarePlatform_ = hardWarePlatform;
this->server_ = server;
//Get client from strategy sdk
this->client_ = this->hardWarePlatform_->GetClient();
//Set Client to MQTT client
this->mqtt_client_.setClient(*this->client_);
//Setup server and port number.
this->mqtt_client_.setServer(this->server_, MQTT_PORT);
}
void iFraSDK::addSensor(char * sensor_name, char * unit, float value){
JsonObject doc = _doc.createNestedObject();
doc["n"] = sensor_name;
doc["u"] = unit;
doc["v"] = value;
_recordCount++;
}
void iFraSDK::addActuator(void (*callbackFunc)(char * actuator_name, int * value , char * topic, byte * payload, unsigned int length)){
this->mqtt_client_.setCallback([this,callbackFunc](char * topic, byte * payload, unsigned int length) {
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(3) + 30;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, payload);
JsonObject root_0 = doc[0];
const char* actuator_name_ = root_0["n"]; // "Name"
const char* root_0_u = root_0["u"]; // "Unit"
int value_ = root_0["v"]; // Value
callbackFunc(( char*)actuator_name_,&value_, topic, payload, length);
});
}
void iFraSDK::addActuator(void (*callbackFunc)(char * actuator_name, float * value , char * topic, byte * payload, unsigned int length)){
this->mqtt_client_.setCallback([this,callbackFunc](char * topic, byte * payload, unsigned int length) {
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(3) + 30;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, payload);
JsonObject root_0 = doc[0];
const char* actuator_name_ = root_0["n"]; // "Name"
const char* root_0_u = root_0["u"]; // "Unit"
float value_ = root_0["v"]; // Value
callbackFunc(( char*)actuator_name_,&value_, topic, payload, length);
});
}
void iFraSDK::addEventActuator(char * actuator_name, int actuator_value , void (*callbackFunc)(char * actuator_name, float * value)){
this->mqtt_client_.setCallback([this,actuator_name,actuator_value,callbackFunc](char * topic, byte * payload, unsigned int length) {
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(3) + 30;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, payload);
JsonObject root_0 = doc[0];
const char* actuator_name_ = root_0["n"]; // "Name"
const char* root_0_u = root_0["u"]; // "Unit"
long value_ = root_0["v"]; // Value
if (*actuator_name_ == *actuator_name && value_ == (long) actuator_value) {
Serial.print("callback:");
Serial.println(actuator_name_);
callbackFunc(( char*)actuator_name_, (float*)value_);
}
});
}
void iFraSDK::send() {
if (this->mqtt_client_.connected()) {
String channel(this->channel_);
String topic = "organization/"+channel+"/messages";
char message[4096];
serializeJson(_doc, message);
this->mqtt_client_.publish(topic.c_str(), message);
this->mqtt_client_.loop();
Serial.println(message);
}else{
this->reconnect();
}
_doc.clear();
_recordCount = 0;
}
void iFraSDK::wifiConnection(){
this->hardWarePlatform_->Connect();
while (!this->mqtt_client_.connected()) {
reconnect();
}
}
bool iFraSDK::connected(){
this->mqtt_client_.loop();
return this->mqtt_client_.connected();
}
void iFraSDK::reconnect(){
Serial.println("Trying connect IFRA MQTT...");
if (this->mqtt_client_.connect(this->username_, this->username_, this->password_)) {
Serial.println("connected ^_^");
String channel(this->channel_);
String topic = "organization/"+ channel +"/messages/actuator";
this->mqtt_client_.subscribe(topic.c_str());
} else {
Serial.print("failed, rc=");
Serial.print(this->mqtt_client_.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}