Click or drag to resize

IEventManagerRegister(EventReceiver) Method

Registers an event receiver for all events.

Namespace: Bosch.Vms.SDK
Assembly: Bosch.Vms.SDK (in Bosch.Vms.SDK.dll) Version: 1.9.0.0
Syntax
C#
bool Register(
	EventReceiver eventReceiver
)

Parameters

eventReceiver  EventReceiver
Event receiver.

Return Value

Boolean
true if the receiver is registered, false if the maximum number of receivers have already been reached.
Remarks
For each event the OnEvent(EventData) callback gets called. Events may deliver state change information (EventData). In order to receive the current states, you may call GetInitialStates (see example below).

Usage Note
The number of receivers that are allowed to register for events are currently limited to 100 receivers per Management Server. Therefore, do call Unregister(EventReceiver) when the event receiver is no longer needed.
The SDK does not support reference counting for registered receivers. This means that registering the same receiver instance multiple times will succeed, but only one call to Unregister(EventReceiver) is necessary to remove all registrations of the same receiver instance.

Example
This sample shows how to maintain a device state table. First, derive a class from EventReceiver and overwrite the event callback method OnEvent(EventData).
C#
sealed class MyEventReceiver : EventReceiver
{
    // device state table
    private readonly Dictionary<Guid, Dictionary<string, EventData.StateInfo>> allCurrentDeviceStates = new Dictionary<Guid, Dictionary<string, EventData.StateInfo>>();
    private readonly object syncStateTable = new object();

    public override void OnEvent(EventData eventData)
    {
        if (eventData.State.IsValid)
        {
            lock (syncStateTable)
            {
                Dictionary<string, EventData.StateInfo> deviceStates;
                if (!allCurrentDeviceStates.TryGetValue(eventData.Device.Id, out deviceStates))
                {
                    deviceStates = new Dictionary<string, EventData.StateInfo>();
                    allCurrentDeviceStates.Add(eventData.Device.Id, deviceStates);
                }

                deviceStates[eventData.Type] = eventData.State;
            }
        }
    }

    public string GetCurrentDeviceState(Guid deviceId, string eventType)
    {
        lock (syncStateTable)
        {
            Dictionary<string, EventData.StateInfo> deviceStates;
            if (allCurrentDeviceStates.TryGetValue(deviceId, out deviceStates))
            {
                EventData.StateInfo stateInfo;
                if (deviceStates.TryGetValue(eventType, out stateInfo))
                {
                    return stateInfo.New;
                }
            }
        }
        return String.Empty;
    }

}

After registering the event receiver, call GetInitialStates to retrieve and initialize the device state table with the currently known device states.

C#
MyEventReceiver eventReceiver = new MyEventReceiver();
remoteServerApi.EventManager.Register(eventReceiver);
remoteServerApi.DeviceManager.GetInitialStates();

Note: Instead of registering for all events of all devices, you may register e.g. only for "InputState" events using Register(EventReceiver, String) and then call GetInitialStates() from the IInputManager only.

Now, you may query the last reported device state like this:

C#
VirtualInput device = serverSdk.VirtualInputManager.GetVirtualInputByNumber(1);
Console.WriteLine("Current input state of virtual input 1 is: "+eventReceiver.GetCurrentDeviceState(device.Id, "InputState"));
Do not forget to unregister the event receiver.
C#
remoteServerApi.EventManager.Unregister(eventReceiver);

See Also