public void Initialize() { State = ZWaveManagerState.Initializing; var hasAnyControllers = LoadControllers(); SetOptions(); _manager = ZWManager.Instance; _manager.NotificationReceived += (s, e) => { var homeId = e.Notification.HomeId; var path = _manager.GetControllerPath(homeId); var controller = _controllers.FirstOrDefault(x => x.Path.Equals(path)); var nodeId = e.Notification.NodeId; var node = _nodes.FirstOrDefault(x => x.Id.Equals(nodeId) && x.HomeId.Equals(homeId)); var valueId = e.Notification.ValueId; var notificationType = e.Notification.Type; switch (notificationType) { case ZWNotificationType.ControllerCommand: { ControllerStateHandle((ZWControllerState)e.Notification.Event); } break; case ZWNotificationType.DriverRemoved: { _nodes.RemoveAll(x => x.HomeId.Equals(homeId)); _callbacksPool.Dequeue(true, nameof(RemoveController)); } break; case ZWNotificationType.DriverFailed: { controller.Failed = true; if (_controllers.All(x => x.Failed)) { State = ZWaveManagerState.Initialized; ManagerInitializedCallbacksPool.ExecuteAll(this); } _callbacksPool.Dequeue(false, nameof(AddController), nameof(RemoveController)); } break; case ZWNotificationType.DriverReady: { controller.Failed = false; controller.HomeID = homeId; _callbacksPool.Dequeue( true, nameof(AddController)); } break; case ZWNotificationType.AwakeNodesQueried: case ZWNotificationType.AllNodesQueriedSomeDead: case ZWNotificationType.AllNodesQueried: { _nodes.Where(x => !x.Initialized).All(x => x.Failed = true); _manager.WriteConfig(homeId); State = ZWaveManagerState.Initialized; ManagerInitializedCallbacksPool.ExecuteAll(this); } break; case ZWNotificationType.NodeAdded: case ZWNotificationType.NodeNew: { node = new Node(nodeId, homeId, ZWManager.Instance); _nodes.Add(node); node.Controller = controller; _manager.RequestNodeDynamic(node.HomeId, node.Id); _manager.RequestAllConfigParams(node.HomeId, node.Id); _callbacksPool.Dequeue(true, nameof(AddNewDevice), nameof(AddNewSecureDevice)); } break; case ZWNotificationType.EssentialNodeQueriesComplete: case ZWNotificationType.NodeQueriesComplete: { node.Initialized = true; } break; case ZWNotificationType.NodeProtocolInfo: case ZWNotificationType.NodeNaming: { node.Refresh(); node.Failed = false; } break; case ZWNotificationType.NodeRemoved: { _nodes.Remove(node); _callbacksPool.Dequeue(true, nameof(RemoveDevice)); } break; case ZWNotificationType.ValueAdded: { if (!node.Values.Any(x => x.Id == valueId.Id)) //crutch { var nodeValue = new NodeValue(valueId, node); if (valueId.Genre == ZWValueGenre.Config && valueId.Index < 256) { node.RequestConfigParam((byte)valueId.Index); //crutch } node.Values.Add(nodeValue); nodeValue.Refresh(); NodeValueChanged?.Invoke(this, new EventsArgs <NodeValue>(nodeValue)); } } break; case ZWNotificationType.ValueRefreshed: { var nodeValue = node.Values.FirstOrDefault(x => x.Id.Equals(valueId)); nodeValue.Refresh(); NodeValueChanged?.Invoke(this, new EventsArgs <NodeValue>(nodeValue)); } break; case ZWNotificationType.ValueRemoved: { if (node != null) { var nodeValue = node.Values.FirstOrDefault(x => x.Id.Equals(valueId.Id)); node.Values.Remove(nodeValue); } } break; case ZWNotificationType.ValueChanged: { var nodeValue = node.Values.FirstOrDefault(x => x.Id.Equals(valueId.Id)); nodeValue.CurrentGroupIdx = e.Notification.GroupIndex; nodeValue.InternalSet(Helper.GetValue(_manager, valueId, nodeValue.ZWValueType, nodeValue.PossibleValues)); NodeValueChanged?.Invoke(this, new EventsArgs <NodeValue>(nodeValue)); } break; } }; _manager.Initialize(); foreach (var controller in _controllers) { var ctrl = controller; _callbacksPool.ExecuteBool( () => _manager.AddDriver(ctrl.Path, ctrl.IsHID ? ZWControllerInterface.Hid : ZWControllerInterface.Serial), (result) => { if (!result) { ctrl.Failed = true; if (_controllers.All(x => x.Failed) && State != ZWaveManagerState.Initialized) { State = ZWaveManagerState.Initialized; ManagerInitializedCallbacksPool.ExecuteAll(this); } } else { ctrl.Failed = false; } }, 60, "ControllerLoading" + controller.Path); _manager.TestNetwork(controller.HomeID, 1); } if ((!_controllers.Any() || _controllers.All(x => x.Failed)) && State != ZWaveManagerState.Initialized) { State = ZWaveManagerState.Initialized; ManagerInitializedCallbacksPool.ExecuteAll(this); } }