From 753302a1db84367fbcad3579553ee9956057b3eb Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Fri, 4 Oct 2024 13:50:40 +0800 Subject: [PATCH] =?utf8?q?=E7=9D=A1=E7=9C=A0=EF=BC=9A=E5=A2=9E=E5=8A=A0?= =?utf8?q?=E5=B0=8F=E7=9D=A1=E6=95=B0=E6=8D=AE=E7=9A=84=E7=9B=B8=E5=85=B3?= =?utf8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/sleep/include/SleepModel.h | 15 +- .../nativeui/sleep/src/SleepModel.cpp | 1790 ++++++++--------- 2 files changed, 877 insertions(+), 928 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h b/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h index a0114443d..2f06b25c4 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h @@ -39,7 +39,7 @@ namespace OHOS { -#pragma pack(1) +#pragma pack(push, 1) typedef struct { uint32_t fall_asleep_timestamp; // 入睡 @@ -67,7 +67,7 @@ namespace OHOS uint16_t total_wake_time; // 动眼 } SLEEP_T; -#pragma pack() +#pragma pack (pop) bool save_nap_data(NAP_T *data); bool sleep_mutex_init(void); @@ -115,6 +115,13 @@ namespace OHOS std::string get_sleep_notifiy(); // 获取睡眠时间 std::string get_wake_notifiy(); // 获取起床时间 + //小睡数据 + int get_nap_hour(); + int get_nap_min(); + std::string get_napsleep_str(); // 解析入睡时间 + std::string get_napwake_str(); // 解析醒来时间 + + void set_sleep_state(SLEEPSTATE state); SLEEPSTATE get_sleep_state(); SleepDataUI *ReadSleepData(); // 读取睡眠数据 @@ -147,6 +154,8 @@ namespace OHOS std::string sleepStr{"--"}; std::string wakeStr{"--"}; std::string goalStr{"--"}; + std::string napsleepStr{"--"}; + std::string napwakeStr{"--"}; std::string goalnotifyStr{"--"}; std::string goalsleepStr{"--"}; @@ -156,6 +165,8 @@ namespace OHOS int sleep_total_time = 0; int goal_total_time = 0; + int nap_total_time = 0; + SLEEPSTATE sleepstate_ = SLEEPSTATE::WAKE; int CradId = 0; // 主界面状态 diff --git a/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp index ee5ee5fdd..da801dbc2 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp @@ -3,1019 +3,957 @@ * @Date: SLEPPNOTIFY24-06-26 19:36:48 * @LastEditors: penglei 1624952103@qq.com * @LastEditTime: 2024-10-03 18:18:41 - * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp + * @FilePath: + * /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp * @Description: * * Copyright (c) SLEPPNOTIFY24 by ${git_name_email}, All Rights Reserved. */ #include "sleep/include/SleepModel.h" +#include "ConfigModel.h" +#include "UiConfig.h" +#include "custom_utils.h" +#include "heart_blood/health_service/health_service.h" #include "kv_store.h" +#include "samgr_lite.h" #include "securec.h" #include "sleep/include/SleepControlPresenter.h" -#include "custom_utils.h" -#include -#include "UiConfig.h" #include "ui_resource_image.h" -#include "samgr_lite.h" -#include -#include -#include +#include "wearable_log.h" +#include #include +#include #include -#include -#include "wearable_log.h" -#include "ConfigModel.h" -#include "heart_blood/health_service/health_service.h" -#include "custom_utils.h" -#include +#include #include +#include #include -#include +#include +#include +#include +#include "ConfigModelC.h" +#include "MainModel.h" #include "broadcast_feature.h" #include "broadcast_service.h" -#include -#include -#include "thread_adapter.h" #include "cmsis_os.h" -#include "ui_service.h" -#include "ConfigModelC.h" -#include "power_display_service.h" -#include "MainModel.h" -#include "cywee_service.h" #include "custom_runtime.h" +#include "cywee_service.h" +#include "power_display_service.h" #include "sleep/include/SleepModelC.h" +#include "thread_adapter.h" +#include "ui_service.h" +#include +#include -extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char date_separator, char time_separator, date_format_t format); +extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, + char date_separator, char time_separator, + date_format_t format); extern "C" int get_sleep_time_from_config_model(void); extern "C" int get_wake_up_time_from_config_model(); -namespace OHOS -{ +namespace OHOS { #define SLEPPNOTIFY 20 - static NAP_T nap_data = {0}; - static SLEEP_T sleep_data = {0}; - static SLEEP_STATE_T sleep_status = {0}; - static uint32_t sleep_update_timestamp = 0; - static uint32_t nap_update_timestamp = 0; +static NAP_T nap_data = {0}; +static SLEEP_T sleep_data = {0}; +static SLEEP_STATE_T sleep_status = {0}; +static uint32_t sleep_update_timestamp = 0; +static uint32_t nap_update_timestamp = 0; - static uint8_t sleep_mutex_installed = 0; - static osal_mutex sleep_mutex; +static uint8_t sleep_mutex_installed = 0; +static osal_mutex sleep_mutex; - bool sleep_mutex_init(void) - { - if(sleep_mutex_installed == 1) - return true; - - if(sleep_mutex_installed == 0) - { - errcode_t ret = osal_mutex_init(&sleep_mutex); - if (ret != ERRCODE_SUCC) - { - PRINT("sleep_mutex lock fail! ret=0x%x\n", ret); - return false; - } - sleep_mutex_installed = 1; - } - return true; +bool sleep_mutex_init(void) { + if (sleep_mutex_installed == 1) + return true; + + if (sleep_mutex_installed == 0) { + errcode_t ret = osal_mutex_init(&sleep_mutex); + if (ret != ERRCODE_SUCC) { + PRINT("sleep_mutex lock fail! ret=0x%x\n", ret); + return false; } + sleep_mutex_installed = 1; + } + return true; +} - /*获取最新睡眠时间戳*/ - uint32_t get_sleep_update_timestamp(void) - { - if(nap_update_timestamp<=sleep_update_timestamp) - return sleep_update_timestamp; - else - return nap_update_timestamp; +/*获取最新睡眠时间戳*/ +uint32_t get_sleep_update_timestamp(void) { + if (nap_update_timestamp <= sleep_update_timestamp) + return sleep_update_timestamp; + else + return nap_update_timestamp; +} + +bool save_nap_data(NAP_T *data) { + struct stat info; + char dir_path[64] = {0}; + char file_path[64] = {0}; + char day[12] = {0}; + uapi_get_YYMMDD_Time(day, '\0'); + + sprintf_s(dir_path, 64, SLEEP_DIR_PATH, day); + if (stat("user/userdata/sleep", &info) != 0) { + if (mkdir("user/userdata/sleep", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != + 0) { + return false; // Failed to create folder } + } + if (stat(dir_path, &info) != 0) { + if (mkdir(dir_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "mkdir fail %s", dir_path); + return false; // Failed to create folder + } + } + uint32_t creationTime; + uapi_get_timestampe_ss(&creationTime); - bool save_nap_data(NAP_T *data) - { - struct stat info; - char dir_path[64] = {0}; - char file_path[64] = {0}; - char day[12] = {0}; - uapi_get_YYMMDD_Time(day, '\0'); + sprintf_s(file_path, 64, NAP_PATH, day, creationTime); - sprintf_s(dir_path, 64, SLEEP_DIR_PATH, day); - if (stat("user/userdata/sleep", &info) != 0) - { - if (mkdir("user/userdata/sleep", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) - { - return false; // Failed to create folder - } - } + FILE *fp; + fp = fopen(file_path, "wb+"); + if (fp == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data fopen fail"); + return false; + } - if (stat(dir_path, &info) != 0) - { - if (mkdir(dir_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "mkdir fail %s", dir_path); - return false; // Failed to create folder - } - } - uint32_t creationTime; - uapi_get_timestampe_ss(&creationTime); + uint8_t sleep_type = 0; + fwrite(&sleep_type, 1, 1, fp); - sprintf_s(file_path, 64, NAP_PATH, day, creationTime); + uint32_t fall_asleep_timestamp = data->fall_asleep_timestamp; + fwrite(&fall_asleep_timestamp, 1, 4, fp); - FILE *fp; - fp = fopen(file_path, "wb+"); - if (fp == NULL) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data fopen fail"); - return false; - } + uint32_t wake_up_timestamp = 0; - uint8_t sleep_type = 0; - fwrite(&sleep_type, 1, 1, fp); + if (data->wake_up_timestamp == 0) + uapi_get_timestampe_ss(&wake_up_timestamp); + else + wake_up_timestamp = data->wake_up_timestamp; - uint32_t fall_asleep_timestamp = data->fall_asleep_timestamp; - fwrite(&fall_asleep_timestamp, 1, 4, fp); + nap_update_timestamp = wake_up_timestamp; + fwrite(&wake_up_timestamp, 1, 4, fp); - uint32_t wake_up_timestamp = 0; + uint16_t total_time = data->total_time; - if(data->wake_up_timestamp == 0) - uapi_get_timestampe_ss(&wake_up_timestamp); - else - wake_up_timestamp = data->wake_up_timestamp; + uint8_t total_time_hour = (total_time / 60); + fwrite(&total_time_hour, 1, 1, fp); - nap_update_timestamp = wake_up_timestamp; + uint8_t total_time_minute = (total_time % 60); + fwrite(&total_time_minute, 1, 1, fp); - fwrite(&wake_up_timestamp, 1, 4, fp); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data succ"); - uint16_t total_time = data->total_time; + fclose(fp); - uint8_t total_time_hour = (total_time / 60); - fwrite(&total_time_hour, 1, 1, fp); + return true; +} - uint8_t total_time_minute = (total_time % 60); - fwrite(&total_time_minute, 1, 1, fp); +void set_nap_data(NAP_T *data) { + memset(&nap_data, 0, sizeof(NAP_T)); + memcpy_s(&nap_data, sizeof(NAP_T), data, sizeof(NAP_T)); +} - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data succ"); +uint32_t read_sleep_nap_data(char *data, uint32_t size, FILE *fp) { + return fread(data, 1, size, fp); +} - fclose(fp); +static uint16_t total_time = 0; +static uint16_t total_deep_sleep_time = 0; +static uint16_t total_mild_sleep_time = 0; +static uint16_t total_rem_sleep_time = 0; +static uint16_t total_wake_time = 0; +; +static char time_file_path[64] = {0}; +static char status_file_path[64] = {0}; + +static uint32_t fall_asleep_timestamp = 0; +static uint32_t history_wake_up_timestamp = 0; +static char day[12] = {0}; +static char dir_path[64] = {0}; +void clean_sleep_data(void) { + total_time = 0; + total_deep_sleep_time = 0; + total_mild_sleep_time = 0; + total_rem_sleep_time = 0; + total_wake_time = 0; + memset(time_file_path, 0, sizeof(time_file_path)); + memset(status_file_path, 0, sizeof(status_file_path)); + memset(day, 0, sizeof(day)); + memset(dir_path, 0, sizeof(dir_path)); + fall_asleep_timestamp = 0; +} - return true; - } +// void clean_sleep_file(void) +// { +// uint32_t sleep_time = get_sleep_time_from_config_model(); +// uint32_t wake_up_time = get_wake_up_time_from_config_model(); +// uint32_t sleep_min = ((sleep_time & 0xFFFF0000) >> 16) * 60 + sleep_time +// & 0x0000FFFF; uint32_t wake_min = ((wake_up_time & 0xFFFF0000) >> 16) * +// 60 + wake_up_time & 0x0000FFFF; if(sleep_min < wake_min) +// { +// uapi_delete_files(dir_path); +// } +// } + +bool create_sleep_folder(void) { + struct stat info; + + uint32_t sleep_time = get_sleep_time_from_config_model(); + uint32_t wake_up_time = get_wake_up_time_from_config_model(); + uint32_t sleep_min = + ((sleep_time & 0xFFFF0000) >> 16) * 60 + sleep_time & 0x0000FFFF; + uint32_t wake_min = + ((wake_up_time & 0xFFFF0000) >> 16) * 60 + wake_up_time & 0x0000FFFF; + uint32_t current_min = 0; + + if (sleep_min > wake_min) { + time_t rawtime; + struct tm *timeinfo; + + time(&rawtime); + timeinfo = localtime(&rawtime); + + current_min = timeinfo->tm_hour * 60 + timeinfo->tm_min; + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "sleep_min : %d ,wake_min : %d ,current_min : %d ", + sleep_min, wake_min, current_min); + + if (current_min >= sleep_min && current_min < 24 * 60) + uapi_get_YYMMDD_Time_extend(day, '\0', -1); + else if (current_min >= 0 && current_min < wake_min) + uapi_get_YYMMDD_Time_extend(day, '\0', 0); + } else + uapi_get_YYMMDD_Time_extend(day, '\0', 0); + + sprintf_s(dir_path, 64, SLEEP_DIR_PATH, day); + if (stat("user/userdata/sleep", &info) != 0) { + if (mkdir("user/userdata/sleep", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != + 0) { + return false; // Failed to create folder + } + } + + if (stat(dir_path, &info) != 0) { + if (mkdir(dir_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "mkdir fail %s", dir_path); + return false; // Failed to create folder + } + } + + return true; +} - void set_nap_data(NAP_T *data) - { - memset(&nap_data, 0, sizeof(NAP_T)); - memcpy_s(&nap_data, sizeof(NAP_T), data, sizeof(NAP_T)); - } +bool save_sleep_data(SLEEP_T *data) { + // errcode_t ret; + // ret = osal_mutex_lock(&sleep_mutex); + // if (ret != ERRCODE_SUCC) + // { + // PRINT(" sleep_mutex lock fail! ret=0x%x\n", ret); + // return false; + // } - uint32_t read_sleep_nap_data(char *data, uint32_t size, FILE *fp) - { - return fread(data, 1, size, fp); - } + if (access(time_file_path, F_OK) == -1) { + memset(time_file_path, 0, sizeof(time_file_path)); + sprintf_s(time_file_path, 64, SLEEP_TIME_PATH, day); + } - static uint16_t total_time = 0; - static uint16_t total_deep_sleep_time = 0; - static uint16_t total_mild_sleep_time = 0; - static uint16_t total_rem_sleep_time = 0; - static uint16_t total_wake_time = 0;; - static char time_file_path[64] = {0}; - static char status_file_path[64] = {0}; - - static uint32_t fall_asleep_timestamp = 0; - static uint32_t history_wake_up_timestamp = 0; - static char day[12] = {0}; - static char dir_path[64] = {0}; - void clean_sleep_data(void) - { - total_time = 0; - total_deep_sleep_time = 0; - total_mild_sleep_time = 0; - total_rem_sleep_time = 0; - total_wake_time = 0; - memset(time_file_path,0,sizeof(time_file_path)); - memset(status_file_path,0,sizeof(status_file_path)); - memset(day,0,sizeof(day)); - memset(dir_path,0,sizeof(dir_path)); - fall_asleep_timestamp = 0; - } + FILE *fp; + fp = fopen(time_file_path, "wb+"); + if (fp == NULL) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "save_sleep_data fopen fail"); + return false; + } - // void clean_sleep_file(void) - // { - // uint32_t sleep_time = get_sleep_time_from_config_model(); - // uint32_t wake_up_time = get_wake_up_time_from_config_model(); - // uint32_t sleep_min = ((sleep_time & 0xFFFF0000) >> 16) * 60 + sleep_time & 0x0000FFFF; - // uint32_t wake_min = ((wake_up_time & 0xFFFF0000) >> 16) * 60 + wake_up_time & 0x0000FFFF; - // if(sleep_min < wake_min) - // { - // uapi_delete_files(dir_path); - // } - // } + uint8_t sleep_type = 1; + fwrite(&sleep_type, 1, 1, fp); - bool create_sleep_folder(void) - { - struct stat info; - - uint32_t sleep_time = get_sleep_time_from_config_model(); - uint32_t wake_up_time = get_wake_up_time_from_config_model(); - uint32_t sleep_min = ((sleep_time & 0xFFFF0000) >> 16) * 60 + sleep_time & 0x0000FFFF; - uint32_t wake_min = ((wake_up_time & 0xFFFF0000) >> 16) * 60 + wake_up_time & 0x0000FFFF; - uint32_t current_min = 0; - - if(sleep_min > wake_min) - { - time_t rawtime; - struct tm* timeinfo; - - time(&rawtime); - timeinfo = localtime(&rawtime); - - current_min = timeinfo->tm_hour * 60 + timeinfo->tm_min; - - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sleep_min : %d ,wake_min : %d ,current_min : %d ", sleep_min,wake_min,current_min); - - if(current_min >= sleep_min && current_min < 24 * 60) - uapi_get_YYMMDD_Time_extend(day, '\0' , -1); - else if(current_min >= 0 && current_min < wake_min) - uapi_get_YYMMDD_Time_extend(day, '\0' , 0); - } - else - uapi_get_YYMMDD_Time_extend(day, '\0' , 0); - - sprintf_s(dir_path, 64, SLEEP_DIR_PATH, day); - if (stat("user/userdata/sleep", &info) != 0) - { - if (mkdir("user/userdata/sleep", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) - { - return false; // Failed to create folder - } - } - - if(stat(dir_path, &info) != 0) - { - if (mkdir(dir_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "mkdir fail %s", dir_path); - return false; // Failed to create folder - } - } - - return true; - } + if (fall_asleep_timestamp == 0) { + fall_asleep_timestamp = data->fall_asleep_timestamp; + } + fwrite(&fall_asleep_timestamp, 1, 4, fp); - bool save_sleep_data(SLEEP_T *data) - { - // errcode_t ret; - // ret = osal_mutex_lock(&sleep_mutex); - // if (ret != ERRCODE_SUCC) - // { - // PRINT(" sleep_mutex lock fail! ret=0x%x\n", ret); - // return false; - // } - - if(access(time_file_path, F_OK) == -1) - { - memset(time_file_path,0,sizeof(time_file_path)); - sprintf_s(time_file_path, 64, SLEEP_TIME_PATH, day); - } - - FILE *fp; - fp = fopen(time_file_path, "wb+"); - if (fp == NULL) - { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "save_sleep_data fopen fail"); - return false; - } - - uint8_t sleep_type = 1; - fwrite(&sleep_type, 1, 1, fp); - - if(fall_asleep_timestamp == 0) - { - fall_asleep_timestamp = data->fall_asleep_timestamp; - } - fwrite(&fall_asleep_timestamp, 1, 4, fp); - - uint32_t wake_up_timestamp = 0; - - if(data->wake_up_timestamp == 0) - uapi_get_timestampe_ss(&wake_up_timestamp); - else - wake_up_timestamp = data->wake_up_timestamp; - - sleep_update_timestamp = wake_up_timestamp; - - fwrite(&wake_up_timestamp, 1, 4, fp); - - total_time = (wake_up_timestamp - fall_asleep_timestamp) / 60; - - uint8_t total_time_hour = (total_time / 60); - fwrite(&total_time_hour, 1, 1, fp); - - uint8_t total_time_minute = (total_time % 60); - fwrite(&total_time_minute, 1, 1, fp); - - total_deep_sleep_time += data->total_deep_sleep_time; - - total_time_hour = (total_deep_sleep_time / 60); - fwrite(&total_time_hour, 1, 1, fp); - - total_time_minute = (total_deep_sleep_time % 60); - fwrite(&total_time_minute, 1, 1, fp); - - total_mild_sleep_time += data->total_mild_sleep_time; - total_time_hour = (total_mild_sleep_time / 60); - fwrite(&total_time_hour, 1, 1, fp); - - total_time_minute = (total_mild_sleep_time % 60); - fwrite(&total_time_minute, 1, 1, fp); - - total_rem_sleep_time += data->total_rem_sleep_time; - total_time_hour = (total_rem_sleep_time / 60); - fwrite(&total_time_hour, 1, 1, fp); - - total_time_minute = (total_rem_sleep_time % 60); - fwrite(&total_time_minute, 1, 1, fp); - - total_wake_time = total_time - total_rem_sleep_time - total_mild_sleep_time - total_deep_sleep_time; - - total_time_hour = (total_wake_time / 60); - fwrite(&total_time_hour, 1, 1, fp); - - total_time_minute = (total_wake_time % 60); - fwrite(&total_time_minute, 1, 1, fp); - - fclose(fp); - - if(data->wake_up_timestamp == 0) // 临时数据清掉 - { - total_deep_sleep_time -= data->total_deep_sleep_time; - total_mild_sleep_time -= data->total_mild_sleep_time; - total_rem_sleep_time -= data->total_rem_sleep_time; - } - - if(access(status_file_path, F_OK) == -1) //文件不存在 直接写入 - { - memset(status_file_path,0,sizeof(status_file_path)); - sprintf_s(status_file_path, 64, SLEEP_STATUS_PATH, day); - fp = fopen(status_file_path, "wb+"); - if(fp == NULL) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_sleep_data fopen fail"); - // osal_mutex_unlock(&sleep_mutex); - return false; - } - // if(wake_up_timestamp != 0) - // { - // history_wake_up_timestamp = wake_up_timestamp; //更新历史出睡时间戳 - // } - fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp); - } - else //文件已存在 需要判断是否需要补上清醒状态 - { - fp = fopen(status_file_path, "rb+"); - if(fp == NULL) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_sleep_data fopen fail"); - // osal_mutex_unlock(&sleep_mutex); - return false; - } - //fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp); - - //如果本次出睡时间与历史出睡时间不等 且两次睡眠间隔小于2小时 补上清醒状态 - - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "history_wake_up_timestamp : %d wake_up_timestamp : %d\n",history_wake_up_timestamp,wake_up_timestamp); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "data->fall_asleep_timestamp - history_wake_up_timestamp : %d \n",data->fall_asleep_timestamp - history_wake_up_timestamp); - if((history_wake_up_timestamp > 0) && (history_wake_up_timestamp != wake_up_timestamp)) - { - fseek(fp, -sizeof(SLEEP_STATE_T), SEEK_END); - SLEEP_STATE_T sleep_status = {0,0}; - fread(&sleep_status, sizeof(SLEEP_STATE_T), 1, fp); - if(sleep_status.stat == 4) //如果存在出睡睡觉 替换为清醒 - { - sleep_status.stat = 3; //清醒状态 - sleep_status.timestamp = history_wake_up_timestamp; //清醒状态时间为上一次的出睡时间 - fseek(fp, -sizeof(SLEEP_STATE_T), SEEK_END); - fwrite(&sleep_status, sizeof(SLEEP_STATE_T), 1, fp); //补上清醒状态 - // fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp); //写入本次睡眠状态 - fwrite(&data->sleep_status[1], sizeof(SLEEP_STATE_T), data->status_num - 1, fp); //跳过入睡状态写入文件 - } - else //不满足补清醒状态条件 直接写入本次睡眠状态 - { - fseek(fp, 0, SEEK_END); - fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp); - } - } - else //不满足补清醒状态条件 直接写入本次睡眠状态 - { - fseek(fp, 0, SEEK_END); - fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp); - } - } - - if(wake_up_timestamp) - { - history_wake_up_timestamp = wake_up_timestamp; //更新历史出睡时间戳 - } - - fclose(fp); - - // osal_mutex_unlock(&sleep_mutex); - - return true; - } + uint32_t wake_up_timestamp = 0; - bool is_regular_file(const std::string &file_name) - { - struct stat file_stat; - if (stat(file_name.c_str(), &file_stat) != 0) - { - return false; - } + if (data->wake_up_timestamp == 0) + uapi_get_timestampe_ss(&wake_up_timestamp); + else + wake_up_timestamp = data->wake_up_timestamp; - return S_ISREG(file_stat.st_mode); // 检查文件是否为普通文件 + sleep_update_timestamp = wake_up_timestamp; + + fwrite(&wake_up_timestamp, 1, 4, fp); + + total_time = (wake_up_timestamp - fall_asleep_timestamp) / 60; + + uint8_t total_time_hour = (total_time / 60); + fwrite(&total_time_hour, 1, 1, fp); + + uint8_t total_time_minute = (total_time % 60); + fwrite(&total_time_minute, 1, 1, fp); + + total_deep_sleep_time += data->total_deep_sleep_time; + + total_time_hour = (total_deep_sleep_time / 60); + fwrite(&total_time_hour, 1, 1, fp); + + total_time_minute = (total_deep_sleep_time % 60); + fwrite(&total_time_minute, 1, 1, fp); + + total_mild_sleep_time += data->total_mild_sleep_time; + total_time_hour = (total_mild_sleep_time / 60); + fwrite(&total_time_hour, 1, 1, fp); + + total_time_minute = (total_mild_sleep_time % 60); + fwrite(&total_time_minute, 1, 1, fp); + + total_rem_sleep_time += data->total_rem_sleep_time; + total_time_hour = (total_rem_sleep_time / 60); + fwrite(&total_time_hour, 1, 1, fp); + + total_time_minute = (total_rem_sleep_time % 60); + fwrite(&total_time_minute, 1, 1, fp); + + total_wake_time = total_time - total_rem_sleep_time - total_mild_sleep_time - + total_deep_sleep_time; + + total_time_hour = (total_wake_time / 60); + fwrite(&total_time_hour, 1, 1, fp); + + total_time_minute = (total_wake_time % 60); + fwrite(&total_time_minute, 1, 1, fp); + + fclose(fp); + + if (data->wake_up_timestamp == 0) // 临时数据清掉 + { + total_deep_sleep_time -= data->total_deep_sleep_time; + total_mild_sleep_time -= data->total_mild_sleep_time; + total_rem_sleep_time -= data->total_rem_sleep_time; + } + + if (access(status_file_path, F_OK) == -1) //文件不存在 直接写入 + { + memset(status_file_path, 0, sizeof(status_file_path)); + sprintf_s(status_file_path, 64, SLEEP_STATUS_PATH, day); + fp = fopen(status_file_path, "wb+"); + if (fp == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_sleep_data fopen fail"); + // osal_mutex_unlock(&sleep_mutex); + return false; } + // if(wake_up_timestamp != 0) + // { + // history_wake_up_timestamp = wake_up_timestamp; //更新历史出睡时间戳 + // } + fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp); + } else //文件已存在 需要判断是否需要补上清醒状态 + { + fp = fopen(status_file_path, "rb+"); + if (fp == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_sleep_data fopen fail"); + // osal_mutex_unlock(&sleep_mutex); + return false; + } + // fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp); + + //如果本次出睡时间与历史出睡时间不等 且两次睡眠间隔小于2小时 补上清醒状态 + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "history_wake_up_timestamp : %d wake_up_timestamp : %d\n", + history_wake_up_timestamp, wake_up_timestamp); + WEARABLE_LOGD( + WEARABLE_LOG_MODULE_APP, + "data->fall_asleep_timestamp - history_wake_up_timestamp : %d \n", + data->fall_asleep_timestamp - history_wake_up_timestamp); + if ((history_wake_up_timestamp > 0) && + (history_wake_up_timestamp != wake_up_timestamp)) { + fseek(fp, -sizeof(SLEEP_STATE_T), SEEK_END); + SLEEP_STATE_T sleep_status = {0, 0}; + fread(&sleep_status, sizeof(SLEEP_STATE_T), 1, fp); + if (sleep_status.stat == 4) //如果存在出睡睡觉 替换为清醒 + { + sleep_status.stat = 3; //清醒状态 + sleep_status.timestamp = + history_wake_up_timestamp; //清醒状态时间为上一次的出睡时间 + fseek(fp, -sizeof(SLEEP_STATE_T), SEEK_END); + fwrite(&sleep_status, sizeof(SLEEP_STATE_T), 1, fp); //补上清醒状态 + // fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, + // fp); //写入本次睡眠状态 + fwrite(&data->sleep_status[1], sizeof(SLEEP_STATE_T), + data->status_num - 1, fp); //跳过入睡状态写入文件 + } else //不满足补清醒状态条件 直接写入本次睡眠状态 + { + fseek(fp, 0, SEEK_END); + fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp); + } + } else //不满足补清醒状态条件 直接写入本次睡眠状态 + { + fseek(fp, 0, SEEK_END); + fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp); + } + } + + if (wake_up_timestamp) { + history_wake_up_timestamp = wake_up_timestamp; //更新历史出睡时间戳 + } + + fclose(fp); + + // osal_mutex_unlock(&sleep_mutex); + + return true; +} - bool read_sleep_data(SLEEP_T *data) - { - // errcode_t ret; - - // ret = osal_mutex_lock(&sleep_mutex); - - // if (ret != ERRCODE_SUCC) - // { - // PRINT(" sleep_mutex lock fail! ret=0x%x\n", ret); - // return false; - // } - - char day[12] = {0}; - uapi_get_YYMMDD_Time(day, '\0'); - - char file_path[64] = {0}; - sprintf_s(file_path, 64, SLEEP_TIME_PATH, day); - - FILE *fp = fopen(file_path, "rb"); - if (fp == NULL) - { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "read_sleep_data fopen fail"); - osal_mutex_unlock(&sleep_mutex); - return false; - } - - // 读取睡眠类型 - uint8_t sleep_type; - fread(&sleep_type, 1, 1, fp); - - if (sleep_type == 0) - { - fclose(fp); - osal_mutex_unlock(&sleep_mutex); - return false; - } - - // 入睡时间 - uint32_t fall_asleep_timestamp; - fread(&fall_asleep_timestamp, 1, 4, fp); - - // 起床时间 - uint32_t wake_up_timestamp; - fread(&wake_up_timestamp, 1, 4, fp); - - // 睡眠总时间 - uint8_t total_time_hour, total_time_minute; - fread(&total_time_hour, 1, 1, fp); - fread(&total_time_minute, 1, 1, fp); - uint16_t total_time = total_time_hour * 60 + total_time_minute; - - // 累加睡眠数据 - data->fall_asleep_timestamp = fall_asleep_timestamp; - data->wake_up_timestamp =wake_up_timestamp; - data->total_time = total_time; - - sleep_update_timestamp = wake_up_timestamp; - - fclose(fp); - - // osal_mutex_unlock(&sleep_mutex); - - return true; +bool is_regular_file(const std::string &file_name) { + struct stat file_stat; + if (stat(file_name.c_str(), &file_stat) != 0) { + return false; + } + + return S_ISREG(file_stat.st_mode); // 检查文件是否为普通文件 } - void set_sleep_data(SLEEP_T *data) - { - memset(&sleep_data, 0, sizeof(SLEEP_T)); - memcpy_s(&sleep_data, sizeof(SLEEP_T), data, sizeof(SLEEP_T)); - // 刷新model里面的数据 - // SleepModel::GetInstance().update_sleep(&sleep_data); - } - /* -------------------------------------------------------------------------- */ - SleepModel::SleepModel() - { - // 初始化配置睡眠目标 - set_sleep_goal(); - // 刷新数据 - ReInitData(); - } - SleepModel::~SleepModel() {} - SleepModel &SleepModel::GetInstance() - { - static SleepModel instance; - return instance; - } +bool read_sleep_data(SLEEP_T *data) { + // errcode_t ret; - void SleepModel::ReInitData() - { - // 配置睡眠数据 - int cmd = 0; - do_cywee_service_check_msg(CYWEE_SERVICE_CMD_GET_SLEEP_DATA, &cmd); //获取睡眠数据 但不中止睡眠 - LOS_Msleep(300); - do_custom_fileoperate_service_check_msg(CUSTOM_FILEOPERATE_MSG_TYPE_SLEEPDATA_READ,nullptr,0); - LOS_Msleep(300); - - // SLEEP_T *data = new SLEEP_T(); - - // if (read_sleep_data(data)) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ReadSleepData sleepStr %u wakeStr %u sleep_total_time %d", - sleep_data.fall_asleep_timestamp, sleep_data.wake_up_timestamp, sleep_data.total_time); - if(sleep_data.fall_asleep_timestamp) - { - update_time_info(sleep_data.fall_asleep_timestamp, sleepStr); - update_time_info(sleep_data.wake_up_timestamp, wakeStr); - } - else - { - sleepStr = "--"; - wakeStr = "--"; - } - sleep_total_time = sleep_data.total_time; - } - // else - // { - // sleepStr = "--"; - // wakeStr = "--"; - // sleep_total_time = 0; - // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to read sleep data"); - // } - - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ReadSleepData sleepStr %s wakeStr %s sleep_total_time %d", - sleepStr.c_str(), wakeStr.c_str(), sleep_total_time); - - // delete data; // 释放内存 - } + // ret = osal_mutex_lock(&sleep_mutex); - void SleepModel::UpdateSleepView() - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "睡眠数据更新"); - SleepControlView *p_SleepControlView = SleepControlView::GetInstance(); - if (p_SleepControlView != nullptr) - { - if (p_SleepControlView->GetSleepView()) - { - p_SleepControlView->GetSleepView()->SleepDataRefresh(); - } - if (p_SleepControlView->GetSleepTimeView()) - { - p_SleepControlView->GetSleepTimeView()->SleepDataRefresh(); - } - } - } - static constexpr const char *SLEEP_JSON_PATH = "/user/userdata/health/sleep.json"; - // 读取睡眠数据 - SleepDataUI *SleepModel::ReadSleepData() - { + // if (ret != ERRCODE_SUCC) + // { + // PRINT(" sleep_mutex lock fail! ret=0x%x\n", ret); + // return false; + // } - // 判断文件夹是否存在 - if (!HealthService::GetInstance()->CreateHealthFolder()) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CreateHealthFolder file"); - return nullptr; - } - - // 读取文件 - std::ifstream inFile(SLEEP_JSON_PATH); - if (!inFile.is_open()) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open JSON file"); - return nullptr; - } - - // 读取文件内容到字符串 - std::stringstream buffer; - buffer << inFile.rdbuf(); - std::string jsonDataString = buffer.str(); - const char *jsonData = jsonDataString.c_str(); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "jsonData: %s", jsonData); - - // 关闭文件 - inFile.close(); - if (jsonDataString.empty()) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "jsonDataString is empty"); - return nullptr; - } - - cJSON *sleep_root = cJSON_Parse(jsonData); - if (sleep_root == NULL) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleep_root is empty"); - return nullptr; - } - - SleepDataUI *data = new SleepDataUI(); - - cJSON *sleeptime_item = cJSON_GetObjectItem(sleep_root, "sleeptime"); - cJSON *waketime_item = cJSON_GetObjectItem(sleep_root, "waketime"); - cJSON *totaltime_item = cJSON_GetObjectItem(sleep_root, "totaltime"); - cJSON *sleep_timestamp_item = cJSON_GetObjectItem(sleep_root, "sleep_timestamp"); - cJSON *wake_timestamp_item = cJSON_GetObjectItem(sleep_root, "wake_timestamp"); - - if (sleeptime_item && waketime_item && totaltime_item) - { - data->sleeptime = sleeptime_item->valuestring; - data->waketime = waketime_item->valuestring; - data->totaltime = totaltime_item->valueint; - // 增加时间戳 - data->sleep_timestamp = sleep_timestamp_item->valueint; - data->wake_timestamp = wake_timestamp_item->valueint; - } - else - { - // 读取的数据异常 - cJSON_Delete(sleep_root); - delete data; - return nullptr; - } - - cJSON_Delete(sleep_root); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ReadSleepData is ok!"); - - // 返回结构体 - return data; - } + char day[12] = {0}; + uapi_get_YYMMDD_Time(day, '\0'); - // 更新睡眠数据 - bool SleepModel::SaveSleepData(const SleepDataUI &data) - { - // 判断文件夹是否存在 - if (!HealthService::GetInstance()->CreateHealthFolder()) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CreateHealthFolder file"); - return false; - } - - // 覆盖存储 - std::ofstream outFile(SLEEP_JSON_PATH, std::ios::trunc); - if (!outFile.is_open()) - { - // 文件创建失败 - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create or open JSON file for writing"); - return false; - } - - // 创建一个 JSON 对象 - cJSON *sleep_root = cJSON_CreateObject(); - if (sleep_root == NULL) - { - outFile.close(); - return false; - } - - cJSON_AddItemToObject(sleep_root, "sleeptime", cJSON_CreateString(data.sleeptime.c_str())); - cJSON_AddItemToObject(sleep_root, "waketime", cJSON_CreateString(data.waketime.c_str())); - cJSON_AddItemToObject(sleep_root, "totaltime", cJSON_CreateNumber(data.totaltime)); - cJSON_AddItemToObject(sleep_root, "sleep_timestamp", cJSON_CreateNumber(data.sleep_timestamp)); - cJSON_AddItemToObject(sleep_root, "wake_timestamp", cJSON_CreateNumber(data.wake_timestamp)); - - char *jsonData = cJSON_Print(sleep_root); - if (jsonData == NULL) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to generate JSON data"); - cJSON_free(sleep_root); - outFile.close(); - return false; - } - - // 写入 JSON 数据到文件 - outFile << jsonData; - - cJSON_free(sleep_root); - free(jsonData); - outFile.close(); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateHealthData JSON is ok"); - - return true; - } - std::string SleepModel::GetUpdateSleepTime() - { - sleep_time_str.clear(); - // 获取睡眠最新时间 - uint32_t sleep_time = get_sleep_update_timestamp(); - // 获取时间戳 - update_time_info(sleep_time, sleep_time_str); - return sleep_time_str; - } - void SleepModel::update_sleep(SLEEP_T *data) - { - if (data == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid input data"); - return; - } - - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "fall_asleep_timestamp:%d wake_up_timestamp:%d total_time:%d", - data->fall_asleep_timestamp, data->wake_up_timestamp, data->total_time); - - SleepDataUI *UIdata = ReadSleepData(); - if (UIdata != nullptr) - { - // 入睡时间是最早时间 - if (data->fall_asleep_timestamp < UIdata->sleep_timestamp || UIdata->sleep_timestamp == 0) - { - UIdata->sleep_timestamp = data->fall_asleep_timestamp; - } - // 起床时间是最晚时间 - if (data->wake_up_timestamp > UIdata->wake_timestamp || UIdata->wake_timestamp == 0) - { - UIdata->wake_timestamp = data->wake_up_timestamp; - } - // 总时间需要累加 - UIdata->totaltime += data->total_time; - - update_time_info(UIdata->sleep_timestamp, sleepStr); - update_time_info(UIdata->wake_timestamp, wakeStr); - UIdata->sleeptime = sleepStr; - UIdata->waketime = wakeStr; - - sleep_total_time = UIdata->totaltime; - - SaveSleepData(*UIdata); - } - delete UIdata; // Free memory if dynamically allocated - } + char file_path[64] = {0}; + sprintf_s(file_path, 64, SLEEP_TIME_PATH, day); - // 清空睡眠数据 - bool SleepModel::DeleteSleepData() - { - ReInitData(); // 刷新 - return true; - } + FILE *fp = fopen(file_path, "rb"); + if (fp == NULL) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "read_sleep_data fopen fail"); + osal_mutex_unlock(&sleep_mutex); + return false; + } - // 时间戳->字符串 - void SleepModel::update_time_info(uint32_t timestamp, std::string &timeStr) - { - // 如果时间戳为0,设置时间字符串为默认值 - if (timestamp == 0) - { - timeStr = "--"; - return; - } - // 解析时间戳 - char labelTime[20]; - if (uapi_get_TIME_STR(timestamp, labelTime, '.', ':', DATE_H_M) == ERRCODE_SUCC) - { - timeStr.assign(labelTime); - } - else - { - timeStr = "--"; // 如果获取失败,设置为默认值 - } - } + // 读取睡眠类型 + uint8_t sleep_type; + fread(&sleep_type, 1, 1, fp); - /* - 设置睡眠目标 - 前2个字节是显示睡眠小时 - 后2个字节显示睡眠分钟 - 需要单独解析出来。 - */ - void SleepModel::set_sleep_goal() - { - // 将原3个字符串清空 - goalnotifyStr.clear(); - goalsleepStr.clear(); - goalwakeStr.clear(); - - int goal_sleep = get_sleep_time_from_config_model(); - int goal_wake = get_wake_up_time_from_config_model(); - - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "goal_sleep:%d goal_wake:%d", goal_sleep, goal_wake); - /* -------------------------------------------------------------------------- */ - // 提取小时部分(前2字节) - int goal_sleep_hour = goal_sleep >> 16; - // 提取分钟部分(后2字节) - int goal_sleep_min = goal_sleep & 0x00FF; - std::ostringstream sleeposs; - sleeposs << std::setfill('0') << std::setw(2) << goal_sleep_hour << ":" << std::setw(2) << goal_sleep_min; - // 睡眠时间点字符串 - goalsleepStr = sleeposs.str(); - - /* -------------------------------------------------------------------------- */ - // 需要配置提醒字符串 减去SLEPPNOTIFY分钟的计算 - int notify_min = (goal_sleep_min >= SLEPPNOTIFY) ? goal_sleep_min - SLEPPNOTIFY : ((goal_sleep_min + 60) - SLEPPNOTIFY); // 避免分钟数为负 - int notify_hour = goal_sleep_hour; - if (goal_sleep_min < SLEPPNOTIFY) - { // 如果需要借位 - notify_hour -= 1; // 小时减1 - } - // 确保小时数正确 - notify_hour = (notify_hour < 0) ? 23 : notify_hour; // 处理从0点前SLEPPNOTIFY分钟的情况,变为前一天的23点40分 - // 格式化提醒时间字符串 - std::ostringstream notifyoss; - notifyoss << std::setfill('0') << std::setw(2) << notify_hour << ":" << std::setw(2) << notify_min; - // 设置提醒字符串 - goalnotifyStr = notifyoss.str(); - /* -------------------------------------------------------------------------- */ - - int goal_wake_hour = goal_wake >> 16; - int goal_wake_min = goal_wake & 0x00FF; - std::ostringstream wakeoss; - wakeoss << std::setfill('0') << std::setw(2) << goal_wake_hour << ":" << std::setw(2) << goal_wake_min; - // 起床时间点字符串 - goalwakeStr = wakeoss.str(); - /* -------------------------------------------------------------------------- */ - - // 睡眠字符串 以及总时长 - goalStr = goalsleepStr + "-" + goalwakeStr; - // goal_total_time 计算 - int total_hours; - if (goal_wake_hour < goal_sleep_hour) - { - total_hours = (goal_wake_hour + 24) - goal_sleep_hour; // 跨天情况 - } - else - { - total_hours = goal_wake_hour - goal_sleep_hour; - } - - int total_minutes = goal_wake_min - goal_sleep_min; - if (total_minutes < 0) - { - total_hours--; // 如果分钟差为负数,小时差减1 - total_minutes += 60; // 转换为正数,加60分钟 - } - goal_total_time = total_hours; // 确保最小时间为0小时 - - /*通知界面更新*/ - SendMsgToUIService(UI_UPDATE_SLEEP_DATA, 0, nullptr, 0); - } - // 获取睡眠总时长(小时) - int SleepModel::get_sleep_hour() - { - return sleep_total_time / 60; - } - // 获取睡眠总时长(分钟) - int SleepModel::get_sleep_min() - { - return sleep_total_time % 60; - } - // 获取睡眠总时长(目标) - int SleepModel::get_sleep_goal() - { - return goal_total_time; - } - // 获取睡眠总时长(实际) - int SleepModel::get_sleep_total() - { - return sleep_total_time; - } - // 获取实际入睡时间str - std::string SleepModel::get_rest_str() - { - return sleepStr; - } - // 获取实际醒来时间str - std::string SleepModel::get_wake_str() - { - return wakeStr; - } - // 获取目标休息时间str - std::string SleepModel::get_goal_str() - { - return goalStr; - } - // 获取 目标睡眠提醒 时间点str - std::string SleepModel::get_notifiy() - { - return goalnotifyStr; - } - // 获取 目标睡眠 时间点str - std::string SleepModel::get_sleep_notifiy() - { - return goalsleepStr; - } + if (sleep_type == 0) { + fclose(fp); + osal_mutex_unlock(&sleep_mutex); + return false; + } - // 获取 目标起床 时间点str - std::string SleepModel::get_wake_notifiy() - { - return goalwakeStr; + // 入睡时间 + uint32_t fall_asleep_timestamp; + fread(&fall_asleep_timestamp, 1, 4, fp); + + // 起床时间 + uint32_t wake_up_timestamp; + fread(&wake_up_timestamp, 1, 4, fp); + + // 睡眠总时间 + uint8_t total_time_hour, total_time_minute; + fread(&total_time_hour, 1, 1, fp); + fread(&total_time_minute, 1, 1, fp); + uint16_t total_time = total_time_hour * 60 + total_time_minute; + + // 累加睡眠数据 + data->fall_asleep_timestamp = fall_asleep_timestamp; + data->wake_up_timestamp = wake_up_timestamp; + data->total_time = total_time; + + sleep_update_timestamp = wake_up_timestamp; + + fclose(fp); + + // osal_mutex_unlock(&sleep_mutex); + + return true; +} + +void set_sleep_data(SLEEP_T *data) { + memset(&sleep_data, 0, sizeof(SLEEP_T)); + memcpy_s(&sleep_data, sizeof(SLEEP_T), data, sizeof(SLEEP_T)); + // 刷新model里面的数据 + // SleepModel::GetInstance().update_sleep(&sleep_data); +} +/* -------------------------------------------------------------------------- */ +SleepModel::SleepModel() { + // 初始化配置睡眠目标 + set_sleep_goal(); + // 刷新数据 + ReInitData(); +} +SleepModel::~SleepModel() {} +SleepModel &SleepModel::GetInstance() { + static SleepModel instance; + return instance; +} + +void SleepModel::ReInitData() { + // 配置睡眠数据 + int cmd = 0; + do_cywee_service_check_msg(CYWEE_SERVICE_CMD_GET_SLEEP_DATA, + &cmd); //获取睡眠数据 但不中止睡眠 + LOS_Msleep(300); + do_custom_fileoperate_service_check_msg( + CUSTOM_FILEOPERATE_MSG_TYPE_SLEEPDATA_READ, nullptr, 0); + LOS_Msleep(300); + + // SLEEP_T *data = new SLEEP_T(); + + // if (read_sleep_data(data)) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "ReadSleepData sleepStr %u wakeStr %u sleep_total_time %d", + sleep_data.fall_asleep_timestamp, + sleep_data.wake_up_timestamp, sleep_data.total_time); + if (sleep_data.fall_asleep_timestamp) { + update_time_info(sleep_data.fall_asleep_timestamp, sleepStr); + update_time_info(sleep_data.wake_up_timestamp, wakeStr); + } else { + sleepStr = "--"; + wakeStr = "--"; + } + sleep_total_time = sleep_data.total_time; + } + // else + // { + // sleepStr = "--"; + // wakeStr = "--"; + // sleep_total_time = 0; + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to read sleep data"); + // } + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "ReadSleepData sleepStr %s wakeStr %s sleep_total_time %d", + sleepStr.c_str(), wakeStr.c_str(), sleep_total_time); + + // delete data; // 释放内存 +} + +void SleepModel::UpdateSleepView() { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "睡眠数据更新"); + SleepControlView *p_SleepControlView = SleepControlView::GetInstance(); + if (p_SleepControlView != nullptr) { + if (p_SleepControlView->GetSleepView()) { + p_SleepControlView->GetSleepView()->SleepDataRefresh(); } - // 设置睡眠状态 - void SleepModel::set_sleep_state(SLEEPSTATE state) - { - sleepstate_ = state; + if (p_SleepControlView->GetSleepTimeView()) { + p_SleepControlView->GetSleepTimeView()->SleepDataRefresh(); } - // 获取睡眠状态 - SLEEPSTATE SleepModel::get_sleep_state() + if(p_SleepControlView->GetSleepNapView()) { - return sleepstate_; + p_SleepControlView->GetSleepNapView()->SleepDataRefresh(); } - /* ---------------------------------- 睡眠定时器 --------------------------------- */ + } +} +static constexpr const char *SLEEP_JSON_PATH = + "/user/userdata/health/sleep.json"; +// 读取睡眠数据 +SleepDataUI *SleepModel::ReadSleepData() { + + // 判断文件夹是否存在 + if (!HealthService::GetInstance()->CreateHealthFolder()) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CreateHealthFolder file"); + return nullptr; + } + + // 读取文件 + std::ifstream inFile(SLEEP_JSON_PATH); + if (!inFile.is_open()) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open JSON file"); + return nullptr; + } + + // 读取文件内容到字符串 + std::stringstream buffer; + buffer << inFile.rdbuf(); + std::string jsonDataString = buffer.str(); + const char *jsonData = jsonDataString.c_str(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "jsonData: %s", jsonData); + + // 关闭文件 + inFile.close(); + if (jsonDataString.empty()) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "jsonDataString is empty"); + return nullptr; + } + + cJSON *sleep_root = cJSON_Parse(jsonData); + if (sleep_root == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sleep_root is empty"); + return nullptr; + } + + SleepDataUI *data = new SleepDataUI(); + + cJSON *sleeptime_item = cJSON_GetObjectItem(sleep_root, "sleeptime"); + cJSON *waketime_item = cJSON_GetObjectItem(sleep_root, "waketime"); + cJSON *totaltime_item = cJSON_GetObjectItem(sleep_root, "totaltime"); + cJSON *sleep_timestamp_item = + cJSON_GetObjectItem(sleep_root, "sleep_timestamp"); + cJSON *wake_timestamp_item = + cJSON_GetObjectItem(sleep_root, "wake_timestamp"); + + if (sleeptime_item && waketime_item && totaltime_item) { + data->sleeptime = sleeptime_item->valuestring; + data->waketime = waketime_item->valuestring; + data->totaltime = totaltime_item->valueint; + // 增加时间戳 + data->sleep_timestamp = sleep_timestamp_item->valueint; + data->wake_timestamp = wake_timestamp_item->valueint; + } else { + // 读取的数据异常 + cJSON_Delete(sleep_root); + delete data; + return nullptr; + } + + cJSON_Delete(sleep_root); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ReadSleepData is ok!"); + + // 返回结构体 + return data; +} - // 声明定时器变量 - static osTimerId_t g_timer = NULL; +// 更新睡眠数据 +bool SleepModel::SaveSleepData(const SleepDataUI &data) { + // 判断文件夹是否存在 + if (!HealthService::GetInstance()->CreateHealthFolder()) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CreateHealthFolder file"); + return false; + } + + // 覆盖存储 + std::ofstream outFile(SLEEP_JSON_PATH, std::ios::trunc); + if (!outFile.is_open()) { + // 文件创建失败 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Failed to create or open JSON file for writing"); + return false; + } + + // 创建一个 JSON 对象 + cJSON *sleep_root = cJSON_CreateObject(); + if (sleep_root == NULL) { + outFile.close(); + return false; + } + + cJSON_AddItemToObject(sleep_root, "sleeptime", + cJSON_CreateString(data.sleeptime.c_str())); + cJSON_AddItemToObject(sleep_root, "waketime", + cJSON_CreateString(data.waketime.c_str())); + cJSON_AddItemToObject(sleep_root, "totaltime", + cJSON_CreateNumber(data.totaltime)); + cJSON_AddItemToObject(sleep_root, "sleep_timestamp", + cJSON_CreateNumber(data.sleep_timestamp)); + cJSON_AddItemToObject(sleep_root, "wake_timestamp", + cJSON_CreateNumber(data.wake_timestamp)); + + char *jsonData = cJSON_Print(sleep_root); + if (jsonData == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to generate JSON data"); + cJSON_free(sleep_root); + outFile.close(); + return false; + } + + // 写入 JSON 数据到文件 + outFile << jsonData; + + cJSON_free(sleep_root); + free(jsonData); + outFile.close(); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateHealthData JSON is ok"); + + return true; +} +std::string SleepModel::GetUpdateSleepTime() { + sleep_time_str.clear(); + // 获取睡眠最新时间 + uint32_t sleep_time = get_sleep_update_timestamp(); + // 获取时间戳 + update_time_info(sleep_time, sleep_time_str); + return sleep_time_str; +} +void SleepModel::update_sleep(SLEEP_T *data) { + if (data == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid input data"); + return; + } + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "fall_asleep_timestamp:%d wake_up_timestamp:%d total_time:%d", + data->fall_asleep_timestamp, data->wake_up_timestamp, + data->total_time); + + SleepDataUI *UIdata = ReadSleepData(); + if (UIdata != nullptr) { + // 入睡时间是最早时间 + if (data->fall_asleep_timestamp < UIdata->sleep_timestamp || + UIdata->sleep_timestamp == 0) { + UIdata->sleep_timestamp = data->fall_asleep_timestamp; + } + // 起床时间是最晚时间 + if (data->wake_up_timestamp > UIdata->wake_timestamp || + UIdata->wake_timestamp == 0) { + UIdata->wake_timestamp = data->wake_up_timestamp; + } + // 总时间需要累加 + UIdata->totaltime += data->total_time; + + update_time_info(UIdata->sleep_timestamp, sleepStr); + update_time_info(UIdata->wake_timestamp, wakeStr); + UIdata->sleeptime = sleepStr; + UIdata->waketime = wakeStr; + + sleep_total_time = UIdata->totaltime; + + SaveSleepData(*UIdata); + } + delete UIdata; // Free memory if dynamically allocated +} - /*定时器到期时的处理*/ - static void Sleep_TimerCallbackProc(int data) - { - // 发起新闹钟 - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Sleep_TimerCallbackProc"); - SleepModel::GetInstance().Sleep_StopTimer(); // 关闭定时器 - CustomEventPublish(TOPIC_EVENT_SLEEPNOTE, NULL, 0); // 发送睡眠弹窗 - } +// 清空睡眠数据 +bool SleepModel::DeleteSleepData() { + ReInitData(); // 刷新 + return true; +} - static void Sleep_TimerCallback(int data) - { - GraphicService::GetInstance()->PostGraphicEvent(std::bind(Sleep_TimerCallbackProc, data)); - } +// 时间戳->字符串 +void SleepModel::update_time_info(uint32_t timestamp, std::string &timeStr) { + // 如果时间戳为0,设置时间字符串为默认值 + if (timestamp == 0) { + timeStr = "--"; + return; + } + // 解析时间戳 + char labelTime[20]; + if (uapi_get_TIME_STR(timestamp, labelTime, '.', ':', DATE_H_M) == + ERRCODE_SUCC) { + timeStr.assign(labelTime); + } else { + timeStr = "--"; // 如果获取失败,设置为默认值 + } +} - void SleepModel::Sleep_StartTimer(uint32_t interval) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepModel::Sleep_StartTimer"); - if (g_timer != NULL) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDown_StartTimer Timer already exists!!"); - return; - } - g_timer = osTimerNew((osTimerFunc_t)Sleep_TimerCallback, osTimerOnce, NULL, NULL); - if (g_timer == NULL) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDown_StartTimer Timer create failed!!"); - return; - } - int tick = osMs2Tick(interval); - osStatus_t ret = osTimerStart(g_timer, tick); - if (ret != osOK) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDown_StartTimer] osTimerDelete failed, ret = %lx!!", ret); - if (g_timer != NULL) - { - ret = osTimerDelete(g_timer); - } - g_timer = NULL; - return; - } - return; - } +/* + 设置睡眠目标 + 前2个字节是显示睡眠小时 + 后2个字节显示睡眠分钟 + 需要单独解析出来。 +*/ +void SleepModel::set_sleep_goal() { + // 将原3个字符串清空 + goalnotifyStr.clear(); + goalsleepStr.clear(); + goalwakeStr.clear(); + + int goal_sleep = get_sleep_time_from_config_model(); + int goal_wake = get_wake_up_time_from_config_model(); + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "goal_sleep:%d goal_wake:%d", + goal_sleep, goal_wake); + /* -------------------------------------------------------------------------- + */ + // 提取小时部分(前2字节) + int goal_sleep_hour = goal_sleep >> 16; + // 提取分钟部分(后2字节) + int goal_sleep_min = goal_sleep & 0x00FF; + std::ostringstream sleeposs; + sleeposs << std::setfill('0') << std::setw(2) << goal_sleep_hour << ":" + << std::setw(2) << goal_sleep_min; + // 睡眠时间点字符串 + goalsleepStr = sleeposs.str(); + + /* -------------------------------------------------------------------------- + */ + // 需要配置提醒字符串 减去SLEPPNOTIFY分钟的计算 + int notify_min = + (goal_sleep_min >= SLEPPNOTIFY) + ? goal_sleep_min - SLEPPNOTIFY + : ((goal_sleep_min + 60) - SLEPPNOTIFY); // 避免分钟数为负 + int notify_hour = goal_sleep_hour; + if (goal_sleep_min < SLEPPNOTIFY) { // 如果需要借位 + notify_hour -= 1; // 小时减1 + } + // 确保小时数正确 + notify_hour = + (notify_hour < 0) + ? 23 + : notify_hour; // 处理从0点前SLEPPNOTIFY分钟的情况,变为前一天的23点40分 + // 格式化提醒时间字符串 + std::ostringstream notifyoss; + notifyoss << std::setfill('0') << std::setw(2) << notify_hour << ":" + << std::setw(2) << notify_min; + // 设置提醒字符串 + goalnotifyStr = notifyoss.str(); + /* -------------------------------------------------------------------------- + */ + + int goal_wake_hour = goal_wake >> 16; + int goal_wake_min = goal_wake & 0x00FF; + std::ostringstream wakeoss; + wakeoss << std::setfill('0') << std::setw(2) << goal_wake_hour << ":" + << std::setw(2) << goal_wake_min; + // 起床时间点字符串 + goalwakeStr = wakeoss.str(); + /* -------------------------------------------------------------------------- + */ + + // 睡眠字符串 以及总时长 + goalStr = goalsleepStr + "-" + goalwakeStr; + // goal_total_time 计算 + int total_hours; + if (goal_wake_hour < goal_sleep_hour) { + total_hours = (goal_wake_hour + 24) - goal_sleep_hour; // 跨天情况 + } else { + total_hours = goal_wake_hour - goal_sleep_hour; + } + + int total_minutes = goal_wake_min - goal_sleep_min; + if (total_minutes < 0) { + total_hours--; // 如果分钟差为负数,小时差减1 + total_minutes += 60; // 转换为正数,加60分钟 + } + goal_total_time = total_hours; // 确保最小时间为0小时 + + /*通知界面更新*/ + SendMsgToUIService(UI_UPDATE_SLEEP_DATA, 0, nullptr, 0); +} +// 获取睡眠总时长(小时) +int SleepModel::get_sleep_hour() { return sleep_total_time / 60; } +// 获取睡眠总时长(分钟) +int SleepModel::get_sleep_min() { return sleep_total_time % 60; } +// 获取睡眠总时长(目标) +int SleepModel::get_sleep_goal() { return goal_total_time; } +// 获取睡眠总时长(实际) +int SleepModel::get_sleep_total() { return sleep_total_time; } +// 获取实际入睡时间str +std::string SleepModel::get_rest_str() { return sleepStr; } +// 获取实际醒来时间str +std::string SleepModel::get_wake_str() { return wakeStr; } +// 获取目标休息时间str +std::string SleepModel::get_goal_str() { return goalStr; } +// 获取 目标睡眠提醒 时间点str +std::string SleepModel::get_notifiy() { return goalnotifyStr; } +// 获取 目标睡眠 时间点str +std::string SleepModel::get_sleep_notifiy() { return goalsleepStr; } + +// 获取 目标起床 时间点str +std::string SleepModel::get_wake_notifiy() { return goalwakeStr; } +// 设置睡眠状态 +void SleepModel::set_sleep_state(SLEEPSTATE state) { sleepstate_ = state; } +// 获取睡眠状态 +SLEEPSTATE SleepModel::get_sleep_state() { return sleepstate_; } +//小睡数据 +int SleepModel::get_nap_hour() { return nap_total_time / 60; } +int SleepModel::get_nap_min() { return nap_total_time % 60; } +std::string SleepModel::get_napsleep_str() { return napsleepStr; } +std::string SleepModel::get_napwake_str() { return napwakeStr; } +/* ---------------------------------- 睡眠定时器 + * --------------------------------- */ + +// 声明定时器变量 +static osTimerId_t g_timer = NULL; + +/*定时器到期时的处理*/ +static void Sleep_TimerCallbackProc(int data) { + // 发起新闹钟 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Sleep_TimerCallbackProc"); + SleepModel::GetInstance().Sleep_StopTimer(); // 关闭定时器 + CustomEventPublish(TOPIC_EVENT_SLEEPNOTE, NULL, 0); // 发送睡眠弹窗 +} - void SleepModel::Sleep_StopTimer(void) - { - // 检查定时器是否存在 - if (g_timer != NULL) - { - // 删除定时器并置为 NULL - osTimerStop(g_timer); - osStatus_t ret = osTimerDelete(g_timer); - if (ret != osOK) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret); - } - g_timer = NULL; - } - } - /* -------------------------------------------------------------------------- */ +static void Sleep_TimerCallback(int data) { + GraphicService::GetInstance()->PostGraphicEvent( + std::bind(Sleep_TimerCallbackProc, data)); +} + +void SleepModel::Sleep_StartTimer(uint32_t interval) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepModel::Sleep_StartTimer"); + if (g_timer != NULL) { + WEARABLE_LOGE( + WEARABLE_LOG_MODULE_APP, + "CountDownModel::CountDown_StartTimer Timer already exists!!"); + return; + } + g_timer = + osTimerNew((osTimerFunc_t)Sleep_TimerCallback, osTimerOnce, NULL, NULL); + if (g_timer == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "CountDownModel::CountDown_StartTimer Timer create failed!!"); + return; + } + int tick = osMs2Tick(interval); + osStatus_t ret = osTimerStart(g_timer, tick); + if (ret != osOK) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "[CountDown_StartTimer] osTimerDelete failed, ret = %lx!!", + ret); + if (g_timer != NULL) { + ret = osTimerDelete(g_timer); + } + g_timer = NULL; + return; + } + return; +} + +void SleepModel::Sleep_StopTimer(void) { + // 检查定时器是否存在 + if (g_timer != NULL) { + // 删除定时器并置为 NULL + osTimerStop(g_timer); + osStatus_t ret = osTimerDelete(g_timer); + if (ret != osOK) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "osTimerDelete failed, ret = %lx!!", ret); + } + g_timer = NULL; + } +} +/* -------------------------------------------------------------------------- */ #ifdef __cplusplus #if __cplusplus -extern "C" -{ +extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ - void custom_save_sleep_data(void) - { - uapi_dev_runtime_set_sleep_storage_status(1); - save_sleep_data(&sleep_data); - uapi_dev_runtime_set_sleep_storage_status(0); - } +void custom_save_sleep_data(void) { + uapi_dev_runtime_set_sleep_storage_status(1); + save_sleep_data(&sleep_data); + uapi_dev_runtime_set_sleep_storage_status(0); +} - void custom_save_nap_data(void) - { - uapi_dev_runtime_set_sleep_storage_status(1); - save_nap_data(&nap_data); - uapi_dev_runtime_set_sleep_storage_status(0); - } +void custom_save_nap_data(void) { + uapi_dev_runtime_set_sleep_storage_status(1); + save_nap_data(&nap_data); + uapi_dev_runtime_set_sleep_storage_status(0); +} - void custom_read_sleep_data(void) - { - memset(&sleep_data, 0, sizeof(SLEEP_T)); - read_sleep_data(&sleep_data); - } +void custom_read_sleep_data(void) { + memset(&sleep_data, 0, sizeof(SLEEP_T)); + read_sleep_data(&sleep_data); +} - void custom_read_nap_data(void) - { - // memset(&nap_data, 0, sizeof(NAP_T)); - //read_nap_data(&nap_data); - } +void custom_read_nap_data(void) { + // memset(&nap_data, 0, sizeof(NAP_T)); + // read_nap_data(&nap_data); +} #ifdef __cplusplus #if __cplusplus @@ -1023,4 +961,4 @@ extern "C" #endif /* __cplusplus */ #endif /* __cplusplus */ -} \ No newline at end of file +} // namespace OHOS \ No newline at end of file -- 2.17.1