|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using System; |
|
|
|
using System; |
|
|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Text; |
|
|
|
using System.Text; |
|
|
|
using uPLibrary.Networking.M2Mqtt; |
|
|
|
using uPLibrary.Networking.M2Mqtt; |
|
|
|
using uPLibrary.Networking.M2Mqtt.Messages; |
|
|
|
using uPLibrary.Networking.M2Mqtt.Messages; |
|
|
|
@ -10,6 +11,7 @@ namespace RobotLib.Communication |
|
|
|
private MqttClient client; |
|
|
|
private MqttClient client; |
|
|
|
private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger(); |
|
|
|
private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger(); |
|
|
|
private object clientLock = new object(); |
|
|
|
private object clientLock = new object(); |
|
|
|
|
|
|
|
private List<string> toBeSubscribedTopics = new List<string>(); |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// Singleton pattern. |
|
|
|
/// Singleton pattern. |
|
|
|
@ -99,6 +101,15 @@ namespace RobotLib.Communication |
|
|
|
|
|
|
|
|
|
|
|
success = client.IsConnected; |
|
|
|
success = client.IsConnected; |
|
|
|
log.Info($"Connecting done. Success = {success}"); |
|
|
|
log.Info($"Connecting done. Success = {success}"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// subscribe to topics that have already been appended |
|
|
|
|
|
|
|
if(success) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
foreach (var topic in toBeSubscribedTopics) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
this.OnlineSubscribe(topic); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return success; |
|
|
|
return success; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -141,6 +152,11 @@ namespace RobotLib.Communication |
|
|
|
|
|
|
|
|
|
|
|
public void Publish(string topic, string message) |
|
|
|
public void Publish(string topic, string message) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
if(this.IsConnected == false) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
log.Warn($"Message was discarded, Mqtt broker not connected. Destination topic={topic}"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
lock (clientLock) |
|
|
|
lock (clientLock) |
|
|
|
{ |
|
|
|
{ |
|
|
|
var msgId = client.Publish(topic, Encoding.ASCII.GetBytes(message)); |
|
|
|
var msgId = client.Publish(topic, Encoding.ASCII.GetBytes(message)); |
|
|
|
@ -149,6 +165,20 @@ namespace RobotLib.Communication |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void Subscribe(string topic) |
|
|
|
public void Subscribe(string topic) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// add to list if not already there |
|
|
|
|
|
|
|
if(toBeSubscribedTopics.Contains(topic) == false) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
toBeSubscribedTopics.Add(topic); |
|
|
|
|
|
|
|
log.Debug($"Added topic '{topic}'. To the to be subscribed list."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this.IsConnected) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
OnlineSubscribe(topic); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void OnlineSubscribe(string topic) |
|
|
|
{ |
|
|
|
{ |
|
|
|
lock (clientLock) |
|
|
|
lock (clientLock) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|