-
Notifications
You must be signed in to change notification settings - Fork 3
/
API.hpp
341 lines (314 loc) · 12.7 KB
/
API.hpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* 将所有的 API 请求放到一个文件中方便在其他项目中复用, 此文件以 MIT 协议开源
*
* @author QingChenW
* @copyright MIT license
*/
#ifndef __API_HPP__
#define __API_HPP__
#include <Arduino.h>
#if defined(ESP8266)
#include <WiFiClientSecureBearSSL.h>
#include <ESP8266HTTPClient.h>
using BearSSL::WiFiClientSecure;
#elif defined(ESP32)
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#endif
#include <ArduinoUZlib.h>
#include <ArduinoJson.h>
// 以下参数请按照您的项目需求修改
#include "config.h"
#ifndef QWEATHER_KEY
#define QWEATHER_KEY ""
#endif
struct Weather {
String time;
int8_t temp;
int8_t humidity;
int16_t wind360;
String windDir;
int8_t windScale;
uint8_t windSpeed;
uint16_t icon;
String text;
};
struct DailyWeather {
String date;
String sunrise;
String sunset;
String moonPhase;
uint16_t moonPhaseIcon;
int8_t tempMax;
int8_t tempMin;
int8_t humidity;
uint16_t iconDay;
String textDay;
uint16_t iconNight;
String textNight;
int16_t wind360Day;
String windDirDay;
int8_t windScaleDay;
uint8_t windSpeedDay;
int16_t wind360Night;
String windDirNight;
int8_t windScaleNight;
uint8_t windSpeedNight;
};
struct HourlyForecast {
Weather *weather;
uint8_t length;
uint8_t interval;
};
struct DailyForecast {
DailyWeather *weather;
uint8_t length;
};
struct Hitokoto {
String sentence;
String from;
String from_who;
};
struct Bilibili {
uint64_t follower;
uint64_t view;
uint64_t likes;
};
template <uint8_t MAX_RETRY = 3>
class API {
using callback = std::function<bool(JsonDocument&)>;
using precall = std::function<void()>;
private:
WiFiClientSecure client;
HTTPClient http;
/**
* @brief 向指定的 Restful API 发送请求并自动将返回的数据解析为 json 对象, 如果失败则重试最多 MAX_RETRY 次
*
* @param url Restful API 地址
* @param cb 数据解析完成回调, 返回值代表是否成功解析
* @param pre 请求发送前拦截器, 可以用于添加 Cookie 或 Token 等
* @return 是否成功请求
*/
bool getRestfulAPI(String url, callback cb, precall pre = precall()) {
static const char *headers[] = {"Content-Encoding"};
Serial.print(F("Request "));
Serial.println(url);
for (uint8_t i = 0; i < MAX_RETRY; i++) {
bool shouldRetry = false;
if (http.begin(client, url)) {
http.collectHeaders(headers, sizeof(headers[0]) / sizeof(headers));
if (pre) pre();
int httpCode = http.GET();
if (httpCode == 200) {
DeserializationError error;
if (http.header(headers[0]).indexOf("gzip") > -1) {
uint8_t *response = (uint8_t*) malloc(http.getSize());
http.getStream().readBytes(response, http.getSize());
uint8_t *buffer = nullptr;
uint32_t size = 0;
ArduinoUZlib::decompress(response, http.getSize(), buffer, size);
free(response);
DynamicJsonDocument doc(6144); // ESP8266 内存不足, 只能分配这么多
error = deserializeJson(doc, buffer, size);
if (!error) {
bool ret = cb(doc);
free(buffer); // 字符串是零拷贝的, 不能在调用回调前释放
http.end();
return ret;
}
free(buffer);
} else {
DynamicJsonDocument doc(8192);
error = deserializeJson(doc, client);
if (!error) {
bool ret = cb(doc);
http.end();
return ret;
}
}
if (error) {
Serial.print(F("Parse JSON failed, error: "));
Serial.println(error.f_str());
shouldRetry = error == DeserializationError::IncompleteInput;
}
} else {
Serial.print(F("Get failed, error: "));
if (httpCode < 0) {
Serial.println(http.errorToString(httpCode));
shouldRetry =
#if defined(ESP8266)
httpCode == HTTPC_ERROR_CONNECTION_FAILED ||
#elif defined(ESP32)
httpCode == HTTPC_ERROR_CONNECTION_REFUSED ||
#endif
httpCode == HTTPC_ERROR_CONNECTION_LOST ||
httpCode == HTTPC_ERROR_READ_TIMEOUT;
} else {
Serial.println(httpCode);
}
}
http.end();
} else {
Serial.println(F("Unable to connect"));
}
if (!shouldRetry) break;
Serial.println(F("Retry after 30 second"));
delay(30000);
}
return false;
}
public:
API() {
// client.setFingerprint(fingerprint);
// 不安全就不安全吧, 主要是我比较懒(
client.setInsecure();
#ifdef ESP8266
// 默认Buffer大小是16KB+512B......
client.setBufferSizes(4096, 512);
#endif
// 默认超时时间是5000ms, 如果觉得不够长就取消下面的注释
// http.setTimeout(10000);
}
~API() {}
// 获取 WiFiClient
WiFiClientSecure& wifiClient() { return client; }
// 获取 HTTPClient
HTTPClient& httpClient() { return http; }
// 从我的服务器获取当前时间戳(毫秒)
bool getTimestamp(uint64_t &result) {
return getRestfulAPI("https://api.dawncraft.cc/misc/timestamp", [&result](JsonDocument& json) {
if (json["code"] != 0) {
Serial.print(F("Get time failed, error: "));
Serial.println(json["msg"].as<const char*>());
return false;
}
result = json["data"];
return true;
});
}
// 和风天气 - 实时天气: https://dev.qweather.com/docs/api/weather/weather-now/
bool getWeatherNow(Weather &result, uint32_t locid) {
return getRestfulAPI("https://devapi.qweather.com/v7/weather/now?gzip=n&key=" QWEATHER_KEY "&location=" + String(locid), [&result](JsonDocument& json) {
if (strcmp(json["code"], "200") != 0) {
Serial.print(F("Get weather failed, error: "));
Serial.println(json["code"].as<const char*>());
return false;
}
JsonObject now = json["now"];
result.time = now["obsTime"].as<const char*>();
result.temp = atoi(now["temp"]);
result.humidity = atoi(now["humidity"]);
result.wind360 = atoi(now["wind360"]);
result.windDir = now["windDir"].as<const char*>();
result.windScale = atoi(now["windScale"]);
result.windSpeed = atoi(now["windSpeed"]);
result.icon = atoi(now["icon"]);
result.text = now["text"].as<const char*>();
return true;
});
}
// 和风天气 - 逐小时天气预报: https://dev.qweather.com/docs/api/weather/weather-hourly-forecast/
bool getForecastHourly(HourlyForecast &result, uint32_t locid) {
return getRestfulAPI("https://devapi.qweather.com/v7/weather/24h?gzip=n&key=" QWEATHER_KEY "&location=" + String(locid), [&result](JsonDocument& json) {
if (strcmp(json["code"], "200") != 0) {
Serial.print(F("Get hourly forecast failed, error: "));
Serial.println(json["code"].as<const char*>());
return false;
}
uint8_t i, hours = json["hourly"].size();
for (i = 0; i < result.length; i++) {
if (i * result.interval >= hours) break;
Weather &weather = result.weather[i];
JsonObject hourly = json["hourly"][i * result.interval];
weather.time = hourly["fxTime"].as<const char*>();
weather.temp = atoi(hourly["temp"]);
weather.humidity = atoi(hourly["humidity"]);
weather.wind360 = atoi(hourly["wind360"]);
weather.windDir = hourly["windDir"].as<const char*>();
weather.windScale = atoi(hourly["windScale"]);
weather.windSpeed = atoi(hourly["windSpeed"]);
weather.icon = atoi(hourly["icon"]);
weather.text = hourly["text"].as<const char*>();
}
result.length = i;
return true;
});
}
// 和风天气 - 逐天天气预报: https://dev.qweather.com/docs/api/weather/weather-daily-forecast/
bool getForecastDaily(DailyForecast &result, uint32_t locid) {
return getRestfulAPI("https://devapi.qweather.com/v7/weather/3d?gzip=n&key=" QWEATHER_KEY "&location=" + String(locid), [&result](JsonDocument& json) {
if (strcmp(json["code"], "200") != 0) {
Serial.print(F("Get daily forecast failed, error: "));
Serial.println(json["code"].as<const char*>());
return false;
}
uint8_t i;
for (i = 0; i < result.length; i++) {
DailyWeather &weather = result.weather[i];
JsonObject daily = json["daily"][i];
weather.date = daily["fxDate"].as<const char*>();
weather.sunrise = daily["sunrise"].as<const char*>();
weather.sunset = daily["sunset"].as<const char*>();
weather.moonPhase = daily["moonPhase"].as<const char*>();
weather.moonPhaseIcon = atoi(daily["moonPhaseIcon"]);
weather.tempMax = atoi(daily["tempMax"]);
weather.tempMin = atoi(daily["tempMin"]);
weather.humidity = atoi(daily["humidity"]);
weather.iconDay = atoi(daily["iconDay"]);
weather.textDay = daily["textDay"].as<const char*>();
weather.iconNight = atoi(daily["iconNight"]);
weather.textNight = daily["textNight"].as<const char*>();
weather.wind360Day = atoi(daily["wind360Day"]);
weather.windDirDay = daily["windDirDay"].as<const char*>();
weather.windScaleDay = atoi(daily["windScaleDay"]);
weather.windSpeedDay = atoi(daily["windSpeedDay"]);
weather.wind360Night = atoi(daily["wind360Night"]);
weather.windDirNight = daily["windDirNight"].as<const char*>();
weather.windScaleNight = atoi(daily["windScaleNight"]);
weather.windSpeedNight = atoi(daily["windSpeedNight"]);
}
result.length = i;
return true;
});
}
// 一言: https://developer.hitokoto.cn/sentence/
bool getHitokoto(Hitokoto &result) {
return getRestfulAPI("https://v1.hitokoto.cn/?max_length=15", [&result](JsonDocument& json) {
result.sentence = json["hitokoto"].as<const char*>();
result.from = json["from"].as<const char*>();
result.from_who = json["from_who"].as<const char*>();
return true;
});
}
// B站粉丝
bool getFollower(Bilibili &result, uint32_t uid) {
return getRestfulAPI("https://api.bilibili.com/x/relation/stat?vmid=" + String(uid), [&result](JsonDocument& json) {
if (json["code"] != 0) {
Serial.print(F("Get bilibili follower failed, error: "));
Serial.println(json["message"].as<const char*>());
return false;
}
result.follower = json["data"]["follower"];
return true;
});
}
// B站总播放量和点赞数
bool getLikes(Bilibili &result, uint32_t uid, const char *cookie) {
return getRestfulAPI("https://api.bilibili.com/x/space/upstat?mid=" + String(uid), [&result](JsonDocument& json) {
if (json["code"] != 0) {
Serial.print(F("Get bilibili likes failed, error: "));
Serial.println(json["message"].as<const char*>());
return false;
}
result.view = json["data"]["archive"]["view"];
result.likes = json["data"]["likes"];
return true;
}, [this, &cookie]() {
http.addHeader("Cookie", String("SESSDATA=") + cookie + ";");
});
}
};
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_API)
API<> api;
#endif
#endif // __API_HPP__