You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.9 KiB
63 lines
1.9 KiB
using RobotLib.Communication;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
|
|
namespace RobotLib
|
|
{
|
|
public abstract class DevBase
|
|
{
|
|
protected NLog.Logger log { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Creates a base class.
|
|
/// </summary>
|
|
/// <param name="com">communication instance</param>
|
|
public DevBase(IPublisherSubscriber com) : this(com, new List<string>()) { }
|
|
|
|
/// <summary>
|
|
/// Creates a base class and initializes interested Topics.
|
|
/// </summary>
|
|
/// <param name="com">connunication instance</param>
|
|
/// <param name="interestedTopics">topics that shall be subscribed to</param>
|
|
public DevBase(IPublisherSubscriber com, List<string> interestedTopics)
|
|
{
|
|
log = NLog.LogManager.GetLogger(GetType().ToString());
|
|
|
|
Com = com;
|
|
InterestedTopics = interestedTopics;
|
|
foreach (var topic in InterestedTopics)
|
|
{
|
|
this.Com.Subscribe(topic);
|
|
}
|
|
com.NewMessageArrived += Com_NewMessageArrived;
|
|
}
|
|
|
|
|
|
protected List<string> InterestedTopics { get; }
|
|
protected IPublisherSubscriber Com { get; }
|
|
|
|
protected void Com_NewMessageArrived(object sender, SubscribedMsgArrivedEventArgs e)
|
|
{
|
|
if (InterestedTopics.Contains(e.Topic))
|
|
{
|
|
ParseMessage(e.Topic, e.Message);
|
|
}
|
|
}
|
|
|
|
protected void SendMessage(string topic, string message)
|
|
{
|
|
if (Com.IsConnected)
|
|
{
|
|
Com.Publish(topic, message);
|
|
}
|
|
else
|
|
{
|
|
log.Warn("Not connected! Could not send message: " + message);
|
|
}
|
|
}
|
|
protected virtual void ParseMessage(string fromTopic, string message) { }
|
|
|
|
public virtual void Refresh() { }
|
|
|
|
}
|
|
}
|
|
|