private bool DispatchZigbee2MqttMessage(MqttApplicationMessage msg)
        {
            var match = FriendlyNameExtractor.Match(msg.Topic);

            if (!match.Success)
            {
                return(false);
            }

            var friendlyName = match.Groups["name"].Value;

            if (friendlyName.Equals("bridge"))
            {
                var stateGroup = match.Groups["state"];
                if (stateGroup.Success && msg.Payload != null)
                {
                    var value   = stateGroup.Value;
                    var payload = _utf8.GetString(msg.Payload);
                    if (value.Equals("state"))
                    {
                        var isOnline = payload.Equals("online");
                        _stateService.SetBridgeState(isOnline: isOnline);

                        if (isOnline)
                        {
                            StartPolling();
                        }
                    }
                    else if (value.Equals("config"))
                    {
                        _stateService.SetBridgeConfig(configJson: payload, isJoinAllowed: out var isJoinAllowed);

                        if (ImmutableInterlocked.TryRemove(ref _allowJoinWaitingList, isJoinAllowed, out var tcs))
                        {
                            tcs.TrySetResult(null);
                        }
                    }
                    return(true);
                }
                return(false);
            }

            if (msg.Payload == null)
            {
                return(true);
            }
            else if (match.Groups["state"].Success)
            {
                var payload = _utf8.GetString(msg.Payload);
                _stateService.SetDeviceAvailability(friendlyName, payload.Equals("online"));
            }
            else
            {
                var payload = _utf8.GetString(msg.Payload);
                _stateService.UpdateDevice(friendlyName: friendlyName, payload);
            }

            return(true);
        }
示例#2
0
        private bool DispatchZigbee2MqttMessage(MqttApplicationMessage msg)
        {
            var match = FriendlyNameExtractor.Match(msg.Topic);

            if (!match.Success)
            {
                return(false);
            }

            var friendlyName = match.Groups["name"].Value;

            if (friendlyName.Equals("bridge"))
            {
                var stateGroup = match.Groups["state"];
                if (stateGroup.Success && msg.Payload != null)
                {
                    var value   = stateGroup.Value;
                    var payload = _utf8.GetString(msg.Payload);
                    if (value.Equals("state"))
                    {
                        var isOnline = payload.Equals("online");
                        _stateService.SetBridgeState(isOnline: isOnline);

                        if (isOnline)
                        {
                            StartPolling();
                        }
                    }
                    else if (value.Equals("config"))
                    {
                        _stateService.SetBridgeConfig(configJson: payload, isJoinAllowed: out var isJoinAllowed, logLevel: out var logLevel);

                        if (ImmutableInterlocked.TryRemove(ref _allowJoinWaitingList, isJoinAllowed, out var tcs))
                        {
                            tcs.TrySetResult(null);
                        }

                        if (ImmutableInterlocked.TryRemove(ref _logLevelWaitingList, logLevel, out var tcs2))
                        {
                            tcs2.TrySetResult(null);
                        }
                    }
                    return(true);
                }
                return(false);
            }

            if (msg.Payload == null)
            {
                return(true);
            }

            if (match.Groups["state"].Success)
            {
                var payload = _utf8.GetString(msg.Payload);
                _stateService.SetDeviceAvailability(friendlyName, payload.Equals("online"));
            }
            else
            {
                var setMatch      = SetRemover.Match(friendlyName);
                var warnSetInName = false;
                if (setMatch.Success)
                {
                    if (_stateService.CurrentState.Devices.Length == 0)
                    {
                        return(false);                        // no devices received yet
                    }

                    var realFriendlyName = setMatch.Groups["friendlyName"].Value;
                    if (_stateService.FindDeviceById(realFriendlyName, out _) != null)
                    {
                        // It's not the state of a device, it's a /set/<attribute_name> on a device!
                        // (absolutely no interests for that here: it will just generate parsing exceptions)
                        return(false);
                    }

                    _logger.LogWarning($"Received a message for topic '{friendlyName}'. It looks like it's setting an attribute on a friendly name '{realFriendlyName}', but no such device is known. If the application is starting, you can safely discard this warning and the following potential parsing error.");
                    warnSetInName = true;
                }

                var getMatch = GetRemover.Match(friendlyName);
                if (getMatch.Success)
                {
                    var realFriendlyName = getMatch.Groups["friendlyName"].Value;
                    if (_stateService.FindDeviceById(realFriendlyName, out _) != null)
                    {
                        // It's a /get request on a device - won't contain any information useful for updating
                        return(false);
                    }
                }

                var payload = _utf8.GetString(msg.Payload);
                if (_stateService.UpdateDevice(friendlyName: friendlyName, jsonPayload: payload,
                                               forceLastSeen: out var setLastSeen) != null && setLastSeen && _settings.CurrentSettings.AutosetLastSeen)
                {
                    _ = SetLastSeen();
                }

                if (warnSetInName)
                {
                    _logger.LogWarning($"It look like you actually have a device named '{friendlyName}' after all. THAT'S A BAD NAME: consider changing it!");
                }
            }

            return(true);
        }