-
Notifications
You must be signed in to change notification settings - Fork 12
/
DeviceService.cs
317 lines (270 loc) · 11.6 KB
/
DeviceService.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
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
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace AzureIoTHub.Portal.Server.Services
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
public class DeviceService : IDeviceService
{
private readonly RegistryManager registryManager;
private readonly ServiceClient serviceClient;
private readonly ILogger<DeviceService> log;
public DeviceService(
ILogger<DeviceService> log,
RegistryManager registryManager,
ServiceClient serviceClient)
{
this.log = log;
this.serviceClient = serviceClient;
this.registryManager = registryManager;
}
/// <summary>
/// this function return a list of all edge device wthiout tags.
/// </summary>
/// <param name="continuationToken"></param>
/// <param name="searchText"></param>
/// <param name="searchStatus"></param>
/// <param name="searchType"></param>
/// <param name="pageSize"></param>
/// <returns>IEnumerable twin.</returns>
public async Task<PaginationResult<Twin>> GetAllEdgeDevice(
string continuationToken = null,
string searchText = null,
bool? searchStatus = null,
string searchType = null,
int pageSize = 10)
{
var filter = "WHERE devices.capabilities.iotEdge = true";
if (searchStatus != null)
{
filter += $" AND status = '{(searchStatus.Value ? "enabled" : "disabled")}'";
}
if (!string.IsNullOrWhiteSpace(searchText))
{
#pragma warning disable CA1308 // Normalize strings to uppercase
filter += $" AND (STARTSWITH(deviceId, '{searchText.ToLowerInvariant()}') OR (is_defined(tags.deviceName) AND STARTSWITH(tags.deviceName, '{searchText}')))";
#pragma warning restore CA1308 // Normalize strings to uppercase
}
if (!string.IsNullOrWhiteSpace(searchType))
{
filter += $" AND devices.tags.deviceType = '{ searchType }'";
}
var emptyResult = new PaginationResult<Twin>
{
Items = Enumerable.Empty<Twin>(),
TotalItems = 0
};
var count = await this.registryManager
.CreateQuery($"SELECT COUNT() as totalNumber FROM devices { filter }")
.GetNextAsJsonAsync();
if (!JObject.Parse(count.Single()).TryGetValue("totalNumber", out var result))
{
return emptyResult;
}
if (result.Value<int>() == 0)
{
return emptyResult;
}
var query = this.registryManager
.CreateQuery($"SELECT * FROM devices { filter }", pageSize);
var response = await query
.GetNextAsTwinAsync(new QueryOptions
{
ContinuationToken = continuationToken
});
return new PaginationResult<Twin>
{
Items = response,
TotalItems = result.Value<int>(),
NextPage = response.ContinuationToken
};
}
/// <summary>
/// this function return a list of all device exept edge device.
/// </summary>
/// <param name="continuationToken"></param>
/// <param name="filterDeviceType"></param>
/// <param name="excludeDeviceType"></param>
/// <param name="searchText"></param>
/// <param name="searchStatus"></param>
/// <param name="searchState"></param>
/// <param name="searchTags"></param>
/// <param name="pageSize"></param>
/// <returns>IEnumerable twin.</returns>
public async Task<PaginationResult<Twin>> GetAllDevice(
string continuationToken = null,
string filterDeviceType = null,
string excludeDeviceType = null,
string searchText = null,
bool? searchStatus = null,
bool? searchState = null,
Dictionary<string, string> searchTags = null,
int pageSize = 10)
{
var filter = "WHERE devices.capabilities.iotEdge = false";
if (!string.IsNullOrWhiteSpace(filterDeviceType))
{
filter += $" AND devices.tags.deviceType = '{ filterDeviceType }'";
}
if (!string.IsNullOrWhiteSpace(excludeDeviceType))
{
filter += $" AND (NOT is_defined(tags.deviceType) OR devices.tags.deviceType != '{ excludeDeviceType }')";
}
if (!string.IsNullOrWhiteSpace(searchText))
{
#pragma warning disable CA1308 // Normalize strings to uppercase
filter += $" AND (STARTSWITH(deviceId, '{searchText.ToLowerInvariant()}') OR (is_defined(tags.deviceName) AND STARTSWITH(tags.deviceName, '{searchText}')))";
#pragma warning restore CA1308 // Normalize strings to uppercase
}
if (searchTags != null)
{
var tagsFilterBuilder = new StringBuilder();
foreach (var item in searchTags)
{
_ = tagsFilterBuilder.Append(CultureInfo.InvariantCulture, $" AND is_defined(tags.{item.Key}) AND STARTSWITH(tags.{item.Key}, '{item.Value}')");
}
filter += tagsFilterBuilder;
}
if (searchStatus != null)
{
filter += $" AND status = '{(searchStatus.Value ? "enabled" : "disabled")}'";
}
if (searchState != null)
{
filter += $" AND connectionState = '{(searchState.Value ? "Connected" : "Disconnected")}'";
}
var emptyResult = new PaginationResult<Twin>
{
Items = Enumerable.Empty<Twin>(),
TotalItems = 0
};
var stopWatch = Stopwatch.StartNew();
var count = await this.registryManager
.CreateQuery($"SELECT COUNT() as totalNumber FROM devices { filter }")
.GetNextAsJsonAsync();
this.log.LogDebug($"Count obtained in {stopWatch.Elapsed}");
if (!JObject.Parse(count.Single()).TryGetValue("totalNumber", out var result))
{
return emptyResult;
}
if (result.Value<int>() == 0)
{
return emptyResult;
}
stopWatch.Restart();
var query = this.registryManager
.CreateQuery($"SELECT * FROM devices { filter }", pageSize);
var response = await query
.GetNextAsTwinAsync(new QueryOptions
{
ContinuationToken = continuationToken
});
this.log.LogDebug($"Data obtained in {stopWatch.Elapsed}");
return new PaginationResult<Twin>
{
Items = response,
TotalItems = result.Value<int>(),
NextPage = response.ContinuationToken
};
}
/// <summary>
/// this function return the device we want with the registry manager.
/// </summary>
/// <param name="deviceId">device id.</param>
/// <returns>Device.</returns>
public async Task<Device> GetDevice(string deviceId)
{
return await this.registryManager.GetDeviceAsync(deviceId);
}
/// <summary>
/// this function use the registry manager to find the twin
/// of a device.
/// </summary>
/// <param name="deviceId">id of the device.</param>
/// <returns>Twin of a device.</returns>
public async Task<Twin> GetDeviceTwin(string deviceId)
{
return await this.registryManager.GetTwinAsync(deviceId);
}
/// <summary>
/// this function execute a query with the registry manager to find
/// the twin of the device with this module.
/// </summary>
/// <param name="deviceId">id of the device.</param>
/// <returns>Twin of the device.</returns>
public async Task<Twin> GetDeviceTwinWithModule(string deviceId)
{
var devicesWithModules = this.registryManager.CreateQuery($"SELECT * FROM devices.modules WHERE devices.modules.moduleId = '$edgeAgent' AND deviceId in ['{deviceId}']");
while (devicesWithModules.HasMoreResults)
{
var devicesTwins = await devicesWithModules.GetNextAsTwinAsync();
return devicesTwins.ElementAt(0);
}
return null;
}
/// <summary>
/// This function create a new device with his twin.
/// </summary>
/// <param name="deviceId">the device id.</param>
/// <param name="isEdge">boolean.</param>
/// <param name="twin">the twin of my new device.</param>
/// <param name="isEnabled">the status of the device(disabled by default).</param>
/// <returns>BulkRegistryOperation.</returns>
public async Task<BulkRegistryOperationResult> CreateDeviceWithTwin(string deviceId, bool isEdge, Twin twin, DeviceStatus isEnabled = DeviceStatus.Disabled)
{
var device = new Device(deviceId)
{
Capabilities = new DeviceCapabilities { IotEdge = isEdge },
Status = isEnabled
};
return await this.registryManager.AddDeviceWithTwinAsync(device, twin);
}
/// <summary>
/// This function delete a device.
/// </summary>
/// <param name="deviceId">the device id.</param>
public async Task DeleteDevice(string deviceId)
{
await this.registryManager.RemoveDeviceAsync(deviceId);
}
/// <summary>
/// This function update a device.
/// </summary>
/// <param name="device">the device id.</param>
/// <returns>the updated device.</returns>
public async Task<Device> UpdateDevice(Device device)
{
return await this.registryManager.UpdateDeviceAsync(device);
}
/// <summary>
/// This function update the twin of the device.
/// </summary>
/// <param name="deviceId">the device id.</param>
/// <param name="twin">the new twin.</param>
/// <returns>the updated twin.</returns>
public async Task<Twin> UpdateDeviceTwin(string deviceId, Twin twin)
{
ArgumentNullException.ThrowIfNull(twin, nameof(twin));
return await this.registryManager.UpdateTwinAsync(deviceId, twin, twin.ETag);
}
/// <summary>
/// this function execute a methode on the device.
/// </summary>
/// <param name="deviceId">the device id.</param>
/// <param name="method">the cloud to device method.</param>
/// <returns>CloudToDeviceMethodResult.</returns>
public async Task<CloudToDeviceMethodResult> ExecuteC2DMethod(string deviceId, CloudToDeviceMethod method)
{
return await this.serviceClient.InvokeDeviceMethodAsync(deviceId, "$edgeAgent", method);
}
}
}