Subscribing to DigiLinX

As a developer who is interested in working with the NetStreams DigiLinX infrastructure, you have two basic integration options. This blog has focused mostly on the first option, which is to develop Lua drivers that allow the DigiLinX system to issue commands to an external system. A good example of this is the Proliphix driver I recently published.

The other option, which I haven’t yet covered on this blog, is to communicate with DigiLinX using their ASCII protocol. DigiLinX devices communicate with each other by broadcasting specially formatted ASCII instructions to a multicast address, allowing other devices who are interested to “tune in” for updates. These messages contain data that contains either control or status information. My DigiLinX debugger works in this fashion, listening for all debug messages that arrive at the multicast address.

But what if, as a third party system, we’re not interested in every message that is sent across the wire? What if we’re writing an application that simply wants to know when a song on a stream has changed or when a specific SpeakerLinX amplifier has been turned on? DigiLinX offers us an alternative solution in this case. Using Dealer Setup, one can configure what NetStreams refers to as a “subscription”. By providing an IP address and port number, the integrator can direct the DigiLinX system to sent ASCII messages to a device using UDP. The integrator can further specify what they are interested in by subscribing only to those services which interest him. Let’s look at an example.

Setting Up A Subscriber

My goal for this exercise is to be alerted whenever an event occurs within an amplifier in one of the rooms. Maybe I’d like to shut off one amplifier when another one is turned on. Maybe I’d like to update a UI with the current volume level in each room. Maybe I just need an example.

In this test project, I have one SL220, one TL380, and a CL100. I’ve also added a dummy SL220 to give my system interface a second room.

Project List

I’d like to configure my development machine as a subscriber. I’ll write code that listens on a specific port for messages arriving from the Digi system. To do this, from within Dealer Setup 2.2 I click Tools -> Edit Subscribers. I see the following screen (click to expand):

ConfiguredSilverSubscriber

As you can see, I’ve specified that my development machine Silver, with an IP Address of 10.15.15.56, will be listening on port 10590 for messages from the Bedroom and Study player services. There are other services available to me as well, but they’re outside of the scope of this post. For more information on which services are made available by which devices, refer to the DigiLinX Programming for 3rd Party Control document available in the dealer area on NetStreams.com.

Once my machine is subscribed, I’ll send the updated configuration to the devices and wait for them to restart. When they do, status updates will start being delivered to my machine for the services I’ve subscribed to. Let’s see what they’re saying to me by writing code to listen on that port.

A Basic Client

I’m going to throw together a basic client that simply dumps the arriving messages to the console. Using Visual Studio.Net, I’ll create a new C# Console application. Visual Studio creates a default Program.cs file with a static void Main method in it. This is the code that will run automatically when the console app is executed. I’ll stub out a method call:

private static void Main(string[] args) {
    TestSubscriber();
}

Now I have to write my TestSubscriber method. It need to:

  1. Open a socket and bind to port 10590 (the subscriber port)
  2. Create a buffer that’s large enough to handle the incoming messages
  3. Tell the socket that we want the next arriving UDP packet and then
  4. Write it to the console.

Easy enough, since I do much the same thing with my DigiLinX Debugger. Sorry about the formatting:

private static void TestSubscriber() {
  Socket aSocket = new Socket(AddressFamily.InterNetwork,
                              SocketType.Dgram,
                              ProtocolType.Udp);

  IPEndPoint anEndpoint = new IPEndPoint(IPAddress.Any, 10590);
  aSocket.Bind(anEndpoint);
  char[] myTrimChars = new char[] {”, ‘\n’, ‘\r’};

  while (true) {
      byte[] buffer = new byte[8192];
      int numBytes = aSocket.Receive(buffer);
      Console.WriteLine(“{0} – {1}”,
          numBytes,
          Encoding.ASCII.GetString(buffer).Trim(myTrimChars));
  }
}

And there you go – that’s all the code you need to act as a subscriber. Fire it up, press a few buttons on the TouchLinX while listening to audio, and you’ll see the following:

Subscriber Output

All that’s left now is to write some code to parse the messages, looking for those that interest you. Again, information about the formatting of these messages can be found in the Programming for 3rd Party Control document published by NetStreams. 

Leave a Reply