public new void SensorMain() { Log.Info($"(SensorMain) CPU id: {System.Threading.Thread.GetCurrentProcessorId()} ThreadId: {System.Threading.Thread.CurrentThread.ManagedThreadId}"); sensorHost.Subscribe(MqttMessageBuilder. NewMessage(). AddDeviceIdToTopic. SubscribeMessage. AddTopic("computer"). AddMultiLevelWildcard. DoNotRetain. QueueMessage. Build()); while (this.IsInitialized) { //Log.Info("If you want, you can stay in control for the life of the sensor using something like this."); System.Threading.Thread.Sleep(10000); sensorHost.Publish(MqttMessageBuilder. NewMessage(). AddDeviceIdToTopic. PublishMessage. AddTopic("computer"). AddTopic("shutdown"). DoNotRetain. QueueMessage. Build()); } }
public new void SensorMain() { base.SensorMain(); // Subscribe to ALL mqtt messages with a root multi-level wildcard sensorHost.Subscribe(MqttMessageBuilder. NewMessage(). SubscribeMessage. AddMultiLevelWildcard. DoNotRetain. QueueMessage. Build()); }
public void UnmapAlltopics(SensorHost sensorHost) { foreach (var item in sensorTopics) { if (item.Value == sensorHost && item.Value.IsCompiled && item.Value.sensor.IsInitialized) { var mTopic = MqttMessageBuilder .NewMessage() .AddTopic(item.Key) .UnsubscribeMessage .Build(); Log.Debug($"Unsubscribing from [{item.Key}] for [{item.Value.sensor.GetSensorIdentifier()}]"); _client.Unsubscribe(mTopic); sensorTopics.TryRemove(item.Key, out var removed); } } foreach (var item in sensorMultiLevelWildcardTopics) { if (item.Value == sensorHost && item.Value.IsCompiled && item.Value.sensor.IsInitialized) { var mMulti = MqttMessageBuilder .NewMessage() .AddTopic(item.Key) .UnsubscribeMessage .Build(); Log.Debug($"Unsubscribing from [{item.Key}] for [{item.Value.sensor.GetSensorIdentifier()}]"); _client.Unsubscribe(mMulti); sensorMultiLevelWildcardTopics.Remove(item.Key, out var removed); } } foreach (var item in sensorSingleLevelWildcardTopics) { if (item.Value == sensorHost && item.Value.IsCompiled && item.Value.sensor.IsInitialized) { var mSingle = MqttMessageBuilder .NewMessage() .AddTopic(item.Key) .UnsubscribeMessage .Build(); Log.Debug($"Unsubscribing from [{item.Key}] for [{item.Value.sensor.GetSensorIdentifier()}]"); _client.Unsubscribe(mSingle); sensorSingleLevelWildcardTopics.Remove(item.Key, out var removed); } } }
public new void SensorMain() { Log.Debug($"(SensorMain) CPU id: {System.Threading.Thread.GetCurrentProcessorId()} ThreadId: {System.Threading.Thread.CurrentThread.ManagedThreadId}"); //this.sensorHost.Subscribe("/uptime/get"); //this.sensorHost.Subscribe("/uptime/current"); var msg = MqttMessageBuilder.NewMessage().SubscribeMessage.AddDeviceIdToTopic.AddTopic("/uptime/").AddSingleLevelWildcard.DoNotRetain.Build(); if (!sensorHost.Subscribe(msg)) { Log.Info("Failed to subscribe to /example2/#"); } Log.Info("Requesting my own uptime every 15 seconds.."); fiveSeconds = new Timer(15000); fiveSeconds.Elapsed += delegate { sensorHost.Publish(new MqttMessageBuilder().PublishMessage.AddDeviceIdToTopic.AddTopic("/uptime/get").DoNotRetain.Build()); }; fiveSeconds.Start(); }
/// <inheritdoc cref="SensorBase.SensorMain"/> public new void SensorMain() { // We should be in our own thread. Hopefully. I'm still new to threading.. Log.Debug($"(SensorMain) CPU id: {System.Threading.Thread.GetCurrentProcessorId()} ThreadId: {System.Threading.Thread.CurrentThread.ManagedThreadId}"); // Below are several different ways to create an MqttMessage object. Pick whichever one works best for you! // Subscribe to JUST /example/status topic if (!sensorHost.Subscribe(new MqttMessageBuilder().SubscribeMessage.AddDeviceIdToTopic.AddTopic("/example1/status").Build())) { Log.Info("Failed to subscribe to /example1/status"); } // Since this is a direct MQTT message to /example/status and we're subscribed, we should receive it if (!sensorHost.Publish(new MqttMessageBuilder().PublishMessage.AddDeviceIdToTopic.AddTopic("/example1/status").SetMessage("1").DoNotRetain.Build())) { Log.Info("Failed to publish to /example1/status"); } // subscribe to all levels above and including /example/ // for example, /example/hello/world/test would trigger. var msg = MqttMessageBuilder.NewMessage().SubscribeMessage.AddDeviceIdToTopic.AddTopic("/example2/").AddMultiLevelWildcard.DoNotRetain.Build(); if (!sensorHost.Subscribe(msg)) { Log.Info("Failed to subscribe to /example2/#"); } var msg2 = MqttMessageBuilder .NewMessage() .PublishMessage .DoNotRetain .AddDeviceId .AddTopic("/example2/") .AddTopic("test") .SetMessage("2") .Build(); // We should receive both of these because /# is a multi-level wildcard sensorHost.Publish(msg2); var msg3 = MqttMessageBuilder .NewMessage() .PublishMessage .DoNotRetain .AddDeviceId .AddTopic("/example2/test//") .AddTopic("should_also_receive") .AddTopic("this_message") .SetMessage("3") .Build(); sensorHost.Publish(msg3); // Subscribe to all /example2/ topics one level up. // For example, /example2/hello would trigger // but /example2/hello/test would not. if (!sensorHost.Subscribe(new MqttMessage().SubscribeMessage.AddDeviceId.AddTopic("/example3/").AddSingleLevelWildcard)) { Log.Info("Failed to subscribe to /example3/+"); } // We should receive the first message but not the second, because /+ is only a single-level wildcard. sensorHost.Publish(new MqttMessageBuilder().PublishMessage.AddDeviceId.AddTopic("/example3/test").SetMessage("4").Build()); sensorHost.Publish(MqttMessageBuilder .NewMessage() .PublishMessage .AddDeviceId .AddTopic("/example3/test/should_not_receive") .SetMessage("5") .DoNotRetain .Build()); Log.Info("In 10 seconds I will send an unload message to /example2/unload_example_script"); // Set up the timer we declared above for 10 seconds unloadTimer = new Timer(10000); // We're using a delegate here, but you could call your own method all the same // This will run 10 seconds after .Start() is called below unloadTimer.Elapsed += delegate { Log.Info("Sending unload MQTT message to myself"); if (!sensorHost.Publish(new MqttMessage().PublishMessage.AddDeviceId.AddTopic("/example3/unload_example_script").SetMessage("unload"))) { Log.Info("Failed to publish to /example3/unload_example_script"); } }; // Start the timer unloadTimer.Start(); /* This will not send because you cannot have multiple wildcards in the same sub message! * var dc = new MqttMessageBuilder().SubscribeMessage.AddDeviceId.AddMultiLevelWildcard.AddSingleLevelWildcard.Build(); * sensorHost.Subscribe(dc); */ // At any time you can unsubscribe from all topics but it's not necessary here // sensorHost.UnsubscribeAllTopics(); while (this.IsInitialized) { Log.Info("If you want, you can stay in control for the life of the sensor using something like this."); System.Threading.Thread.Sleep(10000); // You can use a smaller sleep, just make sure you sleep to reduce CPU usage. } }