Pendant_Rtc_Toy/main/weather_api.h
Rdzleo 93f0e19d1d 初始化项目:精灵吊坠 RTC 语音助手 + VEML7700 石头同频匹配
ESP32-S3 吊坠设备固件,集成火山引擎 RTC 语音助手、蓝牙配网、
VEML7700 环境光传感器驱动及石头同频匹配交友功能。

VEML7700 驱动:
- 基于 ESP-IDF i2c_master API 实现,复用项目 I2cDevice 基类
- 支持 ALS + White 双通道、自动量程、Vishay 非线性校正
- 3 次采样取中位数过滤偶发异常

石头同频匹配算法(双维度):
- 维度1:光谱比值 ALS/White(石头固有光学特征,不随光照强度变化)
- 维度2:亮度等级(5级对数划分,排除极端环境差异)
- 比值阈值 15%,实测同石头姿势变化波动 1.6%~9.6%,安全余量充足

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 11:43:57 +08:00

103 lines
3.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <string>
#include <unordered_map>
/**
* @brief WeatherApi类用于封装和风天气API调用
*/
class WeatherApi {
private:
// 和风天气API配置参数
std::string api_host_; // API主机地址
std::string api_key_; // API密钥
std::string default_location_; // 默认城市位置
std::string kid_; // JWT认证ID
std::string project_id_; // 项目ID
std::string private_key_; // 私钥
// 简单缓存管理
std::unordered_map<std::string, std::string> weather_cache_; // 天气数据缓存
std::unordered_map<std::string, std::string> ip_info_cache_; // IP信息缓存
// 天气代码映射表将API返回的天气代码转换为中文描述
static std::unordered_map<std::string, std::string> WEATHER_CODE_MAP;
/**
* @brief 生成JWT令牌预留接口
* @return JWT令牌字符串
*/
std::string GenerateJwtToken();
/**
* @brief 封装HTTP GET请求
* @param url 请求URL
* @param headers 请求头
* @param response 响应内容
* @return 是否请求成功
*/
bool HttpGet(const std::string& url, const std::string& headers, std::string& response);
/**
* @brief 获取城市信息
* @param location 城市名称
* @return 城市详细信息
*/
std::string FetchCityInfo(const std::string& location, const std::string& lang);
/**
* @brief 获取IP位置信息
* @details 自动获取设备当前IP的地理位置信息无需指定IP参数
* @return IP对应的地理位置信息
*/
std::string GetIpInfo();
public:
/**
* @brief 构造函数,初始化配置参数
*/
WeatherApi();
/**
* @brief 获取当前默认城市
*/
std::string GetDefaultLocation() const;
/**
* @brief 析构函数
*/
~WeatherApi() = default;
/**
* @brief 获取天气信息主函数
* @param location 城市名称
* @param lang 语言设置
* @return 格式化的天气信息
*/
std::string GetWeather(const std::string& location, const std::string& lang);
/**
* @brief 自动检测并设置当前位置
* @details 调用太平洋IP查询API自动检测当前地理位置并更新默认城市设置
*/
void AutoDetectLocation();// 自动检测NVS中是否存在当前位置的城市信息并设置当前位置
};
/**
* @brief 全局函数,用于获取天气信息
* @param location 城市名称
* @param lang 语言设置
* @return 格式化的天气信息
*/
extern std::string GetWeatherInfo(const std::string& location, const std::string& lang);
/**
* @brief 全局函数,用于自动检测并设置当前位置
* @details 供应用程序在网络连接后调用,自动检测地理位置并更新默认城市
*/
extern void AutoDetectAndSetLocation();
/**
* @brief 全局WeatherApi实例
*/
extern WeatherApi g_weather_api;