diff --git a/MyCore.Interfaces/DTO/Common/DeviceType.cs b/MyCore.Interfaces/DTO/Common/DeviceType.cs index e7ef430..01eb260 100644 --- a/MyCore.Interfaces/DTO/Common/DeviceType.cs +++ b/MyCore.Interfaces/DTO/Common/DeviceType.cs @@ -14,6 +14,7 @@ namespace MyCore.Interfaces.DTO Light, Sound, Plug, + Multiplug, Thermostat, Valve, Door, diff --git a/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs b/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs index 42df88c..a6d0e6e 100644 --- a/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs +++ b/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs @@ -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 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, diff --git a/MyCore.Interfaces/Models/MyControlPanel/Database/Provider.cs b/MyCore.Interfaces/Models/MyControlPanel/Database/Provider.cs index 592ad02..b8cb0fe 100644 --- a/MyCore.Interfaces/Models/MyControlPanel/Database/Provider.cs +++ b/MyCore.Interfaces/Models/MyControlPanel/Database/Provider.cs @@ -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; } diff --git a/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraCube.cs b/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraCube.cs deleted file mode 100644 index 74402db..0000000 --- a/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraCube.cs +++ /dev/null @@ -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; } - } -} \ No newline at end of file diff --git a/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraDevice.cs b/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraDevice.cs deleted file mode 100644 index de8abf2..0000000 --- a/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraDevice.cs +++ /dev/null @@ -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; } - } -} diff --git a/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraMotion.cs b/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraMotion.cs deleted file mode 100644 index d4417da..0000000 --- a/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraMotion.cs +++ /dev/null @@ -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; } - } -} diff --git a/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraSwitch.cs b/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraSwitch.cs deleted file mode 100644 index d61088e..0000000 --- a/MyCore.Interfaces/Models/Providers/Zigbee/Aqara/AqaraSwitch.cs +++ /dev/null @@ -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 - }*/ -} diff --git a/MyCore.Interfaces/Models/Providers/Zigbee/Ikea/LightBulb.cs b/MyCore.Interfaces/Models/Providers/Zigbee/Ikea/LightBulb.cs deleted file mode 100644 index a0a8422..0000000 --- a/MyCore.Interfaces/Models/Providers/Zigbee/Ikea/LightBulb.cs +++ /dev/null @@ -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; } - } -} diff --git a/MyCore.Interfaces/Models/Providers/Zigbee/Zigbee2Mqtt/Zigbee2MqttDevice.cs b/MyCore.Interfaces/Models/Providers/Zigbee/Zigbee2Mqtt/Zigbee2MqttDevice.cs index d419859..75bce7b 100644 --- a/MyCore.Interfaces/Models/Providers/Zigbee/Zigbee2Mqtt/Zigbee2MqttDevice.cs +++ b/MyCore.Interfaces/Models/Providers/Zigbee/Zigbee2Mqtt/Zigbee2MqttDevice.cs @@ -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; } diff --git a/MyCore/Extensions/MqttClientService.cs b/MyCore/Extensions/MqttClientService.cs index e14ffe9..e26adc0 100644 --- a/MyCore/Extensions/MqttClientService.cs +++ b/MyCore/Extensions/MqttClientService.cs @@ -20,7 +20,7 @@ namespace Mqtt.Client.AspNetCore.Services private static IMqttClient mqttClient; private IMqttClientOptions options; public static List devices = new List(); - public static List devicesNew = new List(); + public static List devicesNew = new List(); public static List groups = new List(); 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>(payload); + var devicesConvert = JsonConvert.DeserializeObject>(payload); devicesNew = devicesConvert; } catch (Exception ex) @@ -186,7 +186,7 @@ namespace Mqtt.Client.AspNetCore.Services { return devices; } - public static List GetDevicesNew() + public static List GetDevicesNew() { return devicesNew; } diff --git a/MyCore/Services/Devices/ActionService.cs b/MyCore/Services/Devices/ActionService.cs index 9912a4d..cfe9698 100644 --- a/MyCore/Services/Devices/ActionService.cs +++ b/MyCore/Services/Devices/ActionService.cs @@ -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(); System.Console.WriteLine($"zigbee2mqtt type !"); var actionDeviceExpose = GetDeviceExposes(actionDeviceToTest); - bool validAction = false; - if (actionDeviceToTest.Type == DeviceType.Light || actionDeviceToTest.Type == DeviceType.Switch) + // Get device last state + var dic = new Dictionary(); + if (actionDeviceToTest.LastState != null) { - 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; + dic = JsonConvert.DeserializeObject>(actionDeviceToTest.LastState); } - if (validAction) - { + try { - var dic = new Dictionary(); - if (actionDeviceToTest.LastState != null) { - dic = JsonConvert.DeserializeObject>(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; - - if (actionDeviceToTest.Type == DeviceType.Light || actionDeviceToTest.Type == DeviceType.Switch) - { - - var requestBefore = new Dictionary(); - requestBefore.Add(action.StateName, action.StateValue); - - request = JsonConvert.SerializeObject(requestBefore); - - if (dic.Count > 0) { - if (dic["state"].ToString().ToLower() == stateValue.ToLower()) + 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) { - alreadyInAskedState = true; - } - } + foreach (var state in action.States) + { + 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()) - { - System.Console.WriteLine($"Toggle action"); - // TODO => Check exposes device etc - switch (dic["state"].ToLower()) - { - case "on": - actionRequest = "off"; - - zigbee2MqttRequest.brightness = 0; - break; - case "off": - case null: - default: - actionRequest = "on"; - zigbee2MqttRequest.brightness = 255; - break; + // TODO clean this + if (state.Name == "brightness") + { + try + { + int brightness = int.Parse(state.Value); + // Add to request + buildRequest.Add(state.Name, brightness); + } catch (Exception ex) + { + System.Console.WriteLine($"zigbee - Parse to int error {ex}"); + } + } + else { + // 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 if (stateValue.ToLower() == DeviceAction.on.ToString().ToLower()) - { - // retrieve device state to check if already in asked state - if (dic["state"].ToLower() == stateValue.ToLower()) - alreadyInAskedState = true; else + throw new Exception("Action device does not have expose of type light|switch"); + break; + default: + foreach (var state in action.States) { - actionRequest = "on"; - zigbee2MqttRequest.brightness = 255; - } - } - else - { - // DeviceAction.off.ToString() => Need to ad validation ? + 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}"); + } - // retrieve device state to check if already in asked state - if (dic["state"].ToLower() == stateValue.ToLower()) - alreadyInAskedState = true; - else - { - actionRequest = "off"; - zigbee2MqttRequest.brightness = 0; } - }*/ + break; } - if (!alreadyInAskedState) - { - /*System.Console.WriteLine($"Before retrieving type etc {zigbee2MqttRequest.state}"); + request = JsonConvert.SerializeObject(buildRequest); - System.Type type2 = zigbee2MqttRequest.GetType(); - PropertyInfo property2 = type2.GetProperty(stateName); - property2.SetValue(zigbee2MqttRequest, actionRequest, null); - var request = JsonConvert.SerializeObject(zigbee2MqttRequest);*/ + // SEND REQUEST + var requestToSend = $"Send request ! zigbee2mqtt/{DeviceNameForAction}/set/{request}"; + System.Console.WriteLine($"Send request ! zigbee2mqtt/{DeviceNameForAction}/set/{request}"); - var requestToSend = $"Send request ! zigbee2mqtt/{actionName}/set/{request}"; - System.Console.WriteLine($"Send request ! zigbee2mqtt/{actionName}/set/{request}"); + MqttClientService.PublishMessage("zigbee2mqtt/" + DeviceNameForAction + "/set", request); - MqttClientService.PublishMessage("zigbee2mqtt/" + actionName + "/set", request); - } - } - else { - // No expose found.. => the device has not the correct state => not correct automation - System.Console.WriteLine($"Invalid action - the request was not sent"); + } 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>(channels.Value); + + List lastStatePlug = new List(); // Off by default + + if (actionDeviceToTest.LastState != null) // Get last state of device + { + try + { + lastStatePlug = JsonConvert.DeserializeObject>(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 statuses = new List(); + 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(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> createdDevices = await DeviceService.CreateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, filteredDevices, zigbee2mqttProvider); } diff --git a/MyCore/Services/Devices/DeviceService.cs b/MyCore/Services/Devices/DeviceService.cs index f18f8e2..db5c9c2 100644 --- a/MyCore/Services/Devices/DeviceService.cs +++ b/MyCore/Services/Devices/DeviceService.cs @@ -125,8 +125,8 @@ namespace MyCore.Services.Devices devices = CreateYeelightDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, yeelightDevices, provider); break; case "zigbee2mqtt": - List zigbee2MqttDevices = MqttClientService.devicesNew; - devices = await CreateFromZigbeeNewAsync(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, zigbee2MqttDevices, provider); + List zigbee2MqttDevices = MqttClientService.devicesNew; + devices = await CreateFromZigbeeAsync(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, zigbee2MqttDevices, provider); /*List 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>> CreateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List zigbee2MqttDevices, Provider provider) + // Old way + /*public static async Task>> CreateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List zigbee2MqttDevices, Provider provider) { List createdZigbeeDevices = new List(); List notSupportedZigbeeDevices = new List(); @@ -197,9 +198,9 @@ namespace MyCore.Services.Devices { "createdDevices", createdZigbeeDevices }, { "notSupportedDevices", notSupportedZigbeeDevices } }; - } + }*/ - public static async Task>> CreateFromZigbeeNewAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List zigbee2MqttDevices, Provider provider) + public static async Task>> CreateFromZigbeeAsync(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List zigbee2MqttDevices, Provider provider) { List createdZigbeeDevices = new List(); List notSupportedZigbeeDevices = new List(); @@ -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; diff --git a/MyCore/Services/Devices/MQTTService-OLD.cs b/MyCore/Services/Devices/MQTTService-OLD.cs index 1485c75..3773c24 100644 --- a/MyCore/Services/Devices/MQTTService-OLD.cs +++ b/MyCore/Services/Devices/MQTTService-OLD.cs @@ -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(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; - } + }*/ }); diff --git a/MyCore/Services/Devices/SupportedDevices/MerossService-OLD.cs b/MyCore/Services/Devices/SupportedDevices/MerossService-OLD.cs index 9eb1746..747c916 100644 --- a/MyCore/Services/Devices/SupportedDevices/MerossService-OLD.cs +++ b/MyCore/Services/Devices/SupportedDevices/MerossService-OLD.cs @@ -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) {