Skip to content

Commit

Permalink
⬆️ Fix default selection of dropdown items v2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jkharrat committed Apr 25, 2021
1 parent 9380996 commit 230efd7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 111 deletions.
Binary file modified Release/com.jk.weather.streamDeckPlugin
Binary file not shown.
2 changes: 1 addition & 1 deletion Sources/com.jk.weather.sdPlugin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"Icon": "resources/pluginIcon",
"PropertyInspectorPath": "pi/main_pi.html",
"URL": "https://github.com/JaouherK",
"Version": "2.0.1",
"Version": "2.0.2",
"OS": [
{
"Platform": "mac",
Expand Down
88 changes: 37 additions & 51 deletions Sources/com.jk.weather.sdPlugin/pi/main_pi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,66 @@ function connectElgatoStreamDeckSocket(inPort, inPropertyInspectorUUID, inRegist
uuid = inPropertyInspectorUUID;
actionInfo = JSON.parse(inActionInfo);

websocket = new WebSocket('ws://localhost:' + inPort);
websocket = new WebSocket("ws://localhost:" + inPort);

websocket.onopen = function()
{
websocket.onopen = function () {
// WebSocket is connected, register the Property Inspector
let json = {
"event": inRegisterEvent,
"uuid": inPropertyInspectorUUID
event: inRegisterEvent,
uuid: inPropertyInspectorUUID
};
websocket.send(JSON.stringify(json));

json = {
"event": "getSettings",
"context": uuid,
event: "getSettings",
context: uuid,
};
websocket.send(JSON.stringify(json));

json = {
"event": "getGlobalSettings",
"context": uuid,
event: "getGlobalSettings",
context: uuid,
};
websocket.send(JSON.stringify(json));
};

websocket.onmessage = function (evt) {
// Received message from Stream Deck
const jsonObj = JSON.parse(evt.data);
if (jsonObj.event === 'didReceiveSettings') {
if (jsonObj.event === "didReceiveSettings") {
const payload = jsonObj.payload.settings;

document.getElementById('cityName').value = payload.cityName;

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";
}
initiateElement("cityName", payload.cityName);
initiateElement("frequency", payload.frequency, 0);
initiateElement("unit", payload.unit, "celsius");
}
if (jsonObj.event === 'didReceiveGlobalSettings') {
if (jsonObj.event === "didReceiveGlobalSettings") {
const payload = jsonObj.payload.settings;

document.getElementById('apiKey').value = payload.apiKey;

if(document.getElementById('apiKey').value === "undefined") {
document.getElementById('apiKey').value = "";
}

const el = document.querySelector('.sdpi-wrapper');
el && el.classList.remove('hidden');
initiateElement("apiKey", payload.apiKey);
const el = document.querySelector(".sdpi-wrapper");
el && el.classList.remove("hidden");
}
};

}

function initiateElement(element, value, fallback = "") {
if (typeof value === 'undefined') {
document.getElementById(element).value = fallback;
return;
}
document.getElementById(element).value = value;
}

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;
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
event: "setSettings",
context: uuid,
payload: payload
};
websocket.send(JSON.stringify(json));
}
Expand All @@ -89,11 +75,11 @@ function updateSettings() {
function updateApiKey() {
if (websocket && (websocket.readyState === 1)) {
let payload = {};
payload.apiKey = document.getElementById('apiKey').value;
payload.apiKey = document.getElementById("apiKey").value;
const json = {
"event": "setGlobalSettings",
"context": uuid,
"payload": payload
event: "setGlobalSettings",
context: uuid,
payload
};
websocket.send(JSON.stringify(json));
}
Expand All @@ -102,9 +88,9 @@ function updateApiKey() {
function openPage(site) {
if (websocket && (websocket.readyState === 1)) {
const json = {
'event': 'openUrl',
'payload': {
'url': 'https://' + site
event: "openUrl",
payload: {
url: `https://${site}`
}
};
websocket.send(JSON.stringify(json));
Expand Down
119 changes: 60 additions & 59 deletions Sources/com.jk.weather.sdPlugin/plugin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
websocket.onopen = function () {
// WebSocket is connected, register the plugin
const json = {
"event": inRegisterEvent,
"uuid": inPluginUUID
event: inRegisterEvent,
uuid: inPluginUUID
};

websocket.send(JSON.stringify(json));
Expand All @@ -21,17 +21,17 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
websocket.onmessage = function (evt) {
// Received message from Stream Deck
const jsonObj = JSON.parse(evt.data);
const context = jsonObj['context'];
const context = jsonObj["context"];

if (jsonObj['event'] === "keyUp") {
if (jsonObj["event"] === "keyUp") {
let 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')
jsonObj.payload.settings.hasOwnProperty("cityName") &&
jsonObj.payload.settings.hasOwnProperty("unit") &&
jsonObj.payload.settings.hasOwnProperty("frequency")
) {
cityName = jsonObj.payload.settings["cityName"].toLowerCase();
unit = jsonObj.payload.settings["unit"];
Expand All @@ -40,8 +40,8 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in

if (cityName === "" || apiKey === "") {
const json = {
"event": "showAlert",
"context": jsonObj.context,
event: "showAlert",
context: jsonObj.context,
};
websocket.send(JSON.stringify(json));
} else {
Expand All @@ -50,15 +50,15 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
setInterval(() => requestSending(context, cityName, unit), frequency);
}
}
} else if (jsonObj['event'] === "didReceiveGlobalSettings") {
if (jsonObj.payload.settings != null && jsonObj.payload.settings.hasOwnProperty('apiKey')) {
} else if (jsonObj["event"] === "didReceiveGlobalSettings") {
if (jsonObj.payload.settings != null && jsonObj.payload.settings.hasOwnProperty("apiKey")) {
apiKey = jsonObj.payload.settings["apiKey"];
}

} else if (jsonObj['event'] === "keyDown") {
} else if (jsonObj["event"] === "keyDown") {
const json = {
"event": "getGlobalSettings",
"context": pluginUUID
event: "getGlobalSettings",
context: pluginUUID
};

websocket.send(JSON.stringify(json));
Expand All @@ -69,76 +69,77 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in

function requestSending(context, cityName, unit) {
const request = new XMLHttpRequest();
request.open("GET", 'https://api.weatherapi.com/v1/current.json?key=' + apiKey + '&q=' + cityName + '&aqi=no');
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) {
if (request.status === 200) {
const response = JSON.parse(request.responseText);
let temperature = "";
if (unit === 'celsius') {
if (unit === "celsius") {
temperature = response.current.temp_c ? response.current.temp_c + "°C" : "NaN";
}
if (unit === 'fahrenheit') {
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
let jsonDeck = {
event: "setTitle",
context,
payload: {
title: temperature,
target: 1
}
};

websocket.send(JSON.stringify(json));
websocket.send(JSON.stringify(jsonDeck));

let json2 = {
"event": "setTitle",
"context": context,
"payload": {
"title": "" + cityName,
"target": 2
let jsonSoftware = {
event: "setTitle",
context: context,
payload: {
title: cityName,
target: 2
}
};

websocket.send(JSON.stringify(json2));

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();
}
websocket.send(JSON.stringify(jsonSoftware));

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));
})
toDataURL(
"https:" + response.current.condition.icon,
(dataUrl) => {
let json = {
event: "setImage",
context,
payload: {
image: dataUrl,
target: 1
}
};
websocket.send(JSON.stringify(json));
})
}
} else {
const json = {
"event": "showAlert",
"context": context,
event: "showAlert",
context,
};
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();
}

0 comments on commit 230efd7

Please sign in to comment.