From cf85feb33f2e25d6b3723e5cc3aaa595669cb439 Mon Sep 17 00:00:00 2001 From: Fransolet Thomas Date: Wed, 6 Apr 2022 18:59:27 +0200 Subject: [PATCH] Add send action http in device controller --- .../Controllers/Devices/DeviceController.cs | 63 ++++++- MyCore/Controllers/Helpers/DevicesHelper.cs | 1 - MyCore/Extensions/MqttClientService.cs | 2 +- MyCore/Services/AutomationService.cs | 175 +++++++++--------- MyCore/Startup.cs | 1 + 5 files changed, 153 insertions(+), 89 deletions(-) diff --git a/MyCore/Controllers/Devices/DeviceController.cs b/MyCore/Controllers/Devices/DeviceController.cs index 4ad142a..df92054 100644 --- a/MyCore/Controllers/Devices/DeviceController.cs +++ b/MyCore/Controllers/Devices/DeviceController.cs @@ -11,6 +11,8 @@ using MongoDB.Bson; using Mqtt.Client.AspNetCore.Services; using MyCore.Interfaces.DTO; using MyCore.Interfaces.Models; +using MyCore.Service.Controllers.Helpers; +using MyCore.Service.Services; using MyCore.Services; using MyCore.Services.Devices; using MyCore.Services.MyControlPanel; @@ -24,17 +26,21 @@ namespace MyCore.Controllers { private DeviceDatabaseService _DeviceDatabaseService; private ProviderDatabaseService _ProviderDatabaseService; + private GroupDatabaseService _GroupDatabaseService; private RoomDatabaseService _RoomDatabaseService; private HomeDatabaseService _HomeDatabaseService; + private AutomationService _automationService; private readonly IMqttClientService _mqttClientService; //private readonly IMqttOnlineClientService _mqttOnlineClientService; - public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, RoomDatabaseService RoomDatabaseService, HomeDatabaseService HomeDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider) + public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, GroupDatabaseService GroupDatabaseService, RoomDatabaseService RoomDatabaseService, HomeDatabaseService HomeDatabaseService, AutomationService AutomationService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider) { this._DeviceDatabaseService = DeviceDatabaseService; this._ProviderDatabaseService = ProviderDatabaseService; + this._GroupDatabaseService = GroupDatabaseService; this._RoomDatabaseService = RoomDatabaseService; this._HomeDatabaseService = HomeDatabaseService; + this._automationService = AutomationService; this._mqttClientService = provider.MqttClientService; //this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService; } @@ -175,6 +181,61 @@ namespace MyCore.Controllers } } + /// + /// Send action to device + /// + /// Action to sent + [ProducesResponseType(typeof(string), 202)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpPost("action")] + public ObjectResult SendAction([FromBody] Interfaces.Models.Action action) + { + try + { + if (action == null) + throw new ArgumentNullException("Incorrect parameters"); + + if (action.DeviceId == null || action.GroupId == null) + throw new ArgumentNullException("Incorrect parameters"); + + Device device = null; + Group group = null; + string homeId; + + if (action.DeviceId != null) { + device = _DeviceDatabaseService.GetById(action.DeviceId); + homeId= device?.HomeId; + } + else + { + group = _GroupDatabaseService.GetById(action.GroupId); + homeId= group?.HomeId; + } + + if (device == null || group == null) + throw new KeyNotFoundException("Device or group does not exist"); + + AutomationService.HandleAction(homeId, action, _ProviderDatabaseService, _DeviceDatabaseService, _GroupDatabaseService); + + return new ObjectResult("Action has been sent successfully") { StatusCode = 202 }; // To test with accepted result + + } + catch (ArgumentNullException ex) + { + return new BadRequestObjectResult(ex.Message) { }; + } + catch (KeyNotFoundException ex) + { + return new NotFoundObjectResult(ex.Message) { }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + /// /// Create devices from provider /// diff --git a/MyCore/Controllers/Helpers/DevicesHelper.cs b/MyCore/Controllers/Helpers/DevicesHelper.cs index 9f7d0a0..6cbd5c9 100644 --- a/MyCore/Controllers/Helpers/DevicesHelper.cs +++ b/MyCore/Controllers/Helpers/DevicesHelper.cs @@ -247,7 +247,6 @@ namespace MyCore.Service.Controllers.Helpers try { - switch (device.Type) { case DeviceType.Light: diff --git a/MyCore/Extensions/MqttClientService.cs b/MyCore/Extensions/MqttClientService.cs index 1c9d06c..01dba27 100644 --- a/MyCore/Extensions/MqttClientService.cs +++ b/MyCore/Extensions/MqttClientService.cs @@ -172,7 +172,7 @@ namespace Mqtt.Client.AspNetCore.Services lastTopic = topic; } - return null; + return new Task(null); } public async Task HandleConnectedAsync(MqttClientConnectedEventArgs eventArgs) diff --git a/MyCore/Services/AutomationService.cs b/MyCore/Services/AutomationService.cs index fa8c2f4..c826951 100644 --- a/MyCore/Services/AutomationService.cs +++ b/MyCore/Services/AutomationService.cs @@ -88,7 +88,6 @@ namespace MyCore.Service.Services validTrigger = false; } - if (validTrigger && automationTrigger.StateValue.ToLower() == triggerStateValueCheck.ToString().ToLower()) { // Correct trigger ! @@ -106,91 +105,7 @@ namespace MyCore.Service.Services foreach (var action in automation.Actions) { - - var DeviceNameForAction = ""; - Device actionDeviceToTest = new Device(); - - // Retrieve action type - switch (action.Type) - { - case ActionType.DEVICE: - var deviceAction = _DeviceDatabaseService.GetById(action.DeviceId); - var providerActionTest = _ProviderDatabaseService.GetById(homeId, 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) - { - // 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(homeId, action.ProviderId); - - // Check if device exist - if (actionDeviceToTest != null && providerAction != null) - { - switch (providerAction.Type) - { - case ProviderType.zigbee2mqtt: - try - { - DevicesHelper.ActionOnZigbee2Mqtt(actionDeviceToTest, action, DeviceNameForAction); - } - catch (Exception ex) - { - System.Console.WriteLine($"ActionOnZigbee2Mqtt result in error: {ex}"); - } - break; - case ProviderType.meross: - try - { - DevicesHelper.ActionOnMeross(_DeviceDatabaseService, actionDeviceToTest, action, DeviceNameForAction); - } - catch (Exception ex) - { - System.Console.WriteLine($"ActionOnMeross result in error: {ex}"); - } - break; - case ProviderType.yeelight: - try - { - DevicesHelper.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"); - } + HandleAction(homeId, action, _ProviderDatabaseService, _DeviceDatabaseService, _GroupDatabaseService) ; } } else @@ -205,6 +120,94 @@ namespace MyCore.Service.Services } } + public static void HandleAction(string homeId, Interfaces.Models.Action action, ProviderDatabaseService providerDatabaseService, DeviceDatabaseService deviceDatabaseService, GroupDatabaseService groupDatabaseService) + { + var DeviceNameForAction = ""; + Device actionDeviceToTest = new Device(); + + // Retrieve action type + switch (action.Type) + { + case ActionType.DEVICE: + var deviceAction = deviceDatabaseService.GetById(action.DeviceId); + var providerActionTest = providerDatabaseService.GetById(homeId, 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) + { + // 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(homeId, action.ProviderId); + + // Check if device exist + if (actionDeviceToTest != null && providerAction != null) + { + switch (providerAction.Type) + { + case ProviderType.zigbee2mqtt: + try + { + DevicesHelper.ActionOnZigbee2Mqtt(actionDeviceToTest, action, DeviceNameForAction); + } + catch (Exception ex) + { + System.Console.WriteLine($"ActionOnZigbee2Mqtt result in error: {ex}"); + } + break; + case ProviderType.meross: + try + { + DevicesHelper.ActionOnMeross(deviceDatabaseService, actionDeviceToTest, action, DeviceNameForAction); + } + catch (Exception ex) + { + System.Console.WriteLine($"ActionOnMeross result in error: {ex}"); + } + break; + case ProviderType.yeelight: + try + { + DevicesHelper.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"); + } + } + public static List CheckConditions(List conditions) { var isConditionsRespected = conditions.Count <= 0 ? new List() { true } : new List(new bool[conditions.Count]); diff --git a/MyCore/Startup.cs b/MyCore/Startup.cs index fd54926..c0c0335 100644 --- a/MyCore/Startup.cs +++ b/MyCore/Startup.cs @@ -161,6 +161,7 @@ namespace MyCore services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped();