MC update bulb state in db when receive info (not a set)

This commit is contained in:
Thomas Fransolet 2021-03-12 19:29:47 +01:00
parent cb4ab29e40
commit ed47b2659d
3 changed files with 107 additions and 87 deletions

View File

@ -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; }
}
}

View File

@ -31,41 +31,59 @@ namespace MyCore.Services.Devices
string[] topicSplit = topic.Split('/');
System.Console.WriteLine($"Received message {message}");
// TODO => Check the diff between <name> and <name>/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();
// 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<AqaraCube>(message);
zigbeeDevice.LastState = message;
break;
case DeviceType.Light:
deserializedReceivedMessage = JsonConvert.DeserializeObject<LightBulb>(message);
zigbeeDevice.LastState = message;
break;
case DeviceType.Plug:
zigbeeDevice.LastState = message;
break;
default:
break;
}
// if is not a set => check automation
if (topicSplit.Length <= 2)
{
zigbeeDevice.LastStateDate = DateTime.Now;
_DeviceDatabaseService.Update(zigbeeDevice);
foreach (var automation in automations)
{
// 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();
Type type = deserializedReceivedMessage.GetType();
PropertyInfo property = type.GetProperty(automationTrigger.StateName);
triggerStateValueCheck = property.GetValue(deserializedReceivedMessage);
triggerStateName = property.Name;
// Todo check state name and value for triggers..
if (automationTrigger.StateName == triggerStateName && automationTrigger.StateValue == triggerStateValueCheck) {
if (automationTrigger.StateName == triggerStateName && automationTrigger.StateValue == triggerStateValueCheck)
{
// Todo check condition
if (automation.Conditions.Count <= 0)
{
@ -96,11 +114,13 @@ namespace MyCore.Services.Devices
System.Console.WriteLine($"zigbeeDeviceAction.Type {zigbeeDeviceAction.Type}");
if (zigbeeDeviceAction.Type == DeviceType.Light)
{
var deserializedReceivedMessage2 = JsonConvert.DeserializeObject<LightBulb>(zigbeeDeviceAction.LastState);
if (stateValue == DeviceAction.toggle.ToString())
{
System.Console.WriteLine($"Toggle action");
// TO CHECK
switch (zigbeeDeviceAction.LastState)
switch (deserializedReceivedMessage2.state)
{
case "ON":
actionRequest = "OFF";
@ -113,8 +133,6 @@ namespace MyCore.Services.Devices
zigbee2MqttRequest.brightness = 255;
break;
}
zigbeeDeviceAction.LastState = actionRequest;
zigbeeDeviceAction.LastStateDate = DateTime.Now;
}
}
@ -130,22 +148,24 @@ namespace MyCore.Services.Devices
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"))
{

View File

@ -167,7 +167,7 @@ namespace MyCore.Services
{
var lightState = JsonConvert.DeserializeObject<LightBulb>(payload);
if (lightState.State == "ON")
if (lightState.state == "ON")
lightStateIkeaBulb = LightState.On;
else
lightStateIkeaBulb = LightState.Off;