This commit is contained in:
2026-05-21 08:07:16 +08:00
parent 90908cf018
commit 84d8874a38
32 changed files with 271 additions and 27 deletions
+15
View File
@@ -25,11 +25,13 @@ extern const char* HTTP_API_TOKEN;
extern uint8_t g_server_connected;
/* --- 私有辅助函数 --- */
/* 函数作用:向 ESP8266 串口发送一条 AT 指令并追加回车换行。 */
static void ESP8266_SendCmd(const char* cmd) {
Serial_SendString(cmd);
Serial_SendString("\r\n");
}
/* 函数作用:在指定超时时间内等待 ESP8266 返回期望关键字。 */
static uint8_t ESP8266_WaitForAck(const char* ack, uint32_t timeout) {
uint32_t time_start = Get_Time_ms();
uint32_t time_elapsed;
@@ -57,11 +59,13 @@ static uint8_t ESP8266_WaitForAck(const char* ack, uint32_t timeout) {
return ESP8266_FAIL;
}
/* 函数作用:判断指定年份是否为闰年。 */
static uint8_t IsLeapYear(uint16_t year)
{
return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 1 : 0;
}
/* 函数作用:返回指定年份月份的天数。 */
static uint8_t DaysInMonth(uint16_t year, uint8_t month)
{
static const uint8_t days_table[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
@@ -69,6 +73,7 @@ static uint8_t DaysInMonth(uint16_t year, uint8_t month)
return days_table[month - 1];
}
/* 函数作用:把 Unix 时间戳转换为北京时间结构体。 */
static void EpochToDateTimeUTC8(uint32_t epoch_seconds, ESP8266_DateTime* datetime)
{
// 转为北京时间(UTC+8)
@@ -99,6 +104,7 @@ static void EpochToDateTimeUTC8(uint32_t epoch_seconds, ESP8266_DateTime* dateti
datetime->day = (uint8_t)(days + 1U);
}
/* 函数作用:从 HTTP 响应中复制正文,并在响应加密时完成解密。 */
static uint8_t ESP8266_CopyDecryptedBody(char* response,
char* body_start,
uint16_t body_len,
@@ -137,6 +143,7 @@ static uint8_t ESP8266_CopyDecryptedBody(char* response,
/* --- 公共接口函数实现 --- */
/* 函数作用:初始化 ESP8266 AT 模式、WiFi 模式和 TCP 单连接模式。 */
uint8_t ESP8266_Setup(void)
{
ESP8266_SendCmd("+++");
@@ -159,6 +166,7 @@ uint8_t ESP8266_Setup(void)
return ESP8266_SUCCESS;
}
/* 函数作用:连接指定 SSID 和密码的 WiFi 热点。 */
uint8_t ESP8266_ConnectWiFi(const char* ssid, const char* password)
{
char cmd_buffer[128];
@@ -172,6 +180,7 @@ uint8_t ESP8266_ConnectWiFi(const char* ssid, const char* password)
/**
* @brief 连接到HTTP服务器(新增)
*/
/* 函数作用:与服务端建立 TCP 连接,为后续 HTTP 请求做准备。 */
uint8_t ESP8266_ConnectHTTPServer(const char* ip, const char* port)
{
char cmd_buffer[100];
@@ -215,6 +224,7 @@ uint8_t ESP8266_ConnectHTTPServer(const char* ip, const char* port)
/**
* @brief 发送加密数据(HTTP POST请求)
*/
/* 函数作用:通过 HTTP POST 上传 AES 加密后的考勤 JSON 数据。 */
uint8_t ESP8266_SendEncryptedData(const char* encrypted_data)
{
char http_request[1024];
@@ -353,6 +363,7 @@ uint8_t ESP8266_SendEncryptedData(const char* encrypted_data)
return ESP8266_FAIL;
}
/* 函数作用:从服务端时间接口获取 Unix 时间戳并转换为本地时间。 */
uint8_t ESP8266_GetNetworkTime(ESP8266_DateTime* datetime)
{
char response_buffer[256];
@@ -405,6 +416,7 @@ uint8_t ESP8266_GetNetworkTime(ESP8266_DateTime* datetime)
return ESP8266_SUCCESS;
}
/* 函数作用:查询 ESP8266 当前 TCP/WiFi 连接状态。 */
uint8_t ESP8266_CheckConnection(void)
{
char* response;
@@ -442,6 +454,7 @@ uint8_t ESP8266_CheckConnection(void)
/**
* @brief 发送HTTP GET请求并接收响应
*/
/* 函数作用:发送带 Token 的 HTTP GET 请求,并返回解密后的响应正文。 */
uint8_t ESP8266_HttpGet(const char* path, char* response_buffer, uint16_t buffer_size)
{
char http_request[320];
@@ -592,6 +605,7 @@ uint8_t ESP8266_HttpGet(const char* path, char* response_buffer, uint16_t buffer
* @param slot_end 时段结束时间(格式"HH:MM")
* @retval 1 表示已签到(重复), 0 表示未签到
*/
/* 函数作用:向服务器查询指定学生在给定时段内是否重复签到。 */
uint8_t ESP8266_CheckDuplicate(const char* student_id, const char* slot_start, const char* slot_end)
{
char path[128];
@@ -639,6 +653,7 @@ uint8_t ESP8266_CheckDuplicate(const char* student_id, const char* slot_start, c
* @param buffer_size 缓冲区大小
* @retval 成功下载的学号数量
*/
/* 函数作用:下载指定时间段内今日已签到学生列表,用于本地查重缓存。 */
uint8_t ESP8266_DownloadTodayRecords(const char* slot_start, const char* slot_end, char* student_ids, uint16_t buffer_size)
{
char path[128];