mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
104 lines
5.0 KiB
C#
104 lines
5.0 KiB
C#
using MyCore.Interfaces.DTO;
|
|
using MyCore.Interfaces.Models;
|
|
using MyCore.Service;
|
|
using MyCore.Service.Services;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MyCore.Services.Devices
|
|
{
|
|
public class ActionService
|
|
{
|
|
private static Provider currentProvider;
|
|
private static Device deviceTrigger;
|
|
private static string providerFromTopic;
|
|
private static string deviceServiceName;
|
|
public static async Task HandleActionFromMQTTAsync(string topic, string message, DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService, ProviderDatabaseService _ProviderDatabaseService, RoomDatabaseService _RoomDatabaseService, AutomationDatabaseService _AutomationDatabaseService, AlarmDatabaseService _AlarmDatabaseService, EventDatabaseService _EventDatabaseService, Home home)
|
|
{
|
|
System.Console.WriteLine($"Received message {message}");
|
|
|
|
string[] topicSplit = topic.Split('/');
|
|
try {
|
|
providerFromTopic = topicSplit[0];
|
|
deviceServiceName = topicSplit[1];
|
|
} catch (Exception ex) {
|
|
System.Console.WriteLine($"Exeption throw when fetching provider and device from topic {topic} - Error: {ex}");
|
|
}
|
|
|
|
switch (providerFromTopic) {
|
|
case "zigbee2mqtt":
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
currentProvider = Enum.IsDefined(typeof(ProviderType), providerFromTopic) ? _ProviderDatabaseService.GetByType(home.Id, (ProviderType) Enum.Parse(typeof(ProviderType), providerFromTopic.ToLower())) : null;
|
|
|
|
if (currentProvider != null)
|
|
{
|
|
deviceTrigger = _DeviceDatabaseService.GetByName(deviceServiceName).FirstOrDefault();
|
|
|
|
if (deviceTrigger != null)
|
|
{
|
|
try
|
|
{
|
|
// if is not a set => check automation
|
|
if (topicSplit.Length <= 2)
|
|
{
|
|
#region Automations
|
|
var automations = _AutomationDatabaseService.GetActiveByProvider(currentProvider.Id);
|
|
AutomationService.HandleAutomation(automations, deviceTrigger, message, _DeviceDatabaseService, _ProviderDatabaseService, _GroupDatabaseService, _EventDatabaseService, home);
|
|
#endregion
|
|
|
|
#region Alarm
|
|
AlarmMode alarmMode = _AlarmDatabaseService.GetCurrentActivatedAlarm(home.Id);
|
|
if (alarmMode != null)
|
|
if (alarmMode.DevicesIds.Contains(deviceTrigger.Id))
|
|
AlarmService.HandleMessageAsync(alarmMode, deviceTrigger, message, _DeviceDatabaseService, _ProviderDatabaseService, _GroupDatabaseService, _EventDatabaseService, home);
|
|
#endregion
|
|
|
|
// We'll try to get last info and update our objects
|
|
if (topicSplit.Length == 2)
|
|
{
|
|
// try the parsing !
|
|
var zigbeeParsedMessage = JsonConvert.DeserializeObject<Zigbee2MqttRequest>(message);
|
|
deviceTrigger = DeviceService.UpdateDeviceFromZigbeeEvent(deviceTrigger, zigbeeParsedMessage);
|
|
}
|
|
|
|
// Update last state of devices
|
|
deviceTrigger.LastMessageDate = DateTime.Now;
|
|
deviceTrigger.LastMessage = message;
|
|
deviceTrigger.LastStateDate = DateTime.Now;
|
|
deviceTrigger.LastState = message;
|
|
_DeviceDatabaseService.Update(deviceTrigger);
|
|
|
|
EventDetailDTO eventDetailDTO = new EventDetailDTO() { Type = EventType.DeviceState, HomeId = home.Id, RoomId = deviceTrigger.RoomId, DeviceState = new DeviceState() { DeviceId = deviceTrigger.Id, DeviceName = deviceTrigger.Name, DeviceType = deviceTrigger.Type, Message = message } };
|
|
EventService.CreateOrUpdate(_EventDatabaseService, home.Id, eventDetailDTO, true);
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Console.WriteLine($"Exeption from automation logic - {ex}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
System.Console.WriteLine($"Current device Trigger not found - {deviceServiceName}");
|
|
}
|
|
}
|
|
else {
|
|
System.Console.WriteLine($"Current provider not found - {providerFromTopic}");
|
|
}
|
|
}
|
|
}
|
|
}
|