增加睡眠UI数据存储
authorCaisking <3449750381@qq.com>
Thu, 4 Jul 2024 12:52:30 +0000 (20:52 +0800)
committerSnickerSun <snickersun@163.com>
Fri, 5 Jul 2024 02:39:07 +0000 (10:39 +0800)
code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h
code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp
code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepView.cpp

index 64a3af5..3b93eb1 100644 (file)
@@ -78,10 +78,9 @@ namespace OHOS
     // 睡眠存储数据
     typedef struct
     {
-        uint8_t sleep_state; // 睡眠状态
-        int sleeptime;       // 睡眠时间
-        int walktime;        // 醒来时间
-        int totaltime;       // 睡眠总时间
+        std::string sleeptime; // 睡眠时间
+        std::string walktime;  // 醒来时间
+        int totaltime;         // 睡眠总时间
     } SleepDataUI;
     class SleepModel
     {
@@ -105,8 +104,8 @@ namespace OHOS
 
         void set_sleep_state(SLEEPSTATE state);
         SLEEPSTATE get_sleep_state();
-        SleepDataUI *ReadSleepData();                  // 读取睡眠数据
-        bool UpdateSleepData(const SleepDataUI &data); // 更新睡眠数据
+        SleepDataUI *ReadSleepData();                // 读取睡眠数据
+        bool SaveSleepData(const SleepDataUI &data); // 更新睡眠数据
 
     private:
         SleepModel();
index 2dd61b3..d39024b 100644 (file)
@@ -26,6 +26,7 @@
 #include <string>
 #include "wearable_log.h"
 #include "ConfigModel.h"
+#include "heart_blood/health_service/health_service.h"
 
 extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char separator, bool include_seconds, bool include_year);
 extern "C" int get_sleep_time_from_config_model(void);
@@ -213,7 +214,17 @@ namespace OHOS
     /* -------------------------------------------------------------------------- */
     SleepModel::SleepModel()
     {
+        // 初始化配置睡眠目标
         set_sleep_goal();
+
+        // 配置睡眠字符串
+        SleepDataUI *data = ReadSleepData();
+        if (data != nullptr)
+        {
+            sleepStr = data->sleeptime;
+            walkStr = data->walktime;
+            sleep_total_time = data->totaltime;
+        }
     }
     SleepModel::~SleepModel() {}
     SleepModel &SleepModel::GetInstance()
@@ -222,6 +233,122 @@ namespace OHOS
         return instance;
     }
 
+    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 *walktime_item = cJSON_GetObjectItem(sleep_root, "walktime");
+        cJSON *totaltime_item = cJSON_GetObjectItem(sleep_root, "totaltime");
+
+        if (sleeptime_item && walktime_item && totaltime_item)
+        {
+            data->sleeptime = sleeptime_item->valuestring;
+            data->walktime = walktime_item->valuestring;
+            data->totaltime = totaltime_item->valueint;
+        }
+        else
+        {
+            // 读取的数据异常
+            cJSON_Delete(sleep_root);
+            delete data;
+            return nullptr;
+        }
+
+        cJSON_Delete(sleep_root);
+        // 返回结构体
+        return data;
+    }
+
+    // 更新睡眠数据
+    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, "walktime", cJSON_CreateString(data.walktime.c_str()));
+        cJSON_AddItemToObject(sleep_root, "totaltime", cJSON_CreateNumber(data.totaltime));
+
+        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_LOGE(WEARABLE_LOG_MODULE_APP, "UpdateHealthData JSON is ok");
+
+        return true;
+    }
     void SleepModel::update_sleep(SLEEP_T *data)
     {
         if (data == NULL)
@@ -234,16 +361,18 @@ namespace OHOS
         WEARABLE_LOGE(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);
 
-        update_time_info(data->fall_asleep_timestamp, sleepStr);
-        update_time_info(data->wake_up_timestamp, walkStr);
+        update_time_info(data->fall_asleep_timestamp, sleepStr); // 睡眠字符串
+        update_time_info(data->wake_up_timestamp, walkStr);      // 醒来
 
         // 更新睡眠总时间
-        sleep_total_time = data->total_time;
-
-        // 更新一下配置
-        set_sleep_goal();
-
-        // 刷新一下文件
+        sleep_total_time = data->total_time; // 睡眠总时长
+
+        SleepDataUI UIdata;
+        UIdata.sleeptime = sleepStr;
+        UIdata.walktime = walkStr;
+        UIdata.totaltime = sleep_total_time;
+        // 数据存储
+        SaveSleepData(UIdata);
     }
 
     void SleepModel::update_time_info(uint32_t timestamp, std::string &timeStr)
@@ -313,7 +442,6 @@ namespace OHOS
 
         // 睡眠字符串 以及总时长
         goalStr = goalsleepStr + "-" + goalwalkStr;
-
         // goal_total_time 计算
         int total_hours;
         if (goal_walk_hour < goal_sleep_hour)
@@ -331,7 +459,6 @@ namespace OHOS
             total_hours--;       // 如果分钟差为负数,小时差减1
             total_minutes += 60; // 转换为正数,加60分钟
         }
-
         goal_total_time = (total_hours == 0 && total_minutes < 60) ? 1 : total_hours; // 确保最小时间为1小时
     }
     // 获取睡眠总时长(小时)
index af57e6a..9f0be04 100644 (file)
@@ -31,7 +31,7 @@ namespace OHOS
     {
         int sleep_hour = SleepModel::GetInstance().get_sleep_hour();
         int sleep_min = SleepModel::GetInstance().get_sleep_min();
-
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SleepDataRefresh %d %d", sleep_hour, sleep_min);
         // 补零操作
         std::string hour_str = (sleep_hour < 10) ? "0" + std::to_string(sleep_hour) : std::to_string(sleep_hour);
         std::string min_str = (sleep_min < 10) ? "0" + std::to_string(sleep_min) : std::to_string(sleep_min);