diff --git a/MyCore.Interfaces/Models/Providers/Zigbee/Ikea/LightBulb.cs b/MyCore.Interfaces/Models/Providers/Zigbee/Ikea/LightBulb.cs index abf37b8..a0a8422 100644 --- a/MyCore.Interfaces/Models/Providers/Zigbee/Ikea/LightBulb.cs +++ b/MyCore.Interfaces/Models/Providers/Zigbee/Ikea/LightBulb.cs @@ -7,7 +7,7 @@ namespace MyCore.Interfaces.Models { public class LightBulb { - public string State { get; set; } - public int Brightness { get; set; } + public string state { get; set; } + public int brightness { get; set; } } } diff --git a/MyCore/Services/Devices/ActionService.cs b/MyCore/Services/Devices/ActionService.cs index 3c66bfa..89e9a1e 100644 --- a/MyCore/Services/Devices/ActionService.cs +++ b/MyCore/Services/Devices/ActionService.cs @@ -31,121 +31,141 @@ namespace MyCore.Services.Devices string[] topicSplit = topic.Split('/'); System.Console.WriteLine($"Received message {message}"); - // TODO => Check the diff between and /set as the first, we receive the state and the second is only the request (we are no sure it has been accepted by device) switch (topicSplit[0]) { case "zigbee2mqtt": var test0 = _ProviderDatabaseService.GetByType(topicSplit[0]).Id; var automations = _AutomationDatabaseService.GetByProvider(test0); - foreach (var automation in automations) { - // TODO filter incoming message, retrieve - var serviceName = topicSplit[1]; - var zigbeeDevice = _DeviceDatabaseService.GetByName(serviceName).FirstOrDefault(); + var serviceName = topicSplit[1]; + var zigbeeDevice = _DeviceDatabaseService.GetByName(serviceName).FirstOrDefault(); - // todo check if not null and if more than one element - if (zigbeeDevice != null && automation.Triggers.Any(t => t.DeviceId == zigbeeDevice.Id)) { - System.Console.WriteLine($"Open automation {automation.Name}"); - var automationTrigger = automation.Triggers.Where(t => t.DeviceId == zigbeeDevice.Id).FirstOrDefault(); - - System.Console.WriteLine($"Correct device ! {zigbeeDevice.Name}"); + if (zigbeeDevice!= null) { + try + { // Todo Deserialize by type - switch (zigbeeDevice.Model) + switch (zigbeeDevice.Type) { - case "MFKZQ01LM": + case DeviceType.Switch: deserializedReceivedMessage = JsonConvert.DeserializeObject(message); + zigbeeDevice.LastState = message; + break; + case DeviceType.Light: + deserializedReceivedMessage = JsonConvert.DeserializeObject(message); + zigbeeDevice.LastState = message; + break; + case DeviceType.Plug: + zigbeeDevice.LastState = message; break; default: break; } - Type type = deserializedReceivedMessage.GetType(); - PropertyInfo property = type.GetProperty(automationTrigger.StateName); - triggerStateValueCheck = property.GetValue(deserializedReceivedMessage); - triggerStateName = property.Name; + // if is not a set => check automation + if (topicSplit.Length <= 2) + { + zigbeeDevice.LastStateDate = DateTime.Now; + _DeviceDatabaseService.Update(zigbeeDevice); - // Todo check state name and value for triggers.. - if (automationTrigger.StateName == triggerStateName && automationTrigger.StateValue == triggerStateValueCheck) { - // Todo check condition - if (automation.Conditions.Count <= 0) + foreach (var automation in automations) { - System.Console.WriteLine("None conditions"); - // => SEND REQUEST - foreach (var action in automation.Actions) + + // todo check if not null and if more than one element + if (zigbeeDevice != null && automation.Triggers.Any(t => t.DeviceId == zigbeeDevice.Id)) { - System.Console.WriteLine($"Check Action ! {action.StateName} {action.StateValue}"); - var stateName = action.StateName; - var stateValue = action.StateValue; + System.Console.WriteLine($"Open automation {automation.Name}"); + var automationTrigger = automation.Triggers.Where(t => t.DeviceId == zigbeeDevice.Id).FirstOrDefault(); - var zigbeeDeviceAction = _DeviceDatabaseService.GetById(action.DeviceId); - var providerAction = _ProviderDatabaseService.GetById(userId, zigbeeDeviceAction.ProviderId); + Type type = deserializedReceivedMessage.GetType(); + PropertyInfo property = type.GetProperty(automationTrigger.StateName); + triggerStateValueCheck = property.GetValue(deserializedReceivedMessage); + triggerStateName = property.Name; - System.Console.WriteLine($"We get a zigbeeDeviceAction ! Name={zigbeeDeviceAction.Name} Type={zigbeeDeviceAction.Type}"); - System.Console.WriteLine($"Check action provider type ! Type={providerAction.Type} Name={providerAction.Name}"); - - switch (providerAction.Type) + // Todo check state name and value for triggers.. + if (automationTrigger.StateName == triggerStateName && automationTrigger.StateValue == triggerStateValueCheck) { - case "zigbee2mqtt": - Zigbee2MqttRequest zigbee2MqttRequest = new Zigbee2MqttRequest() { }; - var actionRequest = ""; - - System.Console.WriteLine($"Zigbee type !"); - - // Todo GET AND CHECK DEVICE ACTION POSSIBLE - // todo check state name (state, action.. ) - System.Console.WriteLine($"zigbeeDeviceAction.Type {zigbeeDeviceAction.Type}"); - if (zigbeeDeviceAction.Type == DeviceType.Light) + // Todo check condition + if (automation.Conditions.Count <= 0) + { + System.Console.WriteLine("None conditions"); + // => SEND REQUEST + foreach (var action in automation.Actions) { - if (stateValue == DeviceAction.toggle.ToString()) + System.Console.WriteLine($"Check Action ! {action.StateName} {action.StateValue}"); + var stateName = action.StateName; + var stateValue = action.StateValue; + + var zigbeeDeviceAction = _DeviceDatabaseService.GetById(action.DeviceId); + var providerAction = _ProviderDatabaseService.GetById(userId, zigbeeDeviceAction.ProviderId); + + System.Console.WriteLine($"We get a zigbeeDeviceAction ! Name={zigbeeDeviceAction.Name} Type={zigbeeDeviceAction.Type}"); + System.Console.WriteLine($"Check action provider type ! Type={providerAction.Type} Name={providerAction.Name}"); + + switch (providerAction.Type) { - System.Console.WriteLine($"Toggle action"); - // TO CHECK - switch (zigbeeDeviceAction.LastState) - { - case "ON": - actionRequest = "OFF"; - zigbee2MqttRequest.brightness = 0; - break; - case "OFF": - case null: - default: - actionRequest = "ON"; - zigbee2MqttRequest.brightness = 255; - break; - } - zigbeeDeviceAction.LastState = actionRequest; - zigbeeDeviceAction.LastStateDate = DateTime.Now; + case "zigbee2mqtt": + Zigbee2MqttRequest zigbee2MqttRequest = new Zigbee2MqttRequest() { }; + var actionRequest = ""; + + System.Console.WriteLine($"Zigbee type !"); + + // Todo GET AND CHECK DEVICE ACTION POSSIBLE + // todo check state name (state, action.. ) + System.Console.WriteLine($"zigbeeDeviceAction.Type {zigbeeDeviceAction.Type}"); + if (zigbeeDeviceAction.Type == DeviceType.Light) + { + var deserializedReceivedMessage2 = JsonConvert.DeserializeObject(zigbeeDeviceAction.LastState); + + if (stateValue == DeviceAction.toggle.ToString()) + { + System.Console.WriteLine($"Toggle action"); + // TO CHECK + switch (deserializedReceivedMessage2.state) + { + case "ON": + actionRequest = "OFF"; + zigbee2MqttRequest.brightness = 0; + break; + case "OFF": + case null: + default: + actionRequest = "ON"; + zigbee2MqttRequest.brightness = 255; + break; + } + } + } + + System.Console.WriteLine($"Before retrieving type etc {zigbee2MqttRequest.state}"); + + Type type2 = zigbee2MqttRequest.GetType(); + PropertyInfo property2 = type2.GetProperty(stateName); + property2.SetValue(zigbee2MqttRequest, actionRequest, null); + var request = JsonConvert.SerializeObject(zigbee2MqttRequest); + + var name = zigbeeDeviceAction.Name.Substring(0, zigbeeDeviceAction.Name.Length - 1); // TODO CHANGE !!!! + + System.Console.WriteLine($"Send request ! zigbee2mqtt/{name}/set/{request}"); + + MqttClientService.PublishMessage("zigbee2mqtt/" + name + "/set", request); + break; + case "meross": + + break; } } - - System.Console.WriteLine($"Before retrieving type etc {zigbee2MqttRequest.state}"); - - Type type2 = zigbee2MqttRequest.GetType(); - PropertyInfo property2 = type2.GetProperty(stateName); - property2.SetValue(zigbee2MqttRequest, actionRequest, null); - var request = JsonConvert.SerializeObject(zigbee2MqttRequest); - - var name = zigbeeDeviceAction.Name.Substring(0, zigbeeDeviceAction.Name.Length - 1); // TODO CHANGE !!!! - - System.Console.WriteLine($"Send request ! zigbee2mqtt/{name}/set/{request}"); - - MqttClientService.PublishMessage("zigbee2mqtt/" + name + "/set", request); - - // Save laststate - - DeviceService.CreateOrUpdate(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, zigbeeDeviceAction.UserId, zigbeeDeviceAction.ToDTO(), false); - break; - case "meross": - - break; + } } - //=> TODO SEND REQUEST } } } + else + { + // do nothing is a set + } } + catch (Exception ex) { } } - // switch case according to device type (topic !) if (topicSplit[1].Contains("MagicCube0")) { diff --git a/MyCore/Services/Devices/MQTTService-OLD.cs b/MyCore/Services/Devices/MQTTService-OLD.cs index 7c7b1c2..1485c75 100644 --- a/MyCore/Services/Devices/MQTTService-OLD.cs +++ b/MyCore/Services/Devices/MQTTService-OLD.cs @@ -167,7 +167,7 @@ namespace MyCore.Services { var lightState = JsonConvert.DeserializeObject(payload); - if (lightState.State == "ON") + if (lightState.state == "ON") lightStateIkeaBulb = LightState.On; else lightStateIkeaBulb = LightState.Off;