This commit is contained in:
Jerry
2025-04-07 11:37:52 +08:00
parent b301ac575b
commit cdbca3ee29
8 changed files with 21 additions and 10 deletions

View File

@@ -112,6 +112,8 @@ A: 1. 换根质量较好的或短一些的USB线或换个USB口插入。 <br>
A: 仅刷新app固件后配置是保留的所以无需重新配置。如果刷新了分区表partition.bin会将esp的nvs区刷新这时候需要重新配置。
## Releases
### 1.0.25 (未测试)
* Fix: 和风天气api变更需要输入API Host在配置页面增加配置项
### 1.0.24
* Refine: 配置页面location约束改为64长度允许输入经纬度设置天气位置e.g. 116.41,39.92
### 1.0.23

View File

@@ -8,6 +8,7 @@
// !!!preferences key限制15字符
#define PREF_SI_CAL_DATE "SI_CAL_DATE" // 屏幕当前显示的日期
#define PREF_SI_WEEK_1ST "SI_WEEK_1ST" // 每周第一天0: 周日默认1:周一
#define PREF_QWEATHER_HOST "QWEATHER_HOST" // QWEATHER HOST
#define PREF_QWEATHER_KEY "QWEATHER_KEY" // QWEATHER KEY/TOKEN
#define PREF_QWEATHER_TYPE "QWEATHER_TYPE" // 0: 每日天气1: 实时天气
#define PREF_QWEATHER_LOC "QWEATHER_LOC" // 地理位置

View File

@@ -163,10 +163,10 @@ public:
}
// 和风天气 - 实时天气: https://dev.qweather.com/docs/api/weather/weather-now/
bool getWeatherNow(Weather& result, const char* key, const char* locid) {
bool getWeatherNow(Weather& result, const char* host, const char* key, const char* locid) {
// return getRestfulAPI("https://www.baidu.com", [&result](JsonDocument& json) {
return getRestfulAPI(
"https://api.qweather.com/v7/weather/now?key=" + String(key) + "&location=" + String(locid), [&result](JsonDocument& json) {
"https://" + String(host) + "/v7/weather/now?key=" + String(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*>());
@@ -188,8 +188,8 @@ public:
}
// 和风天气 - 逐小时天气预报: https://dev.qweather.com/docs/api/weather/weather-hourly-forecast/
bool getForecastHourly(HourlyForecast& result, const char* key, const char* locid) {
return getRestfulAPI("https://api.qweather.com/v7/weather/24h?key=" + String(key) + "&location=" + String(locid), [&result](JsonDocument& json) {
bool getForecastHourly(HourlyForecast& result, const char* host, const char* key, const char* locid) {
return getRestfulAPI("https://" + String(host) + "/v7/weather/24h?key=" + String(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*>());
@@ -216,8 +216,8 @@ public:
}
// 和风天气 - 逐天天气预报: https://dev.qweather.com/docs/api/weather/weather-daily-forecast/
bool getForecastDaily(DailyForecast& result, const char* key, const char* locid) {
return getRestfulAPI("https://api.qweather.com/v7/weather/3d?key=" + String(key) + "&location=" + String(locid), [&result](JsonDocument& json) {
bool getForecastDaily(DailyForecast& result, const char* host, const char* key, const char* locid) {
return getRestfulAPI("https://" + String(host) + "/v7/weather/3d?key=" + String(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*>());
@@ -254,6 +254,7 @@ public:
});
}
// 一言: https://developer.hitokoto.cn/sentence/
bool getHitokoto(Hitokoto& result) {
return getRestfulAPI("https://v1.hitokoto.cn/?max_length=15", [&result](JsonDocument& json) {

View File

@@ -21,11 +21,12 @@ void IRAM_ATTR checkTicks() {
}
WiFiManager wm;
WiFiManagerParameter para_qweather_key("qweather_key", "和风天气Token", "", 32); // 和风天气key
WiFiManagerParameter para_qweather_host("qweather_host", "和风天气Host", "", 64); // 和风天气key
WiFiManagerParameter para_qweather_key("qweather_key", "和风天气API Key", "", 32); // 和风天气key
// const char* test_html = "<br/><label for='test'>天气模式</label><br/><input type='radio' name='test' value='0' checked> 每日天气test </input><input type='radio' name='test' value='1'> 实时天气test</input>";
// WiFiManagerParameter para_test(test_html);
WiFiManagerParameter para_qweather_type("qweather_type", "天气类型0:每日天气1:实时天气)", "0", 2, "pattern='\\[0-1]{1}'"); // 城市code
WiFiManagerParameter para_qweather_location("qweather_loc", "位置ID", "", 9, "pattern='\\d{9}'"); // 城市code
WiFiManagerParameter para_qweather_location("qweather_loc", "位置ID", "", 64); // 城市code
WiFiManagerParameter para_cd_day_label("cd_day_label", "倒数日4字以内", "", 10); // 倒数日
WiFiManagerParameter para_cd_day_date("cd_day_date", "日期yyyyMMdd", "", 8, "pattern='\\d{8}'"); // 城市code
WiFiManagerParameter para_tag_days("tag_days", "日期TagyyyyMMddx详见README", "", 30); // 日期Tag
@@ -187,6 +188,7 @@ void buttonClick(void* oneButton) {
void saveParamsCallback() {
Preferences pref;
pref.begin(PREF_NAMESPACE);
pref.putString(PREF_QWEATHER_HOST, para_qweather_host.getValue());
pref.putString(PREF_QWEATHER_KEY, para_qweather_key.getValue());
pref.putString(PREF_QWEATHER_TYPE, strcmp(para_qweather_type.getValue(), "1") == 0 ? "1" : "0");
pref.putString(PREF_QWEATHER_LOC, para_qweather_location.getValue());
@@ -222,6 +224,7 @@ void buttonDoubleClick(void* oneButton) {
// 根据配置信息设置默认值
Preferences pref;
pref.begin(PREF_NAMESPACE);
String qHost = pref.getString(PREF_QWEATHER_HOST);
String qToken = pref.getString(PREF_QWEATHER_KEY);
String qType = pref.getString(PREF_QWEATHER_TYPE, "0");
String qLoc = pref.getString(PREF_QWEATHER_LOC);
@@ -231,6 +234,7 @@ void buttonDoubleClick(void* oneButton) {
String week1st = pref.getString(PREF_SI_WEEK_1ST, "0");
pref.end();
para_qweather_host.setValue(qHost.c_str(), 64);
para_qweather_key.setValue(qToken.c_str(), 32);
para_qweather_location.setValue(qLoc.c_str(), 64);
para_qweather_type.setValue(qType.c_str(), 1);
@@ -241,6 +245,7 @@ void buttonDoubleClick(void* oneButton) {
wm.setTitle("J-Calendar");
wm.addParameter(&para_si_week_1st);
wm.addParameter(&para_qweather_host);
wm.addParameter(&para_qweather_key);
wm.addParameter(&para_qweather_type);
wm.addParameter(&para_qweather_location);

View File

@@ -4,6 +4,7 @@
TaskHandle_t WEATHER_HANDLER;
String _qweather_host;
String _qweather_key;
String _qweather_loc;
@@ -45,9 +46,9 @@ void task_weather(void* param) {
// 实时天气
bool success;
if (_weather_type == 0) {
success = api.getForecastDaily(_daily_forecast, _qweather_key.c_str(), _qweather_loc.c_str());
success = api.getForecastDaily(_daily_forecast, _qweather_host.c_str(), _qweather_key.c_str(), _qweather_loc.c_str());
} else {
success = api.getWeatherNow(_weather_now, _qweather_key.c_str(), _qweather_loc.c_str());
success = api.getWeatherNow(_weather_now, _qweather_host.c_str(), _qweather_key.c_str(), _qweather_loc.c_str());
}
if (success) {
_weather_status = 1;
@@ -74,6 +75,7 @@ void weather_exec(int status) {
// Preference 获取配置信息。
Preferences pref;
pref.begin(PREF_NAMESPACE);
_qweather_host = pref.getString(PREF_QWEATHER_HOST, "api.qweather.com");
_qweather_key = pref.getString(PREF_QWEATHER_KEY, "");
_qweather_loc = pref.getString(PREF_QWEATHER_LOC, "");
pref.end();