-
Notifications
You must be signed in to change notification settings - Fork 3
/
FindPassive.ino
181 lines (163 loc) · 4.44 KB
/
FindPassive.ino
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "FindPassive.h"
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#ifdef INCLUDE_TIMESTAMP
#include <NTPClient.h> //https://github.com/arduino-libraries/NTPClient
#include <WiFiUdp.h>
#endif
#define ARDUINOJSON_USE_LONG_LONG 1
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <vector>
FindPassive::FindPassive(String server, String group, short sVersion) {
_group = group;
_server = server;
if (sVersion == -1) {
HTTPRes http = getHttp("/now"); // Try a URL that only exists in find3
_sVersion = http.rCode == 200 ? 3 : 2;
} else {
_sVersion = sVersion;
}
}
FindPassive::~FindPassive() {
}
void FindPassive::AddWifiSignal(String mac, int rssi) {
_wifiSignals.push_back({mac, rssi});
}
String FindPassive::getJSON() {
String request;
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
switch (_sVersion) {
case 2: {
root["node"] = FIND_NODE;
root["group"] = _group;
#ifdef INCLUDE_TIMESTAMP
root["timestamp"] = getTimestamp();
#endif
JsonArray& signals = root.createNestedArray("signals");
for (wifiSignal s : _wifiSignals) {
JsonObject& signal = signals.createNestedObject();
signal["mac"] = s.mac;
signal["rssi"] = s.rssi;
}
break;
}
case 3: {
root["d"] = FIND_NODE;
root["f"] = _group;
#ifdef INCLUDE_TIMESTAMP
root["t"] = getTimestamp();
#endif
JsonObject& signals = root.createNestedObject("s");
JsonObject& wifi = signals.createNestedObject("wifi");
for (wifiSignal s : _wifiSignals) {
wifi[s.mac] = s.rssi;
}
break;
}
}
root.printTo(request);
return request;
}
HTTPRes FindPassive::sendData() {
HTTPUrl purl = parseURL(_server + "/passive");
HTTPRes res;
HTTPClient http;
WiFiClient client;
res.url = purl.proto + "://" + purl.host + ":" + String(purl.port) + purl.path;
http.begin(client, purl.host, purl.port, purl.path);
http.addHeader("Content-Type", "application/json");
res.rCode = http.POST(getJSON());
http.end();
return res;
}
HTTPRes FindPassive::getHttp(String url = "/") {
HTTPRes res;
HTTPUrl purl = parseURL(_server + url);
WiFiClient client;
HTTPClient http;
const char* headerNames[] = { "Location" };
if (purl.proto == "https") {
_ishttps = true;
Serial.println(F("https is not supported at this time"));
#ifdef __EXCEPTIONS
throw 443;
#endif
}
res.url = purl.proto + "://" + purl.host + ":" + String(purl.port) + purl.path;
http.begin(client, purl.host, purl.port, purl.path);
http.collectHeaders(headerNames, sizeof(headerNames) / sizeof(headerNames[0]));
res.rCode = http.GET();
if (http.hasHeader("Location")) {
String newServer = http.header("Location");
_server = newServer.substring(0, newServer.lastIndexOf(url));
http.end();
return getHttp(url);
}
res.body = http.getString();
http.end();
return res;
}
HTTPUrl FindPassive::parseURL(String url) {
String tmp;
HTTPUrl res;
size_t found;
// Determine protocol
found = url.indexOf("://");
if (found == -1) {
res.proto = "http";
tmp = url;
} else {
res.proto = url.substring(0, found);
tmp = url.substring(found + 3);
}
// Determine path
found = tmp.indexOf("/");
if (found == -1) {
res.host = tmp;
} else {
res.host = tmp.substring(0, found);
res.path = tmp.substring(found);
}
// Determine port
found = res.host.indexOf(":");
if (found == -1) {
if (res.proto == "https")
res.port = 443;
else
res.port = 80;
} else {
res.port = res.host.substring(found + 1).toInt();
res.host = res.host.substring(0, found);
}
return res;
}
#ifdef INCLUDE_TIMESTAMP
unsigned long FindPassive::getTimestamp() {
switch (_sVersion) {
case 3: {
HTTPRes http = getHttp("/now");
String tst = http.body.substring(0, http.body.length() - 4);
return tst.toInt();
break;
}
case 2:
default: {
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
_sVersion = 2;
timeClient.begin();
unsigned long startMillis = millis();
unsigned long currentMillis = startMillis;
while (!timeClient.update()) {
currentMillis = millis();
if (currentMillis - startMillis >= 1000) {
break;
}
}
return timeClient.getEpochTime();
break;
}
}
}
#endif