@ -5,7 +5,7 @@ using uPLibrary.Networking.M2Mqtt.Messages;
namespace RobotLib.Communication
namespace RobotLib.Communication
{
{
internal class MqttPublisherSubscriber : IPublisherSubscriber
public class MqttPublisherSubscriber : IPublisherSubscriber
{
{
private MqttClient client ;
private MqttClient client ;
private static readonly NLog . Logger log = NLog . LogManager . GetCurrentClassLogger ( ) ;
private static readonly NLog . Logger log = NLog . LogManager . GetCurrentClassLogger ( ) ;
@ -29,7 +29,24 @@ namespace RobotLib.Communication
private MqttPublisherSubscriber ( )
private MqttPublisherSubscriber ( )
{ }
{ }
/// <summary>
/// Connect anonymously to the broker.
/// </summary>
/// <param name="brokerIp">IP of the broker</param>
/// <returns>success</returns>
public bool Connect ( string brokerIp )
public bool Connect ( string brokerIp )
{
return this . Connect ( brokerIp , null , null ) ;
}
/// <summary>
/// Connect with username/password to the broker.
/// </summary>
/// <param name="brokerIp">IP of the broker</param>
/// <param name="username">username</param>
/// <param name="password">password</param>
/// <returns>success</returns>
public bool Connect ( string brokerIp , string username , string password )
{
{
bool success = false ;
bool success = false ;
lock ( clientLock )
lock ( clientLock )
@ -46,8 +63,15 @@ namespace RobotLib.Communication
client . ConnectionClosed + = Client_ConnectionClosed ;
client . ConnectionClosed + = Client_ConnectionClosed ;
// generate a clientID and connect to Broker
// generate a clientID and connect to Broker
string clientId = Guid . NewGuid ( ) . ToString ( ) ;
string clientId = Guid . NewGuid ( ) . ToString ( ) ;
var result = client . Connect ( clientId ) ;
if ( String . IsNullOrEmpty ( username ) | | String . IsNullOrEmpty ( password ) )
success = result = = 0 ? true : false ;
{
client . Connect ( clientId ) ;
}
else
{
client . Connect ( clientId , username , password ) ;
}
success = client . IsConnected ;
log . Info ( $"Connecting done. Success = {success}" ) ;
log . Info ( $"Connecting done. Success = {success}" ) ;
}
}
return success ;
return success ;
@ -79,7 +103,7 @@ namespace RobotLib.Communication
lock ( clientLock )
lock ( clientLock )
{
{
SubscribedMsgArrivedEventArgs eventArgs = new ( e . Topic , Encoding . UTF8 . GetString ( e . Message ) ) ;
SubscribedMsgArrivedEventArgs eventArgs = new ( e . Topic , Encoding . UTF8 . GetString ( e . Message ) ) ;
log . Trace ( $"Message received on topic {eventArgs.Topic}: {eventArgs.Message}" ) ;
log . Trace ( $"Message received on topic {eventArgs.Topic}: \n {eventArgs.Message}" ) ;
NewMessageArrived ? . Invoke ( this , eventArgs ) ;
NewMessageArrived ? . Invoke ( this , eventArgs ) ;
}
}
}
}
@ -88,8 +112,8 @@ namespace RobotLib.Communication
{
{
lock ( clientLock )
lock ( clientLock )
{
{
var result = client . Publish ( topic , Encoding . ASCII . GetBytes ( message ) ) ;
var msgId = client . Publish ( topic , Encoding . ASCII . GetBytes ( message ) ) ;
log . Trace ( $"Published to topic '{topic}'. Message = {message}" , result = = 0 ? true : false ) ;
log . Trace ( $"Published to topic '{topic}'. MessageId = {msgId}" ) ;
}
}
}
}
@ -97,8 +121,8 @@ namespace RobotLib.Communication
{
{
lock ( clientLock )
lock ( clientLock )
{
{
var result = client . Subscribe ( new string [ ] { topic } , new byte [ ] { MqttMsgBase . QOS_LEVEL_EXACTLY_ONCE } ) ;
var msgId = client . Subscribe ( new string [ ] { topic } , new byte [ ] { MqttMsgBase . QOS_LEVEL_EXACTLY_ONCE } ) ;
log . Info ( $"Subscribed to topic '{topic}'. Success = %s" , result = = 0 ? true : false ) ;
log . Info ( $"Subscribed to topic '{topic}'. MessageId = {msgId}" ) ;
}
}
}
}
}
}