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 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<DeviceDetailDTO> Devices { get; set; }
|
||||
}
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -40,7 +40,7 @@ namespace MyCore.Service.Controllers
|
||||
/// Get all automations for the specified user
|
||||
/// </summary>
|
||||
/// <param name="userId">Id of user</param>
|
||||
[ProducesResponseType(typeof(List<RoomSummaryDTO>), 200)]
|
||||
[ProducesResponseType(typeof(List<AutomationDTO>), 200)]
|
||||
[ProducesResponseType(typeof(string), 500)]
|
||||
[HttpGet("{userId}")]
|
||||
public ObjectResult GetAll(string userId)
|
||||
|
||||
@ -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<Expose> 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<Dictionary<string, string>>(message);
|
||||
triggerStateValueCheck = dic[automationTrigger.StateName]; // if action slide => get slide
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
validTrigger = false;
|
||||
}
|
||||
List<Expose> 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<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))
|
||||
{
|
||||
|
||||
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<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)
|
||||
// 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<RawRequestMQTT>(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;
|
||||
|
||||
@ -33,6 +33,11 @@ namespace MyCore.Services.MyControlPanel
|
||||
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)
|
||||
{
|
||||
return _Automations.Find<Automation>(d => d.Id == id).FirstOrDefault() != null ? true : false;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user