Skip to content

1.4 EventServer Class

Kirk edited this page May 7, 2017 · 67 revisions

EventServer class

This class contains all the Methods, Properties and Events used to setup an EventServer object. The EventServer object is a Tcp listener that runs in a separate thread and listens for event notification from your devices. You must send a "Subcribe" to the device for the service you want to receive event notifications from. Not all services support event notifications. (See below)

Once the EvenetServer object is running, and a Device's service has been subscribed, any Event Notifications will automatically update any changed State Variables for this service in the SonyDevice object associated.


Syntax

public class APILibrary.EventServer()


Constructor

APILibrary.EventServer myEventServer = new APILibrary.EventServer()


Methods

Start()

This method is used to Start the EventServer object.
You MUST set the following properties before executing
IpAddress - The IP Address the EventServer is running on.
Port - The Port the EventServer is running on.(See Properties below)
CallBackUrl - The URL used by the device to send notification back.

Syntax:
EventServer.Start()

Example:

 APILibrary.EventServer myEventServer = new APILibrary.EventServer();
 myEventServer.IpAddress = "192.168.0.1";
 myEventServer.Port = 8080;
 myEventServer.CallBackUrl = @"http://" + mmyEventServer.IpAddress + ":" + myEventServer.Port;
 myEventServer.Start();

Stop()

This method is used to Stop the EventServer.
Executing this method will also Unsubscribe all Event notifications previously subscribed.
This is suggested before exiting your application.

Syntax:
EventServer.Stop()

Example:

myEventServer.Stop()

SubscribeToEvents(SonyDevice, ServiceIdendifier, Duration)

This method will send a SUBSCRIBE request to the Sony device for the Service specified.
If successful, the Sony device will be added to the EventedDevices list.(See below)
The EventServer must be running before execution.

Syntax:
EventServer.SubscribeToEvents(SonyDevice, SonyDevice.Service.ServiceIdentifier, Duration)
SonyDevice - The SonyDevice object to subscribe to.
ServiceIdentifier - A string representation of a ServiceIdentifier.
Duration - An Integer that represents the number of Seconds to keep subscription alive.

Example:

myEventServer.SubscribeToEvents(mySonyDevice, mySonyDevice.RenderingControl.ServiceIdentifier, 300)

Or

myEventServer.SubscribeToEvents(mySonyDevice, "RenderingControl:1", 300)

NOTES - Not all UPnP/DLNA Services have Eventing capabilities
Currently, this library has the ability to receive event notifications from the following services:

  1. RenderingControl:1
  2. ControlManager:1
  3. AVTransport:1
  4. Party:1

The Sony IRCC:1 service does NOT support eventing!

ReSubscribeToEvents(DeviceName, ServiceIdendifier)

This method will send a RESUBSCRIBE request to the Sony device for the Service specified.
If successful, the Sony device will be updated in the EventedDevices list.(See below)
The EventServer must be running before execution.
This Device and Service MUST have been previously Subscribed!

Syntax:
EventServer.ReSubscribeToEvents(DeviceName, SonyDevice.Service.ServiceIdentifier)
DeviceName - A string representation of a SonyDevice object that has been previously subscribed to.
ServiceIdentifier - A string representation of a ServiceIdentifier.

Example:

myEventServer.ReSubscribeToEvents(mySonyDevice.Name, mySonyDevice.RenderingControl.ServiceIdentifier)

Or

myEventServer.ReSubscribe("Bravia55", "RenderingControl:1")  

UnSubscribeToEvents(DeviceName, ServiceIdendifier)

This method will send an UNSUBSCRIBE request to the Sony device for the Service specified.
If successful, the Sony device will be removed from the EventedDevices list.(See below)
The EventServer must be running before execution.
This Device and Service MUST have been previously Subscribed!

Syntax:
EventServer.UnSubscribeToEvents(DeviceName, ServiceIdentifier)
DeviceName - A string representation of a SonyDevice object that has been previously subscribed to.
ServiceIdentifier - A string representation of a ServiceIdentifier.

Example:

myEventServer.UnSubscribeToEvents(mySonyDevice.Name, mySonyDevice.RenderingControl.ServiceIdentifier)

Or

myEventServer.UnSubscribe("Bravia55", "RenderingControl:1")

Properties

IpAddress

[String]
Gets or Sets the IP Address of the TPC Listener Server.
This property MUST be set before executing the Start() method.
Example:

 myEventServer.IpAddress = "127.0.0.1";  
 myEventServer.IpAddress = "192.168.0.100";

Port

[Integer]
Gets or Sets the Port of the TPC Listener Server.
This property MUST be set before executing the Start() method.
Example:

 myEventServer.Port = 8080;  

IsRunning

[Boolean]
Returns the Status of the TPC Listener Server.
Example:

 boolean IsRun = myEventServer.IsRunning;  

CallBackUrl

[String]
Gets or Sets the CallBackUrl of the TPC Listener Server.
This is used in the Subscribe() method, and tells the device the return URL to send the event notifications to.
This property MUST be set before executing the Start() method.
Example:

 myEventServer.CallBackUrl = @"Http://" + myEventServer.IpAddress + ":" + myEventServer.port;

Output

[String]
Returns the last received Event Notification Information.
Example:

 string myNotificationInfo = myEventServer.Output;

Events

PropertyChanged()

This event will fire every time the EventServer object's Output property changes.
This will indicate that a device has sent an Event Notification.
Included with the notification will be State Variable properties that have changed at the device.
You can integrate this in to your own application by subscribing to this event.
It is best to set this before executing the Start() method.

Syntax:
myEventServer.PropertyChanged += YourMethodName;

Example:

class Program
{
    private static APILibrary mySonyLib = new APILibrary();
    private static APILibrary.SonyDevice mySonyDevice = new APILibrary.SonyDevice();
    private static APILibrary.EventServer myEventServer = new APILibrary.EventServer();

    static void Main(string[] args)
    {
        mySonyLib.Log.Enable = true;
        mySonyDevice = mySonyLib.Locator.DeviceLoad(@"C:\myDevices\STR-DN840.xml");
        myEventServer.IpAddress = "192.168.0.100";
        myEventServer.Port = 8080;
        myEventServer.CallBackUrl = @"http://192.168.0.100:" + myEventServer.Port;
        myEventServer.PropertyChanged += EventServerOnChange;
        myEventServer.Start();
        myEventServer.SubscribeToEvents(mySonyDevice,mySonyDevice.RenderingControl.ServiceIdentifier,1800);
        while (true)
        {
            Console.WriteLine("Type ‘End’ and hit Enter to Quit.");
            string cki = Console.ReadLine();
            if (cki == "End")
            {
                ShutDown(myEventServer);
            }
        }
    }

    private static void ShutDown(APILibrary.EventServer x)
    {
        x.Stop();
        Environment.Exit(1);
    }

    static void EventServerOnChange(object sender, EventArgs args)
    {
        Console.WriteLine(myEventServer.Output);
    }
}

The above console application example will create an EventServer object called myEventServer, that Subscribes to the loaded SonyDevice for the RenderingControl:1 Service. We also subscribe our apllication to the EventServer's PropertyChanged event that will call our method EventServerOnChange. Once running, when a notification is receied, the EventServerOnChange method will show the notification information received from the device for the RenderingControl:1 service in the console window. For example, if you run the above application, you should then be able to change the Volume at the device, and see these changes happen in the console window.


EventedDevices

The EventedDevices is a list of all currently subscribed devices. This list is used by the EventServer to determine which device and/or service has sent the event notification. This also allows the EventServer to automatically update the devices state variables for the service subscribed as they change! Devices are added to the list when there has been a successful subscription made. Devices are Updated when a successful Re-subscription is made, and are removed when a successful Unsubscribe has been made.