添加小睡与睡眠数据存储 404/1
authorpenglei <1624952103@qq.com>
Fri, 28 Jun 2024 09:22:19 +0000 (17:22 +0800)
committerpenglei <1624952103@qq.com>
Fri, 28 Jun 2024 09:22:19 +0000 (17:22 +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/service/msg_center/src/msg_center_customer.cpp

index 1c1f9e5..a596d09 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: penglei 1624952103@qq.com
  * @Date: 2024-06-26 19:36:48
  * @LastEditors: penglei 1624952103@qq.com
- * @LastEditTime: 2024-06-28 14:25:40
+ * @LastEditTime: 2024-06-28 15:51:01
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h
  * @Description: 
  * 
@@ -34,6 +34,7 @@
 
 namespace OHOS
 {
+    #pragma pack(1)
     typedef struct
     {
         uint32_t fall_asleep_timestamp; //入睡
@@ -41,8 +42,17 @@ namespace OHOS
         uint16_t total_time; //总时长
     } NAP_T;
 
-     typedef struct
+    typedef struct
     {
+        uint32_t timestamp; //
+        uint8_t stat; //状态
+       
+    } SLEEP_STATE_T;
+
+    typedef struct
+    {
+        SLEEP_STATE_T sleep_status[20];
+        uint8_t status_num;
         uint32_t fall_asleep_timestamp; //入睡
         uint32_t wake_up_timestamp; //出睡
         uint16_t total_time; //总时长
@@ -51,23 +61,17 @@ namespace OHOS
         uint16_t total_rem_sleep_time;//清醒
         uint16_t total_wake_time;//动眼
     } SLEEP_T;
-
-    typedef struct
-    {
-        uint32_t timestamp; //
-        uint8_t stat; //状态
-       
-    } SLEEP_STATE_T;
+    
+    #pragma pack()
 
     bool save_nap_data(NAP_T *data);
 
-    uint32_t read_nap_data(char *data,uint32_t size,FILE *fp);
+    uint32_t read_sleep_nap_data(char *data,uint32_t size,FILE *fp);
     
     void set_nap_data(NAP_T *data);
 
     void set_sleep_data(SLEEP_T *data);
-    void set_sleep_status(SLEEP_STATE_T *data);
-    uint32_t read_sleep_data(char *data,uint32_t size,FILE *fp);
+
     bool save_sleep_data(SLEEP_T *data);
 
     class SleepModel
index 6719583..2a430e0 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: penglei 1624952103@qq.com
  * @Date: 2024-06-26 19:36:48
  * @LastEditors: penglei 1624952103@qq.com
- * @LastEditTime: 2024-06-28 15:49:01
+ * @LastEditTime: 2024-06-28 16:52:58
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp
  * @Description: 
  * 
@@ -32,10 +32,19 @@ namespace OHOS
         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
             }
         }
@@ -48,7 +57,7 @@ namespace OHOS
         fp=fopen(file_path,"wb+");
         if(fp==NULL)
         {
-            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data fopen fail");
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data fopen fail");
             return false;
         }
 
@@ -65,6 +74,7 @@ namespace OHOS
         fwrite(&total_time,1,2,fp);
 
 
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data succ");
 
         fclose(fp);
         return true;
@@ -78,13 +88,77 @@ namespace OHOS
         save_nap_data(&nap_data);
     }
 
-    uint32_t read_nap_data(char *data,uint32_t size,FILE *fp)
+    uint32_t read_sleep_nap_data(char *data,uint32_t size,FILE *fp)
     {
         return fread(data,1,size,fp);
     }
    
     bool save_sleep_data(SLEEP_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;
+        int ret =uapi_get_timestampe_ss(&creationTime);
+
+        sprintf_s(file_path,64,SLEEP_PATH,day,creationTime);
+
+        FILE *fp;
+        fp=fopen(file_path,"wb+");
+        if(fp==NULL)
+        {
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data fopen fail");
+            return false;
+        }
+
+        uint8_t sleep_type = 1;
+        fwrite(&sleep_type,1,1,fp);
+
+        uint32_t fall_asleep_timestamp = data->fall_asleep_timestamp;
+        fwrite(&fall_asleep_timestamp,1,4,fp);
+
+        uint32_t wake_up_timestamp = data->wake_up_timestamp;
+        fwrite(&wake_up_timestamp,1,4,fp);
+
+        uint16_t total_time=data->total_time;
+        fwrite(&total_time,1,2,fp);
+
+        uint16_t total_deep_sleep_time=data->total_deep_sleep_time;
+        fwrite(&total_deep_sleep_time,1,2,fp);
+
+        uint16_t total_mild_sleep_time=data->total_mild_sleep_time;
+        fwrite(&total_mild_sleep_time,1,2,fp);
+
+        uint16_t total_rem_sleep_time=data->total_rem_sleep_time;
+        fwrite(&total_rem_sleep_time,1,2,fp);
+
+        uint16_t total_wake_time=data->total_wake_time;
+        fwrite(&total_wake_time,1,2,fp);
+
+        fwrite(data->sleep_status,sizeof(SLEEP_STATE_T),data->status_num,fp);
+
+        fclose(fp);
         return true;
     }
 
@@ -95,19 +169,6 @@ namespace OHOS
         save_sleep_data(&sleep_data);
     }
 
-    void set_sleep_status(SLEEP_STATE_T *data)
-    {   
-        memset(&sleep_status,0,sizeof(SLEEP_STATE_T));
-        memcpy_s(&sleep_status,sizeof(SLEEP_STATE_T),data,sizeof(SLEEP_STATE_T));
-
-        //save_sleep_data(&sleep_status);
-    }
-
-    uint32_t read_sleep_data(char *data,uint32_t size,FILE *fp)
-    {
-        return fread(data,1,size,fp);
-    }
-   
     SleepModel::SleepModel() {}
     SleepModel::~SleepModel() {}
     SleepModel &SleepModel::GetInstance()
index f03333e..99537d4 100644 (file)
@@ -980,6 +980,7 @@ void MsgCenterNotifyActivityExtraInfoProc(const Topic topic, Request* req)
 
 static SLEEP_T sleep_data = {0};
 static NAP_T nap_data = {0};
+static int state_num = 0;
 
 void MsgCenterNotifyInactivityProc(const Topic topic, Request *req)
 {
@@ -1029,9 +1030,12 @@ void MsgCenterNotifyInactivityProc(const Topic topic, Request *req)
         case INACTIVITY_MODE_SLEEPING_OUTPUT:
 
             uapi_get_timestampe_ss(&timestamp);
-            state_data.timestamp = timestamp;
-            state_data.stat = cywee_Inactivity_Info->status;
-            set_sleep_status(&state_data);
+            if(state_num < 20)
+            {
+                sleep_data.sleep_status[state_num].timestamp = timestamp;
+                sleep_data.sleep_status[state_num].stat = cywee_Inactivity_Info->status;
+                state_num ++ ;
+            }
 
             if(cywee_Inactivity_Info->status == 6) //0进入睡眠  1潜睡 2深睡 3清醒 4结束睡眠 6入睡 7出睡最后一笔输出 8未出睡最后一笔输出 12快速动眼 13取消入睡
             {
@@ -1100,9 +1104,17 @@ void MsgCenterNotifyInactivityAccumulateTimeProc(const Topic topic, Request* req
     sleep_data.total_mild_sleep_time = cywee_Inactivity_Accumulate_Time_Info->total_mild_sleep_time;
     sleep_data.total_wake_time = cywee_Inactivity_Accumulate_Time_Info->total_wake_time;
     sleep_data.total_rem_sleep_time = cywee_Inactivity_Accumulate_Time_Info->total_rem_sleep_time;
+    sleep_data.status_num = state_num;
+
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "total sleep time[%d], total mild sleep time[%d], total deep sleep time[%d], total wake time[%d], total rem sleep time[%d], status num[%d], status[%d], timestamp[%d]",
+        sleep_data.total_time, sleep_data.total_mild_sleep_time,
+        sleep_data.total_deep_sleep_time, sleep_data.total_wake_time, 
+        sleep_data.total_rem_sleep_time,sleep_data.status_num,
+        sleep_data.sleep_status[0].stat,sleep_data.sleep_status[1].timestamp);
 
     set_sleep_data(&sleep_data);
     memset(&sleep_data,0,sizeof(SLEEP_T));
+    state_num = 0;
     free(cywee_Inactivity_Accumulate_Time_Info);
     cywee_Inactivity_Accumulate_Time_Info = NULL;
 }