Skip to content

Commit

Permalink
Fixed bug in add node functionality that was occurring only on the fi…
Browse files Browse the repository at this point in the history
…rst node added; Fixed occasional 'collection was modified' exception;
  • Loading branch information
genemars committed Jun 11, 2020
1 parent fbb0e80 commit e47d7b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Test.ZWave/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -163,7 +163,7 @@ private static void RunCommandInteractive(ZWaveController controller)
{
Console.WriteLine(e.Message);
}

}

private static void ShowMenu()
Expand Down
68 changes: 37 additions & 31 deletions ZWaveLib/ZWaveController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace ZWaveLib
/// </summary>
public class ZWaveController : IDisposable
{

#region Private fields

private SerialPortInput serialPort;
Expand Down Expand Up @@ -270,7 +270,7 @@ public ZWaveMessage QueueMessage(ZWaveMessage message)
/// <param name="message">Message.</param>
public bool SendMessage(ZWaveMessage message)
{
#region Debug
#region Debug
Utility.logger.Trace("[[[ BEGIN REQUEST ]]]");
var stopWatch = new Stopwatch();
stopWatch.Start();
Expand All @@ -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
Expand Down Expand Up @@ -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<ZWaveNode>(nodeList);
foreach (ZWaveNode zn in nodeListCopy)
{
if (controllerStatus != ControllerStatus.Ready || !serialPort.IsConnected)
{
Expand All @@ -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)
{
Expand All @@ -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);

Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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();
}
Expand All @@ -746,7 +747,7 @@ public ZWaveMessage GetNeighborsRoutingInfo(byte nodeId)
0x00,
0x00,
0x03,
0x00
0x00
};
return QueueMessage(new ZWaveMessage(msg, MessageDirection.Outbound, false));
}
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -822,43 +823,48 @@ private void ReceiveMessage(ZWaveMessage msg)

case MessageType.Request:

if (nodeList.Count == 0)
break;

switch (msg.Function)
{

case ZWaveFunction.NotSet:
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);

Expand Down Expand Up @@ -893,7 +899,7 @@ private void ReceiveMessage(ZWaveMessage msg)
break;

case NodeAddStatus.Done:

UpdateOperationProgress(0x01, NodeQueryStatus.NodeAddDone);
SetQueryStage(QueryStage.Complete);
break;
Expand All @@ -915,7 +921,7 @@ private void ReceiveMessage(ZWaveMessage msg)
{

case NodeRemoveStatus.LearnReady:

UpdateOperationProgress(0x01, NodeQueryStatus.NodeRemoveReady);
SetQueryStage(QueryStage.Complete);
break;
Expand All @@ -924,7 +930,7 @@ private void ReceiveMessage(ZWaveMessage msg)

UpdateOperationProgress(rawData[6], NodeQueryStatus.NodeRemoveStarted);
break;

case NodeRemoveStatus.Done:

if (rawData[6] != 0x00)
Expand Down

0 comments on commit e47d7b3

Please sign in to comment.