126 lines
5.0 KiB
C++
126 lines
5.0 KiB
C++
#include "TjdUiAppSleepModel.h"
|
||
#include "rtc_api.h"
|
||
// #include "sql_fit.h"
|
||
#include <cstdio>
|
||
#include <iomanip>
|
||
#include <iostream>
|
||
#include <sstream>
|
||
|
||
namespace TJD {
|
||
|
||
static void transfer_and_reverse(uint16_t sql_week[7], uint16_t ui_week[7], uint8_t wday)
|
||
{
|
||
// 确保 wday 在 0 到 6 的范围内
|
||
if (wday < 0 || wday > 6) {
|
||
printf("Error: wday is out of range (0-6).\n");
|
||
return;
|
||
}
|
||
|
||
memcpy(ui_week, sql_week + 1, sizeof(uint16_t) * 6);
|
||
ui_week[7] = sql_week[0];
|
||
// 将 sql_week 的数据倒序排列到 ui_week 中,使得 ui_week[6] 是今天的数据
|
||
// for (int ui_index = 6; ui_index >= 0; ui_index--) {
|
||
// int sql_index = (wday - (6 - ui_index) + 7) % 7;
|
||
// ui_week[ui_index] = sql_week[sql_index];
|
||
// }
|
||
}
|
||
|
||
TjdUiAppSleepModel &TjdUiAppSleepModel::GetInstance(void)
|
||
{
|
||
static TjdUiAppSleepModel instance;
|
||
return instance;
|
||
}
|
||
|
||
void TjdUiAppSleepModel::LoadSleepData(void)
|
||
{
|
||
sleep_total_data_t sleep_data;
|
||
struct rtc_time localTime;
|
||
uint8_t cur_wday = 0;
|
||
tjd_driver_rtc_get_ops()->get_rtc_time(&localTime);
|
||
if (localTime.tm_hour >= 22 && localTime.tm_hour <= 23) {
|
||
cur_wday = localTime.tm_wday;
|
||
sleep_data = sql_fit_get_total_sleep_daydata(cur_wday);
|
||
} else {
|
||
if (localTime.tm_wday == 0) {
|
||
cur_wday = 6;
|
||
sleep_data = sql_fit_get_total_sleep_daydata(cur_wday);
|
||
} else {
|
||
cur_wday = localTime.tm_wday - 1;
|
||
sleep_data = sql_fit_get_total_sleep_daydata(cur_wday);
|
||
}
|
||
}
|
||
|
||
blinkTime = sleep_data.mins_blink_sleep;
|
||
wakeupTime = sleep_data.mins_wake_sleep;
|
||
deepSleepTime = sleep_data.mins_deep_sleep;
|
||
lightSleepTime = sleep_data.mins_light_sleep;
|
||
totalSleepTime = sleep_data.mins_total;
|
||
|
||
uint16_t tempDeepArray[7] = {0};
|
||
uint16_t tempLightArray[7] = {0};
|
||
uint16_t tempBlinkArray[7] = {0};
|
||
uint16_t tempWakeupArray[7] = {0};
|
||
uint16_t tempTotalArray[7] = {0};
|
||
|
||
for (int i = 0; i < 7; i++) {
|
||
tempDeepArray[i] = sql_fit_get_total_sleep_daydata(i).mins_deep_sleep;
|
||
tempLightArray[i] = sql_fit_get_total_sleep_daydata(i).mins_light_sleep;
|
||
tempWakeupArray[i] = sql_fit_get_total_sleep_daydata(i).mins_wake_sleep;
|
||
tempBlinkArray[i] = sql_fit_get_total_sleep_daydata(i).mins_blink_sleep;
|
||
tempTotalArray[i] = sql_fit_get_total_sleep_daydata(i).mins_total;
|
||
printf("i = %d:tempDeepArray%d tempLightArray%d tempBlinkArray%d tempWakeupArray%d \n", i, tempDeepArray[i],
|
||
tempLightArray[i], tempBlinkArray[i], tempWakeupArray[i]);
|
||
}
|
||
|
||
// uint16_t tempDeepArray[7] = {120, 90, 150, 0, 80, 130, 60}; // 每天深睡时间(分钟)
|
||
// uint16_t tempLightArray[7] = {90, 120, 60, 0, 100, 70, 90}; // 每天浅睡时间
|
||
// uint16_t tempBlinkArray[7] = {30, 15, 45, 0, 20, 10, 30}; // 每天眨眼时间
|
||
// uint16_t tempWakeupArray[7] = {60, 75, 45, 0, 100, 90, 120}; // 每天清醒时间
|
||
// uint16_t tempTotalArray[7] = {300, 300, 300, 0, 300, 300, 300}; // 总睡眠时间(分钟)
|
||
|
||
for (int i = 0; i < 7; i++) {
|
||
tempWakeupArray[i] += tempBlinkArray[i];
|
||
tempLightArray[i] += tempWakeupArray[i];
|
||
tempDeepArray[i] += tempLightArray[i];
|
||
printf("line = 85 ;i = %d:tempDeepArray%d tempLightArray%d tempBlinkArray%d tempWakeupArray%d \n", i, tempDeepArray[i],
|
||
tempLightArray[i], tempBlinkArray[i], tempWakeupArray[i]);
|
||
}
|
||
|
||
// transfer_and_reverse(tempDeepArray, deepSleepTimeArray, cur_wday);
|
||
// transfer_and_reverse(tempLightArray, lightSleepTimeArray, cur_wday);
|
||
// transfer_and_reverse(tempBlinkArray, blinkSleepTimeArray, cur_wday);
|
||
// transfer_and_reverse(tempWakeupArray, wakeupSleepTimeArray, cur_wday);
|
||
memcpy(deepSleepTimeArray, tempDeepArray, sizeof(tempDeepArray));
|
||
memcpy(lightSleepTimeArray, tempLightArray, sizeof(tempLightArray));
|
||
memcpy(blinkSleepTimeArray, tempBlinkArray, sizeof(tempBlinkArray));
|
||
memcpy(wakeupSleepTimeArray, tempWakeupArray, sizeof(tempWakeupArray));
|
||
memcpy(totalSleepTimeArray, tempTotalArray, sizeof(tempTotalArray));
|
||
}
|
||
|
||
std::string TjdUiAppSleepModel::GenerateTimeString(int minutes)
|
||
{
|
||
int hours = minutes / 60;
|
||
int remainingMinutes = minutes % 60;
|
||
|
||
// 使用std::setw和std::setfill来格式化小时和分钟为两位数字
|
||
std::ostringstream oss;
|
||
oss << std::setfill('0') << std::setw(2) << hours << " H " << std::setfill('0') << std::setw(2) << remainingMinutes
|
||
<< " min";
|
||
|
||
// 如果小时和分钟都为0,则替换为"-- H -- min"
|
||
if (hours == 0 && remainingMinutes == 0) {
|
||
return "-- H -- min";
|
||
}
|
||
|
||
// 否则,返回格式化后的字符串(但注意,这里不需要额外添加"--",因为已经格式化了)
|
||
return oss.str();
|
||
}
|
||
|
||
uint8_t TjdUiAppSleepModel::GetWeekDay(void)
|
||
{
|
||
struct rtc_time localTime;
|
||
tjd_driver_rtc_get_ops()->get_rtc_time(&localTime);
|
||
return localTime.tm_wday - 1;
|
||
}
|
||
|
||
} // namespace TJD
|