mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
100 lines
4.7 KiB
C#
100 lines
4.7 KiB
C#
using Mqtt.Client.AspNetCore.Services;
|
|
using MyCore.Interfaces.DTO;
|
|
using MyCore.Interfaces.Models;
|
|
using MyCore.Interfaces.Models.Providers.Zigbee.Zigbee2Mqtt;
|
|
using MyCore.Service;
|
|
using MyCore.Service.Controllers.Helpers;
|
|
using MyCore.Service.Services;
|
|
using MyCore.Services.MyControlPanel;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using static Mqtt.Client.AspNetCore.Services.MqttClientMerossService;
|
|
|
|
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, string homeId)
|
|
{
|
|
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((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, homeId);
|
|
#endregion
|
|
|
|
#region Alarm
|
|
AlarmMode alarmMode = _AlarmDatabaseService.GetCurrentActivatedAlarm(homeId);
|
|
if (alarmMode != null)
|
|
if (alarmMode.DevicesIds.Contains(deviceTrigger.Id))
|
|
AlarmService.HandleMessage(alarmMode, deviceTrigger, message, _DeviceDatabaseService, _ProviderDatabaseService, _GroupDatabaseService, _EventDatabaseService, homeId);
|
|
#endregion
|
|
|
|
// Update last state of devices
|
|
deviceTrigger.LastStateDate = DateTime.Now;
|
|
deviceTrigger.LastState = message;
|
|
_DeviceDatabaseService.Update(deviceTrigger);
|
|
|
|
EventDetailDTO eventDetailDTO = new EventDetailDTO() { Type = EventType.DeviceState, HomeId = homeId, RoomId = deviceTrigger.RoomId, DeviceState = new DeviceState() { DeviceId = deviceTrigger.Id, DeviceName = deviceTrigger.Name, DeviceType = deviceTrigger.Type, Message = message } };
|
|
EventService.CreateOrUpdate(_EventDatabaseService, homeId, eventDetailDTO, true);
|
|
}
|
|
else
|
|
{
|
|
// do nothing is a set
|
|
}
|
|
}
|
|
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}");
|
|
}
|
|
}
|
|
}
|
|
}
|