| 1 |
/* |
| 2 |
* Copyright (C) 2012-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "WeatherJob.h" |
| 10 |
|
| 11 |
#include "GUIUserMessages.h" |
| 12 |
#include "LangInfo.h" |
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "XBDateTime.h" |
| 15 |
#include "addons/AddonManager.h" |
| 16 |
#include "addons/addoninfo/AddonType.h" |
| 17 |
#include "guilib/GUIComponent.h" |
| 18 |
#include "guilib/GUIWindowManager.h" |
| 19 |
#include "guilib/LocalizeStrings.h" |
| 20 |
#include "interfaces/generic/ScriptInvocationManager.h" |
| 21 |
#include "network/Network.h" |
| 22 |
#include "settings/Settings.h" |
| 23 |
#include "settings/SettingsComponent.h" |
| 24 |
#include "settings/lib/Setting.h" |
| 25 |
#include "utils/POUtils.h" |
| 26 |
#include "utils/StringUtils.h" |
| 27 |
#include "utils/URIUtils.h" |
| 28 |
#include "utils/Variant.h" |
| 29 |
#include "utils/XTimeUtils.h" |
| 30 |
#include "utils/log.h" |
| 31 |
|
| 32 |
#define LOCALIZED_TOKEN_FIRSTID 370 |
| 33 |
#define LOCALIZED_TOKEN_LASTID 395 |
| 34 |
#define LOCALIZED_TOKEN_FIRSTID2 1350 |
| 35 |
#define LOCALIZED_TOKEN_LASTID2 1449 |
| 36 |
#define LOCALIZED_TOKEN_FIRSTID3 11 |
| 37 |
#define LOCALIZED_TOKEN_LASTID3 17 |
| 38 |
#define LOCALIZED_TOKEN_FIRSTID4 71 |
| 39 |
#define LOCALIZED_TOKEN_LASTID4 97 |
| 40 |
|
| 41 |
using namespace ADDON; |
| 42 |
|
| 43 |
using namespace std::chrono_literals; |
| 44 |
|
| 45 |
CWeatherJob::CWeatherJob(int location) |
| 46 |
{ |
| 47 |
m_location = location; |
| 48 |
} |
| 49 |
|
| 50 |
bool CWeatherJob::DoWork() |
| 51 |
{ |
| 52 |
// wait for the network |
| 53 |
if (!CServiceBroker::GetNetwork().IsAvailable()) |
| 54 |
return false; |
| 55 |
|
| 56 |
AddonPtr addon; |
| 57 |
if (!CServiceBroker::GetAddonMgr().GetAddon( |
| 58 |
CServiceBroker::GetSettingsComponent()->GetSettings()->GetString( |
| 59 |
CSettings::SETTING_WEATHER_ADDON), |
| 60 |
addon, AddonType::SCRIPT_WEATHER, OnlyEnabled::CHOICE_YES)) |
| 61 |
return false; |
| 62 |
|
| 63 |
// initialize our sys.argv variables |
| 64 |
std::vector<std::string> argv; |
| 65 |
argv.push_back(addon->LibPath()); |
| 66 |
|
| 67 |
std::string strSetting = std::to_string(m_location); |
| 68 |
argv.push_back(strSetting); |
| 69 |
|
| 70 |
// Download our weather |
| 71 |
CLog::Log(LOGINFO, "WEATHER: Downloading weather"); |
| 72 |
// call our script, passing the areacode |
| 73 |
int scriptId = -1; |
| 74 |
if ((scriptId = CScriptInvocationManager::GetInstance().ExecuteAsync(argv[0], addon, argv)) >= 0) |
| 75 |
{ |
| 76 |
while (true) |
| 77 |
{ |
| 78 |
if (!CScriptInvocationManager::GetInstance().IsRunning(scriptId)) |
| 79 |
break; |
| 80 |
KODI::TIME::Sleep(100ms); |
| 81 |
} |
| 82 |
|
| 83 |
SetFromProperties(); |
| 84 |
|
| 85 |
// and send a message that we're done |
| 86 |
CGUIMessage msg(GUI_MSG_NOTIFY_ALL,0,0,GUI_MSG_WEATHER_FETCHED); |
| 87 |
CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg); |
| 88 |
} |
| 89 |
else |
| 90 |
CLog::Log(LOGERROR, "WEATHER: Weather download failed!"); |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
const CWeatherInfo &CWeatherJob::GetInfo() const |
| 96 |
{ |
| 97 |
return m_info; |
| 98 |
} |
| 99 |
|
| 100 |
void CWeatherJob::LocalizeOverviewToken(std::string &token) |
| 101 |
{ |
| 102 |
// This routine is case-insensitive. |
| 103 |
std::string strLocStr; |
| 104 |
if (!token.empty()) |
| 105 |
{ |
| 106 |
ilocalizedTokens i; |
| 107 |
i = m_localizedTokens.find(token); |
| 108 |
if (i != m_localizedTokens.end()) |
| 109 |
{ |
| 110 |
strLocStr = g_localizeStrings.Get(i->second); |
| 111 |
} |
| 112 |
} |
| 113 |
if (strLocStr == "") |
| 114 |
strLocStr = token; //if not found, let fallback |
| 115 |
token = strLocStr; |
| 116 |
} |
| 117 |
|
| 118 |
void CWeatherJob::LocalizeOverview(std::string &str) |
| 119 |
{ |
| 120 |
std::vector<std::string> words = StringUtils::Split(str, " "); |
| 121 |
for (std::vector<std::string>::iterator i = words.begin(); i != words.end(); ++i) |
| 122 |
LocalizeOverviewToken(*i); |
| 123 |
str = StringUtils::Join(words, " "); |
| 124 |
} |
| 125 |
|
| 126 |
void CWeatherJob::FormatTemperature(std::string &text, double temp) |
| 127 |
{ |
| 128 |
CTemperature temperature = CTemperature::CreateFromCelsius(temp); |
| 129 |
text = StringUtils::Format("{:.0f}", temperature.To(g_langInfo.GetTemperatureUnit())); |
| 130 |
} |
| 131 |
|
| 132 |
void CWeatherJob::LoadLocalizedToken() |
| 133 |
{ |
| 134 |
// We load the english strings in to get our tokens |
| 135 |
std::string language = LANGUAGE_DEFAULT; |
| 136 |
std::shared_ptr<CSettingString> languageSetting = std::static_pointer_cast<CSettingString>(CServiceBroker::GetSettingsComponent()->GetSettings()->GetSetting(CSettings::SETTING_LOCALE_LANGUAGE)); |
| 137 |
if (languageSetting != NULL) |
| 138 |
language = languageSetting->GetDefault(); |
| 139 |
|
| 140 |
// Load the strings.po file |
| 141 |
CPODocument PODoc; |
| 142 |
if (PODoc.LoadFile(URIUtils::AddFileToFolder(CLangInfo::GetLanguagePath(language), "strings.po"))) |
| 143 |
{ |
| 144 |
int counter = 0; |
| 145 |
|
| 146 |
while (PODoc.GetNextEntry()) |
| 147 |
{ |
| 148 |
if (PODoc.GetEntryType() != ID_FOUND) |
| 149 |
continue; |
| 150 |
|
| 151 |
uint32_t id = PODoc.GetEntryID(); |
| 152 |
PODoc.ParseEntry(ISSOURCELANG); |
| 153 |
|
| 154 |
if (id > LOCALIZED_TOKEN_LASTID2) break; |
| 155 |
if ((LOCALIZED_TOKEN_FIRSTID <= id && id <= LOCALIZED_TOKEN_LASTID) || |
| 156 |
(LOCALIZED_TOKEN_FIRSTID2 <= id && id <= LOCALIZED_TOKEN_LASTID2) || |
| 157 |
(LOCALIZED_TOKEN_FIRSTID3 <= id && id <= LOCALIZED_TOKEN_LASTID3) || |
| 158 |
(LOCALIZED_TOKEN_FIRSTID4 <= id && id <= LOCALIZED_TOKEN_LASTID4)) |
| 159 |
{ |
| 160 |
if (!PODoc.GetMsgid().empty()) |
| 161 |
{ |
| 162 |
m_localizedTokens.insert(make_pair(PODoc.GetMsgid(), id)); |
| 163 |
counter++; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
CLog::Log(LOGDEBUG, "POParser: loaded {} weather tokens", counter); |
| 169 |
return; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
std::string CWeatherJob::ConstructPath(std::string in) // copy intended |
| 174 |
{ |
| 175 |
if (in.find('/') != std::string::npos || in.find('\\') != std::string::npos) |
| 176 |
return in; |
| 177 |
if (in.empty() || in == "N/A") |
| 178 |
in = "na.png"; |
| 179 |
|
| 180 |
return URIUtils::AddFileToFolder(ICON_ADDON_PATH, in); |
| 181 |
} |
| 182 |
|
| 183 |
void CWeatherJob::SetFromProperties() |
| 184 |
{ |
| 185 |
// Load in our tokens if necessary |
| 186 |
if (m_localizedTokens.empty()) |
| 187 |
LoadLocalizedToken(); |
| 188 |
|
| 189 |
CGUIWindow* window = CServiceBroker::GetGUI()->GetWindowManager().GetWindow(WINDOW_WEATHER); |
| 190 |
if (window) |
| 191 |
{ |
| 192 |
CDateTime time = CDateTime::GetCurrentDateTime(); |
| 193 |
m_info.lastUpdateTime = time.GetAsLocalizedDateTime(false, false); |
| 194 |
m_info.currentConditions = window->GetProperty("Current.Condition").asString(); |
| 195 |
m_info.currentIcon = ConstructPath(window->GetProperty("Current.OutlookIcon").asString()); |
| 196 |
LocalizeOverview(m_info.currentConditions); |
| 197 |
FormatTemperature(m_info.currentTemperature, |
| 198 |
strtod(window->GetProperty("Current.Temperature").asString().c_str(), nullptr)); |
| 199 |
FormatTemperature(m_info.currentFeelsLike, |
| 200 |
strtod(window->GetProperty("Current.FeelsLike").asString().c_str(), nullptr)); |
| 201 |
m_info.currentUVIndex = window->GetProperty("Current.UVIndex").asString(); |
| 202 |
LocalizeOverview(m_info.currentUVIndex); |
| 203 |
CSpeed speed = CSpeed::CreateFromKilometresPerHour(strtol(window->GetProperty("Current.Wind").asString().c_str(),0,10)); |
| 204 |
std::string direction = window->GetProperty("Current.WindDirection").asString(); |
| 205 |
if (direction == "CALM") |
| 206 |
m_info.currentWind = g_localizeStrings.Get(1410); |
| 207 |
else |
| 208 |
{ |
| 209 |
LocalizeOverviewToken(direction); |
| 210 |
m_info.currentWind = StringUtils::Format(g_localizeStrings.Get(434), direction, |
| 211 |
(int)speed.To(g_langInfo.GetSpeedUnit()), |
| 212 |
g_langInfo.GetSpeedUnitString()); |
| 213 |
} |
| 214 |
std::string windspeed = StringUtils::Format("{} {}", (int)speed.To(g_langInfo.GetSpeedUnit()), |
| 215 |
g_langInfo.GetSpeedUnitString()); |
| 216 |
window->SetProperty("Current.WindSpeed",windspeed); |
| 217 |
FormatTemperature(m_info.currentDewPoint, |
| 218 |
strtod(window->GetProperty("Current.DewPoint").asString().c_str(), nullptr)); |
| 219 |
if (window->GetProperty("Current.Humidity").asString().empty()) |
| 220 |
m_info.currentHumidity.clear(); |
| 221 |
else |
| 222 |
m_info.currentHumidity = |
| 223 |
StringUtils::Format("{}%", window->GetProperty("Current.Humidity").asString()); |
| 224 |
m_info.location = window->GetProperty("Current.Location").asString(); |
| 225 |
for (int i=0;i<NUM_DAYS;++i) |
| 226 |
{ |
| 227 |
std::string strDay = StringUtils::Format("Day{}.Title", i); |
| 228 |
m_info.forecast[i].m_day = window->GetProperty(strDay).asString(); |
| 229 |
LocalizeOverviewToken(m_info.forecast[i].m_day); |
| 230 |
strDay = StringUtils::Format("Day{}.HighTemp", i); |
| 231 |
FormatTemperature(m_info.forecast[i].m_high, |
| 232 |
strtod(window->GetProperty(strDay).asString().c_str(), nullptr)); |
| 233 |
strDay = StringUtils::Format("Day{}.LowTemp", i); |
| 234 |
FormatTemperature(m_info.forecast[i].m_low, |
| 235 |
strtod(window->GetProperty(strDay).asString().c_str(), nullptr)); |
| 236 |
strDay = StringUtils::Format("Day{}.OutlookIcon", i); |
| 237 |
m_info.forecast[i].m_icon = ConstructPath(window->GetProperty(strDay).asString()); |
| 238 |
strDay = StringUtils::Format("Day{}.Outlook", i); |
| 239 |
m_info.forecast[i].m_overview = window->GetProperty(strDay).asString(); |
| 240 |
LocalizeOverview(m_info.forecast[i].m_overview); |
| 241 |
} |
| 242 |
} |
| 243 |
} |