This commit is contained in:
Luis Delgado (codit.swiss) 2019-02-06 21:18:05 +01:00 коммит произвёл sushilraje
Родитель 752b535597
Коммит bc91c62363
7 изменённых файлов: 64 добавлений и 50 удалений

Просмотреть файл

@ -123,7 +123,7 @@ namespace Services.Test
{
token = await this.users.GetToken(audience);
}
catch (InvalidConfigurationException e)
catch (InvalidConfigurationException)
{
// Assert
Assert.True(exceptionThrown);

Просмотреть файл

@ -71,7 +71,7 @@ namespace WebService.Test.Controllers
Assert.Equal(filename, package.Name);
Assert.Equal(type, package.packageType.ToString());
}
catch (Exception e)
catch (Exception)
{
Assert.True(expectException);
}

Просмотреть файл

@ -154,20 +154,20 @@ namespace Services.Test
public async Task TestConnectedEdgeDevice()
{
// Arrange
var twins = CreateTestListOfTwins();
var connectedTwins = CreateTestListOfTwins();
connectedTwins.RemoveAt(3);
this.registryMock
.Setup(x => x.CreateQuery(It.Is<string>(s => s.Equals("SELECT * FROM devices"))))
.Returns(new ResultQuery(4));
.Returns(new ResultQuery(twins));
// Set only 3 of the devices to be marked as connected
// The first two are non-edge devices so it shouldn't be listed
// as connected in the result
this.registryMock
.Setup(x => x.CreateQuery(It.Is<string>(s => s.Equals("SELECT * FROM devices.modules where connectionState = 'Connected'"))))
.Returns(new ResultQuery(3));
this.registryMock
.Setup(x => x.GetDevicesAsync(1000))
.Returns(Task.FromResult(this.CreateTestListOfDevices()));
.Returns(new ResultQuery(connectedTwins));
// Act
var allDevices = await this.devices.GetListAsync("", "");
@ -219,6 +219,7 @@ namespace Services.Test
Properties = new TwinProperties(),
Capabilities = isEdgeDevice ? new DeviceCapabilities() { IotEdge = true } : null
};
twin.DeviceId = $"device{valueToReport}";
twin.Properties.Reported = new TwinCollection("{\"test\":\"value" + valueToReport + "\"}");
twin.Properties.Desired = new TwinCollection("{\"test\":\"value" + valueToReport + "\"}");
return twin;
@ -240,22 +241,20 @@ namespace Services.Test
Capabilities = isEdgeDevice ? new DeviceCapabilities() {IotEdge = true} : null
};
return dvc;
}
}
/// <summary>
/// Returns a set of edge and non-edge devices
/// Returns a set of edge and non-edge twins
/// </summary>
/// <returns></returns>
private IEnumerable<Device> CreateTestListOfDevices()
private List<Twin> CreateTestListOfTwins()
{
return new List<Device>()
return new List<Twin>()
{
DevicesTest.CreateTestDevice("device0", false),
DevicesTest.CreateTestDevice("device1", false),
DevicesTest.CreateTestDevice("device2", true),
DevicesTest.CreateTestDevice("device3", true),
DevicesTest.CreateTestDevice("device4", false),
DevicesTest.CreateTestDevice("device5", true),
DevicesTest.CreateTestTwin(0, false),
DevicesTest.CreateTestTwin(1, false),
DevicesTest.CreateTestTwin(2, true),
DevicesTest.CreateTestTwin(3, true)
};
}
}

Просмотреть файл

@ -26,10 +26,18 @@ namespace Services.Test.helpers
for (int i = 0; i < numResults; i++)
{
this.results.Add(ResultQuery.CreateTestTwin(i));
this.deviceQueryResults.Add("{" + $"'{DEVICE_ID_KEY}':'device{i}'" + "}");
this.deviceQueryResults.Add($"{{'{DEVICE_ID_KEY}':'device{i}'}}");
this.HasMoreResults = true;
}
}
public ResultQuery(List<Twin> twins)
{
this.results = twins;
this.deviceQueryResults = twins.Select(x => $"{{'{DEVICE_ID_KEY}':'device{x.DeviceId}'}}").ToList();
this.HasMoreResults = true;
}
public Task<IEnumerable<Twin>> GetNextAsTwinAsync()
{
this.HasMoreResults = false;

Просмотреть файл

@ -98,25 +98,20 @@ namespace Microsoft.Azure.IoTSolutions.IotHubManager.Services
query = QueryConditionTranslator.ToQueryString(query);
}
// normally we need deviceTwins for all devices to show device list
var devices = await this.registry.GetDevicesAsync(MAX_GET_LIST);
var twins = await this.GetTwinByQueryAsync(QUERY_PREFIX,
query,
continuationToken,
MAX_GET_LIST);
var twinsMap = twins.Result.ToDictionary(twin => twin.DeviceId, twin => twin);
var devicesList = devices.Where(dvc => twinsMap.ContainsKey(dvc.Id)).ToList();
var connectedEdgeDevices = this.GetConnectedEdgeDevices(devicesList, twinsMap).Result;
var connectedEdgeDevices = await this.GetConnectedEdgeDevices(twins.Result);
// since deviceAsync does not support continuationToken for now, we need to ignore those devices which does not shown in twins
return new DeviceServiceListModel(devicesList
.Select(azureDevice => new DeviceServiceModel(azureDevice,
twinsMap[azureDevice.Id],
var resultModel = new DeviceServiceListModel(twins.Result
.Select(azureTwin => new DeviceServiceModel(azureTwin,
this.ioTHubHostName,
connectedEdgeDevices.ContainsKey(azureDevice.Id))),
twins.ContinuationToken);
connectedEdgeDevices.ContainsKey(azureTwin.DeviceId))),
twins.ContinuationToken);
return resultModel;
}
/// <summary>
@ -285,29 +280,20 @@ namespace Microsoft.Azure.IoTSolutions.IotHubManager.Services
}
/// <summary>
/// Retrieves the list of edge devices which are reporting as connected based on
/// connectivity of their modules. If any of the modules are connected than the edge device
/// Retrieves the list of edge twins which are reporting as connected based on
/// connectivity of their modules. If any of the modules are connected then the edge device
/// should report as connected.
/// </summary>
/// <param name="devicesList">The list of devices to check</param>
/// <param name="twinsMap">Map of associated twins for those devices</param>
/// <param name="twins">The list of twins to check</param>
/// <returns>Dictionary of edge device ids and the device</returns>
private async Task<Dictionary<string, Device>> GetConnectedEdgeDevices(List<Device> devicesList,
Dictionary<string, Twin> twinsMap)
private async Task<Dictionary<string, Twin>> GetConnectedEdgeDevices(List<Twin> twins)
{
if (!devicesList.Any(dvc => dvc.Capabilities?.IotEdge ??
twinsMap.Values.Any(twin => twin.Capabilities?.IotEdge ?? false)))
{
return new Dictionary<string, Device>();
}
var devicesWithConnectedModules = await this.GetDevicesWithConnectedModules();
var edgeDevices = devicesList
.Where(device => device.Capabilities?.IotEdge ??
twinsMap[device.Id].Capabilities?.IotEdge ?? false)
.Where(edgeDvc => devicesWithConnectedModules.Contains(edgeDvc.Id))
.ToDictionary(edgeDevice => edgeDevice.Id, edgeDevice => edgeDevice);
return edgeDevices;
var edgeTwins = twins
.Where(twin => twin.Capabilities?.IotEdge ?? twin.Capabilities?.IotEdge ?? false)
.Where(edgeDvc => devicesWithConnectedModules.Contains(edgeDvc.DeviceId))
.ToDictionary(edgeDevice => edgeDevice.DeviceId, edgeDevice => edgeDevice);
return edgeTwins;
}
/// <summary>

Просмотреть файл

@ -81,6 +81,25 @@ namespace Microsoft.Azure.IoTSolutions.IotHubManager.Services.Models
{
}
public DeviceServiceModel(Twin azureTwin, string ioTHubHostName, bool isConnected) :
this(
etag: azureTwin.ETag,
id: azureTwin.DeviceId,
c2DMessageCount: azureTwin.CloudToDeviceMessageCount ?? azureTwin.CloudToDeviceMessageCount ?? 0,
lastActivity: azureTwin.LastActivityTime ?? azureTwin.LastActivityTime ?? new DateTime(),
connected: isConnected || azureTwin.ConnectionState.Equals(DeviceConnectionState.Connected),
enabled: azureTwin.Status.Equals(DeviceStatus.Enabled),
isEdgeDevice: azureTwin.Capabilities?.IotEdge ?? azureTwin.Capabilities?.IotEdge ?? false,
lastStatusUpdated: azureTwin.StatusUpdatedTime ?? azureTwin.StatusUpdatedTime ?? new DateTime(),
twin: new TwinServiceModel(azureTwin),
ioTHubHostName: ioTHubHostName,
authentication: null
)
{
}
public Device ToAzureModel(bool ignoreEtag = true)
{
var device = new Device(this.Id)

Просмотреть файл

@ -74,7 +74,9 @@ namespace Microsoft.Azure.IoTSolutions.IotHubManager.WebService.v1.Models
this.IsEdgeDevice = device.IsEdgeDevice;
this.LastStatusUpdated = device.LastStatusUpdated;
this.IoTHubHostName = device.IoTHubHostName;
this.Authentication = new AuthenticationMechanismApiModel(device.Authentication);
this.Authentication = new AuthenticationMechanismApiModel(
device.Authentication ?? new AuthenticationMechanismServiceModel()
);
if (device.Twin != null)
{