睡眠Model从bin里面读取数据 513/2
authorCaisking <3449750381@qq.com>
Fri, 12 Jul 2024 11:48:27 +0000 (19:48 +0800)
committerCaisking <3449750381@qq.com>
Fri, 12 Jul 2024 11:48:27 +0000 (19:48 +0800)
code/sdk/application/wearable/fd230app/nativeui/alarmclocktimerconfig/src/AlarmClockTimerConfig.cpp
code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h
code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp

index 38ce1d8..23ff14c 100644 (file)
@@ -323,6 +323,7 @@ extern "C"
                     OHOS::ConfigModel::GetInstance()->SetDnotdisturb(false); // 关闭勿扰\r
                 }\r
                 LOS_Msleep(1000);\r
+                OHOS::SleepModel::GetInstance().ReInitData();//更新睡眠数据\r
                 do_cywee_service_check_msg(CYWEE_SERVICE_CMD_NAP_SEDENTARY_MODE, NULL);\r
             }\r
             // 进入睡眠模式\r
index 70c39cb..3143580 100644 (file)
@@ -30,6 +30,8 @@
 
 #define SLEEP_DIR_PATH "user/userdata/sleep/%s"
 
+#define SLEEP_DATA_DIR "user/userdata/sleep/"
+
 namespace OHOS
 {
 #pragma pack(1)
@@ -67,6 +69,7 @@ namespace OHOS
     void set_nap_data(NAP_T *data);
     void set_sleep_data(SLEEP_T *data);
     bool save_sleep_data(SLEEP_T *data);
+    bool read_sleep_data(SLEEP_T *data);
 
     // 睡眠状态
     enum class SLEEPSTATE
index 809028a..877130c 100644 (file)
 #include "ConfigModel.h"
 #include "heart_blood/health_service/health_service.h"
 #include "custom_utils.h"
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <dirent.h>
 
 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);
@@ -204,13 +208,94 @@ namespace OHOS
         fclose(fp);
         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); // 检查文件是否为普通文件
+    }
+
+    bool read_sleep_data(SLEEP_T *data)
+    {
+        char day[12] = {0};
+        uapi_get_YYMMDD_Time(day, '\0');
+
+        char file_path[64] = {0};
+        sprintf_s(file_path, 64, "%s%s/", SLEEP_DATA_DIR, day);
+
+        // 打开目录
+        DIR *dir;
+        struct dirent *ent;
+        if ((dir = opendir(file_path)) != NULL)
+        {
+            while ((ent = readdir(dir)) != NULL)
+            {
+                char file_name[64];
+                sprintf_s(file_name, 64, "%s%s", file_path, ent->d_name);
+
+                if (is_regular_file(file_name)) // 判断是否为文件
+                {
+                    FILE *fp = fopen(file_name, "rb");
+                    if (fp == NULL)
+                    {
+                        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "read_sleep_data fopen fail");
+                        continue;
+                    }
+
+                    // 读取睡眠类型
+                    uint8_t sleep_type;
+                    fread(&sleep_type, 1, 1, fp);
+
+                    if (sleep_type == 0)
+                    {
+                        fclose(fp);
+                        continue;
+                    }
+
+                    // 入睡时间
+                    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 = (data->fall_asleep_timestamp == 0) ? fall_asleep_timestamp : min(data->fall_asleep_timestamp, fall_asleep_timestamp);
+                    data->wake_up_timestamp = max(data->wake_up_timestamp, wake_up_timestamp);
+                    data->total_time += total_time;
+
+                    fclose(fp);
+                }
+            }
+            closedir(dir);
+        }
+        else
+        {
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "cannot open directory");
+            return false;
+        }
+
+        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));
         save_sleep_data(&sleep_data);
         // 刷新model里面的数据
-        SleepModel::GetInstance().update_sleep(&sleep_data);
+        // SleepModel::GetInstance().update_sleep(&sleep_data);
     }
     /* -------------------------------------------------------------------------- */
     SleepModel::SleepModel()
@@ -229,18 +314,28 @@ namespace OHOS
 
     void SleepModel::ReInitData()
     {
-        // 配置睡眠字符串
-        SleepDataUI *data = ReadSleepData();
-        if (data != nullptr)
+        // 配置睡眠数据
+        SLEEP_T *data = new SLEEP_T();
+        if (read_sleep_data(data))
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ReadSleepData sleepStr %s  wakeStr %s sleep_total_time %d",
-                          data->sleeptime.c_str(), data->waketime.c_str(), data->totaltime);
-            update_time_info(data->sleep_timestamp, sleepStr);
-            update_time_info(data->wake_timestamp, wakeStr);
-            sleep_total_time = data->totaltime;
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ReadSleepData sleepStr %u  wakeStr %u sleep_total_time %d",
+                          data->fall_asleep_timestamp, data->wake_up_timestamp, data->total_time);
+            update_time_info(data->fall_asleep_timestamp, sleepStr);
+            update_time_info(data->wake_up_timestamp, wakeStr);
+            sleep_total_time = 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; // 释放内存
     }
     static constexpr const char *SLEEP_JSON_PATH = "/user/userdata/health/sleep.json";
     // 读取睡眠数据
@@ -410,22 +505,8 @@ namespace OHOS
     // 清空睡眠数据
     bool SleepModel::DeleteSleepData()
     {
-        SleepDataUI data;
-        data.sleeptime = "--";
-        data.waketime = "--";
-        data.totaltime = 0;
-        data.sleep_timestamp = 0;
-        data.wake_timestamp = 0;
-
-        if (SaveSleepData(data))
-        {
-            ReInitData(); // 刷新
-            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "DeleteSleepData is ok");
-            return true;
-        }
-
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DeleteSleepData is error");
-        return false;
+        ReInitData(); // 刷新
+        return true;
     }
 
     void SleepModel::update_time_info(uint32_t timestamp, std::string &timeStr)