1 | //------------------------------------------------------------------------------
|
---|
2 | // <copyright file="SensorStreamHandlerBase.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;
|
---|
10 | using System.Collections.Generic;
|
---|
11 | using System.Linq;
|
---|
12 | using System.Net;
|
---|
13 | using System.Threading.Tasks;
|
---|
14 |
|
---|
15 | using Microsoft.Kinect;
|
---|
16 |
|
---|
17 | using PropertyMap = System.Collections.Generic.Dictionary<string, object>;
|
---|
18 |
|
---|
19 | /// <summary>
|
---|
20 | /// Base implementation for <see cref="ISensorStreamHandler"/> interface.
|
---|
21 | /// </summary>
|
---|
22 | public class SensorStreamHandlerBase : ISensorStreamHandler
|
---|
23 | {
|
---|
24 | /// <summary>
|
---|
25 | /// Map of supported stream names to corresponding stream configurations.
|
---|
26 | /// </summary>
|
---|
27 | private readonly Dictionary<string, StreamConfiguration> streamHandlerConfiguration = new Dictionary<string, StreamConfiguration>();
|
---|
28 |
|
---|
29 | /// <summary>
|
---|
30 | /// Object used to discard errors when clients didn't specify an error dictionary.
|
---|
31 | /// </summary>
|
---|
32 | private readonly IDictionary<string, object> errorSink = new PropertyMap();
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// Array of supported stream names.
|
---|
36 | /// </summary>
|
---|
37 | private string[] supportedStreams;
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Initializes a new instance of the <see cref="SensorStreamHandlerBase"/> class.
|
---|
41 | /// </summary>
|
---|
42 | protected SensorStreamHandlerBase()
|
---|
43 | {
|
---|
44 | }
|
---|
45 |
|
---|
46 | /// <summary>
|
---|
47 | /// Get the names of the stream(s) supported by this stream handler.
|
---|
48 | /// </summary>
|
---|
49 | /// <returns>
|
---|
50 | /// An array of stream names.
|
---|
51 | /// </returns>
|
---|
52 | /// <remarks>
|
---|
53 | /// These names will be used in JSON objects to refer to individual streams.
|
---|
54 | /// </remarks>
|
---|
55 | public string[] GetSupportedStreamNames()
|
---|
56 | {
|
---|
57 | return this.supportedStreams ?? (this.supportedStreams = this.streamHandlerConfiguration.Keys.ToArray());
|
---|
58 | }
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | /// Lets ISensorStreamHandler know that Kinect Sensor associated with this stream
|
---|
62 | /// handler has changed.
|
---|
63 | /// </summary>
|
---|
64 | /// <param name="newSensor">
|
---|
65 | /// New KinectSensor.
|
---|
66 | /// </param>
|
---|
67 | public virtual void OnSensorChanged(KinectSensor newSensor)
|
---|
68 | {
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Process data from one Kinect color frame.
|
---|
73 | /// </summary>
|
---|
74 | /// <param name="colorData">
|
---|
75 | /// Kinect color data.
|
---|
76 | /// </param>
|
---|
77 | /// <param name="colorFrame">
|
---|
78 | /// <see cref="ColorImageFrame"/> from which we obtained color data.
|
---|
79 | /// </param>
|
---|
80 | public virtual void ProcessColor(byte[] colorData, ColorImageFrame colorFrame)
|
---|
81 | {
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Process data from one Kinect depth frame.
|
---|
86 | /// </summary>
|
---|
87 | /// <param name="depthData">
|
---|
88 | /// Kinect depth data.
|
---|
89 | /// </param>
|
---|
90 | /// <param name="depthFrame">
|
---|
91 | /// <see cref="DepthImageFrame"/> from which we obtained depth data.
|
---|
92 | /// </param>
|
---|
93 | public virtual void ProcessDepth(DepthImagePixel[] depthData, DepthImageFrame depthFrame)
|
---|
94 | {
|
---|
95 | }
|
---|
96 |
|
---|
97 | /// <summary>
|
---|
98 | /// Process data from one Kinect skeleton frame.
|
---|
99 | /// </summary>
|
---|
100 | /// <param name="skeletons">
|
---|
101 | /// Kinect skeleton data.
|
---|
102 | /// </param>
|
---|
103 | /// <param name="skeletonFrame">
|
---|
104 | /// <see cref="SkeletonFrame"/> from which we obtained skeleton data.
|
---|
105 | /// </param>
|
---|
106 | public virtual void ProcessSkeleton(Skeleton[] skeletons, SkeletonFrame skeletonFrame)
|
---|
107 | {
|
---|
108 | }
|
---|
109 |
|
---|
110 | /// <summary>
|
---|
111 | /// Gets the state property values associated with the specified stream name.
|
---|
112 | /// </summary>
|
---|
113 | /// <param name="streamName">
|
---|
114 | /// Name of stream for which property values should be returned.
|
---|
115 | /// </param>
|
---|
116 | /// <returns>
|
---|
117 | /// Dictionary mapping property names to property values.
|
---|
118 | /// </returns>
|
---|
119 | public IDictionary<string, object> GetState(string streamName)
|
---|
120 | {
|
---|
121 | var propertyMap = new Dictionary<string, object>();
|
---|
122 |
|
---|
123 | StreamConfiguration config;
|
---|
124 | if (!this.streamHandlerConfiguration.TryGetValue(streamName, out config))
|
---|
125 | {
|
---|
126 | throw new ArgumentException(@"Unsupported stream name", "streamName");
|
---|
127 | }
|
---|
128 |
|
---|
129 | config.GetPropertiesCallback(propertyMap);
|
---|
130 |
|
---|
131 | return propertyMap;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /// <summary>
|
---|
135 | /// Attempts to set the specified state property values associated with the specified
|
---|
136 | /// stream name.
|
---|
137 | /// </summary>
|
---|
138 | /// <param name="streamName">
|
---|
139 | /// Name of stream for which property values should be set.
|
---|
140 | /// </param>
|
---|
141 | /// <param name="properties">
|
---|
142 | /// Dictionary mapping property names to property values that should be set.
|
---|
143 | /// Must not be null.
|
---|
144 | /// </param>
|
---|
145 | /// <param name="errors">
|
---|
146 | /// Dictionary meant to receive mappings between property names to errors encountered
|
---|
147 | /// while trying to set each property value.
|
---|
148 | /// May be null.
|
---|
149 | /// </param>
|
---|
150 | /// <returns>
|
---|
151 | /// true if there were no errors encountered while setting state. false otherwise.
|
---|
152 | /// </returns>
|
---|
153 | /// <remarks>
|
---|
154 | /// If <paramref name="errors"/> is non-null, it is expected that it will be empty
|
---|
155 | /// when method is called.
|
---|
156 | /// </remarks>
|
---|
157 | public bool SetState(string streamName, IReadOnlyDictionary<string, object> properties, IDictionary<string, object> errors)
|
---|
158 | {
|
---|
159 | bool successful = true;
|
---|
160 |
|
---|
161 | if (properties == null)
|
---|
162 | {
|
---|
163 | throw new ArgumentException(@"properties must not be null", "properties");
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (errors == null)
|
---|
167 | {
|
---|
168 | // Guarantee code down the line that errors will not be null, but don't let
|
---|
169 | // the error sink ever get too large.
|
---|
170 | this.errorSink.Clear();
|
---|
171 | errors = this.errorSink;
|
---|
172 | }
|
---|
173 |
|
---|
174 | StreamConfiguration config;
|
---|
175 | if (!this.streamHandlerConfiguration.TryGetValue(streamName, out config))
|
---|
176 | {
|
---|
177 | throw new ArgumentException(@"Unsupported stream name", "streamName");
|
---|
178 | }
|
---|
179 |
|
---|
180 | foreach (var keyValuePair in properties)
|
---|
181 | {
|
---|
182 | try
|
---|
183 | {
|
---|
184 | var error = config.SetPropertyCallback(keyValuePair.Key, keyValuePair.Value);
|
---|
185 | if (error != null)
|
---|
186 | {
|
---|
187 | errors.Add(keyValuePair.Key, error);
|
---|
188 | successful = false;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | catch (InvalidOperationException)
|
---|
192 | {
|
---|
193 | successful = false;
|
---|
194 | errors.Add(keyValuePair.Key, Properties.Resources.PropertySetError);
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | return successful;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /// <summary>
|
---|
202 | /// Handle an http request.
|
---|
203 | /// </summary>
|
---|
204 | /// <param name="streamName">
|
---|
205 | /// Name of stream for which property values should be set.
|
---|
206 | /// </param>
|
---|
207 | /// <param name="requestContext">
|
---|
208 | /// Context containing HTTP request data, which will also contain associated
|
---|
209 | /// response upon return.
|
---|
210 | /// </param>
|
---|
211 | /// <param name="subpath">
|
---|
212 | /// Request URI path relative to the stream name associated with this sensor stream
|
---|
213 | /// handler in the stream handler owner.
|
---|
214 | /// </param>
|
---|
215 | /// <returns>
|
---|
216 | /// Await-able task.
|
---|
217 | /// </returns>
|
---|
218 | /// <remarks>
|
---|
219 | /// Return value should never be null. Implementations should use Task.FromResult(0)
|
---|
220 | /// if function is implemented synchronously so that callers can await without
|
---|
221 | /// needing to check for null.
|
---|
222 | /// </remarks>
|
---|
223 | public virtual Task HandleRequestAsync(string streamName, HttpListenerContext requestContext, string subpath)
|
---|
224 | {
|
---|
225 | KinectRequestHandler.CloseResponse(requestContext, HttpStatusCode.NotFound);
|
---|
226 | return SharedConstants.EmptyCompletedTask;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /// <summary>
|
---|
230 | /// Cancel all pending operations
|
---|
231 | /// </summary>
|
---|
232 | public virtual void Cancel()
|
---|
233 | {
|
---|
234 | }
|
---|
235 |
|
---|
236 | /// <summary>
|
---|
237 | /// Lets handler know that it should clean up resources associated with sensor stream
|
---|
238 | /// handling.
|
---|
239 | /// </summary>
|
---|
240 | /// <returns>
|
---|
241 | /// Await-able task.
|
---|
242 | /// </returns>
|
---|
243 | /// <remarks>
|
---|
244 | /// Return value should never be null. Implementations should use Task.FromResult(0)
|
---|
245 | /// if function is implemented synchronously so that callers can await without
|
---|
246 | /// needing to check for null.
|
---|
247 | /// </remarks>
|
---|
248 | public virtual Task UninitializeAsync()
|
---|
249 | {
|
---|
250 | return SharedConstants.EmptyCompletedTask;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /// <summary>
|
---|
254 | /// Add a configuration corresponding to the specified stream name.
|
---|
255 | /// </summary>
|
---|
256 | /// <param name="name">
|
---|
257 | /// Stream name.
|
---|
258 | /// </param>
|
---|
259 | /// <param name="configuration">
|
---|
260 | /// Stream configuration.
|
---|
261 | /// </param>
|
---|
262 | protected void AddStreamConfiguration(string name, StreamConfiguration configuration)
|
---|
263 | {
|
---|
264 | this.streamHandlerConfiguration.Add(name, configuration);
|
---|
265 | this.supportedStreams = null;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /// <summary>
|
---|
269 | /// Helper class used to configure SensorStreamHandlerBase subclass behavior.
|
---|
270 | /// </summary>
|
---|
271 | protected class StreamConfiguration
|
---|
272 | {
|
---|
273 | /// <summary>
|
---|
274 | /// Initializes a new instance of the <see cref="StreamConfiguration"/> class.
|
---|
275 | /// </summary>
|
---|
276 | /// <param name="getPropertiesCallback">
|
---|
277 | /// Callback function used to get all state property values.
|
---|
278 | /// </param>
|
---|
279 | /// <param name="setPropertyCallback">
|
---|
280 | /// Callback function used to set individual state property values.
|
---|
281 | /// </param>
|
---|
282 | [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Outer generic is a functional type and won't require cumbersome syntax to use")]
|
---|
283 | public StreamConfiguration(Action<PropertyMap> getPropertiesCallback, Func<string, object, string> setPropertyCallback)
|
---|
284 | {
|
---|
285 | this.GetPropertiesCallback = getPropertiesCallback;
|
---|
286 | this.SetPropertyCallback = setPropertyCallback;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /// <summary>
|
---|
290 | /// Gets the callback function used to get all state property values.
|
---|
291 | /// </summary>
|
---|
292 | /// <remarks>
|
---|
293 | /// Callback parameter is a property name->value map where property values should
|
---|
294 | /// be set.
|
---|
295 | /// </remarks>
|
---|
296 | [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Outer generic is a functional type and won't require cumbersome syntax to use")]
|
---|
297 | public Action<PropertyMap> GetPropertiesCallback { get; private set; }
|
---|
298 |
|
---|
299 | /// <summary>
|
---|
300 | /// Gets the callback function used to set individual state property values.
|
---|
301 | /// </summary>
|
---|
302 | public Func<string, object, string> SetPropertyCallback { get; private set; }
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|