forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUPnPDevice.cs
More file actions
317 lines (264 loc) · 7.31 KB
/
UPnPDevice.cs
File metadata and controls
317 lines (264 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Waher.Networking.UPnP
{
/// <summary>
/// Contains information about a device.
/// </summary>
public class UPnPDevice
{
private XmlElement xml;
private string deviceType;
private string friendlyName;
private string manufacturer;
private string manufacturerURL;
private string modelDescription;
private string modelName;
private string modelNumber;
private string modelURL;
private string serialNumber;
private string udn;
private string upc;
private UPnPIcon[] icons;
private UPnPService[] services;
private UPnPDevice[] devices;
private string presentationURL;
private Uri manufacturerURI;
private Uri modelURI;
private Uri presentationURI;
internal UPnPDevice(XmlElement Xml, Uri BaseUri, UPnPClient Client)
{
List<UPnPIcon> Icons = new List<UPnPIcon>();
List<UPnPService> Services = new List<UPnPService>();
List<UPnPDevice> Devices = new List<UPnPDevice>();
this.xml = Xml;
foreach (XmlNode N in Xml.ChildNodes)
{
switch (N.LocalName)
{
case "deviceType":
this.deviceType = N.InnerText;
break;
case "friendlyName":
this.friendlyName = N.InnerText;
break;
case "manufacturer":
this.manufacturer = N.InnerText;
break;
case "manufacturerURL":
this.manufacturerURL = N.InnerText;
this.manufacturerURI = new Uri(BaseUri, this.manufacturerURL);
break;
case "modelDescription":
this.modelDescription = N.InnerText;
break;
case "modelName":
this.modelName = N.InnerText;
break;
case "modelNumber":
this.modelNumber = N.InnerText;
break;
case "modelURL":
this.modelURL = N.InnerText;
this.modelURI = new Uri(BaseUri, this.modelURL);
break;
case "serialNumber":
this.serialNumber = N.InnerText;
break;
case "UDN":
this.udn = N.InnerText;
break;
case "UPC":
this.upc = N.InnerText;
break;
case "iconList":
foreach (XmlNode N2 in N.ChildNodes)
{
if (N2.LocalName == "icon")
Icons.Add(new UPnPIcon((XmlElement)N2, BaseUri));
}
break;
case "serviceList":
foreach (XmlNode N2 in N.ChildNodes)
{
if (N2.LocalName == "service")
Services.Add(new UPnPService((XmlElement)N2, BaseUri, Client));
}
break;
case "deviceList":
foreach (XmlNode N2 in N.ChildNodes)
{
if (N2.LocalName == "device")
Devices.Add(new UPnPDevice((XmlElement)N2, BaseUri, Client));
}
break;
case "presentationURL":
this.presentationURL = N.InnerText;
this.presentationURI = new Uri(BaseUri, this.presentationURL);
break;
}
}
this.icons = Icons.ToArray();
this.services = Services.ToArray();
this.devices = Devices.ToArray();
}
/// <summary>
/// Underlying XML definition.
/// </summary>
public XmlElement Xml
{
get { return this.xml; }
}
/// <summary>
/// Device type
/// </summary>
public string DeviceType { get { return this.deviceType; } }
/// <summary>
/// Short user-friendly title
/// </summary>
public string FriendlyName { get { return this.friendlyName; } }
/// <summary>
/// Manufacturer name
/// </summary>
public string Manufacturer { get { return this.manufacturer; } }
/// <summary>
/// URL to manufacturer site
/// </summary>
public string ManufacturerURL { get { return this.manufacturerURL; } }
/// <summary>
/// Long user-friendly title
/// </summary>
public string ModelDescription { get { return this.modelDescription; } }
/// <summary>
/// Model name
/// </summary>
public string ModelName { get { return this.modelName; } }
/// <summary>
/// Model number
/// </summary>
public string ModelNumber { get { return this.modelNumber; } }
/// <summary>
/// URL to model site
/// </summary>
public string ModelURL { get { return this.modelURL; } }
/// <summary>
/// Manufacturer's serial number
/// </summary>
public string SerialNumber { get { return this.serialNumber; } }
/// <summary>
/// Unique Device Name (uuid:UUID)
/// </summary>
public string UDN { get { return this.udn; } }
/// <summary>
/// Universal Product Code
/// </summary>
public string UPC { get { return this.upc; } }
/// <summary>
/// Icons for the device.
/// </summary>
public UPnPIcon[] Icons { get { return this.icons; } }
/// <summary>
/// Services published by the device.
/// </summary>
public UPnPService[] Services { get { return this.services; } }
/// <summary>
/// Embedded devices.
/// </summary>
public UPnPDevice[] Devices { get { return this.devices; } }
/// <summary>
/// URL for presentation
/// </summary>
public string PresentationURL { get { return this.presentationURL; } }
/// <summary>
/// URI to manufacturer site
/// </summary>
public Uri ManufacturerURI { get { return this.manufacturerURI; } }
/// <summary>
/// URI to model site
/// </summary>
public Uri ModelURI { get { return this.modelURI; } }
/// <summary>
/// URI for presentation
/// </summary>
public Uri PresentationURI { get { return this.presentationURI; } }
/// <summary>
/// <see cref="Object.ToString()"/>
/// </summary>
public override string ToString()
{
return this.friendlyName;
}
/// <summary>
/// Gets a device or embedded device, given its device type.
/// </summary>
/// <param name="DeviceType">Device type.</param>
/// <returns>Device object, if found, null otherwise.</returns>
public UPnPDevice GetDevice(string DeviceType)
{
UPnPDevice Result = null;
if (this.deviceType == DeviceType)
Result = this;
else
{
foreach (UPnPDevice Device in this.devices)
{
Result = Device.GetDevice(DeviceType);
if (!(Result is null))
break;
}
}
return Result;
}
/// <summary>
/// Gets a service, given its service type.
/// </summary>
/// <param name="ServiceType">Service type.</param>
/// <returns>Service object, if found, null otherwise.</returns>
public UPnPService GetService(string ServiceType)
{
UPnPService Result = null;
foreach (UPnPService Service in this.services)
{
if (Service.ServiceType == ServiceType)
return Service;
}
foreach (UPnPDevice Device in this.devices)
{
Result = Device.GetService(ServiceType);
if (!(Result is null))
return Result;
}
return null;
}
/// <summary>
/// Returns all devices, including the device itself and its embedded devices, and their embedded devices, and so on.
/// </summary>
public UPnPDevice[] DevicesRecursive
{
get
{
List<UPnPDevice> Result = new List<UPnPDevice>();
Result.Add(this);
foreach (UPnPDevice Device in this.devices)
Result.AddRange(Device.DevicesRecursive);
return Result.ToArray();
}
}
/// <summary>
/// Returns all services, including the service of itself and services of its embedded devices, and their embedded devices, and so on.
/// </summary>
public UPnPService[] ServicesRecursive
{
get
{
List<UPnPService> Result = new List<UPnPService>();
Result.AddRange(this.services);
foreach (UPnPDevice Device in this.devices)
Result.AddRange(Device.ServicesRecursive);
return Result.ToArray();
}
}
}
}