* @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:
*
#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);
{
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)
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)
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)
{
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);
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;
}
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');
fclose(fp);
- osal_mutex_unlock(&sleep_mutex);
+ // osal_mutex_unlock(&sleep_mutex);
return true;
}
{
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);
}
// 配置睡眠数据
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()
}
}
/* -------------------------------------------------------------------------- */
+
+#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