Skip to content

Commit

Permalink
Work for #51 and #52
Browse files Browse the repository at this point in the history
  • Loading branch information
k-donn committed Mar 28, 2024
1 parent 4daf558 commit b5972ca
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
93 changes: 92 additions & 1 deletion plasmoid/contents/code/pws-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ let iconThemeMap = {
23: "weather-clouds-wind-symbolic",
24: "weather-clouds-wind-symbolic",
25: "weather-snow-symbolic",
26: "weather-clouds-symbolic",
26: "weather-many-clouds-symbolic",
27: "weather-many-clouds-symbolic",
28: "weather-clouds-symbolic",
29: "weather-clouds-night-symbolic",
Expand Down Expand Up @@ -123,6 +123,8 @@ function getCurrentData() {

var res = JSON.parse(req.responseText);

// The nested section name returned in JSON is the units type
// So res cannot be directly assigned to weatherData
var tmp = {};
var tmp = res["observations"][0];

Expand Down Expand Up @@ -312,6 +314,10 @@ function getNearestStation() {
req.send();
}

// TODO: replace with getExtendedConditions
/**
* Get icon code for display in TopPanel and CompactRep
*/
function findIconCode() {
var req = new XMLHttpRequest();

Expand Down Expand Up @@ -373,3 +379,88 @@ function findIconCode() {

req.send();
}

/**
* Get broad weather info from station area including textual/icon description of conditions and weather warnings.
*/
function getExtendedConditions() {
var req = new XMLHttpRequest();

var long = plasmoid.configuration.longitude;
var lat = plasmoid.configuration.latitude;

var url = "https://api.weather.com/v3/aggcommon/v3-wx-observations-current;v3alertsHeadlines;v3-wx-globalAirQuality";

url += "?geocodes=" + lat + "," + long;
url += "&apiKey=" + API_KEY;
url += "&language=en-US";
url += "&scale=EPA"

if (unitsChoice === 0) {
url += "&units=m";
} else if (unitsChoice === 1) {
url += "&units=e";
} else {
url += "&units=h";
}

url += "&format=json";

req.open("GET", url);

req.setRequestHeader("Accept-Encoding", "gzip");
req.setRequestHeader("Origin", "https://www.wunderground.com");

req.onerror = function () {
printDebug("[pws-api.js] " + req.responseText);
};

printDebug("[pws-api.js] " + url);

req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status == 200) {
var res = JSON.parse(req.responseText);

var combinedVars = res[0]

var condVars = combinedVars["v3-wx-observations-current"]
var alertsVars = combinedVars["v3alertsHeadlines"]
var airQualVars = combinedVars["v3-wx-globalAirQuality"]["globalairquality"]

iconCode = iconThemeMap[condVars["iconCode"]];
conditionNarrative = condVars["wxPhraseLong"];

// Determine if the precipitation is snow or rain
// All of these codes are for snow
if (
iconCode === 5 ||
iconCode === 13 ||
iconCode === 14 ||
iconCode === 15 ||
iconCode === 16 ||
iconCode === 42 ||
iconCode === 43 ||
iconCode === 46
) {
isRain = false;
}

if (alertsVars !== null) {
// TODO: parse and show weather alerts
}

weatherData["aq"]["aqi"] = airQualVars["airQualityIndex"]
weatherData["aq"]["aqhi"] = airQualVars["airQualityCategoryIndex"]
weatherData["aq"]["aqDesc"] = airQualVars["airQualityCategory"]
weatherData["aq"]["aqColor"] = airQualVars["airQualityCategoryIndexColor"]

}
}
};

req.send();
}

/**
* Get air quality
6 changes: 4 additions & 2 deletions plasmoid/contents/ui/DetailsItem.qml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ GridLayout {
source: "gnumeric-object-arrow-symbolic"
//source: "plasmoid/contents/images/wind-barbs/" + Utils.getWindBarb(weatherData["details"]["windSpeed"])+ ".svg"

// wind barb icons are -90/270 degrees deviated from 0 degrees (north)
// wind barb icons are 270 degrees deviated from 0 degrees (north)
//rotation: weatherData["winddir"] - 270

// new rotation for icons:
rotation: weatherData["winddir"] - 135

Expand All @@ -57,6 +58,7 @@ GridLayout {
Layout.preferredWidth: Layout.minimumWidth
Layout.preferredHeight: Layout.minimumHeight
}

PlasmaComponents.Label {
id: windLabel
text: i18n("WIND & GUST")
Expand All @@ -73,7 +75,7 @@ GridLayout {
}
PlasmaComponents.Label {
id: windDirCard
text: i18n("Wind from: %1", Utils.windDirToCard(weatherData["winddir"]))
text: i18n("Wind from: %1 (%2°)", Utils.windDirToCard(weatherData["winddir"]),weatherData["winddir"])
font.pointSize: plasmoid.configuration.propPointSize
}
PlasmaComponents.Label {
Expand Down
2 changes: 1 addition & 1 deletion plasmoid/contents/ui/TopPanel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ RowLayout {
Kirigami.Icon {
id: topPanelIcon

source: iconCode
source: iconCode

Layout.minimumWidth: Kirigami.Units.iconSizes.large
Layout.minimumHeight: Kirigami.Units.iconSizes.large
Expand Down
4 changes: 2 additions & 2 deletions plasmoid/contents/ui/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import "../code/pws-api.js" as StationAPI
PlasmoidItem {
id: root

property var weatherData: null
property var weatherData: {"stationID":"","uv":0,"obsTimeLocal":"","winddir":0,"details":{"temp":0,"windSpeed":0,"windGust":0,"dewpt":0,"precipRate":0,"pressure":0,"precipTotal":0,"elev":0},"aq":{"aqi":"","aqhi":"","aqDesc":"","aqColor":""},"alerts":[]}
property ListModel forecastModel: ListModel {}
property string errorStr: ""
property string toolTipSubTextVar: ""
property string iconCode: "weather-clear" // 32 = sunny
property string conditionNarrative: ""

// TODO: add option for showFORECAST and showFORECASTERROR
// TODO: add option for showFORECASTERROR
property int showCONFIG: 1
property int showLOADING: 2
property int showERROR: 4
Expand Down

0 comments on commit b5972ca

Please sign in to comment.