diff --git a/Test.ZWave/Program.cs b/Test.ZWave/Program.cs index c35d9be..9272265 100644 --- a/Test.ZWave/Program.cs +++ b/Test.ZWave/Program.cs @@ -33,7 +33,7 @@ namespace Test.ZWave { internal class MainClass { - private static string serialPortName = "COM3"; + private static string serialPortName = "COM3"; // "/dev/ttyACM0"; private static ControllerStatus controllerStatus = ControllerStatus.Disconnected; private static bool showDebugOutput = false; private static readonly LoggingRule LoggingRule = LogManager.Configuration.LoggingRules[0]; @@ -163,7 +163,7 @@ private static void RunCommandInteractive(ZWaveController controller) { Console.WriteLine(e.Message); } - + } private static void ShowMenu() diff --git a/ZWaveLib/ZWaveController.cs b/ZWaveLib/ZWaveController.cs index 48cbc6d..dc33c6a 100644 --- a/ZWaveLib/ZWaveController.cs +++ b/ZWaveLib/ZWaveController.cs @@ -42,7 +42,7 @@ namespace ZWaveLib /// public class ZWaveController : IDisposable { - + #region Private fields private SerialPortInput serialPort; @@ -270,7 +270,7 @@ public ZWaveMessage QueueMessage(ZWaveMessage message) /// Message. public bool SendMessage(ZWaveMessage message) { - #region Debug + #region Debug Utility.logger.Trace("[[[ BEGIN REQUEST ]]]"); var stopWatch = new Stopwatch(); stopWatch.Start(); @@ -297,7 +297,7 @@ public bool SendMessage(ZWaveMessage message) Utility.logger.Warn("Controller status error (Node={0}, CallbackId={0}, Function={1}, CommandClass={2})", pendingRequest.NodeId, pendingRequest.CallbackId.ToString("X2"), pendingRequest.Function, pendingRequest.CommandClass); } pendingRequest = null; - #region Debug + #region Debug stopWatch.Stop(); Utility.logger.Trace("[[[ END REQUEST ]]] took {0} ms", stopWatch.ElapsedMilliseconds); #endregion @@ -410,7 +410,8 @@ public void Discovery() OnDiscoveryProgress(new DiscoveryProgressEventArgs(DiscoveryStatus.DiscoveryStart)); discoveryRunning = true; // First pass, we get protocol info for all nodes - foreach (ZWaveNode zn in nodeList) + var nodeListCopy = new List(nodeList); + foreach (ZWaveNode zn in nodeListCopy) { if (controllerStatus != ControllerStatus.Ready || !serialPort.IsConnected) { @@ -423,7 +424,7 @@ public void Discovery() GetNodeProtocolInfo(zn.Id); } // Second pass, we query additional node informations - foreach (ZWaveNode zn in nodeList) + foreach (ZWaveNode zn in nodeListCopy) { if (controllerStatus != ControllerStatus.Ready || !serialPort.IsConnected) { @@ -439,7 +440,7 @@ public void Discovery() GetNodeInformationFrame(zn.Id).Wait(); else OnNodeUpdated(new NodeUpdatedEventArgs(zn.Id, new NodeEvent(zn, EventParameter.NodeInfo, BitConverter.ToString(zn.NodeInformationFrame).Replace("-", " "), 0))); - + // For nodes that support version command class, query each one for its version. GetNodeCcsVersion(zn); @@ -508,7 +509,7 @@ public void HealNetwork() } public void GetNodeCcsVersion(ZWaveNode zn) - { + { // If node support version command class, query each one for its version. if (zn.SupportCommandClass(CommandClass.Version)) { @@ -724,7 +725,7 @@ public ZWaveMessage RequestNeighborsUpdate(byte nodeId) (byte)ZWaveFunction.RequestNodeNeighborsUpdate, nodeId, 0x00, - 0x00 + 0x00 }; return QueueMessage(new ZWaveMessage(msg, MessageDirection.Outbound, true)).Wait(); } @@ -746,7 +747,7 @@ public ZWaveMessage GetNeighborsRoutingInfo(byte nodeId) 0x00, 0x00, 0x03, - 0x00 + 0x00 }; return QueueMessage(new ZWaveMessage(msg, MessageDirection.Outbound, false)); } @@ -777,7 +778,7 @@ private void QueueManagerTask() Thread.Sleep(commandRetryDelay); } msg.sentAck.Set(); - + if (msg.ResendCount == ZWaveMessage.ResendAttemptsMax) { Utility.logger.Warn("Delivery of message to Node {0} failed (CallbackId={1}).", msg.NodeId, msg.CallbackId.ToString("X2")); @@ -822,9 +823,6 @@ private void ReceiveMessage(ZWaveMessage msg) case MessageType.Request: - if (nodeList.Count == 0) - break; - switch (msg.Function) { @@ -832,33 +830,41 @@ private void ReceiveMessage(ZWaveMessage msg) break; case ZWaveFunction.NodeAdd: - + var nodeAddStatus = NodeAddStatus.None; Enum.TryParse(rawData[5].ToString(), out nodeAddStatus); switch (nodeAddStatus) { case NodeAddStatus.LearnReady: - + UpdateOperationProgress(0x01, NodeQueryStatus.NodeAddReady); SetQueryStage(QueryStage.Complete); break; case NodeAddStatus.AddingSlave: - + var newNode = CreateNode(rawData[6], 0x00); - // Extract node information frame - int nodeInfoLength = (int)rawData[7]; - byte[] nodeInfo = new byte[nodeInfoLength]; - Array.Copy(rawData, 8, nodeInfo, 0, nodeInfoLength); - // Update node properties - newNode.NodeInformationFrame = nodeInfo; - newNode.ProtocolInfo.BasicType = rawData[8]; - newNode.ProtocolInfo.GenericType = rawData[9]; - newNode.ProtocolInfo.SpecificType = rawData[10]; - // Add it to the node list and save it - nodeList.Add(newNode); - SaveNodesConfig(); + var existingNode = nodeList.Find((n) => n.Id == newNode.Id); + if (existingNode == null) + { + // Extract node information frame + int nodeInfoLength = (int)rawData[7]; + byte[] nodeInfo = new byte[nodeInfoLength]; + Array.Copy(rawData, 8, nodeInfo, 0, nodeInfoLength); + // Update node properties + newNode.NodeInformationFrame = nodeInfo; + newNode.ProtocolInfo.BasicType = rawData[8]; + newNode.ProtocolInfo.GenericType = rawData[9]; + newNode.ProtocolInfo.SpecificType = rawData[10]; + // Add it to the node list and save it + nodeList.Add(newNode); + SaveNodesConfig(); + } + else + { + newNode = existingNode; + } UpdateOperationProgress(newNode.Id, NodeQueryStatus.NodeAddStarted); @@ -893,7 +899,7 @@ private void ReceiveMessage(ZWaveMessage msg) break; case NodeAddStatus.Done: - + UpdateOperationProgress(0x01, NodeQueryStatus.NodeAddDone); SetQueryStage(QueryStage.Complete); break; @@ -915,7 +921,7 @@ private void ReceiveMessage(ZWaveMessage msg) { case NodeRemoveStatus.LearnReady: - + UpdateOperationProgress(0x01, NodeQueryStatus.NodeRemoveReady); SetQueryStage(QueryStage.Complete); break; @@ -924,7 +930,7 @@ private void ReceiveMessage(ZWaveMessage msg) UpdateOperationProgress(rawData[6], NodeQueryStatus.NodeRemoveStarted); break; - + case NodeRemoveStatus.Done: if (rawData[6] != 0x00)