diff --git a/README.md b/README.md index 86cbbcf..4e38791 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Description -`Weather` is a plugin that displays a weather condition and temperature. It is connected to weatherApi provider and requires an API Key and a city as inputs. +`Weather` is a plugin that displays a weather condition and temperature. It is connected to weatherApi provider and requires an API Key and a city as required inputs. It has also selection for frequency of fetching and temperature unit. # Features diff --git a/Release/com.jk.weather.streamDeckPlugin b/Release/com.jk.weather.streamDeckPlugin index 77e9ea7..c5eb6fc 100644 Binary files a/Release/com.jk.weather.streamDeckPlugin and b/Release/com.jk.weather.streamDeckPlugin differ diff --git a/Sources/com.jk.weather.sdPlugin/pi/main_pi.html b/Sources/com.jk.weather.sdPlugin/pi/main_pi.html index e0e66ce..237827d 100644 --- a/Sources/com.jk.weather.sdPlugin/pi/main_pi.html +++ b/Sources/com.jk.weather.sdPlugin/pi/main_pi.html @@ -2,7 +2,7 @@ - + com.jk.weather.pi @@ -10,17 +10,53 @@ diff --git a/Sources/com.jk.weather.sdPlugin/pi/main_pi.js b/Sources/com.jk.weather.sdPlugin/pi/main_pi.js index 226af44..9476a90 100644 --- a/Sources/com.jk.weather.sdPlugin/pi/main_pi.js +++ b/Sources/com.jk.weather.sdPlugin/pi/main_pi.js @@ -42,6 +42,18 @@ function connectElgatoStreamDeckSocket(inPort, inPropertyInspectorUUID, inRegist if(document.getElementById('cityName').value === "undefined") { document.getElementById('cityName').value = ""; } + + document.getElementById('frequency').value = payload.frequency; + + if(document.getElementById('frequency').value === "undefined") { + document.getElementById('frequency').value = "0"; + } + + document.getElementById('unit').value = payload.unit; + + if(document.getElementById('unit').value === "undefined") { + document.getElementById('unit').value = "celsius"; + } } if (jsonObj.event === 'didReceiveGlobalSettings') { const payload = jsonObj.payload.settings; @@ -59,18 +71,19 @@ function connectElgatoStreamDeckSocket(inPort, inPropertyInspectorUUID, inRegist } -function updateCityName() { +function updateSettings() { if (websocket && (websocket.readyState === 1)) { let payload = {}; payload.cityName = document.getElementById('cityName').value; + payload.frequency = document.getElementById('frequency').value; + payload.unit = document.getElementById('unit').value; const json = { "event": "setSettings", "context": uuid, "payload": payload }; websocket.send(JSON.stringify(json)); - console.log(json) - } + } } function updateApiKey() { @@ -83,8 +96,7 @@ function updateApiKey() { "payload": payload }; websocket.send(JSON.stringify(json)); - console.log(json) - } + } } function openPage(site) { diff --git a/Sources/com.jk.weather.sdPlugin/plugin/main.js b/Sources/com.jk.weather.sdPlugin/plugin/main.js index 431e080..93de2a7 100644 --- a/Sources/com.jk.weather.sdPlugin/plugin/main.js +++ b/Sources/com.jk.weather.sdPlugin/plugin/main.js @@ -21,69 +21,33 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in websocket.onmessage = function (evt) { // Received message from Stream Deck const jsonObj = JSON.parse(evt.data); - let context = jsonObj['context']; + const context = jsonObj['context']; if (jsonObj['event'] === "keyUp") { let cityName = ""; - if (jsonObj.payload.settings != null && jsonObj.payload.settings.hasOwnProperty('cityName')) { + let unit = ""; + let frequency = null; + if ( + jsonObj.payload.settings != null && + jsonObj.payload.settings.hasOwnProperty('cityName') && + jsonObj.payload.settings.hasOwnProperty('unit') && + jsonObj.payload.settings.hasOwnProperty('frequency') + ) { cityName = jsonObj.payload.settings["cityName"]; + unit = jsonObj.payload.settings["unit"]; + frequency = (jsonObj.payload.settings["frequency"] !== "0") ? parseInt(jsonObj.payload.settings["frequency"]) : false; } if (cityName === "" || apiKey === "") { - console.log('Missing fields here...'); const json = { "event": "showAlert", "context": jsonObj.context, }; websocket.send(JSON.stringify(json)); } else { - const request = new XMLHttpRequest(); - request.open("GET", 'https://api.weatherapi.com/v1/current.json?key=' + apiKey + '&q=' + cityName + '&aqi=no'); - request.send(); - request.onreadystatechange = function () { - if (request.readyState === XMLHttpRequest.DONE) { - const response = JSON.parse(request.responseText); - const temperature = response.current.temp_c ? response.current.temp_c : "NaN"; - let json = { - "event": "setTitle", - "context": context, - "payload": { - "title": "" + temperature + "°C", - "target": 1 - } - }; - - websocket.send(JSON.stringify(json)); - - function toDataURL(url, callback) { - let xhr = new XMLHttpRequest(); - xhr.onload = function () { - let reader = new FileReader(); - reader.onloadend = function () { - callback(reader.result); - } - reader.readAsDataURL(xhr.response); - }; - xhr.open('GET', url); - xhr.responseType = 'blob'; - xhr.send(); - } - - if (response.current.condition.icon != null) { - toDataURL("https:" + response.current.condition.icon, function (dataUrl) { - let json = { - "event": "setImage", - "context": context, - "payload": { - "image": '' + dataUrl, - "target": 1 - } - }; - - websocket.send(JSON.stringify(json)); - }) - } - } + requestSending(context, cityName, unit); + if (frequency) { + setInterval(() => requestSending(context, cityName, unit), frequency); } } } else if (jsonObj['event'] === "didReceiveGlobalSettings") { @@ -101,3 +65,62 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in } }; } + + +function requestSending(context, cityName, unit) { + console.log('Sending'); + const request = new XMLHttpRequest(); + request.open("GET", 'https://api.weatherapi.com/v1/current.json?key=' + apiKey + '&q=' + cityName + '&aqi=no'); + request.send(); + request.onreadystatechange = function () { + if (request.readyState === XMLHttpRequest.DONE) { + const response = JSON.parse(request.responseText); + let temperature = ""; + if (unit === 'celsius') { + temperature = response.current.temp_c ? response.current.temp_c + "°C" : "NaN"; + } + if (unit === 'fahrenheit') { + temperature = response.current.temp_f ? response.current.temp_f + "°F" : "NaN"; + } + let json = { + "event": "setTitle", + "context": context, + "payload": { + "title": "" + temperature, + "target": 1 + } + }; + + websocket.send(JSON.stringify(json)); + + function toDataURL(url, callback) { + let xhr = new XMLHttpRequest(); + xhr.onload = function () { + let reader = new FileReader(); + reader.onloadend = function () { + callback(reader.result); + } + reader.readAsDataURL(xhr.response); + }; + xhr.open('GET', url); + xhr.responseType = 'blob'; + xhr.send(); + } + + if (response.current.condition.icon != null) { + toDataURL("https:" + response.current.condition.icon, function (dataUrl) { + let json = { + "event": "setImage", + "context": context, + "payload": { + "image": '' + dataUrl, + "target": 1 + } + }; + + websocket.send(JSON.stringify(json)); + }) + } + } + } +} diff --git a/screenshot.png b/screenshot.png index 8c055fb..8bc8a3f 100644 Binary files a/screenshot.png and b/screenshot.png differ