#include "bloodoxygen/include/BloodResultPresenter.h"
#include "DialModelSPO2.h"
+#include "graphic_service.h"
#include "wearable_log.h"
-#include "heart_blood/health_service/health_service.h"
-
-#include "custom_utils.h"
-
-extern "C" errcode_t uapi_get_timestamp_ms(uint32_t *tm);
+#include "health/health_service/health_service.h"
#include "custom_utils.h"
#ifdef SUPPORT_PPG_SERVICE
+#include "ppg_hal.h"
#include "ppg_service.h"
#endif
-#include "ConfigModel.h"
#include "AppViewIDs.h"
+#include "ConfigModel.h"
+#include "MainViewSample.h"
#include "NativeAbility.h"
#include "battery_hal.h"
-#include "heart_blood/BloodCardView.h"
-#include "MainViewSample.h"
+#include "health/BloodCardView.h"
#include <custom_save_runtime.h>
#define MAX_UNWARE_COUNT 10
#define MAX_VALID_SPO2_COUNT 30
#define MAX_DISVALID_SPO2_COUNT 100
-namespace OHOS
-{
- /**
- * @brief Constructor for the BloodOxygenModel class.
- */
- BloodOxygenModel::BloodOxygenModel()
- {
- BloodOxygenData *data = HealthService::GetInstance()->ReadBloodOxygenData();
- if (data)
- {
- currentspo2 = data->SPO2;
- timestamp_ms = data->timestamp;
- // 更新表盘血氧数据
- DialModelSPO2::GetInstance()->UpdateSPO2(currentspo2);
- delete data;
- }
- else
- {
- currentspo2 = 0;
- timestamp_ms = 0;
- }
- }
- BloodOxygenModel::~BloodOxygenModel() {}
- BloodOxygenModel *BloodOxygenModel::GetInstance(void)
- {
- static BloodOxygenModel instance;
- return &instance;
- }
- /* ---------------------------------- 状态检测 ---------------------------------- */
+#define BLOOD_TIMER_DATA 1
- void BloodOxygenModel::ClearDisplayData()
- {
- // currentspo2 = 0;
- // timestamp_ms = 0;
- // 不需要清空
- }
+namespace OHOS {
+/**
+ * @brief Constructor for the BloodOxygenModel class.
+ */
+BloodOxygenModel::BloodOxygenModel() {
+ BloodOxygenData *data = HealthService::GetInstance()->ReadBloodOxygenData();
+ if (data) {
+ currentspo2 = data->SPO2;
+ timestamp_ms = data->timestamp;
+ // 更新表盘血氧数据
+ DialModelSPO2::GetInstance()->UpdateSPO2(currentspo2);
+ delete data;
+ } else {
+ currentspo2 = 0;
+ timestamp_ms = 0;
+ }
+}
+BloodOxygenModel::~BloodOxygenModel() {}
+BloodOxygenModel *BloodOxygenModel::GetInstance(void) {
+ static BloodOxygenModel instance;
+ return &instance;
+}
+/* ---------------------------------- 状态检测
+ * ---------------------------------- */
- // 血氧监控
- void BloodOxygenModel::Blood_Control(BloodState state)
- {
- if (state == BloodState::End && startspo2 == true)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "startspo2 End");
- uint16_t md = PPG_SERVICE_CMD_TYPE_STOP_SPO2_TEST;
+void BloodOxygenModel::ClearDisplayData() {
+ // currentspo2 = 0;
+ // timestamp_ms = 0;
+ // 不需要清空
+}
+
+// 血氧监控
+void BloodOxygenModel::Blood_Control(BloodState state) {
+ if (state == BloodState::End && startspo2 == true) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "startspo2 End");
+ uint16_t md = PPG_SERVICE_CMD_TYPE_STOP_SPO2_TEST;
#ifdef SUPPORT_PPG_SERVICE
- send_ppg_cmd_msg(md);
+ send_ppg_cmd_msg(md);
#endif
- startspo2 = false;
+ startspo2 = false;
#ifdef SYSTEM_LOG_SERVICE
- uapi_save_runtime_log(RUNTIME_LOG_OXYGEN_MEASURE_END);
+ uapi_save_runtime_log(RUNTIME_LOG_OXYGEN_MEASURE_END);
#endif
- }
- else if (state == BloodState::Start && startspo2 == false)
- {
- /*充电状态下不进行检测*/
- bool isCharging = get_charge_stat(); // 获取充电状态
- if (isCharging)
- {
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "充电不能检测");
- return;
- }
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "startspo2 Start");
- uint16_t md = PPG_SERVICE_CMD_TYPE_DO_SPO2_TEST;
+ blood_stop_data_timer();
+ } else if (state == BloodState::Start && startspo2 == false) {
+ /*充电状态下不进行检测*/
+ bool isCharging = get_charge_stat(); // 获取充电状态
+ if (isCharging) {
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "充电不能检测");
+ return;
+ }
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "startspo2 Start");
+ uint16_t md = PPG_SERVICE_CMD_TYPE_DO_SPO2_TEST;
#ifdef SUPPORT_PPG_SERVICE
- send_ppg_cmd_msg(md);
+ send_ppg_cmd_msg(md);
#endif
- startspo2 = true;
+ startspo2 = true;
#ifdef SYSTEM_LOG_SERVICE
- uapi_save_runtime_log(RUNTIME_LOG_OXYGEN_MEASURE);
+ uapi_save_runtime_log(RUNTIME_LOG_OXYGEN_MEASURE);
#endif
- }
- }
+ blood_data_timer();
+ }
+}
- uint16_t BloodOxygenModel::GetSPO2data()
- {
- return currentspo2;
- }
+uint16_t BloodOxygenModel::GetSPO2data() { return currentspo2; }
- uint16_t BloodOxygenModel::GetShowSPO2data()
- {
- return displayspo2;
- }
+uint16_t BloodOxygenModel::GetShowSPO2data() { return displayspo2; }
- uint32_t BloodOxygenModel::GetCheckTime()
- {
- return timestamp_ms;
- }
+uint32_t BloodOxygenModel::GetCheckTime() { return timestamp_ms; }
- void BloodOxygenModel::SetCheckTime(uint32_t time)
- {
- timestamp_ms = time;
- }
- void BloodOxygenModel::SetResult(BloodResult result)
- {
- bloodresult_ = result;
- }
- BloodResult BloodOxygenModel::GetResult()
- {
- return bloodresult_;
- }
+void BloodOxygenModel::SetCheckTime(uint32_t time) { timestamp_ms = time; }
+void BloodOxygenModel::SetResult(BloodResult result) { bloodresult_ = result; }
+BloodResult BloodOxygenModel::GetResult() { return bloodresult_; }
- // 开始检测
- void BloodOxygenModel::Blood_Start()
- {
- displayspo2 = 0;
- SetResult(BloodResult::NOT_TESTED); // 成功检测
- // 进行初始化操作
- if (!sensorstate)
- {
- Blood_Control(BloodState::Start);
- sensorstate = true;
- }
- }
+// 开始检测
+void BloodOxygenModel::Blood_Start() {
+ displayspo2 = 0;
+ SetResult(BloodResult::NOT_TESTED); // 成功检测
+ // 进行初始化操作
+ if (!sensorstate) {
+ Blood_Control(BloodState::Start);
+ sensorstate = true;
+ }
+}
- // 结束检测
- void BloodOxygenModel::Blood_Stop()
- {
- valid_counter_ = 0;
- disvalid_counter_ = 0;
- unwear_counter_ = 0;
+// 结束检测
+void BloodOxygenModel::Blood_Stop() {
+ valid_counter_ = 0;
+ disvalid_counter_ = 0;
+ unwear_counter_ = 0;
- if (sensorstate)
- {
- Blood_Control(BloodState::End);
- sensorstate = false;
- Blood_Result(); // 处理结果
- }
- }
+ if (sensorstate) {
+ Blood_Control(BloodState::End);
+ sensorstate = false;
+ Blood_Result(); // 处理结果
+ }
+}
- // 保存检测数据
- void BloodOxygenModel::Blood_Save()
- {
- uint32_t timestamp = BloodResultPresenter::GetInstance()->GetCurrentTime();
- SetCheckTime(timestamp);
+// 保存检测数据
+void BloodOxygenModel::Blood_Save() {
+ uint32_t timestamp = BloodResultPresenter::GetInstance()->GetCurrentTime();
+ SetCheckTime(timestamp);
#if MSG_SAVE_TEST
- BloodOxygenData data;
- data.SPO2 = currentspo2;
- data.timestamp = timestamp_ms;
- HealthService::GetInstance()->UpdateHealthData(data);
+ BloodOxygenData data;
+ data.SPO2 = currentspo2;
+ data.timestamp = timestamp_ms;
+ HealthService::GetInstance()->UpdateHealthData(data);
#else
- HealthService::GetInstance()->send_blood_save();
+ HealthService::GetInstance()->send_blood_save();
#endif
- }
+}
- // 显示检测结果// 显示检测结果
- void BloodOxygenModel::Blood_Result()
- {
- uint16_t MiniBlood = ConfigModel::GetInstance()->get_configData_int(ConfigModel::CONFIGMODEL_ITEM_OXYGEN_THRESHOLD_VALUE);
+// 显示检测结果// 显示检测结果
+void BloodOxygenModel::Blood_Result() {
+ uint16_t MiniBlood = ConfigModel::GetInstance()->get_configData_int(
+ ConfigModel::CONFIGMODEL_ITEM_OXYGEN_THRESHOLD_VALUE);
- // 检查当前血氧值并设置结果 需要检测成功
- if(bloodresult_ == BloodResult::SUCCESS){
- if (currentspo2 < MiniBlood && currentspo2 != 0)
- {
- // 测量结果小于最小血氧值
- bloodresult_ = BloodResult::LOW_RESULT;
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 低结果情况, currentspo2 = %d, MiniBlood = %d", currentspo2, MiniBlood);
- }
- }
+ // 检查当前血氧值并设置结果 需要检测成功
+ if (bloodresult_ == BloodResult::SUCCESS) {
+ if (currentspo2 < MiniBlood && currentspo2 != 0) {
+ // 测量结果小于最小血氧值
+ bloodresult_ = BloodResult::LOW_RESULT;
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+ "检测结果: 低结果情况, currentspo2 = %d, MiniBlood = %d",
+ currentspo2, MiniBlood);
+ }
+ }
- // 根据结果进行处理
- switch (bloodresult_)
- {
- case BloodResult::SUCCESS:
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 成功");
- Blood_Save();
- NativeAbility::GetInstance().Back(); // 成功
- break;
- }
- case BloodResult::NOT_WORN: // 未佩戴状态
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 未佩戴状态");
- NativeAbility::GetInstance().ChangeSlice(VIEW_CHECK_ABNORMAL);
- break;
- }
- case BloodResult::TIMEOUT: // 超时情况
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 超时情况");
- NativeAbility::GetInstance().ChangeSlice(VIEW_CHECK_ABNORMAL);
- break;
- }
- case BloodResult::LOW_RESULT: // 低结果情况
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 低结果情况");
- HealthState healthState;
- healthState.checkType = BLOOD_CHECK;
- healthState.resultStatus = LOW;
- healthState.checkResult = MiniBlood;
- // HealthService::GetInstance()->SetHealthCurrent(healthState);
- //保存异常数据
- Blood_Save();
- NativeAbility::GetInstance().ChangeSlice(VIEW_CHECK_INVALID);
- break;
- }
- case BloodResult::INVALID_DATA: // 无效数据情况
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 无效数据情况");
- NativeAbility::GetInstance().ChangeSlice(VIEW_CHECK_ABNORMAL);
- break;
- }
- default:
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 未知状态");
- break;
- }
- }
+ // 根据结果进行处理
+ switch (bloodresult_) {
+ case BloodResult::SUCCESS: {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 成功");
+ Blood_Save();
+ NativeAbility::GetInstance().Back(); // 成功
+ break;
+ }
+ case BloodResult::NOT_WORN: // 未佩戴状态
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 未佩戴状态");
+ NativeAbility::GetInstance().ChangeSlice(VIEW_CHECK_ABNORMAL);
+ break;
+ }
+ case BloodResult::TIMEOUT: // 超时情况
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 超时情况");
+ NativeAbility::GetInstance().ChangeSlice(VIEW_CHECK_ABNORMAL);
+ break;
+ }
+ case BloodResult::LOW_RESULT: // 低结果情况
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 低结果情况");
+ HealthState healthState;
+ healthState.checkType = BLOOD_CHECK;
+ healthState.resultStatus = LOW;
+ healthState.checkResult = MiniBlood;
+ // HealthService::GetInstance()->SetHealthCurrent(healthState);
+ //保存异常数据
+ Blood_Save();
+ NativeAbility::GetInstance().ChangeSlice(VIEW_CHECK_INVALID);
+ break;
+ }
+ case BloodResult::INVALID_DATA: // 无效数据情况
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 无效数据情况");
+ NativeAbility::GetInstance().ChangeSlice(VIEW_CHECK_ABNORMAL);
+ break;
+ }
+ default: {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 未知状态");
+ break;
+ }
+ }
+}
+// 更新佩戴状态
+void BloodOxygenModel::SetWearStatus(int32_t wear) {
+ // 设置佩戴状态
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BloodOxygenModel Weartest::%d", wear);
+ //开启检测才对佩戴进行监听
+ if (sensorstate) {
+ // 计数更新
+ if (wear_ != 1 && wear != 1) {
+ unwear_counter_++; // 佩戴错误计时器计数
+ } else {
+ unwear_counter_ = 0; // 佩戴正常情况下清零计数器
}
- // 更新佩戴状态
- void BloodOxygenModel::SetWearStatus(int32_t wear)
- {
- // 设置佩戴状态
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "BloodOxygenModel Weartest::%d", wear);
- //开启检测才对佩戴进行监听
- if (sensorstate)
- {
- // 计数更新
- if (wear_ != 1 && wear != 1)
- {
- unwear_counter_++; // 佩戴错误计时器计数
- }
- else
- {
- unwear_counter_ = 0; // 佩戴正常情况下清零计数器
- }
- wear_ = wear;
- // 检查是否达到了最大未佩戴计数限制
- if (unwear_counter_ >= MAX_UNWARE_COUNT)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 未佩戴计数最大 unwear_counter: %d", unwear_counter_);
- SetResult(BloodResult::NOT_WORN);
- Blood_Stop();
- }
- }
+ wear_ = wear;
+ // 检查是否达到了最大未佩戴计数限制
+ if (unwear_counter_ >= MAX_UNWARE_COUNT) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+ "检测结果: 未佩戴计数最大 unwear_counter: %d",
+ unwear_counter_);
+ SetResult(BloodResult::NOT_WORN);
+ Blood_Stop();
}
+ }
+}
- // 更新SPO2数据
- void BloodOxygenModel::UpSPO2data(uint8_t spo2)
- {
- displayspo2 = spo2;
+// 更新SPO2数据
+void BloodOxygenModel::UpSPO2data(uint8_t spo2) {
+ displayspo2 = spo2;
- // 处理有效与无效计数
- if (displayspo2 != 0)
- {
- currentspo2 = displayspo2; // 更新 currentspo2
- valid_counter_++; // 增加有效计数器
- disvalid_counter_ = 0; // 清空无效计数器
- }
- else
- {
- disvalid_counter_++; // 增加无效计数器
- valid_counter_ = 0; // 清零有效计数器
- }
+ // 处理有效与无效计数
+ if (displayspo2 != 0) {
+ currentspo2 = displayspo2; // 更新 currentspo2
+ valid_counter_++; // 增加有效计数器
+ disvalid_counter_ = 0; // 清空无效计数器
+ } else {
+ disvalid_counter_++; // 增加无效计数器
+ valid_counter_ = 0; // 清零有效计数器
+ }
- // 检查无效计数器是否达标
- if (disvalid_counter_ >= MAX_DISVALID_SPO2_COUNT)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 无效数达标 disvalid_counter: %d", disvalid_counter_);
- SetResult(BloodResult::INVALID_DATA); // 无效数达标
- Blood_Stop();
- }
+ // 检查无效计数器是否达标
+ if (disvalid_counter_ >= MAX_DISVALID_SPO2_COUNT) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+ "检测结果: 无效数达标 disvalid_counter: %d",
+ disvalid_counter_);
+ SetResult(BloodResult::INVALID_DATA); // 无效数达标
+ Blood_Stop();
+ }
- // 检查有效计数器是否达标
- if (valid_counter_ >= MAX_VALID_SPO2_COUNT)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检测结果: 成功 valid_counter: %d currentspo2: %d", valid_counter_, currentspo2);
- SetResult(BloodResult::SUCCESS); // 成功检测
- Blood_Stop();
- }
- }
+ // 检查有效计数器是否达标
+ if (valid_counter_ >= MAX_VALID_SPO2_COUNT) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+ "检测结果: 成功 valid_counter: %d currentspo2: %d",
+ valid_counter_, currentspo2);
+ SetResult(BloodResult::SUCCESS); // 成功检测
+ Blood_Stop();
+ }
+}
+
+// 更新血氧数据
+void BloodOxygenModel::CleanAllData(void) {
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Nextday!");
+ if (MainViewSample::GetInstance() != nullptr &&
+ BloodCardView::GetInstance() != nullptr) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "更新主界面血氧卡片");
+ BloodCardView::GetInstance()->UpSPO2Text();
+ }
+ if (BloodResultView::GetInstance() != nullptr) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "更新测量血氧卡片");
+ BloodResultView::GetInstance()->UpSPO2Text();
+ }
+}
- // 更新血氧数据
- void BloodOxygenModel::CleanAllData(void)
- {
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Nextday!");
- if (MainViewSample::GetInstance() != nullptr && BloodCardView::GetInstance() != nullptr)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "更新主界面血氧卡片");
- BloodCardView::GetInstance()->UpSPO2Text();
- }
- if (BloodResultView::GetInstance() != nullptr)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "更新测量血氧卡片");
- BloodResultView::GetInstance()->UpSPO2Text();
- }
+//////////////////////////////////////////////////////////////////////////////////
+#if BLOOD_TIMER_DATA
+// 声明定时器变量
+static osTimerId_t g_data_timer = nullptr;
+
+static void blood_data_callbackProc(int data) {
+ //更新佩戴
+ uint8_t wear = 0;
+#ifdef SUPPORT_PPG_SERVICE
+ wear = ppg_get_wear_status();
+ printf("blood_data_callbackProc::wear = %d\n", wear);
+#endif
+ OHOS::BloodOxygenModel::GetInstance()->SetWearStatus(wear);
+ //更新心率
+ uint8_t spo2 = 0;
+#ifdef SUPPORT_PPG_SERVICE
+ spo2 = ppg_get_spo2();
+ printf("blood_data_callbackProc::spo2 = %d\n", spo2);
+#endif
+ OHOS::BloodOxygenModel::GetInstance()->UpSPO2data(spo2);
+}
+
+static void blood_data_timer_callback(int data) {
+ GraphicService::GetInstance()->PostGraphicEvent(
+ std::bind(blood_data_callbackProc, data));
+}
+
+void BloodOxygenModel::blood_data_timer(uint32_t interval) {
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,
+ "BloodOxygenModel::blood_start_screen60s_timer");
+ if (g_data_timer != nullptr) {
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Timer already exists!!");
+ return;
+ }
+ g_data_timer = osTimerNew((osTimerFunc_t)blood_data_timer_callback,
+ osTimerPeriodic, nullptr, nullptr);
+ if (g_data_timer == nullptr) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer create failed!!");
+ return;
+ }
+ int tick = osMs2Tick(interval);
+ osStatus_t ret = osTimerStart(g_data_timer, tick);
+ if (ret != osOK) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!",
+ ret);
+ if (g_data_timer != nullptr) {
+ ret = osTimerDelete(g_data_timer);
}
+ g_data_timer = nullptr;
+ return;
+ }
}
+void BloodOxygenModel::blood_stop_data_timer(void) {
+ // 检查定时器是否存在
+ if (g_data_timer != nullptr) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+ "BloodOxygenModel::blood_stop_screen60s_timer");
+ osTimerStop(g_data_timer);
+ // 删除定时器并置为 nullptr
+ osStatus_t ret = osTimerDelete(g_data_timer);
+ if (ret != osOK) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+ "osTimerDelete failed, ret = %lx!!", ret);
+ }
+ g_data_timer = nullptr;
+ }
+}
+#endif
+} // namespace OHOS