From 8376acb66246574dbd146caac526ec9926ec7680 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Thu, 26 Mar 2020 12:41:45 +0100 Subject: [PATCH] MC Creation of DTOs, Device controller + update models (add required bson + ToDTO function) + add device service --- .../Controllers/Devices/DeviceController.cs | 154 ++++++++++++++++++ .../Devices/ScreenDeviceController.cs | 30 ++-- .../MyControlPanel/TokenController.cs | 4 +- .../MyControlPanel/UserController.cs | 4 +- MyCore/DTO/MyControlPanel/AutomationDTO.cs | 13 ++ MyCore/DTO/MyControlPanel/DeviceDTO.cs | 59 +++++++ MyCore/DTO/MyControlPanel/GroupDTO.cs | 14 ++ MyCore/DTO/MyControlPanel/InformationDTO.cs | 14 ++ MyCore/DTO/MyControlPanel/LocationDTO.cs | 13 ++ MyCore/DTO/MyControlPanel/ProviderDTO.cs | 16 ++ MyCore/Models/MyControlPanel/Automation.cs | 14 ++ MyCore/Models/MyControlPanel/Device.cs | 61 ++++++- MyCore/Models/MyControlPanel/Group.cs | 14 +- MyCore/Models/MyControlPanel/Information.cs | 13 ++ MyCore/Models/MyControlPanel/Location.cs | 11 ++ MyCore/Models/MyControlPanel/Provider.cs | 13 ++ MyCore/Models/MyControlPanel/UserInfo.cs | 6 + MyCore/Services/Devices/DeviceService.cs | 58 +++++++ MyCore/Services/Devices/YeelightService.cs | 2 + .../AutomationDatabaseService.cs} | 10 +- .../DeviceDatabaseService.cs} | 15 +- .../GroupDatabaseService.cs} | 10 +- .../LocationDatabaseService.cs} | 15 +- .../ProviderDatabaseService.cs} | 20 ++- .../Database/ScreenDeviceDatabaseService.cs} | 4 +- .../UserDatabaseService.cs} | 12 +- MyCore/Startup.cs | 8 +- 27 files changed, 544 insertions(+), 63 deletions(-) create mode 100644 MyCore/Controllers/Devices/DeviceController.cs create mode 100644 MyCore/DTO/MyControlPanel/AutomationDTO.cs create mode 100644 MyCore/DTO/MyControlPanel/DeviceDTO.cs create mode 100644 MyCore/DTO/MyControlPanel/GroupDTO.cs create mode 100644 MyCore/DTO/MyControlPanel/InformationDTO.cs create mode 100644 MyCore/DTO/MyControlPanel/LocationDTO.cs create mode 100644 MyCore/DTO/MyControlPanel/ProviderDTO.cs create mode 100644 MyCore/Services/Devices/DeviceService.cs rename MyCore/Services/MyControlPanel/{AutomationService.cs => Database/AutomationDatabaseService.cs} (82%) rename MyCore/Services/MyControlPanel/{DeviceService.cs => Database/DeviceDatabaseService.cs} (74%) rename MyCore/Services/MyControlPanel/{GroupService.cs => Database/GroupDatabaseService.cs} (83%) rename MyCore/Services/MyControlPanel/{LocationService.cs => Database/LocationDatabaseService.cs} (74%) rename MyCore/Services/MyControlPanel/{ProviderService.cs => Database/ProviderDatabaseService.cs} (67%) rename MyCore/Services/{Devices/ScreenDeviceService.cs => MyControlPanel/Database/ScreenDeviceDatabaseService.cs} (92%) rename MyCore/Services/MyControlPanel/{UserService.cs => Database/UserDatabaseService.cs} (80%) diff --git a/MyCore/Controllers/Devices/DeviceController.cs b/MyCore/Controllers/Devices/DeviceController.cs new file mode 100644 index 0000000..d7d8a9a --- /dev/null +++ b/MyCore/Controllers/Devices/DeviceController.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using MongoDB.Bson; +using MyCore.DTO.Common; +using MyCore.DTO.MyControlPanel; +using MyCore.Models; +using MyCore.Models.MyControlPanel; +using MyCore.Services; +using MyCore.Services.Devices; +using MyCore.Services.MyControlPanel; + +namespace MyCore.Controllers.Devices +{ + [Authorize(Roles = "Admin")] + [Route("api/device")] + [ApiController] + public class DeviceController : ControllerBase + { + private readonly DeviceDatabaseService _DeviceDatabaseService; + private readonly ProviderDatabaseService _ProviderDatabaseService; + private readonly LocationDatabaseService _LocationDatabaseService; + + public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService) + { + _DeviceDatabaseService = DeviceDatabaseService; + _ProviderDatabaseService = ProviderDatabaseService; + _LocationDatabaseService = LocationDatabaseService; + } + + // GET: Devices + /// + /// Get all devices summary + /// + [ProducesResponseType(typeof(List), 200)] + [HttpGet] + public ObjectResult GetAll() + { + try + { + List Devices = _DeviceDatabaseService.GetAll(); + + List devicesSummaryDTO = Devices.Select(d => d.ToSummaryDTO()).ToList(); + + return new OkObjectResult(devicesSummaryDTO); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Get a specific device info + /// + /// id of device + [ProducesResponseType(typeof(DeviceDetailDTO), 200)] + [HttpGet("{deviceId}")] + public ObjectResult GetDetail(string deviceId) + { + try + { + Device device = _DeviceDatabaseService.GetById(deviceId); + + return new OkObjectResult(device.ToDTO()); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Create a device + /// + /// Device to create + [ProducesResponseType(typeof(DeviceDetailDTO), 200)] + [HttpPost] + public ObjectResult Create([FromBody] DeviceDetailDTO deviceDetailDTO) + { + try + { + + if (deviceDetailDTO == null) + throw new KeyNotFoundException("Device is null"); + + DeviceDetailDTO deviceCreated = DeviceService.CreateOrUpdate(deviceDetailDTO, true); + + return new OkObjectResult(deviceCreated); + + } + catch (KeyNotFoundException ex) + { + return new BadRequestObjectResult(ex.Message) { StatusCode = 404 }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Update a device + /// + /// Device Id + /// Device to update + [ProducesResponseType(typeof(DeviceDetailDTO), 200)] + [HttpPut("{deviceId}")] + public ObjectResult Update(string deviceId, [FromBody] DeviceDetailDTO deviceDetailDTO) + { + try + { + if (!_DeviceDatabaseService.IsExist(deviceId)) + throw new KeyNotFoundException("Location does not exist"); + + DeviceDetailDTO deviceUpdated = DeviceService.CreateOrUpdate(deviceDetailDTO, false); + + return new OkObjectResult(deviceUpdated); + } + catch (KeyNotFoundException ex) + { + return new BadRequestObjectResult(ex.Message) { StatusCode = 404 }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Delete a device + /// + /// Id of device to delete + [HttpDelete("{deviceId}")] + public ObjectResult Delete(string deviceId) + { + try + { + // Check if exist + _DeviceDatabaseService.Remove(deviceId); + + return new OkObjectResult(201); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + } +} diff --git a/MyCore/Controllers/Devices/ScreenDeviceController.cs b/MyCore/Controllers/Devices/ScreenDeviceController.cs index 9aa1ed5..3bf828f 100644 --- a/MyCore/Controllers/Devices/ScreenDeviceController.cs +++ b/MyCore/Controllers/Devices/ScreenDeviceController.cs @@ -16,11 +16,11 @@ namespace MyCore.Controllers [ApiController] public class ScreenDeviceController : ControllerBase { - private readonly ScreenDeviceService _ScreenDeviceService; + private readonly ScreenDeviceDatabaseService _ScreenDeviceDatabaseService; - public ScreenDeviceController(ScreenDeviceService ScreenDeviceService) + public ScreenDeviceController(ScreenDeviceDatabaseService ScreenDeviceDatabaseService) { - _ScreenDeviceService = ScreenDeviceService; + _ScreenDeviceDatabaseService = ScreenDeviceDatabaseService; } // GET: Devices @@ -33,7 +33,7 @@ namespace MyCore.Controllers { try { - List screenDevices = _ScreenDeviceService.GetAll(); + List screenDevices = _ScreenDeviceDatabaseService.GetAll(); return new OkObjectResult(screenDevices); } @@ -47,14 +47,14 @@ namespace MyCore.Controllers /// /// /// - /// Id of the screen device you want to get information + /// Id of the screen device you want to get information [ProducesResponseType(typeof(ScreenDevice), 200)] - [HttpGet("{idScreenDevice}")] - public ObjectResult GetDeviceInfo(string idScreenDevice) + [HttpGet("{screenDeviceId}")] + public ObjectResult GetDeviceInfo(string screenDeviceId) { try { - ScreenDevice screenDevice = _ScreenDeviceService.GetInfo(idScreenDevice); + ScreenDevice screenDevice = _ScreenDeviceDatabaseService.GetInfo(screenDeviceId); return new OkObjectResult(screenDevice); } @@ -73,7 +73,7 @@ namespace MyCore.Controllers { try { - _ScreenDeviceService.Create(screenDevice); + _ScreenDeviceDatabaseService.Create(screenDevice); return new OkObjectResult(201); } @@ -87,12 +87,12 @@ namespace MyCore.Controllers /// /// /// - [HttpPut("{idScreenDevice}")] - public ObjectResult UpdateDevice(int idScreenDevice, [FromBody] ScreenDevice screenDevice) + [HttpPut("{screenDeviceId}")] + public ObjectResult UpdateDevice(int screenDeviceId, [FromBody] ScreenDevice screenDevice) { try { - _ScreenDeviceService.Update(screenDevice.Id, screenDevice); + _ScreenDeviceDatabaseService.Update(screenDevice.Id, screenDevice); return new OkObjectResult(201); } @@ -106,12 +106,12 @@ namespace MyCore.Controllers /// /// /// - [HttpDelete("{idDevice}")] - public ObjectResult DeleteDevice(int idDevice, [FromBody] string deviceId) + [HttpDelete("{deviceId}")] + public ObjectResult DeleteDevice(string deviceId) { try { - _ScreenDeviceService.Remove(deviceId); + _ScreenDeviceDatabaseService.Remove(deviceId); return new OkObjectResult(201); } diff --git a/MyCore/Controllers/MyControlPanel/TokenController.cs b/MyCore/Controllers/MyControlPanel/TokenController.cs index 28f39c9..b2730f0 100644 --- a/MyCore/Controllers/MyControlPanel/TokenController.cs +++ b/MyCore/Controllers/MyControlPanel/TokenController.cs @@ -22,9 +22,9 @@ namespace MyCore.Controllers public class TokenController : ControllerBase { private TokenService _tokenService; - private UserService _userService; + private UserDatabaseService _userService; - public TokenController(TokenService tokenService, UserService userService) + public TokenController(TokenService tokenService, UserDatabaseService userService) { _tokenService = tokenService; _userService = userService; diff --git a/MyCore/Controllers/MyControlPanel/UserController.cs b/MyCore/Controllers/MyControlPanel/UserController.cs index 3b8ee11..8e3fdb7 100644 --- a/MyCore/Controllers/MyControlPanel/UserController.cs +++ b/MyCore/Controllers/MyControlPanel/UserController.cs @@ -17,10 +17,10 @@ namespace MyCore.Controllers [ApiController] public class UserController : ControllerBase { - private UserService _userService; + private UserDatabaseService _userService; private TokenService _tokenService; - public UserController(UserService userService, TokenService tokenService) + public UserController(UserDatabaseService userService, TokenService tokenService) { _userService = userService; _tokenService = tokenService; diff --git a/MyCore/DTO/MyControlPanel/AutomationDTO.cs b/MyCore/DTO/MyControlPanel/AutomationDTO.cs new file mode 100644 index 0000000..adcd09b --- /dev/null +++ b/MyCore/DTO/MyControlPanel/AutomationDTO.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.DTO.MyControlPanel +{ + public class AutomationDTO + { + public string Id { get; set; } + public string Name { get; set; } + } +} diff --git a/MyCore/DTO/MyControlPanel/DeviceDTO.cs b/MyCore/DTO/MyControlPanel/DeviceDTO.cs new file mode 100644 index 0000000..1009395 --- /dev/null +++ b/MyCore/DTO/MyControlPanel/DeviceDTO.cs @@ -0,0 +1,59 @@ +using MyCore.DTO.Common; +using MyCore.Models.MyControlPanel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.DTO.MyControlPanel +{ + public class DeviceSummaryDTO + { + public string Id { get; set; } + + public string Name { get; set; } + + public ConnectionStatus ConnectionStatus { get; set; } + + public LocationDTO Location { get; set; } + + public bool Battery { get; set; } + + public int BatteryStatus { get; set; } + } + + public class DeviceDetailDTO + { + public string Id { get; set; } + + public string Name { get; set; } + + public ConnectionStatus ConnectionStatus { get; set; } + + public string LocationId { get; set; } + + public LocationDTO Location { get; set; } + + public List MeansOfCommunications { get; set; } + + public DateTime CreatedDate { get; set; } + + public DateTime UpdatedDate { get; set; } + + public string LastMessage { get; set; } // TODO UNIFORMISATION ? + + public DateTime LastMessageDate { get; set; } + + public string IpAddress { get; set; } + + public bool Battery { get; set; } + + public int BatteryStatus { get; set; } + + public string ProviderId { get; set; } + + public List Groups { get; set; } + + public List Informations { get; set; } + } +} diff --git a/MyCore/DTO/MyControlPanel/GroupDTO.cs b/MyCore/DTO/MyControlPanel/GroupDTO.cs new file mode 100644 index 0000000..a857fdc --- /dev/null +++ b/MyCore/DTO/MyControlPanel/GroupDTO.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.DTO.MyControlPanel +{ + public class GroupDTO + { + public string Id { get; set; } + public string Name { get; set; } + public List Devices { get; set; } + } +} diff --git a/MyCore/DTO/MyControlPanel/InformationDTO.cs b/MyCore/DTO/MyControlPanel/InformationDTO.cs new file mode 100644 index 0000000..6b87914 --- /dev/null +++ b/MyCore/DTO/MyControlPanel/InformationDTO.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.DTO.MyControlPanel +{ + public class InformationDTO + { + public string Id { get; set; } + public string Name { get; set; } + public object Value { get; set; } + } +} diff --git a/MyCore/DTO/MyControlPanel/LocationDTO.cs b/MyCore/DTO/MyControlPanel/LocationDTO.cs new file mode 100644 index 0000000..b1a779f --- /dev/null +++ b/MyCore/DTO/MyControlPanel/LocationDTO.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.DTO.MyControlPanel +{ + public class LocationDTO + { + public string Id { get; set; } + public string Name { get; set; } + } +} diff --git a/MyCore/DTO/MyControlPanel/ProviderDTO.cs b/MyCore/DTO/MyControlPanel/ProviderDTO.cs new file mode 100644 index 0000000..2c51d11 --- /dev/null +++ b/MyCore/DTO/MyControlPanel/ProviderDTO.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.DTO.MyControlPanel +{ + public class ProviderDTO + { + public string Id { get; set; } + public string Name { get; set; } + public string Username { get; set; } + public string Password { get; set; } // TODO ENCRYPTED + public string ApiKey { get; set; } // TODO ENCRYPTED + } +} diff --git a/MyCore/Models/MyControlPanel/Automation.cs b/MyCore/Models/MyControlPanel/Automation.cs index ffbe908..1754216 100644 --- a/MyCore/Models/MyControlPanel/Automation.cs +++ b/MyCore/Models/MyControlPanel/Automation.cs @@ -1,6 +1,7 @@ using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using MyCore.DTO.Common; +using MyCore.DTO.MyControlPanel; using System; using System.Collections.Generic; @@ -16,6 +17,7 @@ namespace MyCore.Models public string Id { get; set; } [BsonElement("Name")] + [BsonRequired] public string Name { get; set; } [BsonElement("Triggers")] @@ -26,6 +28,18 @@ namespace MyCore.Models [BsonElement("Actions")] public List Actions { get; set; } + + public AutomationDTO ToDTO() + { + return new AutomationDTO() + { + Id = Id, + Name = Name, + //Triggers = Triggers + //Conditions = Conditions + //Actions = Actions + }; + } } public class Trigger diff --git a/MyCore/Models/MyControlPanel/Device.cs b/MyCore/Models/MyControlPanel/Device.cs index a0d51cb..8494c5e 100644 --- a/MyCore/Models/MyControlPanel/Device.cs +++ b/MyCore/Models/MyControlPanel/Device.cs @@ -1,8 +1,11 @@ using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using MyCore.DTO.Common; +using MyCore.DTO.MyControlPanel; +using MyCore.Services.MyControlPanel; using System; using System.Collections.Generic; +using System.Linq; namespace MyCore.Models.MyControlPanel { @@ -10,19 +13,22 @@ namespace MyCore.Models.MyControlPanel /// Group of devices /// public class Device - { + { + private LocationDatabaseService _LocationDatabaseService; [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } [BsonElement("Name")] + [BsonRequired] public string Name { get; set; } [BsonElement("ConnectionStatus")] public ConnectionStatus ConnectionStatus { get; set; } - [BsonElement("Location")] - public Location Location { get; set; } + [BsonElement("LocationId")] + [BsonRequired] + public string LocationId { get; set; } [BsonElement("MeansOfCommunications")] public List MeansOfCommunications { get; set; } @@ -39,19 +45,58 @@ namespace MyCore.Models.MyControlPanel [BsonElement("LastMessageDate")] public DateTime LastMessageDate { get; set; } + [BsonElement("IpAddress")] + public string IpAddress { get; set; } + [BsonElement("Battery")] public bool Battery { get; set; } [BsonElement("BatteryStatus")] public int BatteryStatus { get; set; } - [BsonElement("Provider")] - public Provider provider { get; set; } + [BsonElement("ProviderId")] + [BsonRequired] + public string ProviderId { get; set; } - [BsonElement("Groups")] - public List Groups { get; set; } + [BsonElement("GroupIds")] + public List GroupIds { get; set; } [BsonElement("Informations")] - public List Informations { get; set; } + public List Information { get; set; } + + public DeviceSummaryDTO ToSummaryDTO() + { + return new DeviceSummaryDTO() + { + Id = Id, + Name = Name, + ConnectionStatus = ConnectionStatus, + Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way + Battery = Battery, + BatteryStatus = BatteryStatus + }; + } + + public DeviceDetailDTO ToDTO() + { + return new DeviceDetailDTO() + { + Id = Id, + Name = Name, + ConnectionStatus = ConnectionStatus, + Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way + MeansOfCommunications = MeansOfCommunications, + CreatedDate = CreatedDate, + UpdatedDate = UpdatedDate, + LastMessage = LastMessage, + LastMessageDate = LastMessageDate, + IpAddress = IpAddress, + //Provider = Provider.ToDTO(device.Provider), + //Groups = device.Groups.Select(i => i.ToDTO(i)).ToList(), + //Informations = device.Informations.Select(i => i.ToDTO(i)).ToList(), + Battery = Battery, + BatteryStatus = BatteryStatus + }; + } } } diff --git a/MyCore/Models/MyControlPanel/Group.cs b/MyCore/Models/MyControlPanel/Group.cs index b4b598b..fab9285 100644 --- a/MyCore/Models/MyControlPanel/Group.cs +++ b/MyCore/Models/MyControlPanel/Group.cs @@ -1,6 +1,7 @@ using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using MyCore.DTO.Common; +using MyCore.DTO.MyControlPanel; using System; using System.Collections.Generic; namespace MyCore.Models.MyControlPanel @@ -15,9 +16,20 @@ namespace MyCore.Models.MyControlPanel public string Id { get; set; } [BsonElement("Name")] + [BsonRequired] public string Name { get; set; } [BsonElement("Devices")] - public List Devices { get; set; } + public List Devices { get; set; } + + public GroupDTO ToDTO() + { + return new GroupDTO() + { + Id = Id, + Name = Name, + Devices = Devices + }; + } } } diff --git a/MyCore/Models/MyControlPanel/Information.cs b/MyCore/Models/MyControlPanel/Information.cs index c8a3522..5b73d58 100644 --- a/MyCore/Models/MyControlPanel/Information.cs +++ b/MyCore/Models/MyControlPanel/Information.cs @@ -1,6 +1,7 @@ using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using MyCore.DTO.Common; +using MyCore.DTO.MyControlPanel; using System; using System.Collections.Generic; @@ -16,9 +17,21 @@ namespace MyCore.Models.MyControlPanel public string Id { get; set; } [BsonElement("Name")] + [BsonRequired] public string Name { get; set; } [BsonElement("Value")] + [BsonRequired] public object Value { get; set; } + + public InformationDTO ToDTO() + { + return new InformationDTO() + { + Id = Id, + Name = Name, + Value = Value + }; + } } } diff --git a/MyCore/Models/MyControlPanel/Location.cs b/MyCore/Models/MyControlPanel/Location.cs index 6c32a4e..0069ec9 100644 --- a/MyCore/Models/MyControlPanel/Location.cs +++ b/MyCore/Models/MyControlPanel/Location.cs @@ -1,5 +1,6 @@ using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; +using MyCore.DTO.MyControlPanel; namespace MyCore.Models.MyControlPanel { @@ -13,6 +14,16 @@ namespace MyCore.Models.MyControlPanel public string Id { get; set; } [BsonElement("Name")] + [BsonRequired] public string Name { get; set; } + + public LocationDTO ToDTO() + { + return new LocationDTO() + { + Id = Id, + Name = Name + }; + } } } \ No newline at end of file diff --git a/MyCore/Models/MyControlPanel/Provider.cs b/MyCore/Models/MyControlPanel/Provider.cs index aa04e08..e2de1f3 100644 --- a/MyCore/Models/MyControlPanel/Provider.cs +++ b/MyCore/Models/MyControlPanel/Provider.cs @@ -1,6 +1,7 @@ using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using MyCore.DTO.Common; +using MyCore.DTO.MyControlPanel; using System; using System.Collections.Generic; @@ -16,15 +17,27 @@ namespace MyCore.Models.MyControlPanel public string Id { get; set; } [BsonElement("Name")] + [BsonRequired] public string Name { get; set; } [BsonElement("Username")] + [BsonRequired] public string Username { get; set; } [BsonElement("Password")] + [BsonRequired] public string Password { get; set; } // TODO ENCRYPTED [BsonElement("ApiKey")] public string ApiKey { get; set; } // TODO ENCRYPTED + + public ProviderDTO ToDTO() + { + return new ProviderDTO() + { + Id = Id, + Name = Name + }; + } } } diff --git a/MyCore/Models/MyControlPanel/UserInfo.cs b/MyCore/Models/MyControlPanel/UserInfo.cs index 7de90c2..3220133 100644 --- a/MyCore/Models/MyControlPanel/UserInfo.cs +++ b/MyCore/Models/MyControlPanel/UserInfo.cs @@ -22,18 +22,23 @@ namespace MyCore.Models public string Role { get; set; } [BsonElement("Email")] + [BsonRequired] public string Email { get; set; } // UNIQUE !.. [BsonElement("Password")] + [BsonRequired] public string Password { get; set; } [BsonElement("FirstName")] + [BsonRequired] public string FirstName { get; set; } [BsonElement("LastName")] + [BsonRequired] public string LastName { get; set; } [BsonElement("Token")] + [BsonRequired] public string Token { get; set; } [BsonElement("Birthday")] @@ -55,6 +60,7 @@ namespace MyCore.Models public string Country { get; set; } [BsonElement("Language")] + [BsonRequired] public string Language { get; set; } [BsonElement("TimeZone")] diff --git a/MyCore/Services/Devices/DeviceService.cs b/MyCore/Services/Devices/DeviceService.cs new file mode 100644 index 0000000..ccc8b9e --- /dev/null +++ b/MyCore/Services/Devices/DeviceService.cs @@ -0,0 +1,58 @@ +using Microsoft.AspNetCore.Mvc; +using MyCore.DTO.Common; +using MyCore.DTO.MyControlPanel; +using MyCore.Models.MyControlPanel; +using MyCore.Services.MyControlPanel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.Services.Devices +{ + public class DeviceService + { + private static DeviceDatabaseService _DeviceDatabaseService; + private static ProviderDatabaseService _ProviderDatabaseService; + private static LocationDatabaseService _LocationDatabaseService; + + public static DeviceDetailDTO CreateOrUpdate(DeviceDetailDTO deviceDetailDTO, bool create) + { + Device device; + if (create) + device = new Device(); + else + { + device = _DeviceDatabaseService.GetById(deviceDetailDTO.Id); + } + + device.Name = deviceDetailDTO.Name; + if (_ProviderDatabaseService.IsExist(deviceDetailDTO.ProviderId)) + device.ProviderId = deviceDetailDTO.ProviderId; + else + throw new KeyNotFoundException("Provider does not exist"); + + if (_LocationDatabaseService.IsExist(deviceDetailDTO.LocationId)) + device.LocationId = deviceDetailDTO.LocationId; + else + throw new KeyNotFoundException("Location does not exist"); + + device.ConnectionStatus = ConnectionStatus.Unknown; + device.CreatedDate = DateTime.Now; + device.UpdatedDate = DateTime.Now; + + device.MeansOfCommunications = deviceDetailDTO.MeansOfCommunications; + device.IpAddress = deviceDetailDTO.IpAddress; + device.Battery = deviceDetailDTO.Battery; + device.BatteryStatus = deviceDetailDTO.BatteryStatus; + device.GroupIds = device.GroupIds; + // Todo structure informations + device.Information = device.Information; + + if (create) + return _DeviceDatabaseService.Create(device).ToDTO(); + else + return _DeviceDatabaseService.Update(device.Id, device).ToDTO(); + } + } +} diff --git a/MyCore/Services/Devices/YeelightService.cs b/MyCore/Services/Devices/YeelightService.cs index b7d9c44..da2743d 100644 --- a/MyCore/Services/Devices/YeelightService.cs +++ b/MyCore/Services/Devices/YeelightService.cs @@ -13,6 +13,8 @@ namespace MyCore.Services public async Task> GetDevices() { devices = await DeviceLocator.Discover(); + + //Toggle(devices[1]); return devices; } diff --git a/MyCore/Services/MyControlPanel/AutomationService.cs b/MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs similarity index 82% rename from MyCore/Services/MyControlPanel/AutomationService.cs rename to MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs index af951bf..d3020ee 100644 --- a/MyCore/Services/MyControlPanel/AutomationService.cs +++ b/MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs @@ -9,27 +9,27 @@ using MyCore.Models.MyControlPanel; namespace MyCore.Services.MyControlPanel { - public class AutomationService + public class AutomationDatabaseService { private readonly IMongoCollection _Automations; - public AutomationService(IConfiguration config) + public AutomationDatabaseService(IConfiguration config) { var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var database = client.GetDatabase("MyCoreDb"); _Automations = database.GetCollection("Automations"); } - public List GetAutomations() + public List GetAll() { return _Automations.Find(d => true).ToList(); } - public Automation GetAutomationById(string id) + public Automation GetById(string id) { return _Automations.Find(a => a.Id == id).FirstOrDefault(); } - public Automation CreateAutomation(Automation automation) + public Automation Create(Automation automation) { _Automations.InsertOne(automation); return automation; diff --git a/MyCore/Services/MyControlPanel/DeviceService.cs b/MyCore/Services/MyControlPanel/Database/DeviceDatabaseService.cs similarity index 74% rename from MyCore/Services/MyControlPanel/DeviceService.cs rename to MyCore/Services/MyControlPanel/Database/DeviceDatabaseService.cs index 78ec6ba..9c585f1 100644 --- a/MyCore/Services/MyControlPanel/DeviceService.cs +++ b/MyCore/Services/MyControlPanel/Database/DeviceDatabaseService.cs @@ -9,27 +9,32 @@ using MyCore.Models.MyControlPanel; namespace MyCore.Services.MyControlPanel { - public class DeviceService + public class DeviceDatabaseService { private readonly IMongoCollection _Devices; - public DeviceService(IConfiguration config) + public DeviceDatabaseService(IConfiguration config) { var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var database = client.GetDatabase("MyCoreDb"); _Devices = database.GetCollection("Devices"); } - public List GetDevices() + public List GetAll() { return _Devices.Find(d => true).ToList(); } - public Device GetDeviceById(string id) + public Device GetById(string id) { return _Devices.Find(d => d.Id == id).FirstOrDefault(); } - public Device CreateDevice(Device device) + public bool IsExist(string id) + { + return _Devices.Find(d => d.Id == id).FirstOrDefault() != null ? true : false; + } + + public Device Create(Device device) { _Devices.InsertOne(device); return device; diff --git a/MyCore/Services/MyControlPanel/GroupService.cs b/MyCore/Services/MyControlPanel/Database/GroupDatabaseService.cs similarity index 83% rename from MyCore/Services/MyControlPanel/GroupService.cs rename to MyCore/Services/MyControlPanel/Database/GroupDatabaseService.cs index 8b02194..3f48fbe 100644 --- a/MyCore/Services/MyControlPanel/GroupService.cs +++ b/MyCore/Services/MyControlPanel/Database/GroupDatabaseService.cs @@ -9,27 +9,27 @@ using MyCore.Models.MyControlPanel; namespace MyCore.Services.MyControlPanel { - public class GroupService + public class GroupDatabaseService { private readonly IMongoCollection _Groups; - public GroupService(IConfiguration config) + public GroupDatabaseService(IConfiguration config) { var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var database = client.GetDatabase("MyCoreDb"); _Groups = database.GetCollection("Groups"); } - public List GetGroups() + public List GetAll() { return _Groups.Find(d => true).ToList(); } - public Group GetGroupById(string id) + public Group GetById(string id) { return _Groups.Find(g => g.Id == id).FirstOrDefault(); } - public Group CreateGroup(Group group) + public Group Create(Group group) { _Groups.InsertOne(group); return group; diff --git a/MyCore/Services/MyControlPanel/LocationService.cs b/MyCore/Services/MyControlPanel/Database/LocationDatabaseService.cs similarity index 74% rename from MyCore/Services/MyControlPanel/LocationService.cs rename to MyCore/Services/MyControlPanel/Database/LocationDatabaseService.cs index d75c8d6..d4be9e3 100644 --- a/MyCore/Services/MyControlPanel/LocationService.cs +++ b/MyCore/Services/MyControlPanel/Database/LocationDatabaseService.cs @@ -9,27 +9,32 @@ using MyCore.Models.MyControlPanel; namespace MyCore.Services.MyControlPanel { - public class LocationService + public class LocationDatabaseService { private readonly IMongoCollection _Locations; - public LocationService(IConfiguration config) + public LocationDatabaseService(IConfiguration config) { var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var database = client.GetDatabase("MyCoreDb"); _Locations = database.GetCollection("Locations"); } - public List GetLocations() + public List GetAll() { return _Locations.Find(l => true).ToList(); } - public Location GetLocationById(string id) + public Location GetById(string id) { return _Locations.Find(l => l.Id == id).FirstOrDefault(); } - public Location CreateLocation(Location location) + public bool IsExist(string id) + { + return _Locations.Find(p => p.Id == id).FirstOrDefault() != null ? true : false; + } + + public Location Create(Location location) { _Locations.InsertOne(location); return location; diff --git a/MyCore/Services/MyControlPanel/ProviderService.cs b/MyCore/Services/MyControlPanel/Database/ProviderDatabaseService.cs similarity index 67% rename from MyCore/Services/MyControlPanel/ProviderService.cs rename to MyCore/Services/MyControlPanel/Database/ProviderDatabaseService.cs index e3df096..3e86a0e 100644 --- a/MyCore/Services/MyControlPanel/ProviderService.cs +++ b/MyCore/Services/MyControlPanel/Database/ProviderDatabaseService.cs @@ -8,27 +8,37 @@ using MongoDB.Driver; using MyCore.Models.MyControlPanel; namespace MyCore.Services.MyControlPanel { - public class ProviderService + public class ProviderDatabaseService { private readonly IMongoCollection _Providers; - public ProviderService(IConfiguration config) + public ProviderDatabaseService(IConfiguration config) { var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var database = client.GetDatabase("MyCoreDb"); _Providers = database.GetCollection("Providers"); } - public List GetProviders() + public List GetAll() { return _Providers.Find(p => true).ToList(); } - public Provider GetProviderById(string id) + public Provider GetById(string id) { return _Providers.Find(p => p.Id == id).FirstOrDefault(); } - public Provider CreateProvider(Provider provider) + public Provider GetByName(string name) + { + return _Providers.Find(p => p.Name == name).FirstOrDefault(); + } + + public bool IsExist(string id) + { + return _Providers.Find(p => p.Id == id).FirstOrDefault() != null ? true : false; + } + + public Provider Create(Provider provider) { _Providers.InsertOne(provider); return provider; diff --git a/MyCore/Services/Devices/ScreenDeviceService.cs b/MyCore/Services/MyControlPanel/Database/ScreenDeviceDatabaseService.cs similarity index 92% rename from MyCore/Services/Devices/ScreenDeviceService.cs rename to MyCore/Services/MyControlPanel/Database/ScreenDeviceDatabaseService.cs index ae9d9be..eabdb18 100644 --- a/MyCore/Services/Devices/ScreenDeviceService.cs +++ b/MyCore/Services/MyControlPanel/Database/ScreenDeviceDatabaseService.cs @@ -8,11 +8,11 @@ using MongoDB.Driver; namespace MyCore.Services { - public class ScreenDeviceService + public class ScreenDeviceDatabaseService { private readonly IMongoCollection _screenDevices; - public ScreenDeviceService(IConfiguration config) + public ScreenDeviceDatabaseService(IConfiguration config) { var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var database = client.GetDatabase("MyCoreDb"); diff --git a/MyCore/Services/MyControlPanel/UserService.cs b/MyCore/Services/MyControlPanel/Database/UserDatabaseService.cs similarity index 80% rename from MyCore/Services/MyControlPanel/UserService.cs rename to MyCore/Services/MyControlPanel/Database/UserDatabaseService.cs index bc81406..0f7046a 100644 --- a/MyCore/Services/MyControlPanel/UserService.cs +++ b/MyCore/Services/MyControlPanel/Database/UserDatabaseService.cs @@ -8,32 +8,32 @@ using MongoDB.Driver; namespace MyCore.Services { - public class UserService + public class UserDatabaseService { private readonly IMongoCollection _Users; - public UserService(IConfiguration config) + public UserDatabaseService(IConfiguration config) { var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var database = client.GetDatabase("MyCoreDb"); _Users = database.GetCollection("Users"); } - public List GetUsers() + public List GetAll() { return _Users.Find(m => true).ToList(); } - public UserInfo GetUserByEmail(string email) + public UserInfo GetByEmail(string email) { return _Users.Find(m => m.Email == email).FirstOrDefault(); } - public UserInfo GetUserById(string id) + public UserInfo GetById(string id) { return _Users.Find(m => m.Id == id).FirstOrDefault(); } - public UserInfo CreateUser(UserInfo user) + public UserInfo Create(UserInfo user) { _Users.InsertOne(user); return user; diff --git a/MyCore/Startup.cs b/MyCore/Startup.cs index 664fc08..29d4733 100644 --- a/MyCore/Startup.cs +++ b/MyCore/Startup.cs @@ -31,6 +31,10 @@ namespace MyCore //MerossService merossService = new MerossService(); //ArloService arloService = new ArloService(); + + YeelightService yeelighService = new YeelightService(); + yeelighService.GetDevices(); + } public IConfiguration Configuration { get; } @@ -41,9 +45,9 @@ namespace MyCore // Add the service (test purpose) services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);