From 5023e100413577ae385c56383384c5cfda125c5f Mon Sep 17 00:00:00 2001 From: xue <2144439972@qq.com> Date: Tue, 23 Sep 2025 19:39:17 +0800 Subject: [PATCH] =?utf8?q?=E4=BC=98=E5=8C=96ota=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeapp/nativeui/ota/src/OtaModel.cpp | 261 ++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 code/sdk/application/wearable/ln/nativeapp/nativeui/ota/src/OtaModel.cpp diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/ota/src/OtaModel.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/ota/src/OtaModel.cpp new file mode 100644 index 000000000..0baf1956f --- /dev/null +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/ota/src/OtaModel.cpp @@ -0,0 +1,261 @@ +/* + * @Author: Assistant + * @Date: 2024-12-19 + * @Description: OTA Model - 独立的OTA升级数据模型实现 + */ +#include "ota/include/OtaModel.h" +#include "wearable_log.h" +#include "ln_api.h" +#include "custom_utils.h" +#include "battery_hal.h" +#include "battery_service.h" +#include "custom_runtime.h" +#include +#include "ota/include/OtaView.h" +#include "ota/include/OtaUpdatingView.h" + +namespace OHOS +{ + static OtaModel *g_pOtaModel = nullptr; + + OtaModel::OtaModel() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::OtaModel"); + g_pOtaModel = this; + + m_otaStatus = 0; + m_otaProgress = 0; + m_countdownActive = false; + m_countdownValue = 0; + m_countdownTimer = nullptr; + } + + OtaModel::~OtaModel() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::~OtaModel"); + StopCountdownTimer(); + g_pOtaModel = nullptr; + } + + OtaModel *OtaModel::GetInstance() + { + if (g_pOtaModel == nullptr) { + g_pOtaModel = new OtaModel(); + } + return g_pOtaModel; + } + + void OtaModel::SetOtaStatus(int status) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::SetOtaStatus: %d", status); + m_otaStatus = status; + } + + int OtaModel::GetOtaStatus() const + { + return m_otaStatus; + } + + void OtaModel::SetOtaProgress(int progress) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::SetOtaProgress: %d", progress); + m_otaProgress = progress; + } + + int OtaModel::GetOtaProgress() const + { + return m_otaProgress; + } + + void OtaModel::SetCountdownActive(bool active) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::SetCountdownActive: %d", active); + m_countdownActive = active; + } + + bool OtaModel::IsCountdownActive() const + { + return m_countdownActive; + } + + void OtaModel::SetCountdownValue(int value) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::SetCountdownValue: %d", value); + m_countdownValue = value; + } + + int OtaModel::GetCountdownValue() const + { + return m_countdownValue; + } + + int OtaModel::StartOtaUpdate() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::StartOtaUpdate"); + + // 检查网络状态 + if (!HasNetwork()) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OtaModel::StartOtaUpdate: No network"); + return -1; + } + + // 检查电量 + if (!IsBatterySufficient()) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OtaModel::StartOtaUpdate: Low battery"); + return -2; + } + + // 检查内存 + if (!IsMemorySufficient()) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OtaModel::StartOtaUpdate: Insufficient memory"); + return -3; + } + + return 0; + } + bool OtaModel::HasNetwork() const + { + // 检查网络连接状态 + int networkStatus = uapi_dev_runtime_get_cat1_networkstat(); + return (networkStatus == 1); + } + + bool OtaModel::IsBatterySufficient() const + { + float voltage = 0.0F; + int Batteryvalue = 0; +#ifdef SUPPORT_BATTERY_SERVICE + Batteryvalue = battery_get_voltage(&voltage); +#endif + return (Batteryvalue >= 20); + } + + bool OtaModel::IsMemorySufficient() const + { + // 检查内存是否足够(至少100MB) + // TODO: 实现实际的内存检查 + // int freeMemory = get_free_memory(); + int freeMemory = 200; // 暂时返回200MB可用内存 + return (freeMemory >= 100); + } + + /** + * @brief 倒计时定时器回调函数 + */ + static void CountdownTimerCallback(uint8_t data) + { + GraphicService::GetInstance()->PostGraphicEvent( + std::bind([]() { + OtaModel::GetInstance()->CountdownCallback(); + })); + } + + /** + * @brief 启动倒计时定时器 + * @param startVal 倒计时初始值,默认5秒 + */ + void OtaModel::StartCountdownTimer(uint32_t startVal) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::StartCountdownTimer: startVal=%u", startVal); + + // 如果定时器已存在,先停止 + if (m_countdownTimer != nullptr) { + StopCountdownTimer(); + } + + // 设置倒计时状态 + m_countdownActive = true; + m_countdownValue = (int)startVal; + + // 创建定时器 + m_countdownTimer = osTimerNew((osTimerFunc_t)CountdownTimerCallback, osTimerPeriodic, nullptr, nullptr); + if (m_countdownTimer == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create countdown timer"); + m_countdownActive = false; + return; + } + + // 启动定时器,每秒触发一次 + osStatus_t ret = osTimerStart(m_countdownTimer, 1000); + if (ret != osOK) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to start countdown timer, ret=%d", ret); + osTimerDelete(m_countdownTimer); + m_countdownTimer = nullptr; + m_countdownActive = false; + return; + } + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Countdown timer started: %d seconds", m_countdownValue); + } + + /** + * @brief 停止倒计时定时器 + */ + void OtaModel::StopCountdownTimer() + { + if (m_countdownTimer != nullptr) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::StopCountdownTimer"); + osTimerStop(m_countdownTimer); + osTimerDelete(m_countdownTimer); + m_countdownTimer = nullptr; + } + + m_countdownActive = false; + m_countdownValue = 0; + } + + /** + * @brief 倒计时回调处理 + */ + void OtaModel::CountdownCallback() + { + if (!m_countdownActive) { + return; + } + + if (m_countdownValue <= 0) { + // 倒计时结束,停止定时器并跳转到升级界面 + m_countdownActive = false; + m_countdownValue = 0; + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OTA countdown completed, switching to updating view"); + + // 跳转到升级中界面 + if (OtaView::GetInstance()) { + do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_OTA_DOWNLOAD, NULL, 0); + OtaView::GetInstance()->ChangeView(OtaView::OTA_VIEW_COUNTDOWN, OtaView::OTA_VIEW_UPDATING); + } + + // 停止定时器 + StopCountdownTimer(); + } else { + // 更新倒计时值 + m_countdownValue--; + if (OtaCountdownView::GetInstance()) { + OtaCountdownView::GetInstance()->UpdateCountdown(m_countdownValue); + } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OTA countdown: %d", m_countdownValue); + + // 这里可以通知UI更新倒计时显示 + // 如果有倒计时界面的话,可以调用相应的更新方法 + } + } + //判断升级结果函数 + void OtaModel::CheckOtaResult(int status) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::CheckOtaResult"); + if(status == 3) + { + OtaView::GetInstance()->ChangeView(OtaView::OTA_VIEW_UPDATING, OtaView::OTA_VIEW_UPDATE_SUCCESS); + } else if(status == 4) { + OtaView::GetInstance()->ChangeView(OtaView::OTA_VIEW_UPDATING, OtaView::OTA_VIEW_UPDATE_FAILED); + } + } + void OtaModel::UpdateOtaProgress(int progress) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OtaModel::UpdateOtaProgress: %d", progress); + OtaUpdatingView::GetInstance()->UpdateProgress(progress); + } + + +} -- 2.17.1