-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDevicesControllerBase.cs
243 lines (205 loc) · 8.82 KB
/
DevicesControllerBase.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
// 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.Controllers.V10
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure;
using Azure.Data.Tables;
using AzureIoTHub.Portal.Models.v10;
using AzureIoTHub.Portal.Server.Factories;
using AzureIoTHub.Portal.Server.Managers;
using AzureIoTHub.Portal.Server.Mappers;
using AzureIoTHub.Portal.Server.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Common.Exceptions;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Logging;
public abstract class DevicesControllerBase<TListItem, TModel> : ControllerBase
where TListItem : DeviceListItem
where TModel : DeviceDetails
{
private readonly IDeviceService devicesService;
private readonly IDeviceTagService deviceTagService;
private readonly IDeviceTwinMapper<TListItem, TModel> deviceTwinMapper;
private readonly ITableClientFactory tableClientFactory;
private readonly IDeviceProvisioningServiceManager deviceProvisioningServiceManager;
protected ILogger Logger { get; }
protected DevicesControllerBase(
ILogger logger,
IDeviceService devicesService,
IDeviceTagService deviceTagService,
IDeviceTwinMapper<TListItem, TModel> deviceTwinMapper,
IDeviceProvisioningServiceManager deviceProvisioningServiceManager,
ITableClientFactory tableClientFactory)
{
Logger = logger;
this.devicesService = devicesService;
this.deviceTagService = deviceTagService;
this.deviceTwinMapper = deviceTwinMapper;
this.deviceProvisioningServiceManager = deviceProvisioningServiceManager;
this.tableClientFactory = tableClientFactory;
}
/// <summary>
/// Gets the device list.
/// </summary>
/// <param name="continuationToken"></param>
/// <param name="searchText"></param>
/// <param name="searchStatus"></param>
/// <param name="searchState"></param>
/// <param name="pageSize"></param>
public virtual async Task<PaginationResult<TListItem>> GetItems(
string continuationToken = null,
string searchText = null,
bool? searchStatus = null,
bool? searchState = null,
int pageSize = 10)
{
var searchTags = new Dictionary<string, string>();
foreach (var tag in this.deviceTagService.GetAllSearchableTagsNames())
{
if (Request.Query.TryGetValue($"tag.{tag}", out var searchTag))
{
searchTags.Add(tag, searchTag.Single());
}
}
var result = await this.devicesService.GetAllDevice(
continuationToken: continuationToken,
pageSize: pageSize,
searchStatus: searchStatus,
searchText: searchText,
searchState: searchState,
searchTags: searchTags,
excludeDeviceType: "LoRa Concentrator");
string nextPage = null;
if (!string.IsNullOrEmpty(result.NextPage))
{
nextPage = Url.RouteUrl(new UrlRouteContext
{
RouteName = nameof(GetItems),
Values = new
{
continuationToken = result.NextPage,
searchText,
searchState,
searchStatus,
pageSize
}
});
var tagsFilterBuilder = new StringBuilder();
foreach (var tag in searchTags)
{
_ = tagsFilterBuilder.Append(CultureInfo.InvariantCulture, $"&tag.{tag.Key}={tag.Value}");
}
}
return new PaginationResult<TListItem>
{
Items = result.Items.Select(x => this.deviceTwinMapper.CreateDeviceListItem(x)),
TotalItems = result.TotalItems,
NextPage = nextPage
};
}
/// <summary>
/// Gets the specified device.
/// </summary>
/// <param name="deviceID">The device identifier.</param>
public virtual async Task<TModel> GetItem(string deviceID)
{
var item = await this.devicesService.GetDeviceTwin(deviceID);
var tagList = this.deviceTagService.GetAllTagsNames();
return this.deviceTwinMapper.CreateDeviceDetails(item, tagList);
}
/// <summary>
/// Creates the device.
/// </summary>
/// <param name="device">The device.</param>
public virtual async Task<IActionResult> CreateDeviceAsync(TModel device)
{
ArgumentNullException.ThrowIfNull(device, nameof(device));
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// Create a new Twin from the form's fields.
var newTwin = new Twin()
{
DeviceId = device.DeviceID
};
this.deviceTwinMapper.UpdateTwin(newTwin, device);
var status = device.IsEnabled ? DeviceStatus.Enabled : DeviceStatus.Disabled;
var result = await this.devicesService.CreateDeviceWithTwin(device.DeviceID, false, newTwin, status);
return Ok(result);
}
catch (DeviceAlreadyExistsException e)
{
Logger?.LogError($"Create device failed -{device.DeviceID}", e);
return BadRequest(e.Message);
}
catch (InvalidOperationException e)
{
Logger?.LogError($"Create device failed - {device.DeviceID} -\n{e.Message}");
return BadRequest(e.Message);
}
}
/// <summary>
/// Updates the device.
/// </summary>
/// <param name="device">The device.</param>
public virtual async Task<IActionResult> UpdateDeviceAsync(TModel device)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
ArgumentNullException.ThrowIfNull(device, nameof(device));
// Device status (enabled/disabled) has to be dealt with afterwards
var currentDevice = await this.devicesService.GetDevice(device.DeviceID);
currentDevice.Status = device.IsEnabled ? DeviceStatus.Enabled : DeviceStatus.Disabled;
_ = await this.devicesService.UpdateDevice(currentDevice);
// Get the current twin from the hub, based on the device ID
var currentTwin = await this.devicesService.GetDeviceTwin(device.DeviceID);
// Update the twin properties
this.deviceTwinMapper.UpdateTwin(currentTwin, device);
_ = await this.devicesService.UpdateDeviceTwin(device.DeviceID, currentTwin);
return Ok();
}
/// <summary>
/// Deletes the specified device.
/// </summary>
/// <param name="deviceID">The device identifier.</param>
public virtual async Task<IActionResult> Delete(string deviceID)
{
await this.devicesService.DeleteDevice(deviceID);
return Ok();
}
/// <summary>
/// Returns the device enrollment credentials.
/// </summary>
/// <param name="deviceID">The device identifier.</param>
public virtual async Task<ActionResult<EnrollmentCredentials>> GetCredentials(string deviceID)
{
var item = await this.devicesService.GetDeviceTwin(deviceID);
if (item == null)
{
return NotFound("Device doesn't exist.");
}
if (!item.Tags.Contains("modelId"))
{
return BadRequest($"Cannot find device type from device {deviceID}");
}
Response<TableEntity> response = await this.tableClientFactory.GetDeviceTemplates()
.GetEntityAsync<TableEntity>("0", item.Tags["modelId"].ToString());
var modelEntity = response.Value;
var credentials = await this.deviceProvisioningServiceManager.GetEnrollmentCredentialsAsync(deviceID, modelEntity[nameof(DeviceModel.Name)].ToString());
return Ok(credentials);
}
}
}