diff --git a/MyCore/Controllers/Devices/DeviceController.cs b/MyCore/Controllers/Devices/DeviceController.cs index 5c86518..1ec853c 100644 --- a/MyCore/Controllers/Devices/DeviceController.cs +++ b/MyCore/Controllers/Devices/DeviceController.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; +using System.Security.Authentication; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; @@ -143,6 +145,14 @@ namespace MyCore.Controllers.Devices { return new BadRequestObjectResult(ex.Message) { StatusCode = 404 }; } + catch (AuthenticationException ex) + { + return new BadRequestObjectResult(ex.Message) { StatusCode = 401 }; + } + catch (HttpRequestException ex) // Not ok if not http request.. + { + return new BadRequestObjectResult(ex.Message) { StatusCode = 421 }; + } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; diff --git a/MyCore/DTO/MyControlPanel/DeviceDTO.cs b/MyCore/DTO/MyControlPanel/DeviceDTO.cs index 146cec4..b4b7718 100644 --- a/MyCore/DTO/MyControlPanel/DeviceDTO.cs +++ b/MyCore/DTO/MyControlPanel/DeviceDTO.cs @@ -60,6 +60,8 @@ namespace MyCore.DTO.MyControlPanel public string IpAddress { get; set; } + public string ServiceIdentification { get; set; } + public bool Battery { get; set; } public int BatteryStatus { get; set; } diff --git a/MyCore/Models/MyControlPanel/Database/Device.cs b/MyCore/Models/MyControlPanel/Database/Device.cs index bbc38ee..e09bcf9 100644 --- a/MyCore/Models/MyControlPanel/Database/Device.cs +++ b/MyCore/Models/MyControlPanel/Database/Device.cs @@ -62,6 +62,9 @@ namespace MyCore.Models.MyControlPanel.Database [BsonElement("IpAddress")] public string IpAddress { get; set; } + [BsonElement("ServiceIdentification")] + public string ServiceIdentification { get; set; } + [BsonElement("Battery")] public bool Battery { get; set; } @@ -116,6 +119,7 @@ namespace MyCore.Models.MyControlPanel.Database LastMessage = LastMessage, LastMessageDate = LastMessageDate, IpAddress = IpAddress, + ServiceIdentification = ServiceIdentification, ProviderId = ProviderId, Groups = GroupIds, Properties = Properties, diff --git a/MyCore/Services/Devices/DeviceService.cs b/MyCore/Services/Devices/DeviceService.cs index f452ade..d93005f 100644 --- a/MyCore/Services/Devices/DeviceService.cs +++ b/MyCore/Services/Devices/DeviceService.cs @@ -7,6 +7,8 @@ using MyCore.Services.MyControlPanel; using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; +using System.Security.Authentication; using System.Threading.Tasks; namespace MyCore.Services.Devices @@ -55,6 +57,7 @@ namespace MyCore.Services.Devices device.MeansOfCommunications = deviceDetailDTO.MeansOfCommunications; device.IpAddress = deviceDetailDTO.IpAddress; + device.ServiceIdentification = deviceDetailDTO.ServiceIdentification; device.Battery = deviceDetailDTO.Battery; device.BatteryStatus = deviceDetailDTO.BatteryStatus; device.GroupIds = device.GroupIds; @@ -76,22 +79,116 @@ namespace MyCore.Services.Devices List createdDevice = new List(); - switch (provider.Name) - { - case "Arlo": - ArloService arloService = new ArloService(provider.Username, provider.Password); - break; - case "Meross": + try { + switch (provider.Name) + { + case "Arlo": + List arloDevices = new ArloService(provider.Username, provider.Password).GetAllDevices(); + createdDevice = CreateArloDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, arloDevices, provider); + break; + case "Meross": + List merossDevices = new MerossService(provider.Username, provider.Password).GetMerossDevices(); + createdDevice = CreateMerossDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, merossDevices, provider); + break; + case "Yeelight": + List yeelightDevices = await new YeelightService().GetDevices(); + createdDevice = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, yeelightDevices, provider); + break; + } + } + catch (AuthenticationException ex) { + throw new AuthenticationException("Bad username or password for service " + provider.Name + " ex: " + ex); + } + catch (HttpRequestException ex) { + throw new HttpRequestException("Error retrieving devices for " + provider.Name + " ex: " + ex); + } + catch (Exception ex) { - break; - case "Yeelight": - List yeelightDevices = await new YeelightService().GetDevices(); - createdDevice = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, yeelightDevices, provider); - break; + } + return createdDevice; + } + + public static List CreateArloDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, List arloDevices, Provider provider) + { + List createdArloDevices = new List(); + + foreach (var arlo in arloDevices) + { + DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO(); + deviceDetailDTO.Name = arlo.deviceName; + deviceDetailDTO.IpAddress = arlo.deviceId; // TO CHECK + deviceDetailDTO.ServiceIdentification = arlo.deviceId; + deviceDetailDTO.ProviderId = provider.Id; + deviceDetailDTO.ConnectionStatus = arlo.connectivity.connected ? ConnectionStatus.Connected : ConnectionStatus.Unknown; + deviceDetailDTO.Status = arlo.connectivity.connected ? true : false; // TODO STATE + deviceDetailDTO.Model = arlo.deviceType; + //deviceDetailDTO.Port = arlo.; // TO CHECK + deviceDetailDTO.FirmwareVersion = arlo.interfaceVersion; // TODO + /*Dictionary properties = new Dictionary(); + foreach (var property in arlo.properties) + { + properties.Add(property.Key, property.Value); + }*/ // TODO + // deviceDetailDTO.Properties = properties; + + // TODO ! + List supportedOperationsDTO = new List(); + supportedOperationsDTO.Add(arlo.lastImageUploaded.GetType().Name + " : " + arlo.lastImageUploaded); + supportedOperationsDTO.Add(arlo.presignedLastImageUrl.GetType().Name + " : " + arlo.presignedLastImageUrl); + supportedOperationsDTO.Add(arlo.presignedFullFrameSnapshotUrl.GetType().Name + " : " + arlo.presignedFullFrameSnapshotUrl); + supportedOperationsDTO.Add(arlo.presignedSnapshotUrl.GetType().Name + " : " + arlo.presignedSnapshotUrl); + deviceDetailDTO.SupportedOperations = supportedOperationsDTO; + + deviceDetailDTO.MeansOfCommunications = new List(); + deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Wifi); // TO CHECK + + deviceDetailDTO.CreatedDate = DateTime.Now; + deviceDetailDTO.UpdatedDate = DateTime.Now; + createdArloDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, deviceDetailDTO, true)); } + return createdArloDevices; + } - return createdDevice; + public static List CreateMerossDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, List merossDevices, Provider provider) + { + List createdMerossDevices = new List(); + + foreach (var meross in merossDevices) + { + DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO(); + deviceDetailDTO.Name = meross.devName; + deviceDetailDTO.IpAddress = meross.uuid; // TO CHECK + deviceDetailDTO.ServiceIdentification = meross.uuid; + deviceDetailDTO.ProviderId = provider.Id; + deviceDetailDTO.ConnectionStatus = meross.onlineStatus == 1 ? ConnectionStatus.Connected : ConnectionStatus.Disconnected; + // deviceDetailDTO.Status = meross. ? true : false; // TODO STATE + deviceDetailDTO.Model = meross.deviceType; + //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) + { + properties.Add(property.devName, property.type); + } + // deviceDetailDTO.SupportedOperations = supportedOperationsDTO; TODO + + deviceDetailDTO.MeansOfCommunications = new List(); + deviceDetailDTO.MeansOfCommunications.Add(MeansOfCommunication.Wifi); // TO CHECK + deviceDetailDTO.CreatedDate = DateTime.Now; + deviceDetailDTO.UpdatedDate = DateTime.Now; + createdMerossDevices.Add(CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, deviceDetailDTO, true)); + } + + return createdMerossDevices; } public static List CreateYeelightDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, List yeelightDevices, Provider provider) diff --git a/MyCore/Services/Devices/SupportedDevices/ArloService.cs b/MyCore/Services/Devices/SupportedDevices/ArloService.cs index 2e08de7..2116350 100644 --- a/MyCore/Services/Devices/SupportedDevices/ArloService.cs +++ b/MyCore/Services/Devices/SupportedDevices/ArloService.cs @@ -125,12 +125,13 @@ namespace MyCore.Services resultToken = JsonConvert.DeserializeObject(data.ToString()); //SSE CONNEXION - ConnexionToSSE(); + // ConnexionToSSE(); // TODO in Raspi not here } } catch (Exception e) { resultToken = null; + throw new AuthenticationException("Bad service username or password"); } } @@ -197,7 +198,9 @@ namespace MyCore.Services var data = ((JObject)JsonConvert.DeserializeObject(deviceTask.Result))["data"]; // RETRIEVE ALL ARLO DEVICES allArloDevices = JsonConvert.DeserializeObject>(data.ToString()); - } + } + else + throw new HttpRequestException("Error retrieving arlo devices"); return allArloDevices; } diff --git a/MyCore/Services/Devices/SupportedDevices/MerossService.cs b/MyCore/Services/Devices/SupportedDevices/MerossService.cs index da1e331..8ff497b 100644 --- a/MyCore/Services/Devices/SupportedDevices/MerossService.cs +++ b/MyCore/Services/Devices/SupportedDevices/MerossService.cs @@ -11,6 +11,7 @@ using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; +using System.Security.Authentication; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -27,7 +28,7 @@ namespace MyCore.Services private string _logUrl = $"{_merossUrl}/v1/log/user"; private string _devList = $"{_merossUrl}/v1/Device/devList"; - private static string username = "thomas.fransolet@hotmail.be"; + private string username = "thomas.fransolet@hotmail.be"; private static string password = "Coconuts07"; private static ResultToken resultToken; @@ -64,11 +65,15 @@ namespace MyCore.Services DeviceList } - public MerossService() + public MerossService(string username, string password) { try { + // TODO + /*this.username = username; + this.password = password;*/ + // LOGIN var loginTask = Task.Run(() => PostURI(new Uri(_loginUrl), RequestType.Login)); loginTask.Wait(); @@ -79,36 +84,43 @@ namespace MyCore.Services var data = ((JObject)JsonConvert.DeserializeObject(loginTask.Result))["data"]; resultToken = JsonConvert.DeserializeObject(data.ToString()); - // GET DEVICE LIST - var deviceTask = Task.Run(() => PostURI(new Uri(_devList), RequestType.DeviceList)); - deviceTask.Wait(); - - if (deviceTask.Result != "") - { - data = ((JObject)JsonConvert.DeserializeObject(deviceTask.Result))["data"]; - // RETRIEVE ALL DEVICES - allDevices = JsonConvert.DeserializeObject>(data.ToString()); - } - - // RETRIEVE LOG - var logTask = Task.Run(() => PostURI(new Uri(_logUrl), RequestType.Log)); + // RETRIEVE LOG TODO Create a GetLogs method + /*var logTask = Task.Run(() => PostURI(new Uri(_logUrl), RequestType.Log)); logTask.Wait(); if (logTask.Result != "") { data = ((JObject)JsonConvert.DeserializeObject(logTask.Result))["data"]; - } + }*/ - //MQTT CONNEXION - ConnexionToMqtt(); + //MQTT CONNEXION TODO Create a Connexion To MQTT method + // ConnexionToMqtt(); } } catch (Exception e) { resultToken = null; + throw new AuthenticationException("Bad service username or password"); } } + public List GetMerossDevices() { + // GET DEVICE LIST + var deviceTask = Task.Run(() => PostURI(new Uri(_devList), RequestType.DeviceList)); + deviceTask.Wait(); + + if (deviceTask.Result != "") + { + var data = ((JObject)JsonConvert.DeserializeObject(deviceTask.Result))["data"]; + // RETRIEVE ALL DEVICES + allDevices = JsonConvert.DeserializeObject>(data.ToString()); + } + else + throw new HttpRequestException("Error retrieving meross devices"); + + return allDevices; + } + static async Task PostURI(Uri u, RequestType requestType) { var response = string.Empty;