Haiku Squeezebox / Logitech Media Center and HaikuHelper

neillt

Active Member
All,
 
I thought I would share the bit of code I wrote to control a Logitech Media Server (also called a Squeezecenter, Squeezebox Center and Squeezebox Media Server) from HaikuHelper.  Hopefully this can be of some use by someone else.
 
This lets you translate actions or triggers on a Lumina or Omni into actions on the squeezeboxes in your house.  Turning off all of the players when you arm the alarm for example... or actions in response to button presses.
 
I only have coded the basic transport commands, the way playlists and what not work are more complicated and I am not sure how much use I would have for that right now as I would change the actual music via the iPeng app.
 
Everything starts off at the top declaring the variables for the LMS.  You need to supply the IP and Port (9000 is the default) of the server, and then the MAC Addresses of each player in an array:
 

// Variables for Logitech Media Center

var lmsIP = "192.168.71.23";
var lmsPort = "9000";
var lmsPlayer = ["00:04:20:1e:18:0a", "00:04:20:1f:49:2d"]; // Master Bedroom, Master Bathroom

The first player listed will be Player 0, the next is Player 1, and so on.
 
Then you need the code that will actually build and submit the HTTP requests to the LMS:
 

// Functions to support HTTP requests to devices


function httpGet(theURL)
{
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET",theURL,false);
xmlHttp.send(null);
return xmlHttp.responseText;
}

// Logitech Media Center actions

function lmsSendCommand(thePlayer,p0,p1,p2,p3)
{
var lmsURL = null;
// Generate the base of the URL
lmsURL = "http://" + lmsIP + ":"+lmsPort+"/status.txt?p0="+p0
// Check and see if Parameter 1 is passed
if (p1 != null) {
lmsURL = lmsURL +"&p1="+p1;
}
// Check and see if Parameter 2 is passed
if (p2 != null) {
lmsURL = lmsURL +"&p2="+p2;
}
// Check and see if Parameter 3 is passed
if (p3 != null) {
lmsURL = lmsURL +"&p3="+p3;
}
// Add the player MAC at the end
lmsURL = lmsURL+"&player=" + lmsPlayer[thePlayer];
// helper.log(controller,"The URL will be "+lmsURL);
httpGet(lmsURL);
}

I have disabled the logging of the actual URL just to clean up the controller log, but if you want to see how the URL looks just uncomment that second to last line.  These two scripts work together to build the HTTP request, including any required parameters.  The httpGet function is generic and can be used for any HTTP request, not just for the LMS.
 
Finally, we have all of the individual functions to perform remote control actions.  Most commands only need the Player Number, as determined by the order you placed them in the array.
 
For example, to start playing on Player 0, you would execute:

lmsPlay(0);


But some commands need extra parameters, the Volume command for example needs the player ID and the new volume from 0 to 100.  This would be how to set the volume of player 0 to the volume level 50:
 

lmsVolume(0,50);

 
Finally, here is the dump of all of the functions that I have coded so far:
 
Code:
// LMS Action - Player Play
function lmsPlay(thePlayer) 
{
	helper.log(controller,"LMS Action - Play Player "+thePlayer);
	lmsSendCommand(thePlayer,"play");
}

// LMS Action - Player Stop
function lmsStop(thePlayer) 
{
	helper.log(controller,"LMS Action - Stop Player "+thePlayer);
	lmsSendCommand(thePlayer,"stop");
}

// LMS Action - Player Sleep
function lmsSleep(thePlayer,seconds) 
{
	helper.log(controller,"LMS Action - Sleep Player "+thePlayer+" for "+seconds+" seconds");
	lmsSendCommand(thePlayer,"sleep",seconds);
}


// LMS Action - Player Pause
function lmsPause(thePlayer) 
{
	helper.log(controller,"LMS Action - Pause Player "+thePlayer);
	lmsSendCommand(thePlayer,"pause","1");
}

// LMS Action - Player Unpause
function lmsUnpause(thePlayer) 
{
	helper.log(controller,"LMS Action - Unpause Player "+thePlayer);
	lmsSendCommand(thePlayer,"pause","0");
}

// LMS Action - Player Pause Toggle
function lmsPauseToggle(thePlayer) 
{
	helper.log(controller,"LMS Action - Pause Toggle Player "+thePlayer);
	lmsSendCommand(thePlayer,"pause");
}

// LMS Action - Player Volume
function lmsVolume(thePlayer,volume) 
{
	helper.log(controller,"LMS Action - Change Player "+thePlayer+" volume to "+volume);
	lmsSendCommand(thePlayer,"mixer","volume",volume);
}

// LMS Action - Player Mute On
function lmsMuteOn(thePlayer) 
{
	helper.log(controller,"LMS Action - Mute On Player "+thePlayer);
	lmsSendCommand(thePlayer,"mixer","muting","1");
}

// LMS Action - Player Mute Off
function lmsMuteOff(thePlayer) 
{
	helper.log(controller,"LMS Action - Mute Off Player "+thePlayer);
	lmsSendCommand(thePlayer,"mixer","muting","0");
}

// LMS Action - Player Mute Toggle
function lmsMuteToggle(thePlayer) 
{
	helper.log(controller,"LMS Action - Mute Toggle Player "+thePlayer);
	lmsSendCommand(thePlayer,"mixer","muting");
}

// LMS Action - Player Display Change
function lmsPlayerDisplay(thePlayer,line1,line2,seconds) 
{
	helper.log(controller,"LMS Action - Display "+line1+" and "+line2+" on player "+thePlayer+" for "+seconds+" seconds");
	lmsSendCommand(thePlayer,"display",line1,line2,seconds);
}

// LMS Action - Press remote button
function lmsPressButton(thePlayer,code) 
{
	helper.log(controller,"LMS Action - Pressing Button Code "+code+" on player "+thePlayer);
	lmsSendCommand(thePlayer,"button",code);
}

// LMS Action - Player Power On
function lmsPowerOn(thePlayer) 
{
	helper.log(controller,"LMS Action - Power On Player "+thePlayer);
	lmsSendCommand(thePlayer,"power","1");
}

// LMS Action - Player Power Off
function lmsPowerOff(thePlayer) 
{
	helper.log(controller,"LMS Action - Power Off Player "+thePlayer);
	lmsSendCommand(thePlayer,"power","0");
}

// LMS Action - Player Power Toggle
function lmsPowerOn(thePlayer) 
{
	helper.log(controller,"LMS Action - Toggle Power Player "+thePlayer);
	lmsSendCommand(thePlayer,"power");
}

// LMS Action - Synchronize Player
function lmsSyncPlayer(thePlayer,syncPlayer) 
{
	helper.log(controller,"LMS Action - Synchronize Player "+thePlayer+" tp player "+syncPlayer);
	syncplayerID=lmsPlayer[syncPlayer];
	lmsSendCommand(thePlayer,"sync",syncplayerID);
}

// LMS Action - Un-Synchronize Player
function lmsUnSyncPlayer(thePlayer) 
{
	helper.log(controller,"LMS Action - Remove Sync on Player "+thePlayer);
	lmsSendCommand(thePlayer,"sync","-");
}
 
Back
Top