yasd/apps/server/lib/Deck.js

79 lines
1.8 KiB
JavaScript

const { openStreamDeck } = require('@elgato-stream-deck/node');
const util = require('./util');
const EVENTS = require('./Events');
let myStreamDeck;
let deckConfig;
let iconBuffers=[];
let activePage = 0;
/**
*
*/
function init(){
console.log(" - Starting deck.js")
eventBus.on(EVENTS.CONFIG_READY, config => {
deckConfig = config;
generateIcons().then(()=>{
drawPage(activePage)
})
});
eventBus.on(EVENTS.CONFIG_CHANGED, newConfig => {
console.log("Config changed event received in DECK");
deckConfig = newConfig;
generateIcons().then(()=>{
drawPage(activePage)
})
});
eventBus.on(EVENTS.GET_ACTIVE_DECK, (e)=>{
e.deck = myStreamDeck;
})
myStreamDeck = openStreamDeck();
myStreamDeck.clearPanel();
registerCallbacks();
}
function registerCallbacks(){
myStreamDeck.on('down', (keyIndex) => {
deckConfig.pages[0].buttons[keyIndex].press()
});
myStreamDeck.on('up', (keyIndex) => {
deckConfig.pages[0].buttons[keyIndex].release()
});
}
function drawPage(pageNum){
activePage = pageNum;
for (let i = 0; i < myStreamDeck.NUM_KEYS; i++) {
const color = util.hexToRGB(deckConfig.pages[pageNum].buttons[i].color)
myStreamDeck.fillKeyColor(i, color.r, color.g, color.b)
const buffer = iconBuffers[pageNum][i];
if(buffer) myStreamDeck.fillKeyBuffer(i, iconBuffers[pageNum][i])
}
}
async function generateIcons(){
for (let i = 0; i < deckConfig.pages.length; i++) {
const page = deckConfig.pages[i];
iconBuffers.push([]);
for (let j = 0; j < page.buttons.length; j++) {
const deckBtn = deckConfig.pages[i].buttons[j];
if(deckBtn.iconPath != ''){
let buffer = await util.buttonImageToBuffer(deckBtn.iconPath, myStreamDeck.ICON_SIZE)
iconBuffers[i][j] = buffer;
}
}
}
}
module.exports = { init }