Beispiel #1
0
        public bool MapTopicToSensor(MqttMessage mqttMessage, SensorHost sensorHost, bool prependDeviceId = true)
        {
            Log.Debug($"Mapping topic [{mqttMessage.GetRawTopic()}] for [{sensorHost.SensorIdentifier}]");

            bool result;

            try
            {
                mqttMessage.GetRawTopic().ValidateTopic();
            }
            catch (Exception ex)
            {
                Log.Debug($"Topic {mqttMessage.GetRawTopic()} validated false: {ex.Message}");
                return(false);
            }

            if (mqttMessage.GetRawTopic().Contains("#")) // multi-level wildcard topic
            {
                result = sensorMultiLevelWildcardTopics.TryAdd(mqttMessage.GetRawTopic(), sensorHost);
            }
            else if (mqttMessage.GetRawTopic().Contains("+"))// single level wildcard topic
            {
                result = sensorSingleLevelWildcardTopics.TryAdd(mqttMessage.GetRawTopic(), sensorHost);
            }
            else
            {
                result = sensorTopics.TryAdd(mqttMessage.GetRawTopic(), sensorHost);
            }

            return(result);
        }
Beispiel #2
0
        public bool Unsubscribe(MqttMessage mqttMessage)
        {
            if (_client == null)
            {
                return(false);
            }

            mqttMessage.SetMessageType(MqttMessage.MqttMessageType.MQTT_UNSUBSCRIBE);

            if (!mqttMessage.sendImmediately)
            {
                _client.QueueMessage(mqttMessage.UnsubscribeMessage);
                _sensorManager.UnmapTopicToSensor(mqttMessage, this);
                return(true);
            }

            var success = _client.Unsubscribe(mqttMessage);

            Log.Verbose($"[{SensorIdentifier}] unsubscribing to [{mqttMessage.GetRawTopic()}] ({success})");

            if (success.messageId > 0)
            {
                _sensorManager.UnmapTopicToSensor(mqttMessage, this);
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        public bool SendMqttMessage(MqttMessage mqttMessage)
        {
            if (_client == null)
            {
                return(false);
            }

            if (mqttMessage.messageType == MqttMessage.MqttMessageType.MQTT_NONE)
            {
                return(false);
            }

            if (!mqttMessage.sendImmediately)
            {
                _client.QueueMessage(mqttMessage);
                _sensorManager.MapTopicToSensor(mqttMessage, this);
                return(true);
            }

            var success = _client.SendMessage(mqttMessage);

            Log.Verbose($"[{SensorIdentifier}] sending message for [{mqttMessage.GetRawTopic()}]");

            if (success.messageId > 0)
            {
                _sensorManager.MapTopicToSensor(mqttMessage, this);

                return(true);
            }

            return(false);
        }
Beispiel #4
0
        public bool Publish(MqttMessage mqttMessage)
        {
            if (_client == null)
            {
                return(false);
            }

            mqttMessage.SetMessageType(MqttMessage.MqttMessageType.MQTT_PUBLISH);

            if (!mqttMessage.sendImmediately)
            {
                _client.QueueMessage(mqttMessage.PublishMessage);
                return(true);
            }

            Log.Verbose($"[{SensorIdentifier}] publishing to [{mqttMessage.GetRawTopic()}]: [{mqttMessage.message}]");

            var success = _client.Publish(mqttMessage);

            if (success.messageId > 0)
            {
                return(true);
            }

            return(false);
        }
        public new void ProcessMessage(MqttMessage mqttMessage)
        {
            var baseStation = "hassio";
            var satellite   = "satZero1";
            var baseTopic   = $"hermes/audioServer/{baseStation}/playBytes/";
            var satTopic    = $"hermes/audioServer/{satellite}/playBytes/";
            var t           = mqttMessage.GetRawTopic();

            if (t.Contains(baseTopic))
            {
                t = mqttMessage.GetRawTopic().Substring(baseTopic.Length);
            }
            else
            {
                return;
            }

            sensorHost.Publish(mqttMessage.SetTopic(satTopic).AddTopic(t));
        }
Beispiel #6
0
        public new void ProcessMessage(MqttMessage mqttMessage)
        {
            Log.Info($"[ProcessMessage] Processing topic [{mqttMessage.GetRawTopic()}]: {mqttMessage.message}");

            var topic = mqttMessage.GetTopicWithoutDeviceId().Split('/');

            if (topic == null || topic.Length <= 1)
            {
                return;
            }

            if (topic[0] == "computer")
            {
                HandleCommand(topic, mqttMessage.message);
            }
            else
            {
                Log.Info($"[ProcessMessage] Unknown topic [{mqttMessage.GetRawTopic()}]");
            }
        }
Beispiel #7
0
        public bool UnmapTopicToSensor(MqttMessage mqttMessage, SensorHost sensorHost)
        {
            Log.Debug($"Unmapping topic [{mqttMessage.GetRawTopic()}] for [{sensorHost.SensorIdentifier}]");

            SensorHost previous;
            bool       result;

            if (mqttMessage.GetRawTopic().Contains("/#")) // multi-level wildcard topic
            {
                sensorMultiLevelWildcardTopics.TryRemove(mqttMessage.GetRawTopic().Replace("/#", ""), out var sMultiWild);
                result = true;
            }
            else if (mqttMessage.GetRawTopic().Contains("/+")) // single level wildcard topic
            {
                sensorSingleLevelWildcardTopics.TryRemove(mqttMessage.GetRawTopic().Replace("/+", ""), out var sSingleWild);
                result = true;
            }
            else
            {
                result = sensorTopics.TryRemove(mqttMessage.GetRawTopic(), out previous);

                if (sensorHost != previous)
                {
                    Log.Warn($"{sensorHost.SensorIdentifier} unsubscribed for [{previous.SensorIdentifier}]");
                }
            }

            return(result);
        }
Beispiel #8
0
        /// <inheritdoc cref="SensorBase.ProcessMessage(MqttMessage)"/>
        public new void ProcessMessage(MqttMessage mqttMessage)
        {
            Log.Info($"[ProcessMessage] Processing topic [{mqttMessage.GetRawTopic()}]: {mqttMessage.message}");

            var topic = mqttMessage.GetTopicWithoutDeviceId();

            // If we receive a message for our unload topic, call sensorHost.Dispose to start the process
            if (mqttMessage.GetTopicWithoutDeviceId() == "example3/unload_example_script" && mqttMessage.message == "unload")
            {
                Log.Info("Disposing of myself in 5 seconds..");
                System.Threading.Thread.Sleep(5000);
                sensorHost.Dispose();
            }
        }
Beispiel #9
0
        public new void ProcessMessage(MqttMessage mqttMessage)
        {
            Log.Debug($"[{GetSensorIdentifier()}] Processing topic [{mqttMessage.GetRawTopic()}]");

            if (mqttMessage.GetTopicWithoutDeviceId() == "uptime/get")
            {
                Log.Info("Received uptime request");
                if (!sensorHost.Publish(new MqttMessageBuilder().PublishMessage.AddDeviceIdToTopic.AddTopic("/uptime/current").SetMessage(GetUpTime().TotalMilliseconds.ToString()).DoNotRetain.Build()))
                {
                    Log.Info("Failed to publish to /example1/status");
                }
            }
            else if (mqttMessage.GetTopicWithoutDeviceId() == "uptime/current")
            {
                //Log.Info("Received uptime message: " + mqttMessage.message + "ms");
                Log.Info("Uptime received: " + HumanReadableTimeSpan(double.Parse(mqttMessage.message)));
            }
        }
Beispiel #10
0
 /// <summary>
 /// This is called by PC2MQTT when a topic this Sensor has subscribed to has received a <see cref="MqttMessage"/>.
 /// </summary>
 /// <param name="mqttMessage"></param>
 public void ProcessMessage(MqttMessage mqttMessage)
 {
     Log.Info($"{mqttMessage.GetRawTopic()}: {mqttMessage.message}");
 }
Beispiel #11
0
 private static void Client_TopicUnsubscribed(MqttMessage mqttMessage)
 {
     Log.Verbose($"Topic Unsubscribed for {mqttMessage.GetRawTopic()}: [{mqttMessage.message}]");
     //throw new NotImplementedException();
 }
Beispiel #12
0
 private static void Client_MessagePublished(MqttMessage mqttMessage)
 {
     Log.Verbose($"Message published for {mqttMessage.GetRawTopic()}: [{mqttMessage.message}]");
     //throw new NotImplementedException();
 }
Beispiel #13
0
        public void ProcessMessage(MqttMessage mqttMessage)
        {
            SensorHost sensorHost;

            if (sensorTopics.TryGetValue(mqttMessage.GetRawTopic(), out sensorHost))
            {
                Log.Verbose($"[NW] Sending message for topic [{mqttMessage.GetRawTopic()}] to [{sensorHost.SensorIdentifier}]");
                sensorHost.sensor.ProcessMessage(mqttMessage);

                // Found a basic topic, no need to search the wildcards
                return;
            }

            foreach (var item in sensorMultiLevelWildcardTopics)
            {
                var wildcardTopic = item.Key.Split("/");
                var messageTopic  = mqttMessage.GetRawTopic().Split("/");

                if (messageTopic.Count() < wildcardTopic.Count())
                {
                    continue;
                }

                bool wildcardFound = false;
                for (int i = 0; i < wildcardTopic.Count(); i++)
                {
                    if (!wildcardFound && i < wildcardTopic.Count())
                    {
                        if (wildcardTopic[i] == "#")
                        {
                            wildcardFound = true;
                        }

                        if ((messageTopic[i] != wildcardTopic[i]) && !wildcardFound)
                        {
                            break;
                        }
                    }

                    if (wildcardFound)
                    {
                        Log.Verbose($"[MW] Sending message for topic [{mqttMessage.GetRawTopic()}] to [{item.Value.SensorIdentifier}]");
                        item.Value.sensor.ProcessMessage(mqttMessage);
                    }
                }
            }

            foreach (var item in sensorSingleLevelWildcardTopics)
            {
                var wildcardTopic = item.Key.Split("/");
                var messageTopic  = mqttMessage.GetRawTopic().Split("/");

                if (messageTopic.Count() != wildcardTopic.Count())
                {
                    continue;
                }

                for (int i = 0; i < messageTopic.Count(); i++)
                {
                    if ((messageTopic[i] != wildcardTopic[i]) && (wildcardTopic[i] != "+"))
                    {
                        break;
                    }

                    if (i == wildcardTopic.Count() - 1)
                    {
                        Log.Verbose($"[SW] Sending message for topic [{mqttMessage.GetRawTopic()}] to [{item.Value.SensorIdentifier}]");
                        item.Value.sensor.ProcessMessage(mqttMessage);
                    }
                }
            }
        }