硬件基本完成

This commit is contained in:
2026-05-01 21:34:10 +08:00
parent df5b05e936
commit 5a31ff2179
14 changed files with 1005 additions and 439 deletions
+53 -6
View File
@@ -22,7 +22,7 @@ const char* WIFI_SSID = "yzfh";
const char* WIFI_PASSWORD = "yzfhroot";
const char* HTTP_SERVER_IP = "27.106.113.205"; // 修改为实际服务器IP
const char* HTTP_SERVER_PORT = "8082"; // HTTP端口(8082)
const char* HTTP_API_TOKEN = "change-me-token"; // 必须与服务端 ATTENDANCE_API_TOKEN 一致
const char* HTTP_API_TOKEN = "atk_nKfVWLhMjL0RGSfRll3R_sdJS6IrIjuV"; // 必须与服务端 ATTENDANCE_API_TOKEN 一致
/* --- 班级配置 --- */
// 每个设备对应一个班级,修改此处的班级代码
@@ -84,6 +84,44 @@ uint32_t last_process_time = 0;
#define PROCESS_INTERVAL_MS 2000
/* --- 辅助函数 --- */
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};
if (month < 1 || month > 12) return 31;
if (month == 2 && IsLeapYear(year)) return 29;
return days_table[month - 1];
}
static void AdvanceSoftwareClockOneSecond(void)
{
g_current_time.second++;
if (g_current_time.second < 60) return;
g_current_time.second = 0;
g_current_time.minute++;
if (g_current_time.minute < 60) return;
g_current_time.minute = 0;
g_current_time.hour++;
if (g_current_time.hour < 24) return;
g_current_time.hour = 0;
g_current_time.day++;
if (g_current_time.day <= DaysInMonth(g_current_time.year, g_current_time.month)) return;
g_current_time.day = 1;
g_current_time.month++;
if (g_current_time.month <= 12) return;
g_current_time.month = 1;
g_current_time.year++;
}
void StartBeepSuccess(void)
{
BUZZER_OFF();
@@ -280,6 +318,7 @@ uint8_t SyncNetworkTime(void)
if (ESP8266_GetNetworkTime(&g_current_time) == ESP8266_SUCCESS) {
last_time_sync = Get_Time_ms();
last_rtc_read = last_time_sync;
DEBUG_PRINT("Time synced: %04d-%02d-%02d %02d:%02d:%02d\r\n",
g_current_time.year, g_current_time.month, g_current_time.day,
@@ -464,20 +503,22 @@ void UpdateCurrentTime(void)
rtc_time.hour, rtc_time.minute, rtc_time.second);
}
}
// 如果DS1302不可用,直接使用NTP时间(每秒更新考勤时间)
// 如果DS1302不可用,基于系统tick推进软件时钟
else if (!g_use_ds1302 && current_ms - last_rtc_read >= RTC_READ_INTERVAL_MS) {
last_rtc_read = current_ms;
while (current_ms - last_rtc_read >= RTC_READ_INTERVAL_MS) {
last_rtc_read += RTC_READ_INTERVAL_MS;
AdvanceSoftwareClockOneSecond();
}
// 直接使用全局NTP时间
Attendance_SetCurrentTime(g_current_time.hour, g_current_time.minute);
// 每10秒打印一次当前时间(调试用)
debug_counter++;
if (debug_counter >= 10) {
debug_counter = 0;
DEBUG_PRINT("[Time] NTP: %04d-%02d-%02d %02d:%02d\r\n",
DEBUG_PRINT("[Time] NTP-soft: %04d-%02d-%02d %02d:%02d:%02d\r\n",
g_current_time.year, g_current_time.month, g_current_time.day,
g_current_time.hour, g_current_time.minute);
g_current_time.hour, g_current_time.minute, g_current_time.second);
}
}
@@ -528,6 +569,9 @@ uint8_t TryReconnectWiFi(void)
// 下载时间段配置
DownloadTimeSlots();
// 刷新当前时段的今日记录缓存,避免重连后缓存过旧
Attendance_LoadTodayRecordsFromServer();
return 1;
} else {
DEBUG_ERROR("HTTP server connection failed");
@@ -562,6 +606,9 @@ uint8_t TryReconnectServer(void)
Delay_ms(50);
BUZZER_OFF();
DownloadTimeSlots();
Attendance_LoadTodayRecordsFromServer();
return 1;
}