|
| 1 | +#include "AdsbExchangeClient.h" |
| 2 | + |
| 3 | + |
| 4 | +AdsbExchangeClient::AdsbExchangeClient() { |
| 5 | + |
| 6 | +} |
| 7 | + |
| 8 | +void AdsbExchangeClient::updateVisibleAircraft(String searchQuery) { |
| 9 | + JsonStreamingParser parser; |
| 10 | + parser.setListener(this); |
| 11 | + WiFiClient client; |
| 12 | + |
| 13 | + // http://public-api.adsbexchange.com/VirtualRadar/AircraftList.json?lat=47.437691&lng=8.568854&fDstL=0&fDstU=20&fAltL=0&fAltU=5000 |
| 14 | + const char host[] = "public-api.adsbexchange.com"; |
| 15 | + String url = "/VirtualRadar/AircraftList.json?" + searchQuery; |
| 16 | + |
| 17 | + const int httpPort = 80; |
| 18 | + if (!client.connect(host, httpPort)) { |
| 19 | + Serial.println("connection failed"); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + Serial.print("Requesting URL: "); |
| 25 | + Serial.println(url); |
| 26 | + |
| 27 | + // This will send the request to the server |
| 28 | + client.print(String("GET ") + url + " HTTP/1.1\r\n" + |
| 29 | + "Host: " + host + "\r\n" + |
| 30 | + "Connection: close\r\n\r\n"); |
| 31 | + |
| 32 | + int retryCounter = 0; |
| 33 | + while(!client.available()) { |
| 34 | + Serial.println("."); |
| 35 | + delay(1000); |
| 36 | + retryCounter++; |
| 37 | + if (retryCounter > 10) { |
| 38 | + return; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + int pos = 0; |
| 43 | + boolean isBody = false; |
| 44 | + char c; |
| 45 | + |
| 46 | + int size = 0; |
| 47 | + client.setNoDelay(false); |
| 48 | + while(client.connected()) { |
| 49 | + while((size = client.available()) > 0) { |
| 50 | + c = client.read(); |
| 51 | + if (c == '{' || c == '[') { |
| 52 | + isBody = true; |
| 53 | + } |
| 54 | + if (isBody) { |
| 55 | + parser.parse(c); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + endDocument(); |
| 60 | +} |
| 61 | + |
| 62 | +String AdsbExchangeClient::getFrom() { |
| 63 | + if (from[CURRENT].length() >=4) { |
| 64 | + int firstComma = from[CURRENT].indexOf(","); |
| 65 | + return from[CURRENT].substring(5, firstComma); |
| 66 | + } |
| 67 | + return ""; |
| 68 | +} |
| 69 | +String AdsbExchangeClient::getFromIcao() { |
| 70 | + if (from[CURRENT].length() >=4) { |
| 71 | + return from[CURRENT].substring(0,4); |
| 72 | + } |
| 73 | + return ""; |
| 74 | +} |
| 75 | +String AdsbExchangeClient::getTo() { |
| 76 | + if (to[CURRENT].length() >=4) { |
| 77 | + int firstComma = to[CURRENT].indexOf(","); |
| 78 | + return to[CURRENT].substring(5, firstComma); |
| 79 | + } |
| 80 | + return ""; |
| 81 | +} |
| 82 | + |
| 83 | +String AdsbExchangeClient::getToIcao() { |
| 84 | + if (to[CURRENT].length() >=4) { |
| 85 | + return to[CURRENT].substring(0,4); |
| 86 | + } |
| 87 | + return ""; |
| 88 | +} |
| 89 | +String AdsbExchangeClient::getAltitude(){ |
| 90 | + return altitude[CURRENT]; |
| 91 | +} |
| 92 | +double AdsbExchangeClient::getDistance() { |
| 93 | + return distance[CURRENT]; |
| 94 | + |
| 95 | +} |
| 96 | +String AdsbExchangeClient::getAircraftType() { |
| 97 | + return aircraftType[CURRENT]; |
| 98 | + |
| 99 | +} |
| 100 | +String AdsbExchangeClient::getOperatorCode() { |
| 101 | + return operatorCode[CURRENT]; |
| 102 | +} |
| 103 | + |
| 104 | +double AdsbExchangeClient::getHeading() { |
| 105 | + return heading[CURRENT]; |
| 106 | +} |
| 107 | + |
| 108 | +void AdsbExchangeClient::whitespace(char c) { |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | +void AdsbExchangeClient::startDocument() { |
| 113 | + counter = 0; |
| 114 | + currentMinDistance = 1000.0; |
| 115 | +} |
| 116 | + |
| 117 | +void AdsbExchangeClient::key(String key) { |
| 118 | + currentKey = key; |
| 119 | +} |
| 120 | + |
| 121 | +void AdsbExchangeClient::value(String value) { |
| 122 | + /*String from = ""; |
| 123 | + String to = ""; |
| 124 | + String altitude = ""; |
| 125 | + String aircraftType = ""; |
| 126 | + String currentKey = ""; |
| 127 | + String operator = ""; |
| 128 | +
|
| 129 | +
|
| 130 | + "Type": "A319", |
| 131 | + "Mdl": "Airbus A319 112", |
| 132 | +
|
| 133 | + "From": "LSZH Z\u00c3\u00bcrich, Zurich, Switzerland", |
| 134 | + "To": "LEMD Madrid Barajas, Spain", |
| 135 | + "Op": "Swiss International Air Lines", |
| 136 | + "OpIcao": "SWR", |
| 137 | + "Dst": 6.23, |
| 138 | + "Year": "1996" |
| 139 | + */ |
| 140 | + if (currentKey == "Id") { |
| 141 | + counter++; |
| 142 | + } else if (currentKey == "From") { |
| 143 | + from[TEMP] = value; |
| 144 | + } else if (currentKey == "To") { |
| 145 | + to[TEMP] = value; |
| 146 | + } else if (currentKey == "OpIcao") { |
| 147 | + operatorCode[TEMP] = value; |
| 148 | + } else if (currentKey == "Dst") { |
| 149 | + distance[TEMP] = value.toFloat(); |
| 150 | + } else if (currentKey == "Mdl") { |
| 151 | + aircraftType[TEMP] = value; |
| 152 | + } else if (currentKey == "Trak") { |
| 153 | + heading[TEMP] = value.toFloat(); |
| 154 | + } else if (currentKey == "Alt") { |
| 155 | + altitude[TEMP] = value; |
| 156 | + } else if (currentKey == "Trt") { |
| 157 | + if (distance[TEMP] < currentMinDistance) { |
| 158 | + currentMinDistance = distance[TEMP]; |
| 159 | + Serial.println("Found a closer aircraft"); |
| 160 | + from[CURRENT] = from[TEMP]; |
| 161 | + to[CURRENT] = to[TEMP]; |
| 162 | + altitude[CURRENT] = altitude[TEMP]; |
| 163 | + distance[CURRENT] = distance[TEMP]; |
| 164 | + aircraftType[CURRENT] = aircraftType[TEMP]; |
| 165 | + operatorCode[CURRENT] = operatorCode[TEMP]; |
| 166 | + heading[CURRENT] = heading[TEMP]; |
| 167 | + } |
| 168 | + } |
| 169 | + Serial.println(currentKey + "=" + value); |
| 170 | +} |
| 171 | + |
| 172 | +int AdsbExchangeClient::getNumberOfVisibleAircrafts() { |
| 173 | + return counter; |
| 174 | +} |
| 175 | + |
| 176 | +void AdsbExchangeClient::endArray() { |
| 177 | + |
| 178 | +} |
| 179 | + |
| 180 | +void AdsbExchangeClient::endObject() { |
| 181 | + |
| 182 | +} |
| 183 | + |
| 184 | +void AdsbExchangeClient::endDocument() { |
| 185 | + Serial.println("Flights: " + String(counter)); |
| 186 | + if (counter == 0 && lastSightingMillis < millis() - MAX_AGE_MILLIS) { |
| 187 | + for (int i = 0; i < 2; i++) { |
| 188 | + from[i] = ""; |
| 189 | + to[i] = ""; |
| 190 | + altitude[i] = ""; |
| 191 | + distance[i] = 1000.0; |
| 192 | + aircraftType[i] = ""; |
| 193 | + operatorCode[i] = ""; |
| 194 | + heading[i] = 0.0; |
| 195 | + } |
| 196 | + } else if (counter > 0) { |
| 197 | + lastSightingMillis = millis(); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +boolean AdsbExchangeClient::isAircraftVisible() { |
| 202 | + return counter > 0 || lastSightingMillis > millis() - MAX_AGE_MILLIS; |
| 203 | +} |
| 204 | + |
| 205 | +void AdsbExchangeClient::startArray() { |
| 206 | + |
| 207 | +} |
| 208 | + |
| 209 | +void AdsbExchangeClient::startObject() { |
| 210 | + |
| 211 | +} |
0 commit comments