-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDeviceService.cs
196 lines (171 loc) · 7.4 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
// 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.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Provisioning.Service;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Configuration;
public class DeviceService : IDeviceService
{
private readonly RegistryManager registryManager;
private readonly ProvisioningServiceClient dps;
private readonly ServiceClient serviceClient;
private readonly HttpClient http;
private readonly IConfiguration configuration;
public DeviceService(
IConfiguration configuration,
RegistryManager registryManager,
ServiceClient serviceClient,
HttpClient http,
ProvisioningServiceClient dps)
{
this.dps = dps;
this.http = http;
this.configuration = configuration;
this.serviceClient = serviceClient;
this.registryManager = registryManager;
}
/// <summary>
/// this function return a list of all edge device wthiout tags.
/// </summary>
/// <returns>IEnumerable twin.</returns>
public async Task<IEnumerable<Twin>> GetAllEdgeDevice()
{
IQuery queryEdgeDevice = this.registryManager.CreateQuery("SELECT * FROM devices.modules WHERE devices.modules.moduleId = '$edgeHub' GROUP BY deviceId", 10);
while (queryEdgeDevice.HasMoreResults)
{
return await queryEdgeDevice.GetNextAsTwinAsync();
}
return Enumerable.Empty<Twin>();
}
/// <summary>
/// this function get and return the list of all the edge device with the tags.
/// </summary>
/// <returns>IEnumerable Twin.</returns>
public async Task<IEnumerable<Twin>> GetAllEdgeDeviceWithTags()
{
IQuery queryEdgeDevice = this.registryManager.CreateQuery("SELECT * FROM devices where devices.capabilities.iotEdge = true", 10);
while (queryEdgeDevice.HasMoreResults)
{
return await queryEdgeDevice.GetNextAsTwinAsync();
}
return Enumerable.Empty<Twin>();
}
/// <summary>
/// this function return a list of all device exept edge device.
/// </summary>
/// <returns>IEnumerable twin.</returns>
public async Task<IEnumerable<Twin>> GetAllDevice()
{
var query = this.registryManager.CreateQuery("SELECT * FROM devices WHERE devices.capabilities.iotEdge = false");
while (query.HasMoreResults)
{
return await query.GetNextAsTwinAsync();
}
return Enumerable.Empty<Twin>();
}
/// <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)
{
IQuery devicesWithModules = this.registryManager.CreateQuery($"SELECT * FROM devices.modules WHERE devices.modules.moduleId = '$edgeAgent' AND deviceId in ['{deviceId}']");
while (devicesWithModules.HasMoreResults)
{
IEnumerable<Twin> devicesTwins = await devicesWithModules.GetNextAsTwinAsync();
return devicesTwins.ElementAt(0);
}
return null;
}
/// <summary>
/// this function get the attestation mechanism of the DPS.
/// </summary>
/// <returns>AttestationMechanism.</returns>
public async Task<AttestationMechanism> GetDpsAttestionMechanism()
{
return await this.dps.GetEnrollmentGroupAttestationAsync(this.configuration["IoTDPS:DefaultEnrollmentGroupe"]);
}
/// <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)
{
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);
}
}
}