Esempio n. 1
0
        private void HandleNodeValueNotification(ZwaveDevice device, ZWValueID valueId)
        {
            if (valueId.CommandClassId == 112)
            {
                // command class configuration
                return;
            }

            if (valueId.CommandClassId == 134)
            {
                // command class version
                return;
            }

            if (valueId.CommandClassId == 37)
            {
                // command class switch binary
                device.IsSwitchBinary = true;
            }

            var valueUnit  = _manager.GetValueUnits(valueId) ?? string.Empty;
            var valueLabel = _manager.GetValueLabel(valueId);
            var valueValue = GetValue(valueId);

            if (valueId.CommandClassId == 128 && valueValue is double)
            {
                // command class battery
                var battery = (double)valueValue;
                if (battery > 90)
                {
                    device.BatteryStatus = DeviceBatteryStatus.Full;
                }
                else if (battery > 25)
                {
                    device.BatteryStatus = DeviceBatteryStatus.Good;
                }
                else
                {
                    device.BatteryStatus = DeviceBatteryStatus.Low;
                }
                return;
            }

            if (valueValue != null)
            {
                valueLabel = valueLabel.Replace(' ', '_');
                OnVariableChanged(device, valueLabel, valueValue, valueUnit);
            }
        }
Esempio n. 2
0
        private ZwaveDevice GetDevice(ZWNotification notification)
        {
            lock (_lock)
            {
                var         devices = _devices.Cast <ZwaveDevice>().ToDictionary(d => d.NodeId);
                ZwaveDevice device;

                if (devices.TryGetValue(notification.NodeId, out device))
                {
                    return(device);
                }

                device = new ZwaveDevice(notification.NodeId, notification.HomeId);
                _devices.Add(device);
                return(device);
            }
        }
        public static void Resolve(ZwaveDeviceLibrary library, ZwaveDevice device)
        {
            var devices = library.Devices
                .Where(d => d.ManufacturerId.Equals(device.ManufacturerId))
                .Where(d => d.ProductId.Equals(device.ProductId))
                .ToList();

            devices = Filter(devices, d => d.ProductTypeId.Equals(device.ProductType));
            devices = Filter(devices, d => d.OemVersion.Equals(device.Application, StringComparison.OrdinalIgnoreCase));
            devices = Filter(devices, d => d.FrequencyName.ToLowerInvariant().Contains("europe"));

            var libraryDevice = devices.FirstOrDefault();
            if (libraryDevice != null)
            {
                device.Manufacturer = libraryDevice.Brand;
                device.ProductName = libraryDevice.Name;
                device.ProductDescription = libraryDevice.Description;
                device.ImagePath = libraryDevice.Image;
            }
        }
Esempio n. 4
0
 private void OnVariableChanged(ZwaveDevice device, string name, object value, string unit)
 {
     _messageQueue.Publish(new UpdateVariableMessage(Name, device.Id, name, value, unit));
 }
        private async Task GetSupportedCommandClasses(ZwaveDevice device, Node node, CancellationToken cancellationToken)
        {
            var commandClasses = await node.GetSupportedCommandClasses();
            var handlers = _commandClassHandlers.ToDictionary(h => h.CommandClass);

            _log.Debug($"Node {node.NodeID} supports {commandClasses.Length} command classes.");

            foreach (var commandClassReport in commandClasses)
            {
                ICommandClassHandler handler;
                if (handlers.TryGetValue(commandClassReport.Class, out handler))
                {
                    handler.Handle(device, node, _nodeCommandQueues[node.NodeID], cancellationToken);
                }
                else
                {
                    _log.Warn($"No CommandClassHandler for CommandClass {commandClassReport.Class} found.");
                }
            }
        }
        private void UpdateDeviceWithLibrary(ZwaveDevice device)
        {
            if (device.Application == null ||
                device.Library == null ||
                device.Protocol == null ||
                device.ManufacturerId == 0 ||
                device.ProductType == 0 ||
                device.ProductId == 0)
            {
                return;
            }

            ZwaveDeviceLibraryResolver.Resolve(_library, device);

            _messageQueue.Publish(new UpdateVariableMessage(Name, device.Id, "Manufacturer", device.Manufacturer));
            _messageQueue.Publish(new UpdateVariableMessage(Name, device.Id, "ProductName", device.ProductName));
            _messageQueue.Publish(new UpdateVariableMessage(Name, device.Id, "Description", device.ProductDescription));

            if (string.IsNullOrEmpty(device.Name))
            {
                device.Name = device.ProductName;
            }
        }
        private async Task GetNodeProductInformation(ZwaveDevice device, Node node)
        {
            var specific = await node.GetCommandClass<ManufacturerSpecific>().Get();

            device.ManufacturerId = specific.ManufacturerID;
            device.ProductType = specific.ProductType;
            device.ProductId = specific.ProductID;

            UpdateDeviceWithLibrary(device);
        }
        private async Task GetNodeVersion(ZwaveDevice device, Node node)
        {
            var version = await node.GetCommandClass<Version>().Get();

            device.Application = version.Application;
            device.Library = version.Library;
            device.Protocol = version.Protocol;

            UpdateDeviceWithLibrary(device);
        }
 private async Task UpdateDeviceProtocolInfo(ZwaveDevice device, Node node)
 {
     var protocolInfo = await node.GetProtocolInfo();
     device.BasicType = (byte)protocolInfo.BasicType;
     device.GenericType = (byte)protocolInfo.GenericType;
     device.SpecificType = protocolInfo.SpecificType;
 }
        private async Task DiscoverNodes(CancellationToken cancellationToken)
        {
            var controllerNodeId = await _controller.GetNodeID();
            var nodes = await _controller.DiscoverNodes();
            var slaveNodes = nodes.Where(n => n.NodeID != controllerNodeId).ToList();

            foreach (var node in slaveNodes)
            {
                if (!_nodeCommandQueues.ContainsKey(node.NodeID))
                {
                    var device = new ZwaveDevice(node.NodeID);
                    var queue = new ZwaveCommandQueue(node);
                    _nodeCommandQueues.Add(node.NodeID, queue);
                    _devices.Add(device);

                    queue.Add("UpdateDeviceProtocolInfo", () => UpdateDeviceProtocolInfo(device, node));
                    queue.Add("GetNodeVersion", () => GetNodeVersion(device, node));
                    queue.Add("GetNodeProductInformation", () => GetNodeProductInformation(device, node));
                    queue.Add("GetSupportedCommandClasses", () => GetSupportedCommandClasses(device, node, cancellationToken));

                    queue.StartOrContinueWorker();
                }
            }
        }