Skip to content

Commit

Permalink
v1.2.14
Browse files Browse the repository at this point in the history
- ZigBee: fix node add/discovery
  • Loading branch information
genemars committed Feb 14, 2023
1 parent ff9edae commit 53a4266
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions MIG.HomeAutomation/ZigBee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ const string EventPath_VersionReport
private IZigBeeTransportTransmit transportTransmit;

private string lastAddedNode;
private string lastRemovedNode;
private bool initialized;
private const string ZigBeeModulesDb = "zigbee_modules.xml";


#endregion

#region MIG Interface members
Expand Down Expand Up @@ -187,11 +187,23 @@ public object InterfaceControl(MigInterfaceCommand request)
var nodeToRemove = networkManager.GetNode(new IeeeAddress(nodeId));
if (nodeToRemove != null && nodeToRemove.NetworkAddress > 0)
{
networkManager
.Leave(nodeToRemove.NetworkAddress, nodeToRemove.IeeeAddress, true)
.Wait();
RemoveNode(nodeToRemove);
returnValue = new ResponseStatus(Status.Ok, $"Removed node {nodeToRemove.IeeeAddress}.");
lastRemovedNode = "";
new Thread(()=>
{
networkManager
.Leave(nodeToRemove.NetworkAddress, nodeToRemove.IeeeAddress, true)
.Wait();
if (String.IsNullOrEmpty(lastRemovedNode)) RemoveNode(nodeToRemove);
}).Start();
for (int i = 0; i < NumberOfRemoveAttempts; i++)
{
if (!String.IsNullOrEmpty(lastRemovedNode))
{
break;
}
Thread.Sleep(DelayBetweenAttempts);
}
returnValue = new ResponseStatus(Status.Ok, $"Removed node {lastRemovedNode}.");
}
else
{
Expand Down Expand Up @@ -301,7 +313,7 @@ public object InterfaceControl(MigInterfaceCommand request)
raiseEvent = true;
break;
case Commands.NodeInfo_Get:
ReadClusterData(node);
QueryNodeData(node);
break;
default:
returnValue = new ResponseStatus(Status.Error, "Command not understood.");
Expand All @@ -318,8 +330,10 @@ public object InterfaceControl(MigInterfaceCommand request)
public bool Connect()
{
initialized = false;
DeserializeModules(ZigBeeModulesDb, modules);
Disconnect();
// load cached modules
DeserializeModules(ZigBeeModulesDb, modules);
// initialize controller
string portName = this.GetOption("Port").Value;
if (String.IsNullOrEmpty(portName))
{
Expand Down Expand Up @@ -432,27 +446,38 @@ public void AddNode(ZigBeeNode node)
if (node.LogicalType == NodeDescriptor.LogicalType.ROUTER || node.LogicalType == NodeDescriptor.LogicalType.END_DEVICE)
{
string address = node.IeeeAddress.ToString();
modules.RemoveAll((m) => m.Address == address);
modules.Add(new InterfaceModule()
var module = modules.Find((m) => m.Address == address);
if (module == null)
{
Domain = "HomeAutomation.ZigBee",
Address = address,
CustomData = new ZigBeeNodeData()
module = new InterfaceModule()
{
// TODO: get type from device node
Type = ModuleTypes.Generic
}
});
Domain = "HomeAutomation.ZigBee",
Address = address,
CustomData = new ZigBeeNodeData()
{
// TODO: get type from device node
Type = ModuleTypes.Generic
}
};
modules.Add(module);
}
if (initialized)
{
ControllerEvent("Controller.Status", "Added node " + node.IeeeAddress);
// get manufacturer name and model identifier
Task.Run(() =>
new Thread(() =>
{
ReadClusterData(node).Wait();
try
{
QueryNodeData(node).Wait();
}
catch (Exception e)
{
//Console.WriteLine(e);
}
OnInterfaceModulesChanged(this.GetDomain());
lastAddedNode = node.IeeeAddress.ToString();
});
}).Start();
}
}
}
Expand All @@ -464,6 +489,7 @@ public void RemoveNode(ZigBeeNode node)
{
ControllerEvent("Controller.Status", "Removed node " + node.IeeeAddress);
OnInterfaceModulesChanged(this.GetDomain());
lastRemovedNode = node.IeeeAddress.ToString();
}
}

Expand Down Expand Up @@ -570,7 +596,6 @@ private void SoftReset()
if (File.Exists(fileName)) {
File.Delete(fileName);
}
modules.Clear();
Connect();
}

Expand All @@ -596,7 +621,7 @@ private async Task SetColorHsb(ZigBeeEndpointAddress endpointAddress, double h,
await networkManager.Send(endpointAddress, cmd);
}

private async Task ReadClusterData(ZigBeeNode node)
private async Task QueryNodeData(ZigBeeNode node)
{
var endpoint = node.GetEndpoints().FirstOrDefault();
// Get manufacturer / model
Expand Down

0 comments on commit 53a4266

Please sign in to comment.