Date:     22.Jul.2008
Categories:      C_Sharp, Home_Automation,
Comment:     2 comments

As my quest for better home automation and move away form OS X continues, I was looking for away to script Pandora on Windows. After Googleing a few times I was unable to find an equivalent to PandoraBoy (http://code.google.com/p/pandoraboy/ scriptable Pandora Client for OS X) for Windows (not that I am complaining, I found PandoraBoy to be buggy when scripting it). I did how ever find an open source project called Open Pandora (http://openpandora.googlepages.com/) that was written in C# and had internal calls to functions like Play, Stop, Next, Like, and Dislike. (And before you ask, it doesn't have all the restrictions that PandoraBoy has, a.k.a. No bugs that I see).

So since there was no scripting interface to Open Pandora I needed to add one. Since the rest of my home automation application is written in Python, the scripting interface needed to accessible to non-.Net languages. This fact rules out the .Net IPC library. Pretty much my entire day job consists of writing WCF code but I fell like that is to heavy of a solution to address this problem. The way I decided to make Open Pandora scriptable is by adding a simple TCP listener into the code. This was when Open Pandora runs it will also listen on a TCP port that I can send command to via any program that has some kind of socket API.

The Open Pandora solution has a file called Pandora.cs that abstracts all the function that the player can do. This is where I decided to add my listener since I would have access to all the functions with out have to modify the rest of the project. Below is a list of code changes I made to the file:

1) Added a global private variable called tcpLinstener of type System.Net.Sockets.TcpListener to the class
private System.Net.Sockets.TcpListener tcpLinstener;

2) In the constructor for the Pandora object, I initialized the listener:
tcpLinstener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 5654);
tcpLinstener.Start();
tcpLinstener.BeginAcceptTcpClient(new AsyncCallback(GotTcpMsg), null);

3) Now I need to create the call back event handler called GotTcpMsg. This event handler should get the tcp stream, and try and figure out what to do with it. I am also useing an enum to make things more clear:
void GotTcpMsg(IAsyncResult result)
{
    System.Net.Sockets.TcpClient client = tcpLinstener.EndAcceptTcpClient(result);
    tcpLinstener.BeginAcceptTcpClient(new AsyncCallback(GotTcpMsg), null);

    PandoraAction action = (PandoraAction)client.GetStream().ReadByte();
    client.Close();

    switch (action)
    {
        case PandoraAction.Hate:
            this.Hate();
            break;
        case PandoraAction.Like:
            this.Like();
            break;
        case PandoraAction.Next:
            this.NextTrack();
            break;
        case PandoraAction.Play:
            this.PlayPause();
            break;
        case PandoraAction.Stop:
            this.PlayPause();
            break;
        case PandoraAction.VolumeUp:
            this.VolumeUp();
            break;
        case PandoraAction.VolumeDown:
            this.VolumeDown();
            break;
    }
}

4) The PandoraAction enum looks something like:
public enum PandoraAction
{
    Play=1,
    Stop=2,
    Next=3,
    Like=4,
    Hate=5,
    VolumeUp=6,
    VolumeDown=7
}

And that is all there is to it. If you want the complete source code or binary, go to the contact page and let me know. I am going to attempt to get with the Open Pandora people and try to get his incorporated in to the project. Below are two really short test clients just to see and example of use:

C#)
//This should toggle the playing of Pandora static void Main(string[] args)
{
    System.Net.Sockets.TcpClient client =
        new System.Net.Sockets.TcpClient("localhost", 5654);
    client.GetStream().WriteByte(1);
    client.Close();
}

Python)

 
 
 

2 Comments

No. 1)  Dtqqrjqh on Dec 24 2008.
Reply to Dtqqrjqh

Hey, i save funny photos <a href=http://nationhog.com/picture-of-doxazosin-mesylate/>here</a>

No. 2)  Sninikekneecy on Jan 4 2009.
Reply to Sninikekneecy

Bite my shiny metal ass, assholes, <a href=http://justnkldkhsjvd.com/>you were joked!</a>

Tell Me What You Think


 

Blog Categories

Projects

Weather