diff --git a/MyCore/Controllers/Devices/DeviceController.cs b/MyCore/Controllers/Devices/DeviceController.cs index 5e6e629..1ddc365 100644 --- a/MyCore/Controllers/Devices/DeviceController.cs +++ b/MyCore/Controllers/Devices/DeviceController.cs @@ -165,6 +165,38 @@ namespace MyCore.Controllers.Devices } } + /// + /// Create devices from provider + /// + /// User Id + [ProducesResponseType(typeof(List), 200)] + [HttpPost("fromZigbee2Mqtt/{userId}")] + public async Task CreateDevicesFromZigbee2Mqtt(string userId) + { + try + { + if (userId == null) + throw new InvalidOperationException("User not found"); + + if (!UserService.IsExist(_UserDatabaseService, userId)) + throw new KeyNotFoundException("User not found"); + + // TODO HANDLE DUPLICATE DEVICES + + List devicesCreated = await DeviceService.CreateFromZigbee(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, userId); + + return new OkObjectResult(devicesCreated); + } + catch (InvalidOperationException ex) + { + return new BadRequestObjectResult(ex.Message) { StatusCode = 400 }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + /// /// Update a device /// diff --git a/MyCore/Services/Devices/ActionService.cs b/MyCore/Services/Devices/ActionService.cs new file mode 100644 index 0000000..309cea9 --- /dev/null +++ b/MyCore/Services/Devices/ActionService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.Services.Devices +{ + public class ActionService + { + // TODO it's here that action are thrown.. Call from Mqtt Or other service like controller if from RpiServices + } +} diff --git a/MyCore/Services/Devices/DeviceService.cs b/MyCore/Services/Devices/DeviceService.cs index 33f9004..ea0db52 100644 --- a/MyCore/Services/Devices/DeviceService.cs +++ b/MyCore/Services/Devices/DeviceService.cs @@ -100,10 +100,10 @@ namespace MyCore.Services.Devices } } catch (AuthenticationException ex) { - throw new AuthenticationException("Bad username or password for service " + provider.Name + " ex: " + ex); + throw new AuthenticationException("Bad username or password for service " + provider.Name + " ex: " + ex.Message); } catch (HttpRequestException ex) { - throw new HttpRequestException("Error retrieving devices for " + provider.Name + " ex: " + ex); + throw new HttpRequestException("Error retrieving devices for " + provider.Name + " ex: " + ex.Message); } catch (Exception ex) { @@ -111,6 +111,25 @@ namespace MyCore.Services.Devices return createdDevice; } + public async static Task> CreateFromZigbee(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId) + { + List createdDevice = new List(); + + try + { + // TODO MQTT Connexion + // TODO Server.. + MQTTService mQTTService = new MQTTService("192.168.31.140", "mqtt", "mqtt"); + + } + catch (UnauthorizedAccessException ex) + { + throw new UnauthorizedAccessException("Error connecting to mqtt server: " + ex.Message); + } + + return createdDevice; + } + public static List CreateArloDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List arloDevices, Provider provider) { List createdArloDevices = new List(); @@ -189,16 +208,7 @@ namespace MyCore.Services.Devices deviceDetailDTO.Type = DeviceType.Actuator; break; } - //deviceDetailDTO.Port = arlo.; // TO CHECK deviceDetailDTO.FirmwareVersion = meross.firmwareVersion; // TODO - /*Dictionary properties = new Dictionary(); - foreach (var property in arlo.properties) - { - properties.Add(property.Key, property.Value); - }*/ // TODO - // deviceDetailDTO.Properties = properties; - - // TODO ! Dictionary properties = new Dictionary(); foreach (var property in meross.channels) { diff --git a/MyCore/Services/Devices/MQTTService.cs b/MyCore/Services/Devices/MQTTService.cs index e8a870b..0d625ae 100644 --- a/MyCore/Services/Devices/MQTTService.cs +++ b/MyCore/Services/Devices/MQTTService.cs @@ -31,10 +31,14 @@ namespace MyCore.Services private YeelightService yeelightService = new YeelightService(); - public MQTTService() + public MQTTService(string server, string user, string password) { try { + _mqttServer = server; + _user = user; + _password = password; + // Create a new MQTT client. _client = new MqttFactory().CreateMqttClient(); @@ -53,6 +57,7 @@ namespace MyCore.Services else { Console.WriteLine($"Error connecting to {_mqttServer}"); + throw new UnauthorizedAccessException("Error connecting to mqtt server: " + _mqttServer); } }); @@ -93,6 +98,9 @@ namespace MyCore.Services var topic = e.ApplicationMessage.Topic; var payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); + // As soon as we received all the info => + //_client.DisconnectAsync(); + // TODO // Here take only zigbee2mqtt * @@ -109,7 +117,7 @@ namespace MyCore.Services // Load everydevice in cache.. ? Performance ? - + // TODO - TO CLARIFY switch (topic) { case "zigbee2mqtt/0x00158d00029a7b65":