mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
90 lines
4.9 KiB
C#
90 lines
4.9 KiB
C#
using Mqtt.Client.AspNetCore.Services;
|
|
using MyCore.Interfaces.Models;
|
|
using MyCore.Services.MyControlPanel;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using static Mqtt.Client.AspNetCore.Services.MqttClientMerossService;
|
|
|
|
namespace MyCore.Services.Devices
|
|
{
|
|
public class ActionService
|
|
{
|
|
public static bool isOpen = false;
|
|
public static long lastActionTime;
|
|
// TODO it's here that action are thrown.. Call from Mqtt Or other service like controller if from RpiServices
|
|
public async static Task HandleActionFromMQTTAsync(string topic, string message, DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId)
|
|
{
|
|
// TODO Check if last action is not too close for each action
|
|
var actionTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
|
|
|
var providers = _ProviderDatabaseService.GetAll(userId);
|
|
string[] topicSplit = topic.Split('/');
|
|
switch (topicSplit[0]) {
|
|
case "zigbee2mqtt":
|
|
// switch case according to device type (topic !)
|
|
if (topicSplit[1].Contains("MagicCube0"))
|
|
{
|
|
var test = JsonConvert.DeserializeObject<AqaraCube>(message);
|
|
if (test.Action == "shake")
|
|
{
|
|
if (YeelightService.devices.Count <= 0) {
|
|
await YeelightService.GetDevices();
|
|
}
|
|
var labLamp = YeelightService.devices.Where(d => d.Hostname == "192.168.31.74").FirstOrDefault();
|
|
if (labLamp != null) {
|
|
Task.Run(async () => { await YeelightService.Toggle(labLamp); });
|
|
}
|
|
}
|
|
if (test.Action == "tap")
|
|
{
|
|
var provider = providers.Where(p => p.Type == "meross").FirstOrDefault();
|
|
var merossDevices = _DeviceDatabaseService.GetByProviderId(provider.Id);
|
|
|
|
var multiprise = merossDevices.Where(md => md.Name == "Multiprise bureau").FirstOrDefault();
|
|
//var prise = merossDevices.Where(md => md.Name == "Imprimante 3D").FirstOrDefault();
|
|
|
|
if (multiprise != null)
|
|
{
|
|
//As multisocket channel 0 is all the sockets, skip 0
|
|
var tests = actionTime - lastActionTime;
|
|
if (tests >= 1000) {
|
|
isOpen = !isOpen;
|
|
if (isOpen)
|
|
{
|
|
/*foreach (var channel in multiprise.channels)
|
|
{
|
|
if (i != 0)
|
|
MerossService.ExecuteCommand(multiprise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.OFF, i);
|
|
i++;
|
|
}*/
|
|
MqttClientMerossService.ExecuteCommand(multiprise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.ON, 1);
|
|
//MqttClientMerossService.ExecuteCommand(prise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.ON, 0);
|
|
}
|
|
else
|
|
{
|
|
MqttClientMerossService.ExecuteCommand(multiprise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.OFF, 1);
|
|
//MqttClientMerossService.ExecuteCommand(prise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.OFF, 0);
|
|
}
|
|
}
|
|
|
|
lastActionTime = actionTime;
|
|
|
|
}
|
|
/*if (lightStateIkeaBulb == LightState.Undefined || lightStateIkeaBulb == LightState.Off)
|
|
PublishMessage("zigbee2mqtt/0x14b457fffe7628fa/set", "{\"state\": \"ON\"}");
|
|
else
|
|
PublishMessage("zigbee2mqtt/0x14b457fffe7628fa/set", "{\"state\": \"OFF\"}");*/
|
|
}
|
|
//await MqttClientOnlineService.PublishMessage("Notification", "Hey magic cube 0 !");
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|