mcu_hi3321_watch/tjd/ui/app/weather/TjdUiAppWeatherModel.cpp
2025-05-26 20:15:20 +08:00

278 lines
9.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "TjdUiAppWeatherModel.h"
#include "TjdUiAppWeatherPresenter.h"
#include "common/image_cache_manager.h"
#include "TjdUiMultiLanguageExt.h"
#include "TjdUiImageIds.h"
#include "ble_api.h"
#include "rtc_api.h"
#include "sql_weather.h"
#include "sys_config.h"
#include "unistd.h"
#include <iomanip>
#include <sstream>
#include <stdio.h>
#define ENABLE_PRINT_INFO 1
#if ENABLE_PRINT_INFO
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开
#define static_print_debug(...) sys_ui_log_d(__VA_ARGS__) // 调试信息打印
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#define static_print_debug(...)
#endif
#define MAIN_WEATHER_BJ_BIN_PATH TJD_IMAGE_PATH "img_weather_bj.bin"
#define MAIN_WEATHER_COMMON_BIN_PATH TJD_IMAGE_PATH "img_weather_common.bin"
using namespace OHOS;
namespace TJD {
static const int32_t WAIT_VIDEO_EXIT_SUCCESS_MS = 10;
static const int32_t REQUEST_BUFFER_REPET_COUNT = 10;
static const uint32_t WAIT_VIDEO_EXIT_MAX_RETRY_COUNT = 100;
uint16_t GetDayOfWeekStr(int weekday)
{
// 根据weekday的值返回对应的星期几的字符串
switch (weekday) {
case 0:
return STR_ID_62;
case 1:
return STR_ID_56;
case 2:
return STR_ID_57;
case 3:
return STR_ID_58;
case 4:
return STR_ID_59;
case 5:
return STR_ID_60;
case 6:
return STR_ID_61;
default:
return STR_ID_645;
}
}
int getDayOfWeek(int year, int month, int day)
{
// 使用tm结构体表示日期
struct tm t = {};
t.tm_year = year - 1900; // 注意tm_year是从1900年开始计算的
t.tm_mon = month - 1; // 注意tm_mon是从0开始的
t.tm_mday = day;
// 使用mktime函数将tm结构体转换为time_t类型这个函数会自动处理闰年等问题
time_t rawtime = mktime(&t);
// 使用localtime函数将time_t类型转换为本地时间的tm结构体
struct tm *timeinfo = localtime(&rawtime);
// 获取星期几weekday是从0星期日到6星期六
int weekday = timeinfo->tm_wday;
return weekday;
}
WeatherType SetCurWeather(uint8_t value)
{
switch (value) {
case 0x01:
return SUNNY;
case 0x02:
return PARTLY_CLOUDY;
case 0x03:
return CLOUDY;
case 0x04:
return LIGHT_RAIN;
case 0x05:
return MODERATE_RAIN;
case 0x06:
return HEAVY_RAIN;
case 0x07:
return SHOWER;
case 0x08:
return THUNDERSTORM;
case 0x09:
return SNOWY;
case 0x0A:
return FOGGY;
case 0x0B:
return DUSTSTORM;
case 0x0C:
return TYPHOON;
default:
return UNKNOW_WEATHER;
}
}
TjdUiAppWeatherModel::TjdUiAppWeatherModel()
{
weartherTypeRes_.reset(new int[WeatherInfo::SQL_WEATHER_TYPE_MAX]);
weartherTypeRes_[WeatherInfo::SQL_SUNNY] = IMG_WEATHER_COMMON_W_ICON_SUNNY;
weartherTypeRes_[WeatherInfo::SQL_PARTLY_CLOUDY] = IMG_WEATHER_COMMON_W_ICON_PARTLY_CLOUDY;
weartherTypeRes_[WeatherInfo::SQL_CLOUDY] = IMG_WEATHER_COMMON_W_ICON_CLOUDY;
weartherTypeRes_[WeatherInfo::SQL_LIGHT_RAIN] = IMG_WEATHER_COMMON_W_ICON_LIGHT_RAIN;
weartherTypeRes_[WeatherInfo::SQL_MODERATE_RAIN] = IMG_WEATHER_COMMON_W_ICON_MODERATE_RAIN;
weartherTypeRes_[WeatherInfo::SQL_HEAVY_RAIN] = IMG_WEATHER_COMMON_W_ICON_HEAVY_RAIN;
weartherTypeRes_[WeatherInfo::SQL_SHOWER] = IMG_WEATHER_COMMON_W_ICON_SHOWER;
weartherTypeRes_[WeatherInfo::SQL_THUNDERSTORM] = IMG_WEATHER_COMMON_W_ICON_THUNDERSTORM;
weartherTypeRes_[WeatherInfo::SQL_SNOWY] = IMG_WEATHER_COMMON_W_ICON_SNOWY;
weartherTypeRes_[WeatherInfo::SQL_FOGGY] = IMG_WEATHER_COMMON_W_ICON_FOGGY;
weartherTypeRes_[WeatherInfo::SQL_DUSTSTORM] = IMG_WEATHER_COMMON_W_ICON_DUST_STORM;
weartherTypeRes_[WeatherInfo::SQL_TYPHOON] = IMG_WEATHER_COMMON_W_ICON_TYPHOON;
}
TjdUiAppWeatherModel &TjdUiAppWeatherModel::GetInstance(void)
{
static TjdUiAppWeatherModel instance;
return instance;
}
uint16_t TjdUiAppWeatherModel::GetCurWeatherType(void) { return sql_weather_get_forecast_protocol_value(0); }
bool TjdUiAppWeatherModel::GetDataValid(void) //目前暂且认为当日出和日落时间都存在时,数据有效
{
uint16_t sunrise_time = sql_weather_get_sunrise_time();
uint16_t sunset_time = sql_weather_get_sunset_time();
printf("sunrise_time:%d sunset_time:%d\n", sunrise_time, sunset_time);
if (sunrise_time && sunset_time) {
return true;
}
return false;
}
void TjdUiAppWeatherModel::LoadCurWeatherData(void)
{
memcpy(location, sql_weather_get_location_txt(), 32);
update_hour = sql_weather_get_update_hour();
update_minute = sql_weather_get_update_minute();
now_temperature = sql_weather_get_now_temperature();
now_humidity = sql_weather_get_humidity();
now_wind_speed = sql_weather_get_wind_speed();
weather = sql_weather_get_forecast_protocol_value(0);
temperature_max = sql_weather_get_forecast_temperature_max(0);
temperature_min = sql_weather_get_forecast_temperature_min(0);
}
// 加载一周天气,返回可获取的预报天数
int8_t TjdUiAppWeatherModel::LoadWeekWeatherInfo(PreWeekWeather *preWeekWeatherInfo)
{
rtc_time time;
struct rtc_class_ops *handle = tjd_driver_rtc_get_ops();
handle->get_rtc_time(&time);
uint8_t dayOfWeek = time.tm_wday;
uint8_t numCount = sql_weather_get_forecast_count();
printf("numCount:%d\n", numCount);
std::string suffix = "/";
uint8_t weatherInfo = 0x00;
for (int i = 0; i < numCount; i++) {
if (i == 0) {
preWeekWeatherInfo[i].weekId = STR_ID_251;
static_print_debug("today is %d", GetDayOfWeekStr(dayOfWeek));
} else {
dayOfWeek = dayOfWeek > SATURDAY ? SUNDAY : dayOfWeek;
preWeekWeatherInfo[i].weekId = GetDayOfWeekStr(dayOfWeek);
}
dayOfWeek++;
int8_t tempMin = sql_weather_get_forecast_temperature_min(i);
int8_t tempMax = sql_weather_get_forecast_temperature_max(i);
tempMin = tempMin < -99 ? 0 : (tempMin > 99 ? 0 : tempMin);
tempMax = tempMax < -99 ? 0 : (tempMax > 99 ? 0 : tempMax);
static_print_debug("sql_weather_get temperature: %d~%d", tempMin, tempMax);
std::string result = std::to_string(tempMin) + suffix + std::to_string(tempMax);
preWeekWeatherInfo[i].tempStr = result;
weatherInfo = sql_weather_get_forecast_protocol_value(i);
static_print_debug("sql_weather_get weatherInfo: %x", weatherInfo);
WeatherType curWeather = SetCurWeather(weatherInfo);
ImageInfo *imgInfo = nullptr;
if (curWeather == UNKNOW_WEATHER) {
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_WEATHER_COMMON_W_ICON_UNKNOW,
MAIN_WEATHER_COMMON_BIN_PATH);
} else {
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(weartherTypeRes_[weatherInfo],
MAIN_WEATHER_COMMON_BIN_PATH);
}
preWeekWeatherInfo[i].iconSrc = imgInfo;
}
return numCount;
}
bool TjdUiAppWeatherModel::GetBtLinkStatus(void) { return tjd_get_ble_is_connect(); }
bool TjdUiAppWeatherModel::IsNeedUpdateWeather(void)
{
printf("tjd_ble_get_weather_today_synchronization:%d\n", tjd_ble_get_weather_today_synchronization());
if (tjd_ble_get_weather_today_synchronization())
return false;
else
return true;
}
std::string TjdUiAppWeatherModel::GetSunRiseTime(void)
{
uint16_t total_min = sql_weather_get_sunrise_time();
static_print_debug("sql_weather_get_sunrise_time:%d", total_min);
uint8_t hour = 0;
uint8_t minte = 0;
if (total_min > 24 * 60) {
hour = 6;
minte = 0;
} else {
hour = total_min / 60;
minte = total_min % 60;
}
static_print_debug("hour:%d minte:%d", hour, minte);
char buffer[10] = {0};
snprintf(buffer, sizeof(buffer), "%02d:%02d", hour, minte);
return std::string(buffer);
}
std::string TjdUiAppWeatherModel::GetSunSetTime(void)
{
uint16_t total_min = sql_weather_get_sunset_time();
static_print_debug("sql_weather_get_sunset_time:%d", total_min);
uint8_t hour = 0;
uint8_t minte = 0;
if (total_min > 24 * 60) {
hour = 18;
minte = 0;
} else {
hour = total_min / 60;
minte = total_min % 60;
}
static_print_debug("hour:%d minte:%d", hour, minte);
char buffer[10] = {0};
snprintf(buffer, sizeof(buffer), "%02d:%02d", hour, minte);
return std::string(buffer);
}
rtc_time TjdUiAppWeatherModel::GetRTCTime(void)
{
static rtc_time time;
memset(&time, 0, sizeof(rtc_time));
rtc_class_ops *handle = tjd_driver_rtc_get_ops();
handle->get_rtc_time(&time);
return time;
}
} // namespace TJD