diff --git a/MyCore.Interfaces/DTO/MyControlPanel/GroupDTO.cs b/MyCore.Interfaces/DTO/MyControlPanel/GroupDTO.cs index aa5c878..aebe437 100644 --- a/MyCore.Interfaces/DTO/MyControlPanel/GroupDTO.cs +++ b/MyCore.Interfaces/DTO/MyControlPanel/GroupDTO.cs @@ -8,11 +8,8 @@ namespace MyCore.Interfaces.DTO public class GroupSummaryDTO { public string Id { get; set; } - public string UserId { get; set; } - public string Name { get; set; } - public string Type { get; set; } public bool IsAlarm { get; set; } } @@ -20,9 +17,7 @@ namespace MyCore.Interfaces.DTO public class GroupDetailDTO : GroupSummaryDTO { public DateTime CreatedDate { get; set; } - public DateTime UpdatedDate { get; set; } - public List Devices { get; set; } } diff --git a/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs b/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs index 39d6c66..c524173 100644 --- a/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs +++ b/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs @@ -51,6 +51,7 @@ namespace MyCore.Interfaces.Models { Id = Id, Name = Name, + Active = Active, UserId = UserId, CreatedDate = CreatedDate, UpdatedDate = UpdatedDate, @@ -66,6 +67,7 @@ namespace MyCore.Interfaces.Models { Id = Id, Name = Name, + Active = Active, UserId = UserId, CreatedDate = CreatedDate, UpdatedDate = UpdatedDate, diff --git a/MyCore/Controllers/AutomationController.cs b/MyCore/Controllers/AutomationController.cs index 24fb5a1..cebfe00 100644 --- a/MyCore/Controllers/AutomationController.cs +++ b/MyCore/Controllers/AutomationController.cs @@ -40,7 +40,7 @@ namespace MyCore.Service.Controllers /// Get all automations for the specified user /// /// Id of user - [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{userId}")] public ObjectResult GetAll(string userId) diff --git a/MyCore/Services/Devices/ActionService.cs b/MyCore/Services/Devices/ActionService.cs index 3d71d17..b026899 100644 --- a/MyCore/Services/Devices/ActionService.cs +++ b/MyCore/Services/Devices/ActionService.cs @@ -44,7 +44,7 @@ namespace MyCore.Services.Devices if (currentProvider != null) { - var automations = _AutomationDatabaseService.GetByProvider(currentProvider.Id); + var automations = _AutomationDatabaseService.GetActiveByProvider(currentProvider.Id); deviceTrigger = _DeviceDatabaseService.GetByName(deviceServiceName).FirstOrDefault(); @@ -57,137 +57,142 @@ namespace MyCore.Services.Devices { foreach (var automation in automations) { - // todo check if not null and if more than one element - if (deviceTrigger != null && automation.Triggers.Any(t => t.DeviceId == deviceTrigger.Id)) - { - var automationTriggers = automation.Triggers.Where(t => t.DeviceId == deviceTrigger.Id).ToList(); - - // Test for each automation trigger found - foreach (var automationTrigger in automationTriggers) + try { + // todo check if not null and if more than one element + if (deviceTrigger != null && automation.Triggers.Any(t => t.DeviceId == deviceTrigger.Id)) { - List exposes = GetDeviceExposes(deviceTrigger); + var automationTriggers = automation.Triggers.Where(t => t.DeviceId == deviceTrigger.Id).ToList(); - var triggerStateValueCheck = (object)null; - - // Try to get automationTrigger.StateName of zigbee device - var expose = exposes.Where(e => e.name == automationTrigger.StateName).FirstOrDefault(); - bool validTrigger = expose != null; // No expose found.. => the device has not the correct state => not correct automation - - try + // Test for each automation trigger found + foreach (var automationTrigger in automationTriggers) { - // Try to get specific field from message - var dic = JsonConvert.DeserializeObject>(message); - triggerStateValueCheck = dic[automationTrigger.StateName]; // if action slide => get slide - } - catch (Exception ex) - { - validTrigger = false; - } + List exposes = GetDeviceExposes(deviceTrigger); + var triggerStateValueCheck = (object)null; - if (validTrigger && automationTrigger.StateValue.ToLower() == triggerStateValueCheck.ToString().ToLower()) - { - // Correct trigger ! - System.Console.WriteLine($"Correct trigger for automation {automation.Name}"); + // Try to get automationTrigger.StateName of zigbee device + var expose = exposes.Where(e => e.name == automationTrigger.StateName).FirstOrDefault(); + bool validTrigger = expose != null; // No expose found.. => the device has not the correct state => not correct automation - var isConditionsRespected = CheckConditions(automation.Conditions); - - if (!isConditionsRespected.Any(cr => !cr)) + try { - System.Console.WriteLine("Conditions respected !"); - foreach (var action in automation.Actions) + // Try to get specific field from message + var dic = JsonConvert.DeserializeObject>(message); + triggerStateValueCheck = dic[automationTrigger.StateName]; // if action slide => get slide + } + catch (Exception ex) + { + validTrigger = false; + } + + + if (validTrigger && automationTrigger.StateValue.ToLower() == triggerStateValueCheck.ToString().ToLower()) + { + // Correct trigger ! + System.Console.WriteLine($"Correct trigger for automation {automation.Name}"); + + var isConditionsRespected = CheckConditions(automation.Conditions); + + if (!isConditionsRespected.Any(cr => !cr)) { - - var DeviceNameForAction = ""; - Device actionDeviceToTest = new Device(); - - // Retrieve action type - switch (action.Type) + System.Console.WriteLine("Conditions respected !"); + foreach (var action in automation.Actions) { - case ActionType.DEVICE: - var deviceAction = _DeviceDatabaseService.GetById(action.DeviceId); - var providerActionTest = _ProviderDatabaseService.GetById(userId, action.ProviderId); - DeviceNameForAction = deviceAction.Name; - actionDeviceToTest = deviceAction; - System.Console.WriteLine($"We get a device action ! Name={deviceAction.Name} Type={deviceAction.Type}"); - System.Console.WriteLine($"Check action provider type ! Type={providerActionTest.Type} Name={providerActionTest.Name}"); - break; - case ActionType.GROUP: - var groupAction = _GroupDatabaseService.GetById(action.GroupId); - DeviceNameForAction = groupAction.Name; + var DeviceNameForAction = ""; + Device actionDeviceToTest = new Device(); - System.Console.WriteLine($"We get a group action ! Name={groupAction.Name} Type={groupAction.Type}"); - System.Console.WriteLine($"Check action zigbeeGroupAction type ! Type={groupAction.Type}"); - - // Check state of first device of a group - actionDeviceToTest = _DeviceDatabaseService.GetByGroup(groupAction.Id).FirstOrDefault(); // TODO : Send error if no device found ! - break; - case ActionType.MQTT: - // take raw request and send it ! - RawRequestMQTT rawRequestMQTT = JsonConvert.DeserializeObject(action.RawRequest); - - if (rawRequestMQTT!= null) { - // SEND REQUEST - System.Console.WriteLine($"Send raw request mqtt! topic:{rawRequestMQTT.topic} - message: {rawRequestMQTT.message}"); - MqttClientService.PublishMessage(rawRequestMQTT.topic, rawRequestMQTT.message); - } - break; - case ActionType.HTTP: // Correct way ? - // TODO - break; - } - - var providerAction = _ProviderDatabaseService.GetById(userId, action.ProviderId); - - // Check if device exist - if (actionDeviceToTest != null && providerAction != null) - { - switch (providerAction.Type) + // Retrieve action type + switch (action.Type) { - case "zigbee2mqtt": - try + case ActionType.DEVICE: + var deviceAction = _DeviceDatabaseService.GetById(action.DeviceId); + var providerActionTest = _ProviderDatabaseService.GetById(userId, action.ProviderId); + + DeviceNameForAction = deviceAction.Name; + actionDeviceToTest = deviceAction; + System.Console.WriteLine($"We get a device action ! Name={deviceAction.Name} Type={deviceAction.Type}"); + System.Console.WriteLine($"Check action provider type ! Type={providerActionTest.Type} Name={providerActionTest.Name}"); + break; + case ActionType.GROUP: + var groupAction = _GroupDatabaseService.GetById(action.GroupId); + DeviceNameForAction = groupAction.Name; + + System.Console.WriteLine($"We get a group action ! Name={groupAction.Name} Type={groupAction.Type}"); + System.Console.WriteLine($"Check action zigbeeGroupAction type ! Type={groupAction.Type}"); + + // Check state of first device of a group + actionDeviceToTest = _DeviceDatabaseService.GetByGroup(groupAction.Id).FirstOrDefault(); // TODO : Send error if no device found ! + break; + case ActionType.MQTT: + // take raw request and send it ! + RawRequestMQTT rawRequestMQTT = JsonConvert.DeserializeObject(action.RawRequest); + + if (rawRequestMQTT != null) { - ActionOnZigbee2Mqtt(actionDeviceToTest, action, DeviceNameForAction); - } catch (Exception ex) { - System.Console.WriteLine($"ActionOnZigbee2Mqtt result in error: {ex}"); + // SEND REQUEST + System.Console.WriteLine($"Send raw request mqtt! topic:{rawRequestMQTT.topic} - message: {rawRequestMQTT.message}"); + MqttClientService.PublishMessage(rawRequestMQTT.topic, rawRequestMQTT.message); } break; - case "meross": - try - { - ActionOnMeross(_DeviceDatabaseService, actionDeviceToTest, action, DeviceNameForAction); - } - catch (Exception ex) - { - System.Console.WriteLine($"ActionOnMeross result in error: {ex}"); - } - break; - case "yeelight": - try - { - ActionOnYeelight(_DeviceDatabaseService, actionDeviceToTest, action); - } - catch (Exception ex) - { - System.Console.WriteLine($"ActionOnYeelight result in error: {ex}"); - } + case ActionType.HTTP: // Correct way ? + // TODO break; } - } - else - { - System.Console.WriteLine($"Device or group found in action incorrect"); + + var providerAction = _ProviderDatabaseService.GetById(userId, action.ProviderId); + + // Check if device exist + if (actionDeviceToTest != null && providerAction != null) + { + switch (providerAction.Type) + { + case "zigbee2mqtt": + try + { + ActionOnZigbee2Mqtt(actionDeviceToTest, action, DeviceNameForAction); + } + catch (Exception ex) + { + System.Console.WriteLine($"ActionOnZigbee2Mqtt result in error: {ex}"); + } + break; + case "meross": + try + { + ActionOnMeross(_DeviceDatabaseService, actionDeviceToTest, action, DeviceNameForAction); + } + catch (Exception ex) + { + System.Console.WriteLine($"ActionOnMeross result in error: {ex}"); + } + break; + case "yeelight": + try + { + ActionOnYeelight(_DeviceDatabaseService, actionDeviceToTest, action); + } + catch (Exception ex) + { + System.Console.WriteLine($"ActionOnYeelight result in error: {ex}"); + } + break; + } + } + else + { + System.Console.WriteLine($"Device or group found in action incorrect"); + } } } - } - else - { - System.Console.WriteLine($"One or more condition aren't respected"); + else + { + System.Console.WriteLine($"One or more condition aren't respected"); + } } } } - } + } catch (Exception ex) { System.Console.WriteLine($"Exeption in one of automation logic - {ex}"); } } // Update last state of devices deviceTrigger.LastStateDate = DateTime.Now; diff --git a/MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs b/MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs index 0d18aa5..ec3b93c 100644 --- a/MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs +++ b/MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs @@ -33,6 +33,11 @@ namespace MyCore.Services.MyControlPanel return _Automations.Find(a => a.Triggers.Any(t => t.ProviderId == id)).ToList(); } + public List GetActiveByProvider(string id) + { + return _Automations.Find(a => a.Triggers.Any(t => t.ProviderId == id) && a.Active).ToList(); + } + public bool IsExist(string id) { return _Automations.Find(d => d.Id == id).FirstOrDefault() != null ? true : false;