diff --git a/MyCore.Interfaces/DTO/MyControlPanel/AutomationDTO.cs b/MyCore.Interfaces/DTO/MyControlPanel/AutomationDTO.cs index fb0f0d4..6b4f7f0 100644 --- a/MyCore.Interfaces/DTO/MyControlPanel/AutomationDTO.cs +++ b/MyCore.Interfaces/DTO/MyControlPanel/AutomationDTO.cs @@ -1,4 +1,5 @@ -using System; +using MyCore.Interfaces.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -9,5 +10,24 @@ namespace MyCore.Interfaces.DTO { public string Id { get; set; } public string Name { get; set; } + public string UserId { get; set; } + public DateTime CreatedDate { get; set; } + public DateTime UpdatedDate { get; set; } + } + + public class AutomationDetailDTO : AutomationDTO + { + public List Triggers { get; set; } + public List Conditions { get; set; } + public List Actions { get; set; } + public List DeviceIds { get; set; } + } + + public class AutomationCreateOrUpdateDetailDTO : AutomationDTO + { + public List Triggers { get; set; } + public List Conditions { get; set; } + public List Actions { get; set; } + public List DeviceIds { get; set; } } } diff --git a/MyCore.Interfaces/DTO/MyControlPanel/DeviceDTO.cs b/MyCore.Interfaces/DTO/MyControlPanel/DeviceDTO.cs index 49bb50b..dbdf787 100644 --- a/MyCore.Interfaces/DTO/MyControlPanel/DeviceDTO.cs +++ b/MyCore.Interfaces/DTO/MyControlPanel/DeviceDTO.cs @@ -38,33 +38,14 @@ namespace MyCore.Interfaces.DTO public int BatteryStatus { get; set; } } - public class DeviceDetailDTO + public class DeviceDetailDTO : DeviceSummaryDTO { - public string Id { get; set; } - - public string UserId { get; set; } - - public string Name { get; set; } - public string Description { get; set; } - - public string Model { get; set; } - - public DeviceType Type { get; set; } - public string FirmwareVersion { get; set; } public string HardwareVersion { get; set; } public int Port { get; set; } - public ConnectionStatus ConnectionStatus { get; set; } - - public bool Status { get; set; } - - public string LocationId { get; set; } - - public LocationDTO Location { get; set; } - public List MeansOfCommunications { get; set; } public DateTime CreatedDate { get; set; } @@ -73,20 +54,10 @@ namespace MyCore.Interfaces.DTO public string LastState { get; set; } // TODO UNIFORMISATION ? - public DateTime LastStateDate { get; set; } - public string IpAddress { get; set; } public string ServiceIdentification { get; set; } - public bool Battery { get; set; } - - public int BatteryStatus { get; set; } - - public string ProviderId { get; set; } - - public string ProviderName { get; set; } - public string ManufacturerName { get; set; } public List GroupIds { get; set; } diff --git a/MyCore.Interfaces/DTO/MyControlPanel/RoomDTO.cs b/MyCore.Interfaces/DTO/MyControlPanel/RoomDTO.cs index cc5dac3..54a1162 100644 --- a/MyCore.Interfaces/DTO/MyControlPanel/RoomDTO.cs +++ b/MyCore.Interfaces/DTO/MyControlPanel/RoomDTO.cs @@ -14,14 +14,8 @@ namespace MyCore.Interfaces.DTO public string Name { get; set; } } - public class RoomDetailDTO + public class RoomDetailDTO : RoomSummaryDTO { - public string Id { get; set; } - - public string UserId { get; set; } - - public string Name { get; set; } - public DateTime CreatedDate { get; set; } public DateTime UpdatedDate { get; set; } @@ -29,14 +23,8 @@ namespace MyCore.Interfaces.DTO public List DeviceIds { get; set; } } - public class RoomCreateOrUpdateDetailDTO + public class RoomCreateOrUpdateDetailDTO : RoomSummaryDTO { - public string Id { get; set; } - - public string UserId { get; set; } - - public string Name { get; set; } - public List DeviceIds { get; set; } } } diff --git a/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs b/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs index ac2a2b2..1bf3154 100644 --- a/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs +++ b/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs @@ -23,24 +23,46 @@ namespace MyCore.Interfaces.Models [BsonRequired] public string Name { get; set; } + [BsonElement("CreatedDate")] + public DateTime CreatedDate { get; set; } + + [BsonElement("UpdatedDate")] + public DateTime UpdatedDate { get; set; } + [BsonElement("Triggers")] public List Triggers { get; set; } [BsonElement("Conditions")] public List Conditions { get; set; } - [BsonElement("DevicesIds")] - public List DevicesIds { get; set; } - [BsonElement("Actions")] public List Actions { get; set; } + [BsonElement("DevicesIds")] + public List DevicesIds { get; set; } + public AutomationDTO ToDTO() { return new AutomationDTO() { Id = Id, Name = Name, + UserId = UserId, + //Triggers = Triggers + //Conditions = Conditions + //Actions = Actions + }; + } + + public AutomationDetailDTO ToDetailDTO() + { + return new AutomationDetailDTO() + { + Id = Id, + Name = Name, + UserId = UserId, + CreatedDate = CreatedDate, + UpdatedDate = UpdatedDate, //Triggers = Triggers //Conditions = Conditions //Actions = Actions diff --git a/MyCore/Controllers/AutomationController.cs b/MyCore/Controllers/AutomationController.cs index 4256d8c..b2179fc 100644 --- a/MyCore/Controllers/AutomationController.cs +++ b/MyCore/Controllers/AutomationController.cs @@ -11,6 +11,7 @@ using MongoDB.Bson; using Mqtt.Client.AspNetCore.Services; using MyCore.Interfaces.DTO; using MyCore.Interfaces.Models; +using MyCore.Service.Services; using MyCore.Services; using MyCore.Services.Devices; using MyCore.Services.MyControlPanel; @@ -61,7 +62,7 @@ namespace MyCore.Service.Controllers /// Get detail info of a specified automation /// /// automation id - [ProducesResponseType(typeof(RoomDetailDTO), 200)] + [ProducesResponseType(typeof(AutomationDetailDTO), 200)] [HttpGet("detail/{roomId}")] public ObjectResult GetDetail(string automationId) { @@ -69,7 +70,7 @@ namespace MyCore.Service.Controllers { Automation automation = _AutomationDatabaseService.GetById(automationId); - return new OkObjectResult(automation.ToDTO()); + return new OkObjectResult(automation.ToDetailDTO()); } catch (Exception ex) { @@ -77,23 +78,23 @@ namespace MyCore.Service.Controllers } } - /* + //// /// Create an automation /// /// Automation to create - [ProducesResponseType(typeof(RoomDetailDTO), 200)] + [ProducesResponseType(typeof(AutomationDTO), 200)] [HttpPost] public ObjectResult Create([FromBody] AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetail) { try { - if (roomCreateOrUpdateDetail == null) - throw new KeyNotFoundException("Room is null"); + if (automationCreateOrUpdateDetail == null) + throw new KeyNotFoundException("Automation is null"); - RoomDetailDTO roomCreated = RoomService.CreateOrUpdate(this._RoomDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, true); + AutomationDTO automationCreated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationCreateOrUpdateDetail.UserId, automationCreateOrUpdateDetail, true); - return new OkObjectResult(roomCreated); + return new OkObjectResult(automationCreated); } catch (Exception ex) { @@ -101,22 +102,23 @@ namespace MyCore.Service.Controllers } } + /// - /// Update a room + /// Update an automation /// - /// room to update - [ProducesResponseType(typeof(RoomCreateOrUpdateDetailDTO), 200)] + /// automation to update + [ProducesResponseType(typeof(AutomationCreateOrUpdateDetailDTO), 200)] [HttpPut] - public ObjectResult Update([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail) + public ObjectResult Update([FromBody] AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetail) { try { - if (!_RoomDatabaseService.IsExist(roomCreateOrUpdateDetail.Id)) - throw new KeyNotFoundException("Room does not exist"); + if (!_AutomationDatabaseService.IsExist(automationCreateOrUpdateDetail.Id)) + throw new KeyNotFoundException("Automation does not exist"); - RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, false); + AutomationDTO automationUpdated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationCreateOrUpdateDetail.UserId, automationCreateOrUpdateDetail, false); - return new OkObjectResult(roomUpdated); + return new OkObjectResult(automationUpdated); } catch (KeyNotFoundException ex) { @@ -126,7 +128,7 @@ namespace MyCore.Service.Controllers { return new ObjectResult(ex.Message) { StatusCode = 500 }; } - }*/ + } /// /// Delete an automation diff --git a/MyCore/Services/AutomationService.cs b/MyCore/Services/AutomationService.cs new file mode 100644 index 0000000..7f7c47e --- /dev/null +++ b/MyCore/Services/AutomationService.cs @@ -0,0 +1,37 @@ +using MyCore.Interfaces.DTO; +using MyCore.Interfaces.Models; +using MyCore.Services.MyControlPanel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.Service.Services +{ + public class AutomationService + { + public static AutomationDTO CreateOrUpdate(AutomationDatabaseService _AutomationDatabaseService, string userId, AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetailDTO, bool create) + { + Automation automation; + if (create) + automation = new Automation(); + else + { + automation = _AutomationDatabaseService.GetById(automationCreateOrUpdateDetailDTO.Id); + } + + automation.UserId = userId; + automation.Name = automationCreateOrUpdateDetailDTO.Name; + automation.CreatedDate = DateTime.Now; + automation.UpdatedDate = DateTime.Now; + automation.Triggers = automationCreateOrUpdateDetailDTO.Triggers; + automation.Conditions = automationCreateOrUpdateDetailDTO.Conditions; + automation.Actions = automationCreateOrUpdateDetailDTO.Actions; + + if (create) + return _AutomationDatabaseService.Create(automation).ToDTO(); + else + return _AutomationDatabaseService.Update(automation.Id, automation).ToDTO(); + } + } +}