mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
54 lines
2.1 KiB
C#
54 lines
2.1 KiB
C#
using MyCore.Interfaces.DTO;
|
|
using MyCore.Interfaces.Models;
|
|
using MyCore.Services.MyControlPanel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MyCore.Service.Services
|
|
{
|
|
public class AutomationService
|
|
{
|
|
public static AutomationDTO CreateOrUpdate(AutomationDatabaseService _AutomationDatabaseService, string userId, AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetailDTO, bool create)
|
|
{
|
|
Automation automation;
|
|
if (create)
|
|
automation = new Automation();
|
|
else
|
|
{
|
|
automation = _AutomationDatabaseService.GetById(automationCreateOrUpdateDetailDTO.Id);
|
|
}
|
|
|
|
automation.UserId = userId;
|
|
automation.Name = automationCreateOrUpdateDetailDTO.Name;
|
|
automation.Active = automationCreateOrUpdateDetailDTO.Active;
|
|
automation.CreatedDate = create ? DateTime.Now : automation.CreatedDate;
|
|
automation.UpdatedDate = DateTime.Now;
|
|
automation.Triggers = automationCreateOrUpdateDetailDTO.Triggers;
|
|
automation.Conditions = automationCreateOrUpdateDetailDTO.Conditions;
|
|
automation.Actions = automationCreateOrUpdateDetailDTO.Actions;
|
|
var allDeviceIds = new List<string>();
|
|
allDeviceIds.AddRange(automation.Triggers.Select(t => t.DeviceId)); // Only take the deviceIds from triggers
|
|
/*allDeviceIds.AddRange(automation.Conditions.Select(t => t.DeviceId));
|
|
|
|
foreach(var action in automation.Actions) {
|
|
if (action.DeviceId != null) {
|
|
allDeviceIds.Add(action.DeviceId);
|
|
}
|
|
if (action.GroupId != null)
|
|
{
|
|
|
|
}
|
|
}*/
|
|
|
|
automation.DevicesIds = allDeviceIds;
|
|
|
|
if (create)
|
|
return _AutomationDatabaseService.Create(automation).ToDTO();
|
|
else
|
|
return _AutomationDatabaseService.Update(automation.Id, automation).ToDTO();
|
|
}
|
|
}
|
|
}
|