mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
MC Add AutomationService + logic (wip) + in auth
This commit is contained in:
parent
83483498a8
commit
b8521bcbc9
@ -4,7 +4,7 @@ using System.Text;
|
||||
|
||||
namespace MyCore.Framework.Business
|
||||
{
|
||||
class ActionLogic
|
||||
public class ActionLogic
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,6 +46,11 @@ namespace MyCore.Interfaces.Models
|
||||
|
||||
public class Trigger
|
||||
{
|
||||
public string ProviderId { get; set; }
|
||||
public string DeviceId { get; set; }
|
||||
public object StateName { get; set; }
|
||||
public object StateValue { get; set; }
|
||||
|
||||
public enum Type
|
||||
{
|
||||
MQTT,
|
||||
@ -56,6 +61,10 @@ namespace MyCore.Interfaces.Models
|
||||
|
||||
public class Condition
|
||||
{
|
||||
public string DeviceId { get; set; }
|
||||
public object StateName { get; set; }
|
||||
public object StateValue { get; set; }
|
||||
|
||||
public enum Type
|
||||
{
|
||||
STATE,
|
||||
@ -65,6 +74,12 @@ namespace MyCore.Interfaces.Models
|
||||
|
||||
public class Action
|
||||
{
|
||||
public string DeviceId { get; set; }
|
||||
public object StateName { get; set; } // example : state
|
||||
public object StateValue { get; set; } // example : ON
|
||||
public string ProviderId { get; set; } // TODO
|
||||
public string DeviceType { get; set; } // TODO
|
||||
|
||||
public enum Type
|
||||
{
|
||||
DELAY,
|
||||
|
||||
@ -33,10 +33,11 @@ namespace MyCore.Service.Controllers
|
||||
private readonly LocationDatabaseService _LocationDatabaseService;
|
||||
private readonly UserDatabaseService _UserDatabaseService;
|
||||
private readonly ActionService _ActionService;
|
||||
private readonly AutomationDatabaseService _AutomationDatabaseService;
|
||||
private readonly IMqttClientService _mqttClientService;
|
||||
private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
||||
|
||||
public AuthenticationController(ILogger<AuthenticationController> logger, TokensService tokensService, DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, ActionService ActionService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
||||
public AuthenticationController(ILogger<AuthenticationController> logger, TokensService tokensService, DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, ActionService ActionService, AutomationDatabaseService AutomationDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
||||
{
|
||||
_logger = logger;
|
||||
_tokensService = tokensService;
|
||||
@ -45,6 +46,7 @@ namespace MyCore.Service.Controllers
|
||||
_LocationDatabaseService = LocationDatabaseService;
|
||||
_UserDatabaseService = UserDatabaseService;
|
||||
_ActionService = ActionService;
|
||||
_AutomationDatabaseService = AutomationDatabaseService;
|
||||
_mqttClientService = provider.MqttClientService;
|
||||
_mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
||||
}
|
||||
@ -54,7 +56,7 @@ namespace MyCore.Service.Controllers
|
||||
try
|
||||
{
|
||||
var token = _tokensService.Authenticate(email.ToLower(), password);
|
||||
MqttClientService.SetServices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, _ActionService, "5fef55bae30e1016d4776bfe"); // TODO Get userId when connected !!
|
||||
MqttClientService.SetServices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, _ActionService, _AutomationDatabaseService, "5fef55bae30e1016d4776bfe"); // TODO Get userId when connected !!
|
||||
return Ok(token);
|
||||
}
|
||||
/*catch (UnauthorizedAccessException ex)
|
||||
|
||||
@ -24,6 +24,7 @@ namespace Mqtt.Client.AspNetCore.Services
|
||||
static DeviceDatabaseService _deviceDatabaseService;
|
||||
static ProviderDatabaseService _providerDatabaseService;
|
||||
static LocationDatabaseService _locationDatabaseService;
|
||||
static AutomationDatabaseService _automationDatabaseService;
|
||||
static ActionService _actionService;
|
||||
|
||||
public MqttClientService(IMqttClientOptions options)
|
||||
@ -64,7 +65,7 @@ namespace Mqtt.Client.AspNetCore.Services
|
||||
var topic = e.ApplicationMessage.Topic;
|
||||
|
||||
if (_actionService != null) {
|
||||
ActionService.HandleActionFromMQTTAsync(topic, payload, _deviceDatabaseService, _providerDatabaseService, _locationDatabaseService, userId);
|
||||
ActionService.HandleActionFromMQTTAsync(topic, payload, _deviceDatabaseService, _providerDatabaseService, _locationDatabaseService, _automationDatabaseService, userId);
|
||||
}
|
||||
|
||||
//if () { }
|
||||
@ -151,11 +152,12 @@ namespace Mqtt.Client.AspNetCore.Services
|
||||
return devices;
|
||||
}
|
||||
|
||||
public static void SetServices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, ActionService _ActionService, string UserId)
|
||||
public static void SetServices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, ActionService _ActionService, AutomationDatabaseService _AutomationDatabaseService, string UserId)
|
||||
{
|
||||
_deviceDatabaseService = _DeviceDatabaseService;
|
||||
_providerDatabaseService = _ProviderDatabaseService;
|
||||
_locationDatabaseService = _LocationDatabaseService;
|
||||
_automationDatabaseService = _AutomationDatabaseService;
|
||||
_actionService = _ActionService;
|
||||
userId = UserId;
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ namespace MyCore.Services.Devices
|
||||
public static bool isOpen = false;
|
||||
public static long lastActionTime;
|
||||
// TODO it's here that action are thrown.. Call from Mqtt Or other service like controller if from RpiServices
|
||||
public async static Task HandleActionFromMQTTAsync(string topic, string message, DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId)
|
||||
public async static Task HandleActionFromMQTTAsync(string topic, string message, DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, AutomationDatabaseService _AutomationDatabaseService, string userId)
|
||||
{
|
||||
// TODO Check if last action is not too close for each action
|
||||
var actionTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
||||
@ -25,6 +25,11 @@ namespace MyCore.Services.Devices
|
||||
string[] topicSplit = topic.Split('/');
|
||||
switch (topicSplit[0]) {
|
||||
case "zigbee2mqtt":
|
||||
var test0 = _ProviderDatabaseService.GetByType(topicSplit[0]).Id;
|
||||
var anotherTest = _AutomationDatabaseService.GetByProvider(test0);
|
||||
|
||||
|
||||
|
||||
// switch case according to device type (topic !)
|
||||
if (topicSplit[1].Contains("MagicCube0"))
|
||||
{
|
||||
|
||||
@ -28,6 +28,11 @@ namespace MyCore.Services.MyControlPanel
|
||||
return _Automations.Find<Automation>(a => a.Id == id).FirstOrDefault();
|
||||
}
|
||||
|
||||
public Automation GetByProvider(string id)
|
||||
{
|
||||
return _Automations.Find<Automation>(a => a.Triggers.Any(t => t.ProviderId == id)).FirstOrDefault();
|
||||
}
|
||||
|
||||
public Automation Create(Automation automation)
|
||||
{
|
||||
_Automations.InsertOne(automation);
|
||||
|
||||
@ -200,6 +200,7 @@ namespace MyCore
|
||||
services.AddScoped<ProviderDatabaseService>();
|
||||
services.AddScoped<DeviceDatabaseService>();
|
||||
services.AddScoped<LocationDatabaseService>();
|
||||
services.AddScoped<AutomationDatabaseService>();
|
||||
services.AddScoped<ActionService>();
|
||||
services.AddScoped<RoomDatabaseService>();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user