IEvent |
bool Register( EventReceiver eventReceiver )
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.
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.
MyEventReceiver eventReceiver = new MyEventReceiver();
remoteServerApi.EventManager.Register(eventReceiver);
remoteServerApi.DeviceManager.GetInitialStates();Now, you may query the last reported device state like this:
VirtualInput device = serverSdk.VirtualInputManager.GetVirtualInputByNumber(1); Console.WriteLine("Current input state of virtual input 1 is: "+eventReceiver.GetCurrentDeviceState(device.Id, "InputState"));
remoteServerApi.EventManager.Unregister(eventReceiver);