优化多段睡眠补上清醒状态 1082/target/3 1087/1 1087/head
authorpenglei <1624952103@qq.com>
Thu, 3 Oct 2024 10:23:58 +0000 (18:23 +0800)
committerpenglei <1624952103@qq.com>
Thu, 3 Oct 2024 10:23:58 +0000 (18:23 +0800)
睡眠文件存储读写改用消息队列实现

code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModelC.h [new file with mode: 0644]
code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp
code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp
code/sdk/drivers/peripheral/cywee/src/main_dml.c
code/sdk/drivers/peripheral/utils/custom_fileoperate_service.c
code/sdk/drivers/peripheral/utils/custom_utils.h

diff --git a/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModelC.h b/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModelC.h
new file mode 100644 (file)
index 0000000..4020422
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * @Author: penglei 1624952103@qq.com
+ * @Date: 2024-10-03 17:09:01
+ * @LastEditors: penglei 1624952103@qq.com
+ * @LastEditTime: 2024-10-03 18:12:11
+ * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModelC.h
+ * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
+ */
+#ifndef SLEEPMODELC_H
+#define SLEEPMODELC_H
+
+#ifdef __cplusplus
+#if __cplusplus
+extern "C"
+{
+#endif
+#endif
+    void custom_save_sleep_data(void);
+    void custom_save_nap_data(void);
+    void custom_read_sleep_data(void);
+    void custom_read_nap_data(void);
+#ifdef __cplusplus
+#if __cplusplus
+}
+#endif
+#endif
+#endif
index 056eb75..ee5ee5f 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: penglei 1624952103@qq.com
  * @Date: SLEPPNOTIFY24-06-26 19:36:48
  * @LastEditors: penglei 1624952103@qq.com
- * @LastEditTime: 2024-10-02 17:56:07
+ * @LastEditTime: 2024-10-03 18:18:41
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp
  * @Description:
  *
@@ -45,6 +45,7 @@
 #include "MainModel.h"
 #include "cywee_service.h"
 #include "custom_runtime.h"
+#include "sleep/include/SleepModelC.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);
@@ -166,19 +167,6 @@ namespace OHOS
     {
         memset(&nap_data, 0, sizeof(NAP_T));
         memcpy_s(&nap_data, sizeof(NAP_T), data, sizeof(NAP_T));
-
-        //睡眠存储加锁与设runtime状态统一在外面处理
-        uapi_dev_runtime_set_sleep_storage_status(1);
-        errcode_t ret = osal_mutex_lock(&sleep_mutex);
-        if (ret != ERRCODE_SUCC) 
-        {
-            PRINT(" sleep_mutex lock fail! ret=0x%x\n", ret);
-            uapi_dev_runtime_set_sleep_storage_status(0);
-            return;
-        }
-        save_nap_data(&nap_data);
-        osal_mutex_unlock(&sleep_mutex);
-        uapi_dev_runtime_set_sleep_storage_status(0);
     }
 
     uint32_t read_sleep_nap_data(char *data, uint32_t size, FILE *fp)
@@ -195,7 +183,7 @@ namespace OHOS
     static char status_file_path[64] = {0};
 
     static uint32_t fall_asleep_timestamp = 0;
-    // static uint32_t history_wake_up_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)
@@ -277,13 +265,13 @@ namespace OHOS
 
     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;
-        }
+        // 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)
         {
@@ -366,7 +354,7 @@ namespace OHOS
             total_rem_sleep_time -= data->total_rem_sleep_time;
         }
 
-        if(access(status_file_path, F_OK) == -1)
+        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);
@@ -374,54 +362,66 @@ namespace OHOS
             if(fp == NULL)
             {
                 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_sleep_data fopen fail");
+                // osal_mutex_unlock(&sleep_mutex);
                 return false;
             }
-            // if(data->wake_up_timestamp != 0)
+            // if(wake_up_timestamp != 0)
             // {
-            //     history_wake_up_timestamp = data->wake_up_timestamp; //更新历史出睡时间戳
+            //     history_wake_up_timestamp = wake_up_timestamp; //更新历史出睡时间戳
             // }
             fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp);
         }
-        else
+        else //文件已存在 需要判断是否需要补上清醒状态 
         {        
-            fp = fopen(status_file_path, "ab+");
+            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);
+                // osal_mutex_unlock(&sleep_mutex);
                 return false;
             }
-            fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp);
-            // if(history_wake_up_timestamp != data->wake_up_timestamp && ((data->fall_asleep_timestamp - history_wake_up_timestamp) < 2 * 60 * 60))
-            // {
-            //     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; //清醒状态时间为上一次的出睡时间
-                //     fwrite(&sleep_status, sizeof(SLEEP_STATE_T), 1, fp); //替换历史出睡数据 改为清醒状态
-                //     fwrite(&data->sleep_status[1], sizeof(SLEEP_STATE_T), data->status_num - 1, fp); //跳过入睡状态写入文件
-                // }
-                // else
-                // {
-                    //fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp);
-                //}
-            // }
-            // else
-            // {
-            //     fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp);
-            // }
+            //fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp);
 
-            // if(data->wake_up_timestamp != 0)
-            // {
-            //     history_wake_up_timestamp = data->wake_up_timestamp; //更新历史出睡时间戳
-            // }
+            //如果本次出睡时间与历史出睡时间不等 且两次睡眠间隔小于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;
     }
 
@@ -438,15 +438,15 @@ namespace OHOS
 
     bool read_sleep_data(SLEEP_T *data)
     {
-        errcode_t ret;
+        // errcode_t ret;
 
-        ret = osal_mutex_lock(&sleep_mutex);
+        // ret = osal_mutex_lock(&sleep_mutex);
 
-        if (ret != ERRCODE_SUCC) 
-        {
-            PRINT(" sleep_mutex lock fail! ret=0x%x\n", ret);
-            return false;
-        }
+        // 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');
@@ -496,7 +496,7 @@ namespace OHOS
 
         fclose(fp);
 
-        osal_mutex_unlock(&sleep_mutex);
+        // osal_mutex_unlock(&sleep_mutex);
 
         return true;
 }
@@ -505,18 +505,6 @@ namespace OHOS
     {
         memset(&sleep_data, 0, sizeof(SLEEP_T));
         memcpy_s(&sleep_data, sizeof(SLEEP_T), data, sizeof(SLEEP_T));
-        //睡眠存储加锁与设runtime状态统一在外面处理
-        uapi_dev_runtime_set_sleep_storage_status(1);
-        errcode_t ret = osal_mutex_lock(&sleep_mutex);
-        if (ret != ERRCODE_SUCC) 
-        {
-            PRINT(" sleep_mutex lock fail! ret=0x%x\n", ret);
-            uapi_dev_runtime_set_sleep_storage_status(0);
-            return;
-        }
-        save_sleep_data(&sleep_data);
-        osal_mutex_unlock(&sleep_mutex);
-        uapi_dev_runtime_set_sleep_storage_status(0);
         // 刷新model里面的数据
         // SleepModel::GetInstance().update_sleep(&sleep_data);
     }
@@ -540,28 +528,40 @@ namespace OHOS
         // 配置睡眠数据
         int cmd = 0;
         do_cywee_service_check_msg(CYWEE_SERVICE_CMD_GET_SLEEP_DATA, &cmd); //获取睡眠数据 但不中止睡眠
-        LOS_Msleep(500);
-        SLEEP_T *data = new SLEEP_T();
-        if (read_sleep_data(data))
+        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",
-                          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");
+                          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; // 释放内存
+        // delete data; // 释放内存
     }
 
     void SleepModel::UpdateSleepView()
@@ -983,4 +983,44 @@ namespace OHOS
         }
     }
     /* -------------------------------------------------------------------------- */
+
+#ifdef __cplusplus
+#if __cplusplus
+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_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_nap_data(void)
+    {
+        // memset(&nap_data, 0, sizeof(NAP_T));
+        //read_nap_data(&nap_data);
+    }
+
+#ifdef __cplusplus
+#if __cplusplus
+}
+#endif /* __cplusplus */
+#endif /* __cplusplus */
+
 }
\ No newline at end of file
index 1fe6fac..f1a0457 100644 (file)
@@ -1403,6 +1403,8 @@ void MsgCenterNotifyInactivityAccumulateTimeProc(const Topic topic, Request* req
     {
         nap_data.total_time = cywee_Inactivity_Accumulate_Time_Info->total_time;
         set_nap_data(&nap_data);
+        do_custom_fileoperate_service_check_msg(
+        CUSTOM_FILEOPERATE_MSG_TYPE_NAPDATA_SAVE, nullptr, 0);
         memset(&nap_data,0,sizeof(NAP_T));
     }
     else if(cywee_Inactivity_Accumulate_Time_Info->output_number == INACTIVITY_MODE_SLEEPING_ACCUMULATE_TIME_OUTPUT)
@@ -1421,6 +1423,8 @@ void MsgCenterNotifyInactivityAccumulateTimeProc(const Topic topic, Request* req
         sleep_data.status_num = state_num;
 
         set_sleep_data(&sleep_data);
+        do_custom_fileoperate_service_check_msg(
+        CUSTOM_FILEOPERATE_MSG_TYPE_SLEEPDATA_SAVE, nullptr, 0);
         memset(&sleep_data,0,sizeof(SLEEP_T));
         state_num = 0;
         //fall_asleep_state_num = 0;
index 9b86a82..01dca4f 100644 (file)
@@ -867,12 +867,15 @@ void CWM_AP_SensorListen(pSensorEVT_t sensorEVT) {
                     uapi_save_sleep_flag(&sleep_flag_t,sizeof(SLEEP_FLAG_T));
                 }
 
-                //0进入睡眠  1潜睡 2深睡 3清醒 4结束睡眠 6入睡 7出睡最后一笔输出 8未出睡最后一笔输出 12快速动眼 13取消入睡
-                if(c_inactivity_result_t.status == 6)
+                //0进入睡眠  1潜睡 2深睡 3清醒 4结束睡眠 6入睡 7出睡最后一笔输出 9未出睡最后一笔输出 12快速动眼 13取消入睡
+                if(c_inactivity_result_t.status >= 6 && c_inactivity_result_t.status <= 9)
                 {
-                    uapi_dev_runtime_set_sleep_status(1);
-                    uapi_get_timestampe_ss(&asleep_timestampe);
-                    break; //入睡通知不发消息,直接退出
+                    if(c_inactivity_result_t.status == 6)
+                    {
+                        uapi_dev_runtime_set_sleep_status(1);
+                        uapi_get_timestampe_ss(&asleep_timestampe);
+                    }
+                    break; //只要是通知都不发消息,直接退出
                 }
                 else if(c_inactivity_result_t.status == 4 || c_inactivity_result_t.status == 13)
                 {
index f4dcf82..6727198 100644 (file)
@@ -41,6 +41,8 @@
 
 #include "safemodemanager/include/SafeModeManagerModelC.h"
 
+#include "sleep/include/SleepModelC.h"
+
 typedef struct
 {
     uint8_t cmd;
@@ -115,6 +117,18 @@ static void custom_fileoperate_service_check_task(void const *arg)
             break;
         case CUSTOM_FILEOPERATE_MSG_TYPE_MESSAGEDATA_SAVE:
             break;
+        case CUSTOM_FILEOPERATE_MSG_TYPE_SLEEPDATA_SAVE:
+            custom_save_sleep_data();
+            break;
+        case CUSTOM_FILEOPERATE_MSG_TYPE_NAPDATA_SAVE:
+            custom_save_nap_data();
+            break;
+        case CUSTOM_FILEOPERATE_MSG_TYPE_SLEEPDATA_READ:
+            custom_read_sleep_data();
+            break;
+        case CUSTOM_FILEOPERATE_MSG_TYPE_NAPDATA_READ:
+            custom_read_nap_data();
+            break;
         default:
             PRINT("Unknown CUSTOM_FILE_OPERATE_MSG cmd 0x%02X \r\n", mail.cmd);
             break;
index 5bfabe8..c424150 100644 (file)
@@ -237,6 +237,11 @@ errcode_t do_custom_cmd_service_check_msg(int cmd, void *params, int paramlen);
 #define CUSTOM_FILEOPERATE_MSG_TYPE_HEARTRATE_SAVE 0x02 //心率数据
 #define CUSTOM_FILEOPERATE_MSG_TYPE_BLOODXYGEN_SAVE 0x03//血氧数据
 #define CUSTOM_FILEOPERATE_MSG_TYPE_MESSAGEDATA_SAVE 0x04//消息数据
+#define CUSTOM_FILEOPERATE_MSG_TYPE_SLEEPDATA_SAVE 0x05//睡眠数据
+#define CUSTOM_FILEOPERATE_MSG_TYPE_NAPDATA_SAVE 0x06//小睡数据
+
+#define CUSTOM_FILEOPERATE_MSG_TYPE_SLEEPDATA_READ 0x07 //读取睡眠时间
+#define CUSTOM_FILEOPERATE_MSG_TYPE_NAPDATA_READ 0x08  //读取小睡数据
 
 errcode_t uapi_custom_fileoperate_service_init(void);
 errcode_t do_custom_fileoperate_service_check_msg(int cmd, void *params, int paramlen);