-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircuitIoTDevice.cpp
140 lines (112 loc) · 3.68 KB
/
CircuitIoTDevice.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
#include "CircuitIoTDevice.h"
CircuitIoTDevice::CircuitIoTDevice(WiFiClientSecure& wifiClient, char token[]) : CircuitIoTMqtt(wifiClient), CircuitIoTUdp() {
this->_token = token;
parseToken();
PubSubClient::setCallback([this] (char* topic, byte* payload, unsigned int length) { this->onCallback(topic, payload, length); });
CircuitIoTUdp::setUdpCallback([this] (char* topic, byte* payload, unsigned int length) { this->onCallback(topic, payload, length); });
};
void CircuitIoTDevice::setup() {
//parseToken();
delay(1000);
this->_subscriptionRoot = String("device/" + _deviceId).c_str();
this->_publishRoot = String("client/" + _deviceId).c_str();
setupMqtt(_deviceId.c_str(), _deviceId.c_str(), _deviceSecret.c_str(), subscriptionApplicable());
setupUdp();
}
void CircuitIoTDevice::run() {
runUdp();
CircuitIoTMqtt::runMqtt();
}
void CircuitIoTDevice::parseToken(){
StaticJsonDocument<200> doc;
int inputStringLength = strlen(_token);
int decodedLength = Decoder.decodedLength(_token, inputStringLength);
char decodedString[decodedLength];
Decoder.decode(decodedString, _token, inputStringLength);
DeserializationError error = deserializeJson(doc, decodedString);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
String id = doc["id"];
String secret = doc["secret"];
String cat = doc["cat"];
_deviceId = id;
_deviceSecret = secret;
_deviceCategory = cat;
int deviceSize = doc["childs"].size();
_deviceSize = deviceSize+1;
if(deviceSize > 0){
_devices[0] = id;
for (int i =0; i< deviceSize; i++) {
String dev = doc["childs"][i];
_devices[i+1] = dev;
}
}
}
void CircuitIoTDevice::onCallback(char* topic, byte* payload, unsigned int length) {
char val[length+1];
for (int i=0;i<length;i++)
{
val[i] = (char)payload[i];
}
val[length] = NULL;
if (strcmp(val, "RES") == 0) {
Serial.println("Resetting device...");
for (int i = 0; i < 96; ++i) {
EEPROM.write(i, 0);
}
EEPROM.commit();
delay(100);
ESP.reset();
}
if (strcmp(val, "RST") == 0) {
Serial.println("Restarting device...");
ESP.reset();
}
String property = String(topic).substring(_rootTopic.length() + 1);
if(property.indexOf("/") > 0){
char *strings[2];
char *ptr = NULL;
byte index = 0;
char* np = (char *) property.c_str();
ptr = strtok(np, "/");
while (ptr != NULL)
{
strings[index] = ptr;
index++;
ptr = strtok(NULL, "/");
}
if (deviceCallback != NULL) deviceCallback(strings[0], strings[1], val);
}else{
if (deviceCallback != NULL) deviceCallback(_deviceId.c_str(), property.c_str(), val);
}
//Need to modify to publish for sub devices as well
String tpcc = String(topic).substring(_rootTopic.length() + 1);
String pbTopic = String(_publishRoot + "/" + tpcc).c_str();
publish(pbTopic.c_str(), val);
//If parent device
if(tpcc.indexOf("/") <= 0){
for (int i =1; i< _deviceSize; i++) {
String ppTopic = String(_publishRoot + "/" + _devices[i] + "/" + tpcc).c_str();
publish(ppTopic.c_str(), val);
}
}
}
void CircuitIoTDevice::setDeviceCallback(DEVICE_CALLBACK_SIGNATURE) {
this->deviceCallback = deviceCallback;
}
bool CircuitIoTDevice::subscriptionApplicable(){
int categorySize = sizeof(subscribeCategories) / sizeof(subscribeCategories[0]);
bool applicable = false;
int x;
for (x=0; x<categorySize; x=x+1){
if (strcmp(subscribeCategories[x], this->_deviceCategory.c_str()) == 0){
applicable = true;
break;
}
}
return applicable;
}