mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
Add send action http in device controller
This commit is contained in:
parent
372cbd3759
commit
cf85feb33f
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send action to device
|
||||
/// </summary>
|
||||
/// <param name="action">Action to sent</param>
|
||||
[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 };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create devices from provider
|
||||
/// </summary>
|
||||
|
||||
@ -247,7 +247,6 @@ namespace MyCore.Service.Controllers.Helpers
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
switch (device.Type)
|
||||
{
|
||||
case DeviceType.Light:
|
||||
|
||||
@ -172,7 +172,7 @@ namespace Mqtt.Client.AspNetCore.Services
|
||||
lastTopic = topic;
|
||||
}
|
||||
|
||||
return null;
|
||||
return new Task(null);
|
||||
}
|
||||
|
||||
public async Task HandleConnectedAsync(MqttClientConnectedEventArgs eventArgs)
|
||||
|
||||
@ -88,7 +88,6 @@ namespace MyCore.Service.Services
|
||||
validTrigger = false;
|
||||
}
|
||||
|
||||
|
||||
if (validTrigger && automationTrigger.StateValue.ToLower() == triggerStateValueCheck.ToString().ToLower())
|
||||
{
|
||||
// Correct trigger !
|
||||
@ -106,7 +105,23 @@ namespace MyCore.Service.Services
|
||||
|
||||
foreach (var action in automation.Actions)
|
||||
{
|
||||
HandleAction(homeId, action, _ProviderDatabaseService, _DeviceDatabaseService, _GroupDatabaseService) ;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Console.WriteLine($"One or more condition aren't respected");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { System.Console.WriteLine($"Exeption in one of automation logic - {ex}"); }
|
||||
}
|
||||
}
|
||||
|
||||
public static void HandleAction(string homeId, Interfaces.Models.Action action, ProviderDatabaseService providerDatabaseService, DeviceDatabaseService deviceDatabaseService, GroupDatabaseService groupDatabaseService)
|
||||
{
|
||||
var DeviceNameForAction = "";
|
||||
Device actionDeviceToTest = new Device();
|
||||
|
||||
@ -114,8 +129,8 @@ namespace MyCore.Service.Services
|
||||
switch (action.Type)
|
||||
{
|
||||
case ActionType.DEVICE:
|
||||
var deviceAction = _DeviceDatabaseService.GetById(action.DeviceId);
|
||||
var providerActionTest = _ProviderDatabaseService.GetById(homeId, action.ProviderId);
|
||||
var deviceAction = deviceDatabaseService.GetById(action.DeviceId);
|
||||
var providerActionTest = providerDatabaseService.GetById(homeId, action.ProviderId);
|
||||
|
||||
DeviceNameForAction = deviceAction.Name;
|
||||
actionDeviceToTest = deviceAction;
|
||||
@ -123,14 +138,14 @@ namespace MyCore.Service.Services
|
||||
System.Console.WriteLine($"Check action provider type ! Type={providerActionTest.Type} Name={providerActionTest.Name}");
|
||||
break;
|
||||
case ActionType.GROUP:
|
||||
var groupAction = _GroupDatabaseService.GetById(action.GroupId);
|
||||
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 !
|
||||
actionDeviceToTest = deviceDatabaseService.GetByGroup(groupAction.Id).FirstOrDefault(); // TODO : Send error if no device found !
|
||||
break;
|
||||
case ActionType.MQTT:
|
||||
// take raw request and send it !
|
||||
@ -148,7 +163,7 @@ namespace MyCore.Service.Services
|
||||
break;
|
||||
}
|
||||
|
||||
var providerAction = _ProviderDatabaseService.GetById(homeId, action.ProviderId);
|
||||
var providerAction = providerDatabaseService.GetById(homeId, action.ProviderId);
|
||||
|
||||
// Check if device exist
|
||||
if (actionDeviceToTest != null && providerAction != null)
|
||||
@ -168,7 +183,7 @@ namespace MyCore.Service.Services
|
||||
case ProviderType.meross:
|
||||
try
|
||||
{
|
||||
DevicesHelper.ActionOnMeross(_DeviceDatabaseService, actionDeviceToTest, action, DeviceNameForAction);
|
||||
DevicesHelper.ActionOnMeross(deviceDatabaseService, actionDeviceToTest, action, DeviceNameForAction);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -178,7 +193,7 @@ namespace MyCore.Service.Services
|
||||
case ProviderType.yeelight:
|
||||
try
|
||||
{
|
||||
DevicesHelper.ActionOnYeelight(_DeviceDatabaseService, actionDeviceToTest, action);
|
||||
DevicesHelper.ActionOnYeelight(deviceDatabaseService, actionDeviceToTest, action);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -192,18 +207,6 @@ namespace MyCore.Service.Services
|
||||
System.Console.WriteLine($"Device or group found in action incorrect");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Console.WriteLine($"One or more condition aren't respected");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { System.Console.WriteLine($"Exeption in one of automation logic - {ex}"); }
|
||||
}
|
||||
}
|
||||
|
||||
public static List<bool> CheckConditions(List<Condition> conditions)
|
||||
{
|
||||
|
||||
@ -161,6 +161,7 @@ namespace MyCore
|
||||
services.AddScoped<RoomDatabaseService>();
|
||||
services.AddScoped<AutomationDatabaseService>();
|
||||
services.AddScoped<ActionService>();
|
||||
services.AddScoped<AutomationService>();
|
||||
services.AddScoped<AlarmDatabaseService>();
|
||||
services.AddScoped<EventDatabaseService>();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user