Skip to content

Commit

Permalink
Fix Code QL issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand committed Feb 10, 2022
1 parent 454bf50 commit f3dab7a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public async Task CreateDeviceAsync_With_Valid_Argument_Should_Return_OkResult()

this.mockDeviceService.Setup(c => c.CreateDeviceWithTwin(
It.Is<string>(x => x == twin.DeviceId),
It.Is<bool>(x => x == false),
It.Is<bool>(x => !x),
It.Is<Twin>(x => x.DeviceId == twin.DeviceId),
It.Is<DeviceStatus>(x => x == DeviceStatus.Enabled)))
.ReturnsAsync(mockResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,24 @@ public ConcentratorsController(
[HttpGet]
public async Task<IActionResult> GetAllDeviceConcentrator()
{
try
{
// Gets all the twins from this devices
var items = await this.devicesService.GetAllDevice();
var itemFilter = items.Where(x => x.Tags["deviceType"] == "LoRa Concentrator");
List<Concentrator> result = new List<Concentrator>();

foreach (var item in itemFilter)
{
result.Add(this.concentratorTwinMapper.CreateDeviceDetails(item));
}
// Gets all the twins from this devices
var items = await this.devicesService.GetAllDevice();
var itemFilter = items.Where(x => x.Tags["deviceType"] == "LoRa Concentrator");
List<Concentrator> result = new List<Concentrator>();

return this.Ok(result);
}
catch (Exception e)
foreach (var item in itemFilter)
{
return this.BadRequest(e.Message);
result.Add(this.concentratorTwinMapper.CreateDeviceDetails(item));
}

return this.Ok(result);
}

[HttpGet("{deviceId}")]
public async Task<IActionResult> GetDeviceConcentrator(string deviceId)
{
try
{
var item = await this.devicesService.GetDeviceTwin(deviceId);
return this.Ok(this.concentratorTwinMapper.CreateDeviceDetails(item));
}
catch (Exception e)
{
return this.BadRequest(e.Message);
}
var item = await this.devicesService.GetDeviceTwin(deviceId);
return this.Ok(this.concentratorTwinMapper.CreateDeviceDetails(item));
}

[HttpPost]
Expand Down Expand Up @@ -105,7 +91,7 @@ public async Task<IActionResult> CreateDeviceAsync(Concentrator device)
}
catch (DeviceAlreadyExistsException e)
{
this.logger.LogError($"{device.DeviceId} - Create device failed", e);
this.logger?.LogError($"{device.DeviceId} - Create device failed", e);
return this.BadRequest(e.Message);
}
catch (InvalidOperationException e)
Expand All @@ -118,34 +104,27 @@ public async Task<IActionResult> CreateDeviceAsync(Concentrator device)
[HttpPut]
public async Task<IActionResult> UpdateDeviceAsync(Concentrator device)
{
try
if (!this.ModelState.IsValid)
{
if (!this.ModelState.IsValid)
{
return this.BadRequest(this.ModelState);
}
return this.BadRequest(this.ModelState);
}

// Device status (enabled/disabled) has to be dealt with afterwards
Device currentDevice = await this.devicesService.GetDevice(device.DeviceId);
currentDevice.Status = device.IsEnabled ? DeviceStatus.Enabled : DeviceStatus.Disabled;
// Device status (enabled/disabled) has to be dealt with afterwards
Device currentDevice = await this.devicesService.GetDevice(device.DeviceId);
currentDevice.Status = device.IsEnabled ? DeviceStatus.Enabled : DeviceStatus.Disabled;

_ = await this.devicesService.UpdateDevice(currentDevice);
_ = await this.devicesService.UpdateDevice(currentDevice);

// Get the current twin from the hub, based on the device ID
Twin currentTwin = await this.devicesService.GetDeviceTwin(device.DeviceId);
device.RouterConfig = await this.routerConfigManager.GetRouterConfig(device.LoraRegion);
// Get the current twin from the hub, based on the device ID
Twin currentTwin = await this.devicesService.GetDeviceTwin(device.DeviceId);
device.RouterConfig = await this.routerConfigManager.GetRouterConfig(device.LoraRegion);

// Update the twin properties
this.concentratorTwinMapper.UpdateTwin(currentTwin, device);
// Update the twin properties
this.concentratorTwinMapper.UpdateTwin(currentTwin, device);

_ = await this.devicesService.UpdateDeviceTwin(device.DeviceId, currentTwin);
_ = await this.devicesService.UpdateDeviceTwin(device.DeviceId, currentTwin);

return this.Ok("Device updated.");
}
catch (Exception e)
{
return this.BadRequest(e.Message);
}
return this.Ok("Device updated.");
}

/// <summary>
Expand All @@ -156,15 +135,8 @@ public async Task<IActionResult> UpdateDeviceAsync(Concentrator device)
[HttpDelete("{deviceId}")]
public async Task<IActionResult> Delete(string deviceId)
{
try
{
await this.devicesService.DeleteDevice(deviceId);
return this.Ok("the device was successfully deleted.");
}
catch (Exception e)
{
return this.BadRequest(e.Message);
}
await this.devicesService.DeleteDevice(deviceId);
return this.Ok("the device was successfully deleted.");
}
}
}
10 changes: 4 additions & 6 deletions src/AzureIoTHub.Portal/Server/Controllers/DevicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@ public async Task<IEnumerable<DeviceListItem>> Get()
// Gets all the twins from this devices
var items = await this.devicesService.GetAllDevice();
List<Twin> listTwin = new List<Twin>();
// var itemFilter = items.Where(x => x.Tags["deviceType"] != "LoRa Concentrator");
foreach (var item in items)
var itemFilter = items.Where(item => item.Tags.Contains("deviceType") && item.Tags["deviceType"] != "LoRa Concentrator");

foreach (var item in itemFilter)
{
if (item.Tags.Contains("deviceType") && item.Tags["deviceType"] != "LoRa Concentrator")
{
listTwin.Add(item);
}
listTwin.Add(item);
}

return listTwin.Select(this.deviceTwinMapper.CreateDeviceListItem);
Expand Down

0 comments on commit f3dab7a

Please sign in to comment.