// ----------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // ----------------------------------------------------------------------- namespace Microsoft.Samples.Kinect.Webserver.Sensor { using System.Collections.Generic; using Microsoft.Kinect; using Microsoft.Samples.Kinect.Webserver.Sensor.Serialization; /// /// Implementation of ISensorStatusStreamHandler that exposes sensor status events /// public class SensorStatusStreamHandler : SensorStreamHandlerBase { /// /// JSON name of sensor stream. /// internal const string SensorStreamName = "sensorStatus"; /// /// JSON name of sensor event category. /// internal const string SensorStatusEventCategory = "sensorStatus"; /// /// JSON name of sensor event type. /// internal const string SensorStatusEventType = "statusChanged"; /// /// JSON name of sensor status connected property. /// internal const string SensorStatusConnectedPropertyName = "connected"; /// /// Context that allows this stream handler to communicate with its owner. /// private readonly SensorStreamHandlerContext ownerContext; /// /// Sensor providing data to sensor state. /// private KinectSensor sensor; /// /// true if kinect sensor is connected. /// private bool isConnected; /// /// Initializes a new instance of the class /// and associates it with a context that allows it to communicate with its owner. /// /// /// An instance of class. /// internal SensorStatusStreamHandler(SensorStreamHandlerContext ownerContext) { this.ownerContext = ownerContext; this.AddStreamConfiguration(SensorStreamName, new StreamConfiguration(this.GetSensorStreamProperties, this.SetSensorStreamProperty)); } /// /// Lets ISensorStatusStreamHandler know that Kinect Sensor associated with this stream /// handler has changed. /// /// /// New KinectSensor. /// public async override void OnSensorChanged(KinectSensor newSensor) { if (this.sensor != newSensor) { bool oldConnected = this.isConnected; this.isConnected = newSensor != null; if (oldConnected != this.isConnected) { await this.ownerContext.SendEventMessageAsync(new SensorStatusEventMessage { category = SensorStatusEventCategory, eventType = SensorStatusEventType, connected = this.isConnected }); } } this.sensor = newSensor; } /// /// Gets a sensor stream property value. /// /// /// Property name->value map where property values should be set. /// private void GetSensorStreamProperties(Dictionary propertyMap) { propertyMap.Add(SensorStatusConnectedPropertyName, this.isConnected); } /// /// Set a sensor stream property value. /// /// /// Name of property to set. /// /// /// Property value to set. /// /// /// null if property setting was successful, error message otherwise. /// private string SetSensorStreamProperty(string propertyName, object propertyValue) { if (propertyName == SensorStatusConnectedPropertyName) { return Properties.Resources.PropertyReadOnly; } else { return Properties.Resources.PropertyNameUnrecognized; } } } }