[weather] Dump old code for day/night calculation by time dataengine

Sync querying of the time dataengine is no longer possible,
also is the calculation of the elevation not depending on other data
then those passed in, so this is not really data hold by the time
data engine. Instead one would calculate the values needed on the fly.

Yet to solve: how to make direct use of the solarsystem code currently
part of the time dataengine only.
parent b5ed2234
......@@ -31,11 +31,12 @@
#include <QJsonObject>
#include <QJsonArray>
#include <QXmlStreamReader>
#include <QTimeZone>
WeatherData::WeatherData()
: obsTime()
, longitude(qQNaN())
, latitude(qQNaN())
: stationLatitude(qQNaN())
, stationLongitude(qQNaN())
, condition()
, temperature_C(qQNaN())
, windSpeed_miles(qQNaN())
......@@ -56,8 +57,6 @@ UKMETIon::UKMETIon(QObject *parent, const QVariantList &args)
: IonInterface(parent, args)
{
// not used while daytime not considered, see below
// m_timeEngine = dataEngine("time");
setInitialized(true);
}
......@@ -592,18 +591,49 @@ void UKMETIon::parseWeatherObservation(const QString& source, WeatherData& data,
QString conditionData = conditionString.mid(splitIndex + 1); // Skip ':'
data.obsTime = conditionString.left(splitIndex);
//TODO: timezone parsing is not yet supported by QDateTime
#if 0
if (data.obsTime.contains('-')) {
if (data.obsTime.contains(QLatin1Char('-'))) {
// Saturday - 13:00 CET
// Saturday - 12:00 GMT
m_dateFormat = QDateTime::fromString(data.obsTime.section('-', 1, 1).trimmed(), "hh:mm ZZZ");
} else {
#endif
m_dateFormat = QDateTime();
#if 0
// timezone parsing is not yet supported by QDateTime, also is there just a dayname
// so try manually
// guess date from day
const QString dayString = data.obsTime.section(QLatin1Char('-'), 0, 0).trimmed();
QDate date = QDate::currentDate();
const QString dayFormat = QStringLiteral("dddd");
const int testDayJumps[4] = {
-1, // first to weekday yesterday
2, // then to weekday tomorrow
-3, // then to weekday before yesterday, not sure if such day offset can happen?
4, // then to weekday after tomorrow, not sure if such day offset can happen?
};
const int dayJumps = sizeof(testDayJumps)/sizeof(testDayJumps[0]);
QLocale cLocale = QLocale::c();
int dayJump = 0;
while (true) {
if (cLocale.toString(date, dayFormat) == dayString) {
break;
}
if (dayJump >= dayJumps) {
// no weekday found near-by, set date invalid
date = QDate();
break;
}
date = date.addDays(testDayJumps[dayJump]);
++dayJump;
}
if (date.isValid()) {
const QString timeString = data.obsTime.section(QLatin1Char('-'), 1, 1).trimmed();
const QTime time = QTime::fromString(timeString.section(QLatin1Char(' '), 0, 0), QStringLiteral("hh:mm"));
const QTimeZone timeZone = QTimeZone(timeString.section(QLatin1Char(' '), 1, 1).toUtf8());
// TODO: if non-IANA timezone id is not known, try to guess timezone from other data
if (time.isValid() && timeZone.isValid()) {
data.observationDateTime = QDateTime(date, time, timeZone);
}
}
}
#endif
if (conditionData.contains(QLatin1Char(','))) {
data.condition = conditionData.section(QLatin1Char(','), 0, 0).trimmed();
......@@ -652,15 +682,15 @@ void UKMETIon::parseWeatherObservation(const QString& source, WeatherData& data,
} else if (elementName == QLatin1String("lat")) {
const QString ordinate = xml.readElementText();
data.latitude = ordinate.toDouble();
data.stationLatitude = ordinate.toDouble();
} else if (elementName == QLatin1String("long")) {
const QString ordinate = xml.readElementText();
data.longitude = ordinate.toDouble();
data.stationLongitude = ordinate.toDouble();
} else if (elementName == QLatin1String("point") &&
xml.namespaceUri() == QLatin1String("http://www.georss.org/georss")) {
const QStringList ordinates = xml.readElementText().split(QLatin1Char(' '));
data.latitude = ordinates[0].toDouble();
data.longitude = ordinates[1].toDouble();
data.stationLatitude = ordinates[0].toDouble();
data.stationLongitude = ordinates[1].toDouble();
} else {
parseUnknownElement(xml);
}
......@@ -835,33 +865,28 @@ void UKMETIon::updateWeather(const QString& source)
}
// qCDebug(IONENGINE_BBCUKMET) << "i18n condition string: " << i18nc("weather condition", weatherData.condition.toUtf8().data());
const double lati = weatherData.latitude;
const double longi = weatherData.longitude;
//TODO: Port to Plasma5, needs also fix of m_dateFormat estimation
const bool stationCoordsValid = (!qIsNaN(weatherData.stationLatitude) && !qIsNaN(weatherData.stationLongitude));
if (stationCoordsValid) {
data.insert(QStringLiteral("Latitude"), weatherData.stationLatitude);
data.insert(QStringLiteral("Longitude"), weatherData.stationLongitude);
}
bool useDayIcon = true;
// TODO: get timeengine's solarsystem code to use directly
#if 0
if (m_dateFormat.isValid()) {
const Plasma::DataEngine::Data timeData = m_timeEngine->query(
QString("Local|Solar|Latitude=%1|Longitude=%2|DateTime=%3")
.arg(lati).arg(longi).arg(m_dateFormat.toString(Qt::ISODate)));
if (weatherData.observationDateTime.isValid() && stationCoordsValid) {
PlasmaWeather::Sun sun;
sun.setPosition(weatherData.stationLatitude, weatherData.stationLongitude);
const int offset = weatherData.observationDateTime.timeZone().offsetFromUtc(weatherData.observationDateTime);
sun.calcForDateTime(weatherData.observationDateTime, offset);
const auto elevation = sun.calcElevation();
// Tell applet which icon to use for conditions and provide mapping for condition type to the icons to display
if (timeData["Corrected Elevation"].toDouble() >= 0.0) {
//qCDebug(IONENGINE_BBCUKMET) << "Using daytime icons\n";
data.insert("Condition Icon", getWeatherIcon(dayIcons(), weatherData.condition));
} else {
data.insert("Condition Icon", getWeatherIcon(nightIcons(), weatherData.condition));
}
} else {
#endif
data.insert(QStringLiteral("Condition Icon"), getWeatherIcon(dayIcons(), weatherData.condition));
#if 0
useDayIcon = (elevation >= 0.0);
}
#endif
if (!qIsNaN(lati) && !qIsNaN(longi)) {
data.insert(QStringLiteral("Latitude"), lati);
data.insert(QStringLiteral("Longitude"), longi);
}
data.insert(QStringLiteral("Condition Icon"), getWeatherIcon(useDayIcon ? dayIcons() : nightIcons(), weatherData.condition));
if (!qIsNaN(weatherData.humidity)) {
data.insert(QStringLiteral("Humidity"), weatherData.humidity);
......
......@@ -44,10 +44,12 @@ public:
QString place;
QString stationName;
double stationLatitude;
double stationLongitude;
// Current observation information.
QString obsTime;
double longitude;
double latitude;
QDateTime observationDateTime;
QString condition;
QString conditionIcon;
......@@ -162,9 +164,6 @@ private:
QHash<KJob *, QXmlStreamReader *> m_forecastJobXml;
QHash<KJob *, QString> m_forecastJobList;
Plasma::DataEngine *m_timeEngine;
QDateTime m_dateFormat;
QStringList m_sourcesToReset;
};
......
......@@ -31,8 +31,8 @@
#include <QTimeZone>
WeatherData::WeatherData()
: stationLat(qQNaN())
, stationLon(qQNaN())
: stationLatitude(qQNaN())
, stationLongitude(qQNaN())
, temperature(qQNaN())
, dewpoint(qQNaN())
, windchill(qQNaN())
......@@ -66,8 +66,6 @@ EnvCanadaIon::EnvCanadaIon(QObject *parent, const QVariantList &args)
{
// Get the real city XML URL so we can parse this
getXMLSetup();
// not used while daytime not considered, see below
// m_timeEngine = dataEngine("time");
}
void EnvCanadaIon::deleteForecasts()
......@@ -795,17 +793,17 @@ void EnvCanadaIon::parseDateTime(WeatherData& data, QXmlStreamReader& xml, Weath
}
} else if (dateType == QLatin1String("observation")) {
xml.readElementText();
QDateTime obsDateTime = QDateTime::fromString(selectTimeStamp, QStringLiteral("yyyyMMddHHmmss"));
QDateTime observationDateTime = QDateTime::fromString(selectTimeStamp, QStringLiteral("yyyyMMddHHmmss"));
QTimeZone timeZone = QTimeZone(dateZone.toUtf8());
// if timezone id not recognized, fallback to utcoffset
if (!timeZone.isValid()) {
timeZone = QTimeZone(dateUtcOffset.toInt() * 3600);
}
if (obsDateTime.isValid() && timeZone.isValid()) {
data.obsDateTime = obsDateTime;
data.obsDateTime.setTimeZone(timeZone);
if (observationDateTime.isValid() && timeZone.isValid()) {
data.observationDateTime = observationDateTime;
data.observationDateTime.setTimeZone(timeZone);
}
data.obsTimestamp = obsDateTime.toString(QStringLiteral("dd.MM.yyyy @ hh:mm"));
data.obsTimestamp = observationDateTime.toString(QStringLiteral("dd.MM.yyyy @ hh:mm"));
} else if (dateType == QLatin1String("forecastIssue")) {
data.forecastTimestamp = xml.readElementText();
} else if (dateType == QLatin1String("sunrise")) {
......@@ -889,8 +887,8 @@ void EnvCanadaIon::parseConditions(WeatherData& data, QXmlStreamReader& xml)
data.condition = i18n("N/A");
data.humidex.clear();
data.stationID = i18n("N/A");
data.stationLat = qQNaN();
data.stationLon = qQNaN();
data.stationLatitude = qQNaN();
data.stationLongitude = qQNaN();
data.pressure = qQNaN();
data.visibility = qQNaN();
data.humidity = qQNaN();
......@@ -907,8 +905,8 @@ void EnvCanadaIon::parseConditions(WeatherData& data, QXmlStreamReader& xml)
if (elementName == QLatin1String("station")) {
data.stationID = xml.attributes().value(QStringLiteral("code")).toString();
QRegularExpression dumpDirection(QStringLiteral("[^0-9.]"));
data.stationLat = xml.attributes().value(QStringLiteral("lat")).toString().remove(dumpDirection).toDouble();
data.stationLon = xml.attributes().value(QStringLiteral("lon")).toString().remove(dumpDirection).toDouble();
data.stationLatitude = xml.attributes().value(QStringLiteral("lat")).toString().remove(dumpDirection).toDouble();
data.stationLongitude = xml.attributes().value(QStringLiteral("lon")).toString().remove(dumpDirection).toDouble();
} else if (elementName == QLatin1String("dateTime")) {
parseDateTime(data, xml);
} else if (elementName == QLatin1String("condition")) {
......@@ -1394,9 +1392,11 @@ void EnvCanadaIon::updateWeather(const QString& source)
data.insert(QStringLiteral("Station"), weatherData.stationID.isEmpty() ? i18n("N/A") : weatherData.stationID.toUpper());
if (!qIsNaN(weatherData.stationLat) && !qIsNaN(weatherData.stationLon)) {
data.insert(QStringLiteral("Latitude"), weatherData.stationLat);
data.insert(QStringLiteral("Longitude"), weatherData.stationLon);
const bool stationCoordValid = (!qIsNaN(weatherData.stationLatitude) && !qIsNaN(weatherData.stationLongitude));
if (stationCoordValid) {
data.insert(QStringLiteral("Latitude"), weatherData.stationLatitude);
data.insert(QStringLiteral("Longitude"), weatherData.stationLongitude);
}
// Real weather - Current conditions
......@@ -1406,34 +1406,33 @@ void EnvCanadaIon::updateWeather(const QString& source)
}
//qCDebug(IONENGINE_ENVCAN) << "i18n condition string: " << qPrintable(condition(source));
// Tell applet which icon to use for conditions and provide mapping for condition type to the icons to display
QMap<QString, ConditionIcons> conditionList;
conditionList = conditionIcons();
//TODO: Port to Plasma5
bool useDayIcon = true;
// TODO: get timeengine's solarsystem code to use directly
#if 0
const double lati = latitude(source).remove(QRegExp("[^0-9.]")).toDouble();
const double longi = longitude(source).remove(QRegExp("[^0-9.]")).toDouble();
const Plasma::DataEngine::Data timeData = m_timeEngine->query(
QString("Local|Solar|Latitude=%1|Longitude=%2")
.arg(lati).arg(-1 * longi));
if (weatherData.observationDateTime.isValid() && stationCoordValid) {
PlasmaWeather::Sun sun;
sun.setPosition(weatherData.stationLatitude, weatherData.stationLongitude);
const int offset = weatherData.observationDateTime.timeZone().offsetFromUtc(weatherData.observationDateTime);
sun.calcForDateTime(weatherData.observationDateTime, offset);
const auto elevation = sun.calcElevation();
// Tell applet which icon to use for conditions and provide mapping for condition type to the icons to display
useDayIcon = (elevation >= 0.0);
}
#endif
QMap<QString, ConditionIcons> conditionList = conditionIcons();
if (timeData["Corrected Elevation"].toDouble() < 0.0) {
if (!useDayIcon) {
conditionList.insert(QStringLiteral("decreasing cloud"), FewCloudsNight);
conditionList.insert(QStringLiteral("mostly cloudy"), PartlyCloudyNight);
conditionList.insert(QStringLiteral("partly cloudy"), PartlyCloudyNight);
conditionList.insert(QStringLiteral("fair"), FewCloudsNight);
//qCDebug(IONENGINE_ENVCAN) << "Before sunrise/After sunset - using night icons\n";
} else {
#endif
conditionList.insert(QStringLiteral("decreasing cloud"), FewCloudsDay);
conditionList.insert(QStringLiteral("mostly cloudy"), PartlyCloudyDay);
conditionList.insert(QStringLiteral("partly cloudy"), PartlyCloudyDay);
conditionList.insert(QStringLiteral("fair"), FewCloudsDay);
//qCDebug(IONENGINE_ENVCAN) << "Using daytime icons\n";
#if 0
}
#endif
data.insert(QStringLiteral("Condition Icon"), getWeatherIcon(conditionList, weatherData.condition));
......
......@@ -76,12 +76,12 @@ public:
QString cityName;
QString regionName;
QString stationID;
double stationLat;
double stationLon;
double stationLatitude;
double stationLongitude;
// Current observation information.
QString obsTimestamp;
QDateTime obsDateTime;
QDateTime observationDateTime;
QString condition;
float temperature;
......@@ -225,7 +225,6 @@ private:
QHash<KJob *, QString> m_jobList;
QStringList m_sourcesToReset;
QXmlStreamReader m_xmlSetup;
Plasma::DataEngine *m_timeEngine;
bool emitWhenSetup;
......
......@@ -28,11 +28,12 @@
#include <KLocalizedString>
#include <QLocale>
#include <QTimeZone>
WeatherData::WeatherData()
: stationLat(qQNaN())
, stationLon(qQNaN())
: stationLatitude(qQNaN())
, stationLongitude(qQNaN())
, temperature_F(qQNaN())
, temperature_C(qQNaN())
, humidity(qQNaN())
......@@ -90,9 +91,6 @@ NOAAIon::NOAAIon(QObject *parent, const QVariantList &args)
{
// Get the real city XML URL so we can parse this
getXMLSetup();
// not used while daytime not considered, see below
// m_timeEngine = dataEngine(QStringLiteral("time"));
}
void NOAAIon::reset()
......@@ -406,9 +404,9 @@ void NOAAIon::parseWeatherSite(WeatherData& data, QXmlStreamReader& xml)
} else if (elementName == QLatin1String("station_id")) {
data.stationID = xml.readElementText();
} else if (elementName == QLatin1String("latitude")) {
parseDouble(data.stationLat, xml);
parseDouble(data.stationLatitude, xml);
} else if (elementName == QLatin1String("longitude")) {
parseDouble(data.stationLon, xml);
parseDouble(data.stationLongitude, xml);
} else if (elementName == QLatin1String("observation_time_rfc822")) {
data.observationDateTime = QDateTime::fromString(xml.readElementText(), Qt::RFC2822Date);
} else if (elementName == QLatin1String("observation_time")) {
......@@ -516,11 +514,11 @@ void NOAAIon::updateWeather(const QString& source)
data.insert(QStringLiteral("Place"), weatherData.locationName);
data.insert(QStringLiteral("Station"), weatherData.stationID);
const double lat = weatherData.stationLat;
const double lon = weatherData.stationLon;
if (!qIsNaN(lat) && !qIsNaN(lon)) {
data.insert(QStringLiteral("Latitude"), lat);
data.insert(QStringLiteral("Longitude"), lon);
const bool stationCoordValid = (!qIsNaN(weatherData.stationLatitude) && !qIsNaN(weatherData.stationLongitude));
if (stationCoordValid) {
data.insert(QStringLiteral("Latitude"), weatherData.stationLatitude);
data.insert(QStringLiteral("Longitude"), weatherData.stationLongitude);
}
// Real weather - Current conditions
......@@ -530,35 +528,25 @@ void NOAAIon::updateWeather(const QString& source)
data.insert(QStringLiteral("Current Conditions"), conditionI18n);
qCDebug(IONENGINE_NOAA) << "i18n condition string: " << qPrintable(conditionI18n);
//TODO: Port to Plasma2
bool useDayIcon = true;
// TODO: get timeengine's solarsystem code to use directly
#if 0
// Determine the weather icon based on the current time and computed sunrise/sunset time.
const Plasma::DataEngine::Data timeData = m_timeEngine->query(
QString("Local|Solar|Latitude=%1|Longitude=%2")
.arg(latitude(source)).arg(longitude(source)));
if (weatherData.observationDateTime.isValid() && stationCoordValid) {
PlasmaWeather::Sun sun;
sun.setPosition(weatherData.stationLatitude, weatherData.stationLongitude);
QTime sunriseTime = timeData["Sunrise"].toDateTime().time();
QTime sunsetTime = timeData["Sunset"].toDateTime().time();
QTime currentTime = QDateTime::currentDateTime().time();
// Provide mapping for the condition-type to the icons to display
if (currentTime > sunriseTime && currentTime < sunsetTime) {
#endif
// Day
QString weather = weatherData.weather.toLower();
ConditionIcons condition = getConditionIcon(weather, true);
data.insert(QStringLiteral("Condition Icon"), getWeatherIcon(condition));
qCDebug(IONENGINE_NOAA) << "Using daytime icons\n";
#if 0
} else {
// Night
QString weather = weatherData.weather.toLower();
ConditionIcons condition = getConditionIcon(weather, false);
data.insert("Condition Icon", getWeatherIcon(condition));
qCDebug(IONENGINE_NOAA) << "Using nighttime icons\n";
const int offset = weatherData.observationDateTime.timeZone().offsetFromUtc(weatherData.observationDateTime);
sun.calcForDateTime(weatherData.observationDateTime, offset);
const auto elevation = sun.calcElevation();
// Tell applet which icon to use for conditions and provide mapping for condition type to the icons to display
useDayIcon = (elevation >= 0.0);
}
#endif
const QString weather = weatherData.weather.toLower();
ConditionIcons condition = getConditionIcon(weather, useDayIcon);
data.insert(QStringLiteral("Condition Icon"), getWeatherIcon(condition));
if (!qIsNaN(weatherData.temperature_F)) {
data.insert(QStringLiteral("Temperature"), weatherData.temperature_F);
}
......@@ -743,8 +731,8 @@ IonInterface::ConditionIcons NOAAIon::getConditionIcon(const QString& weather, b
void NOAAIon::getForecast(const QString& source)
{
const double lat = m_weatherData[source].stationLat;
const double lon = m_weatherData[source].stationLon;
const double lat = m_weatherData[source].stationLatitude;
const double lon = m_weatherData[source].stationLongitude;
if (qIsNaN(lat) || qIsNaN(lon)) {
return;
}
......
......@@ -45,8 +45,8 @@ public:
//QString countryName; // USA
QString locationName;
QString stationID;
double stationLat;
double stationLon;
double stationLatitude;
double stationLongitude;
QString stateName;
// Current observation information.
......@@ -163,7 +163,6 @@ private:
QHash<KJob *, QString> m_jobList;
QXmlStreamReader m_xmlSetup;
Plasma::DataEngine *m_timeEngine;
// bool emitWhenSetup;
QStringList m_sourcesToReset;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment