/
OsInformation.cs
256 lines (220 loc) · 8.02 KB
/
OsInformation.cs
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
//----------------------------------------------------------------------------------------------
// <copyright file="OsInformation.cs" company="Microsoft Corporation">
// Licensed under the MIT License. See LICENSE.TXT in the project root license information.
// </copyright>
//----------------------------------------------------------------------------------------------
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using System.Threading.Tasks;
namespace Microsoft.Tools.WindowsDevicePortal
{
/// <summary>
/// Wrappers for OS Information.
/// </summary>
public partial class DevicePortal
{
/// <summary>
/// API for getting the device family.
/// </summary>
public static readonly string DeviceFamilyApi = "api/os/devicefamily";
/// <summary>
/// API for getting the machine name.
/// </summary>
public static readonly string MachineNameApi = "api/os/machinename";
/// <summary>
/// API for getting the OS information.
/// </summary>
public static readonly string OsInfoApi = "api/os/info";
/// <summary>
/// Device portal platforms
/// </summary>
public enum DevicePortalPlatforms
{
/// <summary>
/// Unknown platform
/// </summary>
Unknown = -1,
/// <summary>
/// Windows platform
/// </summary>
Windows = 0,
/// <summary>
/// Mobile platform
/// </summary>
Mobile,
/// <summary>
/// HoloLens platform
/// </summary>
HoloLens,
/// <summary>
/// Xbox One platform
/// </summary>
XboxOne,
/// <summary>
/// Windows IoT on Dragonboard 410c
/// </summary>
IoTDragonboard410c,
/// <summary>
/// Windows IoT on Minnowboard Max
/// </summary>
IoTMinnowboardMax,
/// <summary>
/// Windows IoT on Raspberry Pi 2
/// </summary>
IoTRaspberryPi2,
/// <summary>
/// Windows IoT on Raspberry Pi 3
/// </summary>
IoTRaspberryPi3,
}
/// <summary>
/// Gets the family name (ex: Windows.Holographic) of the device.
/// </summary>
/// <returns>String containing the device's family.</returns>
public async Task<string> GetDeviceFamily()
{
DeviceOsFamily deviceFamily = await this.Get<DeviceOsFamily>(DeviceFamilyApi);
return deviceFamily.Family;
}
/// <summary>
/// Gets the name of the device.
/// </summary>
/// <returns>String containing the device's name.</returns>
public async Task<string> GetDeviceName()
{
DeviceName deviceName = await this.Get<DeviceName>(MachineNameApi);
return deviceName.Name;
}
/// <summary>
/// Gets information about the device's operating system.
/// </summary>
/// <returns>OperatingSystemInformation object containing details of the installed operating system.</returns>
public async Task<OperatingSystemInformation> GetOperatingSystemInformation()
{
return await this.Get<OperatingSystemInformation>(OsInfoApi);
}
/// <summary>
/// Sets the device's name
/// </summary>
/// <param name="name">The name to assign to the device.</param>
/// <param name="reboot">True to reboot the device after setting the name, false otherwise.</param>
/// <remarks>The new name does not take effect until the device has been restarted.</remarks>
/// <returns>Task tracking setting the device name completion.</returns>
public async Task SetDeviceName(
string name,
bool reboot = true)
{
await this.Post(
MachineNameApi,
string.Format("name={0}", Utilities.Hex64Encode(name)));
// Names do not take effect until after a reboot.
if (reboot)
{
await this.Reboot();
}
// TODO - wait until device has rebooted then reconnect
}
#region Data contract
/// <summary>
/// Device name object.
/// </summary>
[DataContract]
public class DeviceName
{
/// <summary>
/// Gets or sets the name.
/// </summary>
[DataMember(Name = "ComputerName")]
public string Name { get; set; }
}
/// <summary>
/// Device family object.
/// </summary>
[DataContract]
public class DeviceOsFamily
{
/// <summary>
/// Gets or sets the device family name.
/// </summary>
[DataMember(Name = "DeviceType")]
public string Family { get; set; }
}
/// <summary>
/// Operating system information.
/// </summary>
[DataContract]
public class OperatingSystemInformation
{
/// <summary>
/// Gets or sets the OS name.
/// </summary>
[DataMember(Name = "ComputerName")]
public string Name { get; set; }
/// <summary>
/// Gets or sets the language
/// </summary>
[DataMember(Name = "Language")]
public string Language { get; set; }
/// <summary>
/// Gets or sets the edition
/// </summary>
[DataMember(Name = "OsEdition")]
public string OsEdition { get; set; }
/// <summary>
/// Gets or sets the edition Id
/// </summary>
[DataMember(Name = "OsEditionId")]
public uint OsEditionId { get; set; }
/// <summary>
/// Gets or sets the OS version
/// </summary>
[DataMember(Name = "OsVersion")]
public string OsVersionString { get; set; }
/// <summary>
/// Gets or sets the raw platform type
/// </summary>
[DataMember(Name = "Platform")]
public string PlatformName { get; set; }
/// <summary>
/// Gets the platform
/// </summary>
public DevicePortalPlatforms Platform
{
get
{
DevicePortalPlatforms platform = DevicePortalPlatforms.Unknown;
try
{
switch (this.PlatformName)
{
case "Xbox One":
platform = DevicePortalPlatforms.XboxOne;
break;
case "Dragonboard 401c":
platform = DevicePortalPlatforms.IoTDragonboard410c;
break;
case "Minnowboard Max":
platform = DevicePortalPlatforms.IoTMinnowboardMax;
break;
case "Raspberry Pi 2":
platform = DevicePortalPlatforms.IoTRaspberryPi2;
break;
case "Raspberry Pi 3":
platform = DevicePortalPlatforms.IoTRaspberryPi3;
break;
default:
platform = (DevicePortalPlatforms)Enum.Parse(typeof(DevicePortalPlatforms), this.PlatformName);
break;
}
}
catch
{
}
return platform;
}
}
}
#endregion // Data contract
}
}