mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
MC fix return value automation get all + test if active automation in action service + add try catch for one automation
This commit is contained in:
parent
2f10433f94
commit
58fa2a2b4b
@ -8,11 +8,8 @@ namespace MyCore.Interfaces.DTO
|
|||||||
public class GroupSummaryDTO
|
public class GroupSummaryDTO
|
||||||
{
|
{
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
public string UserId { get; set; }
|
public string UserId { get; set; }
|
||||||
|
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
public string Type { get; set; }
|
public string Type { get; set; }
|
||||||
public bool IsAlarm { get; set; }
|
public bool IsAlarm { get; set; }
|
||||||
}
|
}
|
||||||
@ -20,9 +17,7 @@ namespace MyCore.Interfaces.DTO
|
|||||||
public class GroupDetailDTO : GroupSummaryDTO
|
public class GroupDetailDTO : GroupSummaryDTO
|
||||||
{
|
{
|
||||||
public DateTime CreatedDate { get; set; }
|
public DateTime CreatedDate { get; set; }
|
||||||
|
|
||||||
public DateTime UpdatedDate { get; set; }
|
public DateTime UpdatedDate { get; set; }
|
||||||
|
|
||||||
public List<DeviceDetailDTO> Devices { get; set; }
|
public List<DeviceDetailDTO> Devices { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -51,6 +51,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
|
Active = Active,
|
||||||
UserId = UserId,
|
UserId = UserId,
|
||||||
CreatedDate = CreatedDate,
|
CreatedDate = CreatedDate,
|
||||||
UpdatedDate = UpdatedDate,
|
UpdatedDate = UpdatedDate,
|
||||||
@ -66,6 +67,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
Name = Name,
|
Name = Name,
|
||||||
|
Active = Active,
|
||||||
UserId = UserId,
|
UserId = UserId,
|
||||||
CreatedDate = CreatedDate,
|
CreatedDate = CreatedDate,
|
||||||
UpdatedDate = UpdatedDate,
|
UpdatedDate = UpdatedDate,
|
||||||
|
|||||||
@ -40,7 +40,7 @@ namespace MyCore.Service.Controllers
|
|||||||
/// Get all automations for the specified user
|
/// Get all automations for the specified user
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">Id of user</param>
|
/// <param name="userId">Id of user</param>
|
||||||
[ProducesResponseType(typeof(List<RoomSummaryDTO>), 200)]
|
[ProducesResponseType(typeof(List<AutomationDTO>), 200)]
|
||||||
[ProducesResponseType(typeof(string), 500)]
|
[ProducesResponseType(typeof(string), 500)]
|
||||||
[HttpGet("{userId}")]
|
[HttpGet("{userId}")]
|
||||||
public ObjectResult GetAll(string userId)
|
public ObjectResult GetAll(string userId)
|
||||||
|
|||||||
@ -44,7 +44,7 @@ namespace MyCore.Services.Devices
|
|||||||
|
|
||||||
if (currentProvider != null)
|
if (currentProvider != null)
|
||||||
{
|
{
|
||||||
var automations = _AutomationDatabaseService.GetByProvider(currentProvider.Id);
|
var automations = _AutomationDatabaseService.GetActiveByProvider(currentProvider.Id);
|
||||||
|
|
||||||
deviceTrigger = _DeviceDatabaseService.GetByName(deviceServiceName).FirstOrDefault();
|
deviceTrigger = _DeviceDatabaseService.GetByName(deviceServiceName).FirstOrDefault();
|
||||||
|
|
||||||
@ -57,137 +57,142 @@ namespace MyCore.Services.Devices
|
|||||||
{
|
{
|
||||||
foreach (var automation in automations)
|
foreach (var automation in automations)
|
||||||
{
|
{
|
||||||
// todo check if not null and if more than one element
|
try {
|
||||||
if (deviceTrigger != null && automation.Triggers.Any(t => t.DeviceId == deviceTrigger.Id))
|
// 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)
|
|
||||||
{
|
{
|
||||||
List<Expose> exposes = GetDeviceExposes(deviceTrigger);
|
var automationTriggers = automation.Triggers.Where(t => t.DeviceId == deviceTrigger.Id).ToList();
|
||||||
|
|
||||||
var triggerStateValueCheck = (object)null;
|
// Test for each automation trigger found
|
||||||
|
foreach (var automationTrigger in automationTriggers)
|
||||||
// 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
|
|
||||||
{
|
{
|
||||||
// Try to get specific field from message
|
List<Expose> exposes = GetDeviceExposes(deviceTrigger);
|
||||||
var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(message);
|
|
||||||
triggerStateValueCheck = dic[automationTrigger.StateName]; // if action slide => get slide
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
validTrigger = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
var triggerStateValueCheck = (object)null;
|
||||||
|
|
||||||
if (validTrigger && automationTrigger.StateValue.ToLower() == triggerStateValueCheck.ToString().ToLower())
|
// Try to get automationTrigger.StateName of zigbee device
|
||||||
{
|
var expose = exposes.Where(e => e.name == automationTrigger.StateName).FirstOrDefault();
|
||||||
// Correct trigger !
|
bool validTrigger = expose != null; // No expose found.. => the device has not the correct state => not correct automation
|
||||||
System.Console.WriteLine($"Correct trigger for automation {automation.Name}");
|
|
||||||
|
|
||||||
var isConditionsRespected = CheckConditions(automation.Conditions);
|
try
|
||||||
|
|
||||||
if (!isConditionsRespected.Any(cr => !cr))
|
|
||||||
{
|
{
|
||||||
System.Console.WriteLine("Conditions respected !");
|
// Try to get specific field from message
|
||||||
foreach (var action in automation.Actions)
|
var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(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))
|
||||||
{
|
{
|
||||||
|
System.Console.WriteLine("Conditions respected !");
|
||||||
var DeviceNameForAction = "";
|
foreach (var action in automation.Actions)
|
||||||
Device actionDeviceToTest = new Device();
|
|
||||||
|
|
||||||
// Retrieve action type
|
|
||||||
switch (action.Type)
|
|
||||||
{
|
{
|
||||||
case ActionType.DEVICE:
|
|
||||||
var deviceAction = _DeviceDatabaseService.GetById(action.DeviceId);
|
|
||||||
var providerActionTest = _ProviderDatabaseService.GetById(userId, action.ProviderId);
|
|
||||||
|
|
||||||
DeviceNameForAction = deviceAction.Name;
|
var DeviceNameForAction = "";
|
||||||
actionDeviceToTest = deviceAction;
|
Device actionDeviceToTest = new Device();
|
||||||
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}");
|
// Retrieve action type
|
||||||
System.Console.WriteLine($"Check action zigbeeGroupAction type ! Type={groupAction.Type}");
|
switch (action.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<RawRequestMQTT>(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)
|
|
||||||
{
|
{
|
||||||
case "zigbee2mqtt":
|
case ActionType.DEVICE:
|
||||||
try
|
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<RawRequestMQTT>(action.RawRequest);
|
||||||
|
|
||||||
|
if (rawRequestMQTT != null)
|
||||||
{
|
{
|
||||||
ActionOnZigbee2Mqtt(actionDeviceToTest, action, DeviceNameForAction);
|
// SEND REQUEST
|
||||||
} catch (Exception ex) {
|
System.Console.WriteLine($"Send raw request mqtt! topic:{rawRequestMQTT.topic} - message: {rawRequestMQTT.message}");
|
||||||
System.Console.WriteLine($"ActionOnZigbee2Mqtt result in error: {ex}");
|
MqttClientService.PublishMessage(rawRequestMQTT.topic, rawRequestMQTT.message);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "meross":
|
case ActionType.HTTP: // Correct way ?
|
||||||
try
|
// TODO
|
||||||
{
|
|
||||||
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
var providerAction = _ProviderDatabaseService.GetById(userId, action.ProviderId);
|
||||||
{
|
|
||||||
System.Console.WriteLine($"Device or group found in action incorrect");
|
// 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
|
||||||
else
|
{
|
||||||
{
|
System.Console.WriteLine($"One or more condition aren't respected");
|
||||||
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
|
// Update last state of devices
|
||||||
deviceTrigger.LastStateDate = DateTime.Now;
|
deviceTrigger.LastStateDate = DateTime.Now;
|
||||||
|
|||||||
@ -33,6 +33,11 @@ namespace MyCore.Services.MyControlPanel
|
|||||||
return _Automations.Find<Automation>(a => a.Triggers.Any(t => t.ProviderId == id)).ToList();
|
return _Automations.Find<Automation>(a => a.Triggers.Any(t => t.ProviderId == id)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Automation> GetActiveByProvider(string id)
|
||||||
|
{
|
||||||
|
return _Automations.Find<Automation>(a => a.Triggers.Any(t => t.ProviderId == id) && a.Active).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
public bool IsExist(string id)
|
public bool IsExist(string id)
|
||||||
{
|
{
|
||||||
return _Automations.Find<Automation>(d => d.Id == id).FirstOrDefault() != null ? true : false;
|
return _Automations.Find<Automation>(d => d.Id == id).FirstOrDefault() != null ? true : false;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user