Skip to content

Instantly share code, notes, and snippets.

@indieisaconcept
Last active Oct 2, 2021
Embed
What would you like to do?
Flic Hub package for controlling Sonos Home Theatre Settings
/**
* MIT License
*
* Copyright (c) 2021 Jonathan Barnett
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* -----------------------------------------------------------------------------
* In-order to make use of this package name your button as follows
*
* sdk://sonos?ip=<ip-address-of-soundbar>
*
* onClick : Toggle surround & subwoofer ( settings are synced )
* onDoubleClick : Toggle surround
* onHold : Toggle subwoofer
* -----------------------------------------------------------------------------
*/
const register = (function () {
const decode = decodeURIComponent;
const buttonManager = require('buttons');
/**
* Responsible for extracting configuration from a button name assuming
* configuration is pass as a query string argument.
*
* example:
* sdk://sonos?ip=192.168.1&key=value
*
* @param {string} name
* @returns {object}
*/
function getConfiguration(name) {
const config = name.split('?')[1];
if (!config) {
return {};
}
return config.split('&').reduce(function (acc, current) {
const tokens = current.split('=');
const key = tokens[0];
const val = tokens[1];
acc[decode(key)] = decode(val);
return acc;
}, {});
}
/**
* Responsible for determining what type of button event has been generated
* @param event
* @returns {string}
*/
function getClickType(event) {
return event.isSingleClick ? 'onClick' : event.isDoubleClick ? 'onDoubleClick' : 'onHold';
}
/**
* Responsible for registering a new module.
*
* @param {string} namespace The command namespace
* @param {object} commands Command configuration
*/
return function (namespace, commands) {
const namespaceRegex = new RegExp('^sdk://' + namespace, 'i');
buttonManager.on('buttonSingleOrDoubleClickOrHold', function (obj) {
const button = buttonManager.getButton(obj.bdaddr);
if (!namespaceRegex.test(button.name)) {
return;
}
const clickType = getClickType(obj);
const command = commands[clickType];
if (command) {
const config = getConfiguration(button.name);
command(config, button);
}
});
};
}());
const client = (function () {
const http = require('http');
/**
* Responsible for generating the SetEQ action
*
* @param {string} EQType The EQ property to change
* @param {*} value The desired value for the EQ property
* @returns {string}
*/
function createSetEQAction(EQType, value) {
return [
'<u:SetEQ xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1">',
'<InstanceID>0</InstanceID>',
'<EQType>' + EQType + '</EQType>',
'<DesiredValue>' + value + '</DesiredValue>',
'</u:SetEQ>',
].join('');
}
/**
* Responsible for generating the GetEQ action
*
* @param {string} EQType The EQ property to request
* @returns {string}
*/
function createGetEQAction(EQType) {
return [
'<u:GetEQ xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1">',
'<InstanceID>0</InstanceID>',
'<EQType>' + EQType + '</EQType>',
'</u:GetEQ>',
].join('');
}
/**
* Responsible for creating a SOAP envelope
*
* @param {string} actionBody
* @returns {string}
*/
function createSoapEnvelope(actionBody) {
return [
'<?xml version="1.0" encoding="utf-8"?>',
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">',
'<s:Body>',
actionBody,
'</s:Body>',
'</s:Envelope>'
].join('');
}
/**
* Shallow merges of two objects
*
* @param {object} a object to merge
* @param {object} b object to merge
* @returns
*/
function merge(a, b) {
return Object.assign(a, b);
}
/**
* Responsible for toggling an EQ value
*
* @param {object} config Sonos configuration
* @param {string} EQType The EQ property to toggle
* @param {Function} callback Function to call upon completion
*/
function toggleEQValue(config, EQType, callback) {
getEQValue(config, EQType, function (err, result) {
if (err) {
return callback(err);
}
const newValue = result.CurrentValue ? 0 : 1;
setEQValue(config, EQType, newValue, callback);
});
};
/**
* Responsible for retrieving an EQ value
*
* @param {object} config Sonos configuration
* @param {string} EQType The EQ property to toggle
* @param {Function} callback Function to call upon completion
*/
function getEQValue(config, EQType, callback) {
makeRequest({
host: config.ip,
url: '/MediaRenderer/RenderingControl/Control',
content: createGetEQAction(EQType),
headers: {
soapaction: 'RenderingControl:1#GetEQ'
}
},
function (err, result) {
if (err) {
return callback(err);
}
const currentValue = +(/<CurrentValue>(.*?)<\/CurrentValue>/g.exec(result)[1]);
callback(null, {
CurrentValue: currentValue
});
});
}
/**
* Responsible for setting an EQ value
*
* @param {object} config Sonos configuration
* @param {string} EQType The EQ property to toggle
* @param {Function} callback Function to call upon completion
*/
function setEQValue(config, EQType, value, callback) {
const newValue = +value;
makeRequest({
host: config.ip,
url: '/MediaRenderer/RenderingControl/Control',
content: createSetEQAction(EQType, newValue),
headers: {
soapaction: 'RenderingControl:1#SetEQ'
}
},
function (err) {
if (err) {
return callback(err)
}
callback(null, {
CurrentValue: newValue
});
});
}
/**
* Provides a standardised helper for interacting with the Sonos SOAP API
*
* @param {object} options Request options
* @param {Function} callback Function to call upon request completion
*/
function makeRequest(options, callback) {
http.makeRequest({
url: 'http://' + options.host + ':1400' + options.url,
method: 'POST',
headers: {
Host: options.host + ':1400',
soapaction: 'urn:schemas-upnp-org:service:' + options.headers.soapaction,
'Content-Type': 'text/xml; charset="utf-8"'
},
content: createSoapEnvelope(options.content)
}, function (err, result) {
if (err) {
return callback(err);
}
if (result.statusCode !== 200) {
return callback(new Error(result.content));
}
callback(null, result.content);
});
};
/**
*
* @param {object} config Configuration options for the sonos client
* @returns {object}
*/
return function (config) {
return {
getEQValue: getEQValue.bind(null, config),
setEQValue: setEQValue.bind(null, config),
toggleEQValue: toggleEQValue.bind(null, config),
};
};
}());
/**
* Generic callback helper
*
* @param {string} label The name of the action
* @param {Error} err An error instance
* @param {object} result A result
* @returns
*/
const callback = function (name, err, result) {
if (err) {
return console.log(err);
}
console.log('Sonos | ' + name + ' | ' + Boolean(result.CurrentValue));
}
register('sonos', {
onClick: function (config) {
const sonos = client(config);
sonos.toggleEQValue('SurroundEnable', function (err, result) {
if (err) {
return console.log(err);
}
sonos.setEQValue(
'SubEnable',
result.CurrentValue,
callback.bind(null, 'Home Theatre')
)
});
},
onDoubleClick: function (config, button) {
const sonos = client(config);
sonos.toggleEQValue('SurroundEnable', callback.bind(null, 'Surround'));
},
onHold: function (config, button) {
const sonos = client(config);
sonos.toggleEQValue('SubEnable', callback.bind(null, 'Subwoofer'));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment