1 | // -----------------------------------------------------------------------
|
---|
2 | // <copyright file="SensorStatusStreamHandler.cs" company="Microsoft">
|
---|
3 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
---|
4 | // </copyright>
|
---|
5 | // -----------------------------------------------------------------------
|
---|
6 |
|
---|
7 | namespace Microsoft.Samples.Kinect.Webserver.Sensor
|
---|
8 | {
|
---|
9 | using System.Collections.Generic;
|
---|
10 |
|
---|
11 | using Microsoft.Kinect;
|
---|
12 | using Microsoft.Samples.Kinect.Webserver.Sensor.Serialization;
|
---|
13 |
|
---|
14 | /// <summary>
|
---|
15 | /// Implementation of ISensorStatusStreamHandler that exposes sensor status events
|
---|
16 | /// </summary>
|
---|
17 | public class SensorStatusStreamHandler : SensorStreamHandlerBase
|
---|
18 | {
|
---|
19 | /// <summary>
|
---|
20 | /// JSON name of sensor stream.
|
---|
21 | /// </summary>
|
---|
22 | internal const string SensorStreamName = "sensorStatus";
|
---|
23 |
|
---|
24 | /// <summary>
|
---|
25 | /// JSON name of sensor event category.
|
---|
26 | /// </summary>
|
---|
27 | internal const string SensorStatusEventCategory = "sensorStatus";
|
---|
28 |
|
---|
29 | /// <summary>
|
---|
30 | /// JSON name of sensor event type.
|
---|
31 | /// </summary>
|
---|
32 | internal const string SensorStatusEventType = "statusChanged";
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// JSON name of sensor status connected property.
|
---|
36 | /// </summary>
|
---|
37 | internal const string SensorStatusConnectedPropertyName = "connected";
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Context that allows this stream handler to communicate with its owner.
|
---|
41 | /// </summary>
|
---|
42 | private readonly SensorStreamHandlerContext ownerContext;
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Sensor providing data to sensor state.
|
---|
46 | /// </summary>
|
---|
47 | private KinectSensor sensor;
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// true if kinect sensor is connected.
|
---|
51 | /// </summary>
|
---|
52 | private bool isConnected;
|
---|
53 |
|
---|
54 | /// <summary>
|
---|
55 | /// Initializes a new instance of the <see cref="SensorStatusStreamHandler"/> class
|
---|
56 | /// and associates it with a context that allows it to communicate with its owner.
|
---|
57 | /// </summary>
|
---|
58 | /// <param name="ownerContext">
|
---|
59 | /// An instance of <see cref="SensorStreamHandlerContext"/> class.
|
---|
60 | /// </param>
|
---|
61 | internal SensorStatusStreamHandler(SensorStreamHandlerContext ownerContext)
|
---|
62 | {
|
---|
63 | this.ownerContext = ownerContext;
|
---|
64 |
|
---|
65 | this.AddStreamConfiguration(SensorStreamName, new StreamConfiguration(this.GetSensorStreamProperties, this.SetSensorStreamProperty));
|
---|
66 | }
|
---|
67 |
|
---|
68 | /// <summary>
|
---|
69 | /// Lets ISensorStatusStreamHandler know that Kinect Sensor associated with this stream
|
---|
70 | /// handler has changed.
|
---|
71 | /// </summary>
|
---|
72 | /// <param name="newSensor">
|
---|
73 | /// New KinectSensor.
|
---|
74 | /// </param>
|
---|
75 | public async override void OnSensorChanged(KinectSensor newSensor)
|
---|
76 | {
|
---|
77 | if (this.sensor != newSensor)
|
---|
78 | {
|
---|
79 | bool oldConnected = this.isConnected;
|
---|
80 | this.isConnected = newSensor != null;
|
---|
81 |
|
---|
82 | if (oldConnected != this.isConnected)
|
---|
83 | {
|
---|
84 | await this.ownerContext.SendEventMessageAsync(new SensorStatusEventMessage
|
---|
85 | {
|
---|
86 | category = SensorStatusEventCategory,
|
---|
87 | eventType = SensorStatusEventType,
|
---|
88 | connected = this.isConnected
|
---|
89 | });
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | this.sensor = newSensor;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /// <summary>
|
---|
97 | /// Gets a sensor stream property value.
|
---|
98 | /// </summary>
|
---|
99 | /// <param name="propertyMap">
|
---|
100 | /// Property name->value map where property values should be set.
|
---|
101 | /// </param>
|
---|
102 | private void GetSensorStreamProperties(Dictionary<string, object> propertyMap)
|
---|
103 | {
|
---|
104 | propertyMap.Add(SensorStatusConnectedPropertyName, this.isConnected);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /// <summary>
|
---|
108 | /// Set a sensor stream property value.
|
---|
109 | /// </summary>
|
---|
110 | /// <param name="propertyName">
|
---|
111 | /// Name of property to set.
|
---|
112 | /// </param>
|
---|
113 | /// <param name="propertyValue">
|
---|
114 | /// Property value to set.
|
---|
115 | /// </param>
|
---|
116 | /// <returns>
|
---|
117 | /// null if property setting was successful, error message otherwise.
|
---|
118 | /// </returns>
|
---|
119 | private string SetSensorStreamProperty(string propertyName, object propertyValue)
|
---|
120 | {
|
---|
121 | if (propertyName == SensorStatusConnectedPropertyName)
|
---|
122 | {
|
---|
123 | return Properties.Resources.PropertyReadOnly;
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | return Properties.Resources.PropertyNameUnrecognized;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|