Skip to content

Commit

Permalink
Fix a ton of stuff and remove recessive functions, replace with count…
Browse files Browse the repository at this point in the history
…erparts
  • Loading branch information
cryptofyre committed Apr 18, 2023
1 parent d8ae3ac commit 6bbf852
Showing 1 changed file with 41 additions and 76 deletions.
117 changes: 41 additions & 76 deletions src/sh.cider.streamdeck.sdPlugin/app.js
Original file line number Diff line number Diff line change
@@ -1,138 +1,104 @@
/// <reference path="libs/js/action.js" />
/// <reference path="libs/js/stream-deck.js" />

const toggleAction = new Action('sh.cider.streamdeck.toggle');
const skipAction = new Action('sh.cider.streamdeck.skip');
const previousAction = new Action('sh.cider.streamdeck.previous');
const songNameAction = new Action('sh.cider.streamdeck.songname');
const albumArtAction = new Action('sh.cider.streamdeck.albumart');

/**
* The first event fired when Stream Deck starts
*/

$SD.onConnected(() => {
console.log('Stream Deck connected!');
console.debug('[DEBUG] [Init] Stream Deck connected!');
setInterval(setData, 4000);
});

// Album Art & Song Name event handling
async function setData() {
const playbackInfo = await grabPlaybackInfo();

if (playbackInfo === undefined) {
console.log("Playback info is undefined, skipping.")

console.debug("[DEBUG] [PlaybackInfo] Playback info is undefined, skipping.")
// Set defaults.
if (window.toggleActionContext !== null) {
$SD.setImage(window.toggleActionContext, 'actions/playback/assets/play.png', 0);
}

if (window.albumArtActionContext !== null) {
$SD.setImage(window.albumArtActionContext, 'actions/playback/assets/icon.png', 0);
}

if (window.albumArtAction !== null) {
$SD.setTitle(window.songNameActionContext, "No Song Playing", 0)
}

setImage(window.toggleActionContext, 'actions/playback/assets/play.png', 0);
setImage(window.albumArtActionContext, 'actions/playback/assets/icon.png', 0);
setTitle(window.songNameActionContext, "No Song Playing", 0)
return;
}

setPlaybackStatus();

// Set variables for artwork and song name
window.artwork = playbackInfo.info.artwork.url.replace('{w}', '100').replace('{h}', '100');
window.artwork = playbackInfo.info?.artwork?.url?.replace('{w}', '100').replace('{h}', '100');
window.artworkCache;
window.songCache;

// Cache artwork and song name to prevent unnecessary updates
if (window.artworkCache !== window.artwork) {
window.artworkCache = window.artwork;
console.log("Artwork is different, updating.")
console.debug("[DEBUG] [Artwork] Artwork is different, updating.")
let art64 = await getBase64Image(artwork);
if (window.albumArtActionContext !== null) {
$SD.setImage(window.albumArtActionContext, art64, 0);
}
setImage(window.albumArtActionContext, art64, 0);
}

if (window.songCache !== playbackInfo.info.name) {
window.songCache = playbackInfo.info.name;
console.log("Song is different, updating.")
if (window.songNameActionContext !== null) {
$SD.setTitle(window.songNameActionContext, playbackInfo.info.name, 0)
}
if (window.songCache !== playbackInfo.info?.name) {
window.songCache = playbackInfo.info?.name;
console.debug("[DEBUG] [SongName] Song is different, updating.")
setTitle(window.songNameActionContext, playbackInfo.info?.name, 0)
}

// Set Action to use the correct icon
if (playbackInfo.info.status === true || window.toggleActionContext !== null) {
$SD.setImage(window.toggleActionContext, 'actions/playback/assets/pause.png', 0);
if (playbackInfo.info?.status === "playing") {
setImage(window.toggleActionContext, 'actions/playback/assets/pause.png', 0);
}
else if (window.toggleActionContext !== null) {
$SD.setImage(window.toggleActionContext, 'actions/playback/assets/play.png', 0);
else if (playbackInfo.info?.status === "paused") {
setImage(window.toggleActionContext, 'actions/playback/assets/play.png', 0);
}
}

async function setPlaybackStatus() {
// Set Action to use the correct icon
let status = await comRPC("GET", "isPlaying");
if (window.statusCache !== status.is_playing) {
window.statusCache = status.is_playing;
console.log("Status is different, updating.")
if (status.is_playing === true || window.toggleActionContext !== null) {
$SD.setImage(window.toggleActionContext, 'actions/playback/assets/pause.png', 0);
} else if (window.toggleActionContext !== null) {
$SD.setImage(window.toggleActionContext, 'actions/playback/assets/play.png', 0);
console.debug("[DEBUG] [Status] Status is different, updating.")
if (status.is_playing) {
console.debug("[DEBUG] [Status] Setting as playing.")
setImage(window.toggleActionContext, 'actions/playback/assets/pause.png', 0);
} else if (!status.is_playing) {
console.debug("[DEBUG] [Status] Setting as paused.")
setImage(window.toggleActionContext, 'actions/playback/assets/play.png', 0);
}
}
}

// Key events

toggleAction.onKeyUp(() => {
comRPC("GET", "playPause");
setTimeout(setPlaybackStatus, 500);
});

skipAction.onKeyUp(() => {
comRPC("GET", "next");
setTimeout(setData, 500);
});

previousAction.onKeyUp(() => {
comRPC("GET", "previous");
setTimeout(setData, 500);
});

// Fetch Contexts

toggleAction.onWillAppear(({ context }) => {
console.log("ToggleContext: " + context)
console.debug("[DEBUG] [Context] ToggleContext: " + context)
window.toggleActionContext = context;
});

skipAction.onWillAppear(({ context }) => {
console.log("SkipContext: " + context)
console.debug("[DEBUG] [Context] SkipContext: " + context)
window.skipActionContext = context;
});

previousAction.onWillAppear(({ context }) => {
console.log("PreviousContext: " + context)
console.debug("[DEBUG] [Context] PreviousContext: " + context)
window.previousActionContext = context;
});

songNameAction.onWillAppear(({ context }) => {
console.log("SongNameContext: " + context)
console.debug("[DEBUG] [Context] SongNameContext: " + context)
window.songNameActionContext = context;
});

albumArtAction.onWillAppear(({ context }) => {
console.log("AlbumArtContext: " + context)
console.debug("[DEBUG] [Context] AlbumArtContext: " + context)
window.albumArtActionContext = context;
});

// Runtime Timer for playback info fetching.

async function grabPlaybackInfo() {
return fetch('http://localhost:10769/currentPlayingSong', {
method: 'GET',
Expand All @@ -144,13 +110,10 @@ async function grabPlaybackInfo() {
.then(json => {
return json;
})
.catch(error => console.error(error));
.catch(error => console.debug("[DEBUG] [ERROR] An error occurred while processing the request:", error));
}

// RPC Function for Key Events

async function comRPC(method, request) {

return fetch('http://localhost:10769/'+request, {
method: method,
headers: {
Expand All @@ -161,21 +124,23 @@ async function comRPC(method, request) {
.then(json => {
return json;
})
.catch(error => console.error(error));
.catch(error => console.debug("[DEBUG] [ERROR] An error occurred while processing the request:", error));
}


// Utility Functions

function setImage(action, image, context) {
const imageBase64 = getBase64Image(image);
$SD.api.setImage(action, imageBase64, context);
console.log("[DEBUG] IM GETTING CALLED IMAGE")
if (action !== null && image !== null && context !== null) {
console.log("[DEBUG] IM GETTING CALLED IMAGE 2")
$SD.setImage(action, image, context);
}
}

function setTitle(action, title, context) {
$SD.api.setTitle(action, title, context);
console.log("[DEBUG] IM GETTING CALLED SONG")
if (action !== null && title !== null && context !== null) {
console.log("[DEBUG] IM GETTING CALLED SONG 2")
$SD.setTitle(action, title, context);
}
}

function getBase64Image(url) {
return new Promise((resolve, reject) => {
const image = new Image();
Expand All @@ -193,4 +158,4 @@ function getBase64Image(url) {
};
image.src = url;
});
}
}

0 comments on commit 6bbf852

Please sign in to comment.