MC Add get devices from provider, fix new get devices with exposes + clean code device controller (exception etc)

This commit is contained in:
Thomas Fransolet 2021-03-17 09:00:24 +01:00
parent b58473aa34
commit 461c998bb6
3 changed files with 115 additions and 42 deletions

View File

@ -28,12 +28,12 @@ namespace MyCore.Interfaces.Models
{ {
public string date_code { get; set; } public string date_code { get; set; }
public string friendly_name { get; set; } public string friendly_name { get; set; }
public string ieeeAddr { get; set; } public string ieee_address { get; set; }
public bool interview_completed { get; set; } public bool interview_completed { get; set; }
public bool interviewing { get; set; } public bool interviewing { get; set; }
public string model_id { get; set; } public string model_id { get; set; }
public int network_address { get; set; } public int network_address { get; set; }
public string powerSource { get; set; } public string power_source { get; set; }
public string software_build_id { get; set; } public string software_build_id { get; set; }
public bool supported { get; set; } public bool supported { get; set; }
public string type { get; set; } public string type { get; set; }

View File

@ -98,15 +98,17 @@ namespace MyCore.Controllers
{ {
try try
{ {
if (userId != null) if (userId == null)
{ throw new InvalidOperationException("User not found");
List<Device> devices = _DeviceDatabaseService.GetByType(userId, type); List<Device> devices = _DeviceDatabaseService.GetByType(userId, type);
return new OkObjectResult(devices.Select(d => d.ToDTO())); return new OkObjectResult(devices.Select(d => d.ToDTO()));
} }
else { catch (InvalidOperationException ex)
return new ObjectResult("Invalid parameters") { StatusCode = 400 }; {
} return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -125,7 +127,7 @@ namespace MyCore.Controllers
try try
{ {
if (deviceDetailDTO == null) if (deviceDetailDTO == null)
throw new KeyNotFoundException("Device is null"); throw new KeyNotFoundException("Device detail not found");
DeviceDetailDTO deviceCreated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, deviceDetailDTO.UserId, deviceDetailDTO, true); DeviceDetailDTO deviceCreated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, deviceDetailDTO.UserId, deviceDetailDTO, true);
@ -224,6 +226,48 @@ namespace MyCore.Controllers
} }
} }
/// <summary>
/// Get devices from provider
/// </summary>
/// <param name="userId">User Id</param>
/// <param name="providerId">Id of Provider</param>
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
[HttpGet("{userId}/fromProvider/{providerId}")]
public ObjectResult GetDevicesFromProvider(string userId, string providerId)
{
try
{
if (userId == null)
throw new InvalidOperationException("User not found");
if (providerId == null)
throw new InvalidOperationException("Provider not found");
if (!_UserDatabaseService.IsExist(userId))
throw new KeyNotFoundException("User does not exist");
if (!_ProviderDatabaseService.IsExist(providerId))
throw new KeyNotFoundException("Provider does not exist");
List<Device> devices = _DeviceDatabaseService.GetByProviderId(providerId);
return new OkObjectResult(devices.Select(d => d.ToDTO()));
}
catch (InvalidOperationException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
}
catch (KeyNotFoundException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary> /// <summary>
/// Update a device /// Update a device
/// </summary> /// </summary>
@ -260,16 +304,25 @@ namespace MyCore.Controllers
{ {
try try
{ {
if (deviceId != null) if (deviceId == null)
{ throw new InvalidOperationException("Device not found");
// TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ?
if (_DeviceDatabaseService.IsExist(deviceId))
{
_DeviceDatabaseService.Remove(deviceId);
}
}
return new OkObjectResult(201); if (!_DeviceDatabaseService.IsExist(deviceId))
throw new KeyNotFoundException("Device does not exist");
// TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ?
_DeviceDatabaseService.Remove(deviceId);
return new OkObjectResult(200);
}
catch (InvalidOperationException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
}
catch (KeyNotFoundException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -287,16 +340,29 @@ namespace MyCore.Controllers
{ {
try try
{ {
if (userId != null) if (userId == null)
{ throw new InvalidOperationException("User not found");
// TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ?
if (_UserDatabaseService.IsExist(userId))
{
_DeviceDatabaseService.RemoveForProvider(providerId);
}
}
return new OkObjectResult(201); if (providerId == null)
throw new InvalidOperationException("Provider not found");
if (!_UserDatabaseService.IsExist(userId))
throw new KeyNotFoundException("User does not exist");
if (!_ProviderDatabaseService.IsExist(providerId))
throw new KeyNotFoundException("Provider does not exist");
// TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ?
_DeviceDatabaseService.RemoveForProvider(providerId);
return new OkObjectResult(200);
}
catch (InvalidOperationException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
}
catch (KeyNotFoundException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -313,16 +379,23 @@ namespace MyCore.Controllers
{ {
try try
{ {
if (userId != null) if (userId == null)
{ throw new InvalidOperationException("User not found");
// TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ?
if (_UserDatabaseService.IsExist(userId))
{
_DeviceDatabaseService.RemoveForUser(userId);
}
}
return new OkObjectResult(201); if (!_UserDatabaseService.IsExist(userId))
throw new KeyNotFoundException("User does not exist");
// TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ?
_DeviceDatabaseService.RemoveForUser(userId);
return new OkObjectResult(200);
}
catch (InvalidOperationException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
}
catch (KeyNotFoundException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -212,13 +212,13 @@ namespace MyCore.Services.Devices
//zigbee2MqttDevices = await MqttClientService.AskDevicesAsync(); //zigbee2MqttDevices = await MqttClientService.AskDevicesAsync();
} }
zigbee2MqttDevices = zigbee2MqttDevices.Where(yd => !existingDevices.Select(ed => ed.ServiceIdentification).ToList().Contains(yd.ieeeAddr)).ToList(); zigbee2MqttDevices = zigbee2MqttDevices.Where(yd => !existingDevices.Select(ed => ed.ServiceIdentification).ToList().Contains(yd.ieee_address)).ToList();
foreach (var zigbee2MqttDevice in zigbee2MqttDevices) foreach (var zigbee2MqttDevice in zigbee2MqttDevices)
{ {
DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO(); DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
deviceDetailDTO.Name = zigbee2MqttDevice.friendly_name; deviceDetailDTO.Name = zigbee2MqttDevice.friendly_name;
deviceDetailDTO.ServiceIdentification = zigbee2MqttDevice.ieeeAddr; deviceDetailDTO.ServiceIdentification = zigbee2MqttDevice.ieee_address;
deviceDetailDTO.ProviderId = provider.Id; deviceDetailDTO.ProviderId = provider.Id;
deviceDetailDTO.ProviderName = provider.Name; deviceDetailDTO.ProviderName = provider.Name;
@ -227,7 +227,7 @@ namespace MyCore.Services.Devices
deviceDetailDTO.Model = zigbee2MqttDevice.type == "Coordinator" ? "Coordinator" : zigbee2MqttDevice.definition.model; // Is the base to understand incoming messages ! deviceDetailDTO.Model = zigbee2MqttDevice.type == "Coordinator" ? "Coordinator" : zigbee2MqttDevice.definition.model; // Is the base to understand incoming messages !
deviceDetailDTO.FirmwareVersion = zigbee2MqttDevice.software_build_id; deviceDetailDTO.FirmwareVersion = zigbee2MqttDevice.software_build_id;
deviceDetailDTO.Battery = zigbee2MqttDevice.powerSource == null ? false : zigbee2MqttDevice.powerSource.Contains("Battery"); deviceDetailDTO.Battery = zigbee2MqttDevice.power_source == null ? false : zigbee2MqttDevice.power_source.Contains("Battery");
deviceDetailDTO.ManufacturerName = zigbee2MqttDevice.definition?.vendor == null ? provider.Type : zigbee2MqttDevice.definition?.vendor.ToLower(); deviceDetailDTO.ManufacturerName = zigbee2MqttDevice.definition?.vendor == null ? provider.Type : zigbee2MqttDevice.definition?.vendor.ToLower();
deviceDetailDTO.MeansOfCommunications = new List<MeansOfCommunication>(); deviceDetailDTO.MeansOfCommunications = new List<MeansOfCommunication>();
deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Zigbee); deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Zigbee);
@ -251,7 +251,7 @@ namespace MyCore.Services.Devices
deviceDetailDTO.Type = DeviceType.Gateway; deviceDetailDTO.Type = DeviceType.Gateway;
} }
if (zigbee2MqttDevice.supported) if (zigbee2MqttDevice.supported || deviceDetailDTO.Type == DeviceType.Gateway)
{ {
// Supported device ! // Supported device !
createdZigbeeDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, deviceDetailDTO, true)); createdZigbeeDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, deviceDetailDTO, true));