Advanced Distributed Systems module at HSLU
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.
 
 

48 lines
1.3 KiB

using RobotLib.Communication;
using System.Collections.Generic;
namespace RobotLib
{
public abstract class DevBase
{
protected NLog.Logger log { get; private set; }
public DevBase(IPublisherSubscriber com) : this(com, new List<string>()) { }
public DevBase(IPublisherSubscriber com, List<string> interestedTopics)
{
log = NLog.LogManager.GetLogger(GetType().ToString());
Com = com;
InterestedTopics = interestedTopics;
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() { }
}
}