132 lines
3.7 KiB
JavaScript
132 lines
3.7 KiB
JavaScript
|
const fs = require('fs');
|
||
|
const homedir = require('os').homedir();
|
||
|
const configPath = [homedir, process.env.CONFIG_PATH].join('/');
|
||
|
const fullConfigPath = [configPath, process.env.CONFIG_FILE].join('/');
|
||
|
const deckButton = require('./DeckButton');
|
||
|
const EVENTS = require('./Events');
|
||
|
|
||
|
let configState;
|
||
|
|
||
|
/**
|
||
|
* Initializes the configManager.
|
||
|
* This will check for the config file and folder on disk and create a blank configuration if one does not exist.
|
||
|
*/
|
||
|
function init(){
|
||
|
console.log(" - Starting configManager.js")
|
||
|
|
||
|
//check for existance of config folder in user home dir
|
||
|
try {
|
||
|
if(fs.existsSync(fullConfigPath)){
|
||
|
console.log(" - Config file exists");
|
||
|
} else {
|
||
|
console.log(" - No config file found. Creating one now");
|
||
|
let blankConfigObject = {"backgroundImage": "",pages:[{pagename: "Blank Page",buttons: []}]}
|
||
|
for (let i = 0; i < 32; i++) {
|
||
|
blankConfigObject.pages[0].buttons.push(new deckButton('', "Test Button: " + i, "#000000", "builtin:nullAction:"));
|
||
|
}
|
||
|
|
||
|
//Make config folder.
|
||
|
fs.mkdirSync(configPath, {recursive: true}, (err) => {
|
||
|
if(err) throw err;
|
||
|
});
|
||
|
|
||
|
//Make config file.
|
||
|
fs.writeFileSync(fullConfigPath, JSON.stringify(blankConfigObject,null,2), (err) => {
|
||
|
if(err) throw err;
|
||
|
});
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error(" - " + error)
|
||
|
}
|
||
|
|
||
|
//Announce the loaded config on the bus
|
||
|
eventBus.emit(EVENTS.CONFIG_READY, this.getConfig());
|
||
|
|
||
|
eventBus.on(EVENTS.UPDATE_BUTTON, data => {
|
||
|
console.log("Update button with: " + JSON.stringify(data))
|
||
|
updateButton(data.pageNumber, data.buttonIndex, data.iconPath, data.label, data.color, data.pressAction, data.releaseAction, data.toggleable, data.toggleIcon)
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Will read the config from the defined configuration path.
|
||
|
* @returns
|
||
|
* An object of pages.
|
||
|
* Each page is an object with a name and an array of deckButton objects.
|
||
|
* Each array is 32 in length to match the StreamDeckXL.
|
||
|
* All smaller StreamDecks fit within this array
|
||
|
*/
|
||
|
function getConfig(){
|
||
|
const data = JSON.parse(fs.readFileSync(fullConfigPath));
|
||
|
configState = data;
|
||
|
|
||
|
for (let i = 0; i < data.pages.length; i++) {
|
||
|
const page = data.pages[i];
|
||
|
for (let j = 0; j < page.buttons.length; j++) {
|
||
|
const deckBtn = deckButton.from(page.buttons[j]);
|
||
|
configState.pages[i].buttons[j] = deckBtn;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return configState;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Write the current config state to the disk
|
||
|
*/
|
||
|
function writeConfig(){
|
||
|
fs.writeFileSync(fullConfigPath, JSON.stringify(configState, null, 2), err => {
|
||
|
if(err){
|
||
|
console.error(err)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
//Local function to modify config
|
||
|
/**
|
||
|
* Call to notify the event bus of a new config
|
||
|
*/
|
||
|
function configChanged(){
|
||
|
writeConfig();
|
||
|
eventBus.emit('config_changed', configState);
|
||
|
}
|
||
|
|
||
|
function addPage(){
|
||
|
|
||
|
}
|
||
|
|
||
|
function updateButton(pageNumber, buttonIndex, iconPath, label, color, pressAction, releaseAction, toggleable, toggleIcon){
|
||
|
let button = configState.pages[pageNumber].buttons[buttonIndex];
|
||
|
|
||
|
if(iconPath){
|
||
|
button.iconPath = iconPath;
|
||
|
}
|
||
|
|
||
|
if(label){
|
||
|
button.label = label;
|
||
|
}
|
||
|
|
||
|
if(color){
|
||
|
button.color = color;
|
||
|
}
|
||
|
|
||
|
if(pressAction){
|
||
|
button.pressAction = pressAction;
|
||
|
}
|
||
|
|
||
|
if(releaseAction){
|
||
|
button.releaseAction = releaseAction;
|
||
|
}
|
||
|
|
||
|
if(toggleable){
|
||
|
button.toggleable = toggleable;
|
||
|
}
|
||
|
|
||
|
if(toggleIcon){
|
||
|
button.toggleIcon = toggleIcon;
|
||
|
}
|
||
|
|
||
|
configChanged()
|
||
|
}
|
||
|
|
||
|
module.exports = {init, getConfig, writeConfig}
|