diff --git a/MyCore.Interfaces/Models/Providers/Zigbee/Zigbee2Mqtt/Zigbee2MqttDevice.cs b/MyCore.Interfaces/Models/Providers/Zigbee/Zigbee2Mqtt/Zigbee2MqttDevice.cs index 9062c85..a8390c1 100644 --- a/MyCore.Interfaces/Models/Providers/Zigbee/Zigbee2Mqtt/Zigbee2MqttDevice.cs +++ b/MyCore.Interfaces/Models/Providers/Zigbee/Zigbee2Mqtt/Zigbee2MqttDevice.cs @@ -28,12 +28,12 @@ namespace MyCore.Interfaces.Models { public string date_code { 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 interviewing { get; set; } public string model_id { 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 bool supported { get; set; } public string type { get; set; } diff --git a/MyCore/Controllers/Devices/DeviceController.cs b/MyCore/Controllers/Devices/DeviceController.cs index 8e4e7f9..6838436 100644 --- a/MyCore/Controllers/Devices/DeviceController.cs +++ b/MyCore/Controllers/Devices/DeviceController.cs @@ -98,15 +98,17 @@ namespace MyCore.Controllers { try { - if (userId != null) - { - List devices = _DeviceDatabaseService.GetByType(userId, type); + if (userId == null) + throw new InvalidOperationException("User not found"); - return new OkObjectResult(devices.Select(d => d.ToDTO())); - } - else { - return new ObjectResult("Invalid parameters") { StatusCode = 400 }; - } + List devices = _DeviceDatabaseService.GetByType(userId, type); + + return new OkObjectResult(devices.Select(d => d.ToDTO())); + + } + catch (InvalidOperationException ex) + { + return new BadRequestObjectResult(ex.Message) { StatusCode = 400 }; } catch (Exception ex) { @@ -125,7 +127,7 @@ namespace MyCore.Controllers try { 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); @@ -224,6 +226,48 @@ namespace MyCore.Controllers } } + /// + /// Get devices from provider + /// + /// User Id + /// Id of Provider + [ProducesResponseType(typeof(List), 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 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 }; + } + } + /// /// Update a device /// @@ -260,16 +304,25 @@ namespace MyCore.Controllers { try { - if (deviceId != null) - { - // TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ? - if (_DeviceDatabaseService.IsExist(deviceId)) - { - _DeviceDatabaseService.Remove(deviceId); - } - } + if (deviceId == null) + throw new InvalidOperationException("Device not found"); - 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) { @@ -287,16 +340,29 @@ namespace MyCore.Controllers { try { - if (userId != null) - { - // TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ? - if (_UserDatabaseService.IsExist(userId)) - { - _DeviceDatabaseService.RemoveForProvider(providerId); - } - } + if (userId == null) + throw new InvalidOperationException("User not found"); - 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) { @@ -313,16 +379,23 @@ namespace MyCore.Controllers { try { - if (userId != null) - { - // TODO REMOVE DEVICE ID IN AUTOMATION and delete automation if none device ? - if (_UserDatabaseService.IsExist(userId)) - { - _DeviceDatabaseService.RemoveForUser(userId); - } - } + if (userId == null) + throw new InvalidOperationException("User not found"); - 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) { diff --git a/MyCore/Services/Devices/DeviceService.cs b/MyCore/Services/Devices/DeviceService.cs index 295357f..f18f8e2 100644 --- a/MyCore/Services/Devices/DeviceService.cs +++ b/MyCore/Services/Devices/DeviceService.cs @@ -212,13 +212,13 @@ namespace MyCore.Services.Devices //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) { DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO(); deviceDetailDTO.Name = zigbee2MqttDevice.friendly_name; - deviceDetailDTO.ServiceIdentification = zigbee2MqttDevice.ieeeAddr; + deviceDetailDTO.ServiceIdentification = zigbee2MqttDevice.ieee_address; deviceDetailDTO.ProviderId = provider.Id; 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.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.MeansOfCommunications = new List(); deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Zigbee); @@ -251,7 +251,7 @@ namespace MyCore.Services.Devices deviceDetailDTO.Type = DeviceType.Gateway; } - if (zigbee2MqttDevice.supported) + if (zigbee2MqttDevice.supported || deviceDetailDTO.Type == DeviceType.Gateway) { // Supported device ! createdZigbeeDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, deviceDetailDTO, true));