mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
MC Clean code + use zigbee new structure + use States as a list + meross action for zigbee devices
This commit is contained in:
parent
764fcfe414
commit
dcf6ccef5a
@ -14,6 +14,7 @@ namespace MyCore.Interfaces.DTO
|
||||
Light,
|
||||
Sound,
|
||||
Plug,
|
||||
Multiplug,
|
||||
Thermostat,
|
||||
Valve,
|
||||
Door,
|
||||
|
||||
@ -105,13 +105,18 @@ namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public string GroupId { get; set; }
|
||||
public string DeviceId { get; set; }
|
||||
public string StateName { get; set; } // example : state
|
||||
public string StateValue { get; set; } // example : ON
|
||||
public List<State> States { get; set; }
|
||||
public string RawRequest { get; set; } // http, mqtt
|
||||
public string ProviderId { get; set; }
|
||||
public Type Type { get; set; }
|
||||
}
|
||||
|
||||
public class State
|
||||
{
|
||||
public string Name { get; set; } // example : state
|
||||
public string Value { get; set; } // example : ON
|
||||
}
|
||||
|
||||
public enum Type
|
||||
{
|
||||
DELAY,
|
||||
|
||||
@ -42,6 +42,10 @@ namespace MyCore.Interfaces.Models
|
||||
[BsonElement("ApiKey")]
|
||||
public string ApiKey { get; set; } // TODO ENCRYPTED
|
||||
|
||||
[BsonElement("Value")]
|
||||
[BsonRequired]
|
||||
public string Value { get; set; } // AS in kiwix => Access Token, Code, etc // TODO ENCRYPTED Hash with peper from appSettings etc
|
||||
|
||||
[BsonElement("Active")]
|
||||
[BsonRequired]
|
||||
public bool Active { get; set; }
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MyCore.Interfaces.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class AqaraCube : AqaraDevice
|
||||
{
|
||||
public double angle { get; set; }
|
||||
public int side { get; set; }
|
||||
public string action { get; set; }
|
||||
}
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class AqaraDevice
|
||||
{
|
||||
public int LinkQuality { get; set; }
|
||||
public int Battery { get; set; }
|
||||
public int Voltage { get; set; }
|
||||
}
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MyCore.Interfaces.Models.Providers.Zigbee.Aqara
|
||||
{
|
||||
public class AqaraMotion : AqaraDevice
|
||||
{
|
||||
public int illuminance { get; set; }
|
||||
public int illuminance_lux { get; set; }
|
||||
public bool occupancy { get; set; }
|
||||
}
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class AqaraSwitch : AqaraDevice
|
||||
{
|
||||
public string Click { get; set; } //single, double, triple, quadruple, long, long_release
|
||||
|
||||
public int Duration { get; set; }
|
||||
}
|
||||
|
||||
/*public enum ClickOptions {
|
||||
single,
|
||||
double,
|
||||
triple,
|
||||
quadruple,
|
||||
long,
|
||||
long_release
|
||||
}*/
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class LightBulb
|
||||
{
|
||||
public string state { get; set; }
|
||||
public int brightness { get; set; }
|
||||
}
|
||||
}
|
||||
@ -5,7 +5,8 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class Zigbee2MqttDevice
|
||||
// OLD
|
||||
/*public class Zigbee2MqttDevice
|
||||
{
|
||||
public string ieeeAddr { get; set; }
|
||||
public string type { get; set; }
|
||||
@ -22,9 +23,9 @@ namespace MyCore.Interfaces.Models
|
||||
public string softwareBuildID { get; set; }
|
||||
public string dateCode { get; set; }
|
||||
public long lastSeen { get; set; }
|
||||
}
|
||||
}*/
|
||||
|
||||
public class Zigbee2MqttDeviceNew
|
||||
public class Zigbee2MqttDevice
|
||||
{
|
||||
public string date_code { get; set; }
|
||||
public string friendly_name { get; set; }
|
||||
|
||||
@ -20,7 +20,7 @@ namespace Mqtt.Client.AspNetCore.Services
|
||||
private static IMqttClient mqttClient;
|
||||
private IMqttClientOptions options;
|
||||
public static List<Zigbee2MqttDevice> devices = new List<Zigbee2MqttDevice>();
|
||||
public static List<Zigbee2MqttDeviceNew> devicesNew = new List<Zigbee2MqttDeviceNew>();
|
||||
public static List<Zigbee2MqttDevice> devicesNew = new List<Zigbee2MqttDevice>();
|
||||
public static List<Zigbee2MqttGroup> groups = new List<Zigbee2MqttGroup>();
|
||||
public static string userId;
|
||||
static DeviceDatabaseService _deviceDatabaseService;
|
||||
@ -115,7 +115,7 @@ namespace Mqtt.Client.AspNetCore.Services
|
||||
case "zigbee2mqtt/bridge/devices":
|
||||
try
|
||||
{
|
||||
var devicesConvert = JsonConvert.DeserializeObject<List<Zigbee2MqttDeviceNew>>(payload);
|
||||
var devicesConvert = JsonConvert.DeserializeObject<List<Zigbee2MqttDevice>>(payload);
|
||||
devicesNew = devicesConvert;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -186,7 +186,7 @@ namespace Mqtt.Client.AspNetCore.Services
|
||||
{
|
||||
return devices;
|
||||
}
|
||||
public static List<Zigbee2MqttDeviceNew> GetDevicesNew()
|
||||
public static List<Zigbee2MqttDevice> GetDevicesNew()
|
||||
{
|
||||
return devicesNew;
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
using Mqtt.Client.AspNetCore.Services;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Interfaces.Models.Providers.Zigbee.Aqara;
|
||||
using MyCore.Service.Services;
|
||||
using MyCore.Services.MyControlPanel;
|
||||
using Newtonsoft.Json;
|
||||
@ -16,13 +15,7 @@ namespace MyCore.Services.Devices
|
||||
{
|
||||
public class ActionService
|
||||
{
|
||||
public static bool isOpen = false;
|
||||
public static long lastActionTime;
|
||||
//private static dynamic deserializedReceivedMessage;
|
||||
//private static dynamic triggerStateName;
|
||||
//private static dynamic triggerStateValueCheck;
|
||||
|
||||
public static void HandleActionFromMQTTAsync(string topic, string message, DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, AutomationDatabaseService _AutomationDatabaseService, string userId)
|
||||
public static async Task HandleActionFromMQTTAsync(string topic, string message, DeviceDatabaseService _DeviceDatabaseService, GroupDatabaseService _GroupDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, AutomationDatabaseService _AutomationDatabaseService, string userId)
|
||||
{
|
||||
var actionTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
||||
|
||||
@ -114,26 +107,24 @@ namespace MyCore.Services.Devices
|
||||
System.Console.WriteLine("None conditions");
|
||||
foreach (var action in automation.Actions)
|
||||
{
|
||||
System.Console.WriteLine($"Check Action ! {action.StateName} {action.StateValue}");
|
||||
var stateName = action.StateName;
|
||||
var stateValue = action.StateValue;
|
||||
|
||||
var actionName = "";
|
||||
var DeviceNameForAction = "";
|
||||
Device actionDeviceToTest = new Device();
|
||||
|
||||
// Retrieve action type
|
||||
switch (action.Type) {
|
||||
case Interfaces.Models.Type.DEVICE:
|
||||
var zigbeeDeviceAction = _DeviceDatabaseService.GetById(action.DeviceId);
|
||||
var providerActionTest = _ProviderDatabaseService.GetById(userId, action.ProviderId);
|
||||
|
||||
actionName = zigbeeDeviceAction.Name;
|
||||
DeviceNameForAction = zigbeeDeviceAction.Name;
|
||||
actionDeviceToTest = zigbeeDeviceAction;
|
||||
System.Console.WriteLine($"We get a zigbeeDeviceAction ! Name={zigbeeDeviceAction.Name} Type={zigbeeDeviceAction.Type}");
|
||||
System.Console.WriteLine($"Check action provider type ! Type={providerActionTest.Type} Name={providerActionTest.Name}");
|
||||
break;
|
||||
case Interfaces.Models.Type.GROUP:
|
||||
var zigbeeGroupAction = _GroupDatabaseService.GetById(action.GroupId);
|
||||
actionName = zigbeeGroupAction.Name;
|
||||
DeviceNameForAction = zigbeeGroupAction.Name;
|
||||
|
||||
System.Console.WriteLine($"We get a zigbeeGroupAction ! Name={zigbeeGroupAction.Name} Type={zigbeeGroupAction.Type}");
|
||||
System.Console.WriteLine($"Check action zigbeeGroupAction type ! Type={zigbeeGroupAction.Type}");
|
||||
@ -158,123 +149,183 @@ namespace MyCore.Services.Devices
|
||||
switch (providerAction.Type)
|
||||
{
|
||||
case "zigbee2mqtt":
|
||||
//Zigbee2MqttRequest zigbee2MqttRequest = new Zigbee2MqttRequest() { };
|
||||
var request = "";
|
||||
|
||||
// If the state is already good, do not send the request. (in order to not spam mqtt broker)
|
||||
bool alreadyInAskedState = false;
|
||||
var buildRequest = new Dictionary<string, object>();
|
||||
|
||||
System.Console.WriteLine($"zigbee2mqtt type !");
|
||||
|
||||
var actionDeviceExpose = GetDeviceExposes(actionDeviceToTest);
|
||||
bool validAction = false;
|
||||
|
||||
if (actionDeviceToTest.Type == DeviceType.Light || actionDeviceToTest.Type == DeviceType.Switch)
|
||||
{
|
||||
var typeToTest = actionDeviceToTest.Type == DeviceType.Light ? "light" : "switch";
|
||||
var exposeToTest = actionDeviceExpose.Where(e => e.type == typeToTest).FirstOrDefault();
|
||||
validAction = exposeToTest != null;
|
||||
}
|
||||
else {
|
||||
// Retrieve exposes for actionDevice
|
||||
var exposeToTest = actionDeviceExpose.Where(e => e.name == action.StateName).FirstOrDefault();
|
||||
validAction = exposeToTest != null;
|
||||
}
|
||||
|
||||
if (validAction)
|
||||
{
|
||||
|
||||
// Get device last state
|
||||
var dic = new Dictionary<string, object>();
|
||||
if (actionDeviceToTest.LastState != null) {
|
||||
if (actionDeviceToTest.LastState != null)
|
||||
{
|
||||
dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(actionDeviceToTest.LastState);
|
||||
}
|
||||
|
||||
// TODO: Check if switch correct and if we can toggle => Same for Plug etc (depend on request)
|
||||
// TODO: Check if device has brightness possibility..
|
||||
bool isBrightness = false;
|
||||
try {
|
||||
|
||||
if (actionDeviceToTest.Type == DeviceType.Light || actionDeviceToTest.Type == DeviceType.Switch)
|
||||
switch (actionDeviceToTest.Type) {
|
||||
case DeviceType.Light:
|
||||
case DeviceType.Switch:
|
||||
var actionDeviceExposeForFeatures = actionDeviceExpose.Where(e => e.type == ((DeviceType)actionDeviceToTest.Type).ToString().ToLower()).FirstOrDefault();
|
||||
if (actionDeviceExposeForFeatures != null)
|
||||
{
|
||||
|
||||
var requestBefore = new Dictionary<string, object>();
|
||||
requestBefore.Add(action.StateName, action.StateValue);
|
||||
|
||||
request = JsonConvert.SerializeObject(requestBefore);
|
||||
|
||||
if (dic.Count > 0) {
|
||||
if (dic["state"].ToString().ToLower() == stateValue.ToLower())
|
||||
foreach (var state in action.States)
|
||||
{
|
||||
alreadyInAskedState = true;
|
||||
System.Console.WriteLine($"Check Action in light|switch ! {state.Name} {state.Value}");
|
||||
if (actionDeviceExposeForFeatures.features.Any(ade => ade.name == state.Name))
|
||||
{
|
||||
if (dic.Count > 0)
|
||||
{
|
||||
if (dic["state"].ToString().ToLower() == state.Value.ToLower())
|
||||
{
|
||||
throw new Exception($"Action device is already at the good state : {state.Name} {state.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
/*if (stateValue == DeviceAction.toggle.ToString())
|
||||
// TODO clean this
|
||||
if (state.Name == "brightness")
|
||||
{
|
||||
System.Console.WriteLine($"Toggle action");
|
||||
// TODO => Check exposes device etc
|
||||
switch (dic["state"].ToLower())
|
||||
try
|
||||
{
|
||||
case "on":
|
||||
actionRequest = "off";
|
||||
|
||||
zigbee2MqttRequest.brightness = 0;
|
||||
break;
|
||||
case "off":
|
||||
case null:
|
||||
default:
|
||||
actionRequest = "on";
|
||||
zigbee2MqttRequest.brightness = 255;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (stateValue.ToLower() == DeviceAction.on.ToString().ToLower())
|
||||
int brightness = int.Parse(state.Value);
|
||||
// Add to request
|
||||
buildRequest.Add(state.Name, brightness);
|
||||
} catch (Exception ex)
|
||||
{
|
||||
// retrieve device state to check if already in asked state
|
||||
if (dic["state"].ToLower() == stateValue.ToLower())
|
||||
alreadyInAskedState = true;
|
||||
else
|
||||
{
|
||||
actionRequest = "on";
|
||||
zigbee2MqttRequest.brightness = 255;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// DeviceAction.off.ToString() => Need to ad validation ?
|
||||
|
||||
// retrieve device state to check if already in asked state
|
||||
if (dic["state"].ToLower() == stateValue.ToLower())
|
||||
alreadyInAskedState = true;
|
||||
else
|
||||
{
|
||||
actionRequest = "off";
|
||||
zigbee2MqttRequest.brightness = 0;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
if (!alreadyInAskedState)
|
||||
{
|
||||
/*System.Console.WriteLine($"Before retrieving type etc {zigbee2MqttRequest.state}");
|
||||
|
||||
System.Type type2 = zigbee2MqttRequest.GetType();
|
||||
PropertyInfo property2 = type2.GetProperty(stateName);
|
||||
property2.SetValue(zigbee2MqttRequest, actionRequest, null);
|
||||
var request = JsonConvert.SerializeObject(zigbee2MqttRequest);*/
|
||||
|
||||
var requestToSend = $"Send request ! zigbee2mqtt/{actionName}/set/{request}";
|
||||
System.Console.WriteLine($"Send request ! zigbee2mqtt/{actionName}/set/{request}");
|
||||
|
||||
MqttClientService.PublishMessage("zigbee2mqtt/" + actionName + "/set", request);
|
||||
System.Console.WriteLine($"zigbee - Parse to int error {ex}");
|
||||
}
|
||||
}
|
||||
else {
|
||||
// No expose found.. => the device has not the correct state => not correct automation
|
||||
System.Console.WriteLine($"Invalid action - the request was not sent");
|
||||
// Add to request
|
||||
buildRequest.Add(state.Name, state.Value);
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new Exception($"Action device light|switch does not have expose of type {state.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
throw new Exception("Action device does not have expose of type light|switch");
|
||||
break;
|
||||
default:
|
||||
foreach (var state in action.States)
|
||||
{
|
||||
System.Console.WriteLine($"Check Action ! {state.Name} {state.Value}");
|
||||
if (!actionDeviceExpose.Any(ade => ade.name == state.Name)) {
|
||||
throw new Exception($"Action device does not have expose of type {state.Name}");
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
request = JsonConvert.SerializeObject(buildRequest);
|
||||
|
||||
// SEND REQUEST
|
||||
var requestToSend = $"Send request ! zigbee2mqtt/{DeviceNameForAction}/set/{request}";
|
||||
System.Console.WriteLine($"Send request ! zigbee2mqtt/{DeviceNameForAction}/set/{request}");
|
||||
|
||||
MqttClientService.PublishMessage("zigbee2mqtt/" + DeviceNameForAction + "/set", request);
|
||||
|
||||
} catch (Exception ex) {
|
||||
System.Console.WriteLine($"Not a valid action !");
|
||||
}
|
||||
|
||||
break;
|
||||
case "meross":
|
||||
System.Console.WriteLine($"meross type !");
|
||||
try
|
||||
{
|
||||
var merossDevice = _DeviceDatabaseService.GetById(actionDeviceToTest.Id);
|
||||
|
||||
if (merossDevice != null)
|
||||
{
|
||||
//As multisocket channel 0 is all the sockets, skip 0
|
||||
|
||||
// two state = first state = ON/OFF, second var = switch channels number (LIST) // TODO better check
|
||||
if (action.States.Count >= 2 && action.States.Any(s => s.Name.ToLower() == "state" && (s.Value.ToLower() == "on" || s.Value.ToLower() == "off" || s.Value.ToLower() == "toggle")))
|
||||
{
|
||||
var state = action.States.Where(s => s.Name.ToLower() == "state").FirstOrDefault();
|
||||
var channels = action.States.Where(s => s.Name.ToLower() == "channels").FirstOrDefault();
|
||||
var plugIds = JsonConvert.DeserializeObject<List<int>>(channels.Value);
|
||||
|
||||
List<ToggleStatus> lastStatePlug = new List<ToggleStatus>(); // Off by default
|
||||
|
||||
if (actionDeviceToTest.LastState != null) // Get last state of device
|
||||
{
|
||||
try
|
||||
{
|
||||
lastStatePlug = JsonConvert.DeserializeObject<List<ToggleStatus>>(actionDeviceToTest.LastState);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Console.WriteLine($"Meross error: Last state ! - {ex}");
|
||||
}
|
||||
}
|
||||
else {
|
||||
// By default set to OFF
|
||||
foreach (var plug in plugIds)
|
||||
{
|
||||
lastStatePlug.Add(ToggleStatus.OFF);
|
||||
}
|
||||
}
|
||||
|
||||
List<ToggleStatus> statuses = new List<ToggleStatus>();
|
||||
var i = 0;
|
||||
foreach (var plugId in plugIds)
|
||||
{
|
||||
ToggleStatus status = new ToggleStatus();
|
||||
// check if device, check if multiprise
|
||||
switch (merossDevice.Type)
|
||||
{
|
||||
case DeviceType.Multiplug:
|
||||
case DeviceType.Plug:
|
||||
if (state.Value.ToLower() == "toggle")
|
||||
{
|
||||
status = lastStatePlug[i] == ToggleStatus.OFF ? ToggleStatus.ON : ToggleStatus.OFF;
|
||||
}
|
||||
else
|
||||
{
|
||||
status = state.Value.ToLower() == "on" ? ToggleStatus.ON : ToggleStatus.OFF;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// Send request
|
||||
MqttClientMerossService.ExecuteCommand(actionDeviceToTest.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", status, plugId);
|
||||
statuses.Add(status);
|
||||
i++;
|
||||
}
|
||||
|
||||
// Update Last state
|
||||
actionDeviceToTest.LastState = JsonConvert.SerializeObject(statuses);
|
||||
actionDeviceToTest.LastStateDate = DateTime.Now;
|
||||
_DeviceDatabaseService.Update(actionDeviceToTest);
|
||||
}
|
||||
else {
|
||||
throw new Exception("Meross: Request action invalid");
|
||||
}
|
||||
// TODO UPDATE STATE
|
||||
}
|
||||
} catch (Exception ex)
|
||||
{
|
||||
System.Console.WriteLine($"Meross error: {ex}");
|
||||
}
|
||||
break;
|
||||
case "yeelight":
|
||||
|
||||
if (YeelightService.devices.Count <= 0)
|
||||
{
|
||||
await YeelightService.GetDevices();
|
||||
}
|
||||
var labLamp = YeelightService.devices.Where(d => d.Hostname == actionDeviceToTest.IpAddress).FirstOrDefault();
|
||||
if (labLamp != null)
|
||||
{
|
||||
// Check action type..
|
||||
Task.Run(async () => { await YeelightService.Toggle(labLamp); });
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -298,75 +349,6 @@ namespace MyCore.Services.Devices
|
||||
}
|
||||
catch (Exception ex) { }
|
||||
}
|
||||
// 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 == "shake")
|
||||
{
|
||||
// TODO Check state
|
||||
|
||||
Zigbee2MqttRequest zigbee2MqttRequest = new Zigbee2MqttRequest() { state = "OFF", brightness = 0 };
|
||||
var request = JsonConvert.SerializeObject(zigbee2MqttRequest);
|
||||
|
||||
MqttClientService.PublishMessage("zigbee2mqtt/GU10Bureau/set", request);
|
||||
}*/
|
||||
if (test.action == "tap")
|
||||
{
|
||||
var merossProvider = providers.Where(p => p.Type == "meross").FirstOrDefault();
|
||||
var merossDevices = _DeviceDatabaseService.GetByProviderId(merossProvider.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)
|
||||
MqttClientMerossService.ExecuteCommand(multiprise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.OFF, i);
|
||||
i++;
|
||||
}*/
|
||||
MqttClientMerossService.ExecuteCommand(multiprise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.ON, 1);
|
||||
/*MqttClientMerossService.ExecuteCommand(multiprise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.ON, 2);
|
||||
MqttClientMerossService.ExecuteCommand(multiprise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.ON, 3);*/
|
||||
//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(multiprise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.OFF, 2);
|
||||
MqttClientMerossService.ExecuteCommand(multiprise.ServiceIdentification, Method.SET, CommandMqtt.TOGGLEX, "", "", ToggleStatus.OFF, 3);*/
|
||||
//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;
|
||||
@ -443,7 +425,7 @@ namespace MyCore.Services.Devices
|
||||
var existingDevicesAddresses = existingDevices.Select(ed => ed.ServiceIdentification);
|
||||
|
||||
// Filter devices and check if something new
|
||||
var filteredDevices = devices.Where(d => !existingDevicesAddresses.Contains(d.ieeeAddr)).ToList();
|
||||
var filteredDevices = devices.Where(d => !existingDevicesAddresses.Contains(d.ieee_address)).ToList();
|
||||
// Add new devices
|
||||
Dictionary<string, List<DeviceDetailDTO>> createdDevices = await DeviceService.CreateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, filteredDevices, zigbee2mqttProvider);
|
||||
}
|
||||
|
||||
@ -125,8 +125,8 @@ namespace MyCore.Services.Devices
|
||||
devices = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, yeelightDevices, provider);
|
||||
break;
|
||||
case "zigbee2mqtt":
|
||||
List<Zigbee2MqttDeviceNew> zigbee2MqttDevices = MqttClientService.devicesNew;
|
||||
devices = await CreateFromZigbeeNewAsync(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, zigbee2MqttDevices, provider);
|
||||
List<Zigbee2MqttDevice> zigbee2MqttDevices = MqttClientService.devicesNew;
|
||||
devices = await CreateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, zigbee2MqttDevices, provider);
|
||||
/*List<Zigbee2MqttDevice> zigbee2MqttDevices = MqttClientService.devices;
|
||||
devices = await CreateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, zigbee2MqttDevices, provider);*/
|
||||
break;
|
||||
@ -144,7 +144,8 @@ namespace MyCore.Services.Devices
|
||||
return devices;
|
||||
}
|
||||
|
||||
public static async Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<Zigbee2MqttDevice> zigbee2MqttDevices, Provider provider)
|
||||
// Old way
|
||||
/*public static async Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<Zigbee2MqttDevice> zigbee2MqttDevices, Provider provider)
|
||||
{
|
||||
List<DeviceDetailDTO> createdZigbeeDevices = new List<DeviceDetailDTO>();
|
||||
List<DeviceDetailDTO> notSupportedZigbeeDevices = new List<DeviceDetailDTO>();
|
||||
@ -197,9 +198,9 @@ namespace MyCore.Services.Devices
|
||||
{ "createdDevices", createdZigbeeDevices },
|
||||
{ "notSupportedDevices", notSupportedZigbeeDevices }
|
||||
};
|
||||
}
|
||||
}*/
|
||||
|
||||
public static async Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromZigbeeNewAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<Zigbee2MqttDeviceNew> zigbee2MqttDevices, Provider provider)
|
||||
public static async Task<Dictionary<string, List<DeviceDetailDTO>>> CreateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<Zigbee2MqttDevice> zigbee2MqttDevices, Provider provider)
|
||||
{
|
||||
List<DeviceDetailDTO> createdZigbeeDevices = new List<DeviceDetailDTO>();
|
||||
List<DeviceDetailDTO> notSupportedZigbeeDevices = new List<DeviceDetailDTO>();
|
||||
@ -359,10 +360,10 @@ namespace MyCore.Services.Devices
|
||||
switch (deviceDetailDTO.Model) // TODO
|
||||
{
|
||||
case "mss425f":
|
||||
deviceDetailDTO.Type = DeviceType.Plug;
|
||||
deviceDetailDTO.Type = DeviceType.Multiplug;
|
||||
break;
|
||||
case "mss310":
|
||||
deviceDetailDTO.Type = DeviceType.Actuator;
|
||||
deviceDetailDTO.Type = DeviceType.Plug;
|
||||
break;
|
||||
case "msh300":
|
||||
deviceDetailDTO.Type = DeviceType.Gateway;
|
||||
|
||||
@ -82,7 +82,7 @@ namespace MyCore.Services
|
||||
Console.WriteLine("### CONNECTED WITH SERVER ###");
|
||||
|
||||
// Subscribe to a topic
|
||||
await _client.SubscribeAsync(new TopicFilterBuilder().WithTopic("#").Build());
|
||||
await _client.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("#").Build());
|
||||
|
||||
Console.WriteLine("### SUBSCRIBED ###");
|
||||
});
|
||||
@ -125,7 +125,7 @@ namespace MyCore.Services
|
||||
// Load everydevice in cache.. ? Performance ?
|
||||
|
||||
// TODO - TO CLARIFY
|
||||
switch (topic)
|
||||
/*switch (topic)
|
||||
{
|
||||
case "zigbee2mqtt/bridge/config/devices":
|
||||
try
|
||||
@ -146,8 +146,8 @@ namespace MyCore.Services
|
||||
var test = JsonConvert.DeserializeObject<AqaraCube>(payload);
|
||||
if (test.action == "shake")
|
||||
{
|
||||
/*var labLamp = yeelightService.devices.Where(d => d.Hostname == "192.168.31.74").FirstOrDefault();
|
||||
Task.Run(async () => { await yeelightService.Toggle(labLamp); });*/
|
||||
var labLamp = yeelightService.devices.Where(d => d.Hostname == "192.168.31.74").FirstOrDefault();
|
||||
Task.Run(async () => { await yeelightService.Toggle(labLamp); });
|
||||
}
|
||||
if (test.action == "slide")
|
||||
{
|
||||
@ -206,7 +206,7 @@ namespace MyCore.Services
|
||||
default:
|
||||
Console.WriteLine("Hello nothing to do here..");
|
||||
break;
|
||||
}
|
||||
}*/
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -364,7 +364,7 @@ namespace MyCore.Services
|
||||
Console.WriteLine("### CONNECTED WITH SERVER ###");
|
||||
|
||||
var topic = $"/app/{resultToken.userid}-{appId.ToLower()}/subscribe";
|
||||
await _client.SubscribeAsync(new TopicFilterBuilder().WithTopic(topic).Build());
|
||||
await _client.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic(topic).Build());
|
||||
|
||||
/*foreach (var device in allDevices)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user