* @LastEditors: penglei 1624952103@qq.com
* @LastEditTime: 2024-06-28 15:51:01
* @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/include/SleepModel.h
- * @Description:
- *
- * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
+ * @Description:
+ *
+ * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
*/
/***
* @Date: 2024-06-25 10:34:10
#include <cstdint>
#include <map>
-//#include "AppViewIDs.h"
-//#include "UiConfig.h"
-//#include "sleep/include/SleepView.h"
-//#include "sleep/include/SleepControlPresenter.h"
+// #include "AppViewIDs.h"
+// #include "UiConfig.h"
+// #include "sleep/include/SleepView.h"
+// #include "sleep/include/SleepControlPresenter.h"
#include "string"
-
#define SLEEP_PATH "user/userdata/sleep/%s/sleep_%d.bin"
#define SLEEP_DIR_PATH "user/userdata/sleep/%s"
-
namespace OHOS
{
- #pragma pack(1)
+#pragma pack(1)
typedef struct
{
- uint32_t fall_asleep_timestamp; //入睡
- uint32_t wake_up_timestamp; //出睡
- uint16_t total_time; //总时长
+ uint32_t fall_asleep_timestamp; // 入睡
+ uint32_t wake_up_timestamp; // 出睡
+ uint16_t total_time; // 总时长
} NAP_T;
typedef struct
{
uint32_t timestamp; //
- uint8_t stat; //状态
-
+ 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; //总时长
- uint16_t total_deep_sleep_time;//深睡
- uint16_t total_mild_sleep_time;//浅睡
- uint16_t total_rem_sleep_time;//清醒
- uint16_t total_wake_time;//动眼
+ uint32_t fall_asleep_timestamp; // 入睡
+ uint32_t wake_up_timestamp; // 出睡
+ uint16_t total_time; // 总时长
+ uint16_t total_deep_sleep_time; // 深睡
+ uint16_t total_mild_sleep_time; // 浅睡
+ uint16_t total_rem_sleep_time; // 清醒
+ uint16_t total_wake_time; // 动眼
} SLEEP_T;
-
- #pragma pack()
- bool save_nap_data(NAP_T *data);
+#pragma pack()
- uint32_t read_sleep_nap_data(char *data,uint32_t size,FILE *fp);
-
+ bool save_nap_data(NAP_T *data);
+ 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);
-
bool save_sleep_data(SLEEP_T *data);
class SleepModel
{
public:
static SleepModel &GetInstance(void);
- void GetSleepTime(); // 获取睡眠时间
- std::string GetSleepStr(); // 解析睡眠时间
- void GetRestTime(); // 获取入睡时间
- std::string GetRestStr(); // 解析入睡时间
- void GetWalkTime(); // 获取醒来时间
- std::string GetWalkStr(); // 解析醒来时间
- void GetGoalTime(); // 获取目标休息时间(xx-xx)
- std::string GetGoalStr(); // 解析目标休息时间
+ void update_time_info(uint32_t timestamp, std::string &timeStr);
+ void update_sleep(SLEEP_T *data);
+ void set_sleep_goal();
+
+ int get_sleep_hour();
+ int get_sleep_min();
+ int get_sleep_goal();
+ int get_sleep_total();
+ std::string get_rest_str(); // 解析入睡时间
+ std::string get_walk_str(); // 解析醒来时间
+ std::string get_goal_str(); // 解析目标休息时间
private:
SleepModel();
SleepModel &operator=(const SleepModel &);
virtual ~SleepModel();
- std::string sleepStr;
- std::string restStr;
- std::string walkStr;
- std::string goalsleepStr;
- std::string goalwalkStr;
+ std::string sleepStr{"--"};
+ std::string walkStr{"--"};
+ std::string goalStr{"--"};
+
+ uint8_t sleep_total_time = 0;
+ uint8_t goal_total_time = 0;
};
}
* @LastEditors: penglei 1624952103@qq.com
* @LastEditTime: 2024-06-28 16:52:58
* @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp
- * @Description:
- *
- * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
+ * @Description:
+ *
+ * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
*/
#include "sleep/include/SleepModel.h"
#include "sleep/include/SleepControlPresenter.h"
#include "custom_utils.h"
#include <sys/stat.h>
-
-
-
+#include "UiConfig.h"
+#include "ui_resource_image.h"
+#include "samgr_lite.h"
+#include <vector>
+#include <sstream>
+#include <iomanip>
+#include <fstream>
+#include <iostream>
+#include <string>
+#include "wearable_log.h"
+#include "ConfigModel.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);
+extern "C" int get_wake_up_time_from_config_model();
namespace OHOS
{
- static NAP_T nap_data = {0};
- static SLEEP_T sleep_data = {0};
- static SLEEP_STATE_T sleep_status = {0};
+
+ static NAP_T nap_data = {0};
+ static SLEEP_T sleep_data = {0};
+ static SLEEP_STATE_T sleep_status = {0};
+
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');
+ 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);
+ 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)
{
if (mkdir(dir_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "mkdir fail %s",dir_path);
+ 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);
+ int ret = uapi_get_timestampe_ss(&creationTime);
- sprintf_s(file_path,64,SLEEP_PATH,day,creationTime);
+ sprintf_s(file_path, 64, SLEEP_PATH, day, creationTime);
FILE *fp;
- fp=fopen(file_path,"wb+");
- if(fp==NULL)
+ fp = fopen(file_path, "wb+");
+ if (fp == NULL)
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data fopen fail");
return false;
}
- uint8_t sleep_type=0;
- fwrite(&sleep_type,1,1,fp);
+ uint8_t sleep_type = 0;
+ fwrite(&sleep_type, 1, 1, fp);
uint32_t fall_asleep_timestamp = data->fall_asleep_timestamp;
- fwrite(&fall_asleep_timestamp,1,4,fp);
+ 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);
+ fwrite(&wake_up_timestamp, 1, 4, fp);
+ uint16_t total_time = data->total_time;
+ fwrite(&total_time, 1, 2, fp);
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "save_nap_sleep_data succ");
fclose(fp);
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));
+ {
+ memset(&nap_data, 0, sizeof(NAP_T));
+ memcpy_s(&nap_data, sizeof(NAP_T), data, sizeof(NAP_T));
save_nap_data(&nap_data);
}
- uint32_t read_sleep_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);
+ 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');
+ 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);
+ 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)
{
if (mkdir(dir_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "mkdir fail %s",dir_path);
+ 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);
+ int ret = uapi_get_timestampe_ss(&creationTime);
- sprintf_s(file_path,64,SLEEP_PATH,day,creationTime);
+ sprintf_s(file_path, 64, SLEEP_PATH, day, creationTime);
FILE *fp;
- fp=fopen(file_path,"wb+");
- if(fp==NULL)
+ 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);
+ fwrite(&sleep_type, 1, 1, fp);
uint32_t fall_asleep_timestamp = data->fall_asleep_timestamp;
- fwrite(&fall_asleep_timestamp,1,4,fp);
+ fwrite(&fall_asleep_timestamp, 1, 4, fp);
uint32_t wake_up_timestamp = data->wake_up_timestamp;
- fwrite(&wake_up_timestamp,1,4,fp);
+ fwrite(&wake_up_timestamp, 1, 4, fp);
- uint16_t total_time=data->total_time;
- fwrite(&total_time,1,2,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_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_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_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);
+ 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);
+ fwrite(data->sleep_status, sizeof(SLEEP_STATE_T), data->status_num, fp);
fclose(fp);
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));
+ {
+ 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::SleepModel()
+ {
+ set_sleep_goal();
}
-
- SleepModel::SleepModel() {}
SleepModel::~SleepModel() {}
SleepModel &SleepModel::GetInstance()
{
static SleepModel instance;
return instance;
}
+
+ void SleepModel::update_sleep(SLEEP_T *data)
+ {
+ if (data == NULL)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid input data");
+ return;
+ }
+
+ // 打印数据
+ 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);
+
+ // 更新睡眠总时间
+ sleep_total_time = data->total_time;
+
+ // 更新一下配置
+ set_sleep_goal();
+ }
+
+ void SleepModel::update_time_info(uint32_t timestamp, std::string &timeStr)
+ {
+ char labelTime[20];
+ if (uapi_get_TIME_STR(timestamp, labelTime, '.', false, false) == ERRCODE_SUCC)
+ {
+ timeStr = std::string(labelTime);
+ }
+ else
+ {
+ timeStr = "--";
+ }
+ }
+
+ /*
+ 设置睡眠目标
+ 前2个字节是显示睡眠小时
+ 后2个字节显示睡眠分钟
+ 需要单独解析出来。
+ */
+ void SleepModel::set_sleep_goal()
+ {
+ int goal_sleep = get_sleep_time_from_config_model();
+ int goal_walk = get_wake_up_time_from_config_model();
+
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "goal_sleep:%d goal_walk:%d", goal_sleep, goal_walk);
+
+ // 提取小时部分(前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;
+
+ std::string goalsleepStr = sleeposs.str();
+
+ // 提取小时部分(前2字节)
+ int goal_walk_hour = goal_walk >> 16;
+ // 提取分钟部分(后2字节)
+ int goal_walk_min = goal_walk & 0x00FF;
+
+ std::ostringstream walkoss;
+ walkoss << std::setfill('0') << std::setw(2) << goal_walk_hour << ":" << std::setw(2) << goal_walk_min;
+
+ std::string goalwalkStr = walkoss.str();
+
+ goalStr = goalsleepStr + "-" + goalwalkStr;
+ goal_total_time = goal_walk_hour * 60 + goal_walk_min;
+ }
+ // 获取睡眠总时长(小时)
+ 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;
+ }
+ // 解析入睡时间
+ std::string SleepModel::get_rest_str()
+ {
+ return sleepStr;
+ }
+ // 解析醒来时间
+ std::string SleepModel::get_walk_str()
+ {
+ return walkStr;
+ }
+ // 解析目标休息时间
+ std::string SleepModel::get_goal_str()
+ {
+ return goalStr;
+ }
}
\ No newline at end of file