Skip to content

Commit

Permalink
Add round degree option (#13)
Browse files Browse the repository at this point in the history
* Add round degree option

* Simplify temperature ternary for rounding
  • Loading branch information
thepaulstella authored Jun 25, 2021
1 parent 49e3e6f commit d372267
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
12 changes: 12 additions & 0 deletions Sources/com.jk.weather.sdPlugin/pi/main_pi.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@
</select>
</div>

<div class="sdpi-item">
<div class="sdpi-item-label">Round to closest degree</div>
<select
class="sdpi-item-value select"
id="roundDegree"
onchange="updateSettings()"
>
<option value="true">Yes</option>
<option value="false">No</option>
</select>
</div>

<div class="sdpi-item">
<button class="sdpi-item-value" onclick="openPage(getProviderUrl())">
Get my API key
Expand Down
2 changes: 2 additions & 0 deletions Sources/com.jk.weather.sdPlugin/pi/main_pi.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function connectElgatoStreamDeckSocket(
initiateElement("frequency", payload.frequency, 0);
initiateElement("displayCity", payload.displayCity, 0);
initiateElement("unit", payload.unit, "celsius");
initiateElement("roundDegree", payload.roundDegree, "true");
}
if (jsonObj.event === "didReceiveGlobalSettings") {
const payload = jsonObj.payload.settings;
Expand All @@ -69,6 +70,7 @@ function updateSettings() {
payload.cityName = document.getElementById("cityName").value;
payload.frequency = document.getElementById("frequency").value;
payload.unit = document.getElementById("unit").value;
payload.roundDegree = document.getElementById("roundDegree").value;
payload.displayCity = parseInt(
document.getElementById("displayCity").value
);
Expand Down
32 changes: 22 additions & 10 deletions Sources/com.jk.weather.sdPlugin/plugin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,21 @@ function connectElgatoStreamDeckSocket(
let cityName = "";
let unit = "";
let displayCity = 0;
let roundDegree = true;
let frequency = null;

if (
jsonObj.payload.settings != null &&
jsonObj.payload.settings.hasOwnProperty("cityName") &&
jsonObj.payload.settings.hasOwnProperty("unit") &&
jsonObj.payload.settings.hasOwnProperty("frequency")
jsonObj.payload.settings.hasOwnProperty("frequency") &&
jsonObj.payload.settings.hasOwnProperty("roundDegree")

) {
cityName = jsonObj.payload.settings["cityName"].toLowerCase();
unit = jsonObj.payload.settings["unit"];
displayCity = jsonObj.payload.settings["displayCity"];
roundDegree = jsonObj.payload.settings["roundDegree"] === "true";
frequency =
jsonObj.payload.settings["frequency"] !== "0"
? parseInt(jsonObj.payload.settings["frequency"])
Expand All @@ -57,10 +61,10 @@ function connectElgatoStreamDeckSocket(
};
websocket.send(JSON.stringify(json));
} else {
sendRequest(context, cityName, displayCity, unit);
sendRequest(context, cityName, displayCity, unit, roundDegree);
if (frequency) {
setInterval(
() => sendRequest(context, cityName, displayCity, unit),
() => sendRequest(context, cityName, displayCity, unit, roundDegree),
frequency
);
}
Expand Down Expand Up @@ -98,23 +102,31 @@ function prepareUrl(cityName, unit) {
return url;
}

function prepareTemperature(response, unit) {
function prepareTemperature(response, unit, roundDegree) {
let temp;
switch (provider) {
case "weatherApi":
if (unit === "celsius") {
temp = response.current.temp_c ? response.current.temp_c + "°C" : "NaN";
temp = response.current.temp_c
? response.current.temp_c.toFixed(roundDegree ? 0 : 2) + "°C"
: "NaN";
}
if (unit === "fahrenheit") {
temp = response.current.temp_f ? response.current.temp_f + "°F" : "NaN";
temp = response.current.temp_f
? response.current.temp_f.toFixed(roundDegree ? 0 : 2) + "°F"
: "NaN";
}
break;
case "openWeatherMap":
if (unit === "celsius") {
temp = response.main.temp ? response.main.temp + "°C" : "NaN";
temp = response.main.temp
? response.main.temp.toFixed(roundDegree ? 0 : 2) + "°C"
: "NaN";
}
if (unit === "fahrenheit") {
temp = response.main.temp ? response.main.temp + "°F" : "NaN";
temp = response.main.temp
? response.main.temp.toFixed(roundDegree ? 0 : 2) + "°F"
: "NaN";
}
break;
}
Expand Down Expand Up @@ -155,7 +167,7 @@ function setImageAndCity(response, context, city, displayCity) {
return url;
}

function sendRequest(context, cityName, displayCity, unit) {
function sendRequest(context, cityName, displayCity, unit, roundDegree) {
const request = new XMLHttpRequest();
const url = prepareUrl(cityName, unit);
request.open("GET", url);
Expand All @@ -164,7 +176,7 @@ function sendRequest(context, cityName, displayCity, unit) {
if (request.readyState === XMLHttpRequest.DONE) {
if (request.status === 200) {
const response = JSON.parse(request.responseText);
const temperature = prepareTemperature(response, unit);
const temperature = prepareTemperature(response, unit, roundDegree);

let jsonDeck = {
event: "setTitle",
Expand Down

0 comments on commit d372267

Please sign in to comment.