mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
MC update bulb state in db when receive info (not a set)
This commit is contained in:
parent
cb4ab29e40
commit
ed47b2659d
@ -7,7 +7,7 @@ namespace MyCore.Interfaces.Models
|
|||||||
{
|
{
|
||||||
public class LightBulb
|
public class LightBulb
|
||||||
{
|
{
|
||||||
public string State { get; set; }
|
public string state { get; set; }
|
||||||
public int Brightness { get; set; }
|
public int brightness { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,121 +31,141 @@ namespace MyCore.Services.Devices
|
|||||||
string[] topicSplit = topic.Split('/');
|
string[] topicSplit = topic.Split('/');
|
||||||
|
|
||||||
System.Console.WriteLine($"Received message {message}");
|
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]) {
|
switch (topicSplit[0]) {
|
||||||
case "zigbee2mqtt":
|
case "zigbee2mqtt":
|
||||||
var test0 = _ProviderDatabaseService.GetByType(topicSplit[0]).Id;
|
var test0 = _ProviderDatabaseService.GetByType(topicSplit[0]).Id;
|
||||||
var automations = _AutomationDatabaseService.GetByProvider(test0);
|
var automations = _AutomationDatabaseService.GetByProvider(test0);
|
||||||
|
|
||||||
foreach (var automation in automations) {
|
var serviceName = topicSplit[1];
|
||||||
// TODO filter incoming message, retrieve
|
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) {
|
||||||
if (zigbeeDevice != null && automation.Triggers.Any(t => t.DeviceId == zigbeeDevice.Id)) {
|
try
|
||||||
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}");
|
|
||||||
|
|
||||||
// Todo Deserialize by type
|
// Todo Deserialize by type
|
||||||
switch (zigbeeDevice.Model)
|
switch (zigbeeDevice.Type)
|
||||||
{
|
{
|
||||||
case "MFKZQ01LM":
|
case DeviceType.Switch:
|
||||||
deserializedReceivedMessage = JsonConvert.DeserializeObject<AqaraCube>(message);
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Type type = deserializedReceivedMessage.GetType();
|
// if is not a set => check automation
|
||||||
PropertyInfo property = type.GetProperty(automationTrigger.StateName);
|
if (topicSplit.Length <= 2)
|
||||||
triggerStateValueCheck = property.GetValue(deserializedReceivedMessage);
|
{
|
||||||
triggerStateName = property.Name;
|
zigbeeDevice.LastStateDate = DateTime.Now;
|
||||||
|
_DeviceDatabaseService.Update(zigbeeDevice);
|
||||||
|
|
||||||
// Todo check state name and value for triggers..
|
foreach (var automation in automations)
|
||||||
if (automationTrigger.StateName == triggerStateName && automationTrigger.StateValue == triggerStateValueCheck) {
|
|
||||||
// Todo check condition
|
|
||||||
if (automation.Conditions.Count <= 0)
|
|
||||||
{
|
{
|
||||||
System.Console.WriteLine("None conditions");
|
|
||||||
// => SEND REQUEST
|
// todo check if not null and if more than one element
|
||||||
foreach (var action in automation.Actions)
|
if (zigbeeDevice != null && automation.Triggers.Any(t => t.DeviceId == zigbeeDevice.Id))
|
||||||
{
|
{
|
||||||
System.Console.WriteLine($"Check Action ! {action.StateName} {action.StateValue}");
|
System.Console.WriteLine($"Open automation {automation.Name}");
|
||||||
var stateName = action.StateName;
|
var automationTrigger = automation.Triggers.Where(t => t.DeviceId == zigbeeDevice.Id).FirstOrDefault();
|
||||||
var stateValue = action.StateValue;
|
|
||||||
|
|
||||||
var zigbeeDeviceAction = _DeviceDatabaseService.GetById(action.DeviceId);
|
Type type = deserializedReceivedMessage.GetType();
|
||||||
var providerAction = _ProviderDatabaseService.GetById(userId, zigbeeDeviceAction.ProviderId);
|
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}");
|
// Todo check state name and value for triggers..
|
||||||
System.Console.WriteLine($"Check action provider type ! Type={providerAction.Type} Name={providerAction.Name}");
|
if (automationTrigger.StateName == triggerStateName && automationTrigger.StateValue == triggerStateValueCheck)
|
||||||
|
|
||||||
switch (providerAction.Type)
|
|
||||||
{
|
{
|
||||||
case "zigbee2mqtt":
|
// Todo check condition
|
||||||
Zigbee2MqttRequest zigbee2MqttRequest = new Zigbee2MqttRequest() { };
|
if (automation.Conditions.Count <= 0)
|
||||||
var actionRequest = "";
|
{
|
||||||
|
System.Console.WriteLine("None conditions");
|
||||||
System.Console.WriteLine($"Zigbee type !");
|
// => SEND REQUEST
|
||||||
|
foreach (var action in automation.Actions)
|
||||||
// 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)
|
|
||||||
{
|
{
|
||||||
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");
|
case "zigbee2mqtt":
|
||||||
// TO CHECK
|
Zigbee2MqttRequest zigbee2MqttRequest = new Zigbee2MqttRequest() { };
|
||||||
switch (zigbeeDeviceAction.LastState)
|
var actionRequest = "";
|
||||||
{
|
|
||||||
case "ON":
|
System.Console.WriteLine($"Zigbee type !");
|
||||||
actionRequest = "OFF";
|
|
||||||
zigbee2MqttRequest.brightness = 0;
|
// Todo GET AND CHECK DEVICE ACTION POSSIBLE
|
||||||
break;
|
// todo check state name (state, action.. )
|
||||||
case "OFF":
|
System.Console.WriteLine($"zigbeeDeviceAction.Type {zigbeeDeviceAction.Type}");
|
||||||
case null:
|
if (zigbeeDeviceAction.Type == DeviceType.Light)
|
||||||
default:
|
{
|
||||||
actionRequest = "ON";
|
var deserializedReceivedMessage2 = JsonConvert.DeserializeObject<LightBulb>(zigbeeDeviceAction.LastState);
|
||||||
zigbee2MqttRequest.brightness = 255;
|
|
||||||
break;
|
if (stateValue == DeviceAction.toggle.ToString())
|
||||||
}
|
{
|
||||||
zigbeeDeviceAction.LastState = actionRequest;
|
System.Console.WriteLine($"Toggle action");
|
||||||
zigbeeDeviceAction.LastStateDate = DateTime.Now;
|
// 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 !)
|
// switch case according to device type (topic !)
|
||||||
if (topicSplit[1].Contains("MagicCube0"))
|
if (topicSplit[1].Contains("MagicCube0"))
|
||||||
{
|
{
|
||||||
|
|||||||
@ -167,7 +167,7 @@ namespace MyCore.Services
|
|||||||
{
|
{
|
||||||
var lightState = JsonConvert.DeserializeObject<LightBulb>(payload);
|
var lightState = JsonConvert.DeserializeObject<LightBulb>(payload);
|
||||||
|
|
||||||
if (lightState.State == "ON")
|
if (lightState.state == "ON")
|
||||||
lightStateIkeaBulb = LightState.On;
|
lightStateIkeaBulb = LightState.On;
|
||||||
else
|
else
|
||||||
lightStateIkeaBulb = LightState.Off;
|
lightStateIkeaBulb = LightState.Off;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user