diff --git a/ADIS_Csharp/RobotLib/Communication/IPublisherSubscriber.cs b/ADIS_Csharp/RobotLib/Communication/IPublisherSubscriber.cs index f5ee2ff..5ccbe9b 100644 --- a/ADIS_Csharp/RobotLib/Communication/IPublisherSubscriber.cs +++ b/ADIS_Csharp/RobotLib/Communication/IPublisherSubscriber.cs @@ -2,13 +2,18 @@ namespace RobotLib.Communication { - internal interface IPublisherSubscriber + public interface IPublisherSubscriber { /// /// New message to a subscribed topic arrived. /// event EventHandler NewMessageArrived; + /// + /// Connection state. + /// + bool IsConnected { get; } + /// /// Subscribe to a topic. /// diff --git a/ADIS_Csharp/RobotLib/Communication/SubscribedMsgArrivedEventArgs.cs b/ADIS_Csharp/RobotLib/Communication/SubscribedMsgArrivedEventArgs.cs index 10c3d93..9257cfb 100644 --- a/ADIS_Csharp/RobotLib/Communication/SubscribedMsgArrivedEventArgs.cs +++ b/ADIS_Csharp/RobotLib/Communication/SubscribedMsgArrivedEventArgs.cs @@ -10,7 +10,7 @@ namespace RobotLib.Communication public SubscribedMsgArrivedEventArgs(string topic, string message) { Topic = topic; - Message = data; + Message = message; } } } \ No newline at end of file diff --git a/ADIS_Csharp/RobotLib/DevBase.cs b/ADIS_Csharp/RobotLib/DevBase.cs index a1ed02b..3ef7fc9 100644 --- a/ADIS_Csharp/RobotLib/DevBase.cs +++ b/ADIS_Csharp/RobotLib/DevBase.cs @@ -1,8 +1,5 @@ -using System; +using RobotLib.Communication; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace RobotLib { @@ -10,40 +7,40 @@ namespace RobotLib { protected NLog.Logger log { get; private set; } - public DevBase(Com com) : this(com, string.Empty) { } - public DevBase(Com com, string keyword) + public DevBase(IPublisherSubscriber com) : this(com, new List()) { } + public DevBase(IPublisherSubscriber com, List interestedTopics) { log = NLog.LogManager.GetLogger(GetType().ToString()); - Keyword = keyword; Com = com; - com.MessageReveived += MessageReveived; + InterestedTopics = interestedTopics; + com.NewMessageArrived += Com_NewMessageArrived; } - protected string Keyword { get; } - protected Com Com { get; } - protected virtual void MessageReveived(object sender, MessageEventArgs e) + protected List InterestedTopics { get; } + protected IPublisherSubscriber Com { get; } + + protected void Com_NewMessageArrived(object sender, SubscribedMsgArrivedEventArgs e) { - if (e.Message.StartsWith(Keyword)) + if (InterestedTopics.Contains(e.Topic)) { - ParseMessage(e.Message); + ParseMessage(e.Topic, e.Message); } } - protected void SendMessage(string message) + protected void SendMessage(string topic, string message) { if (Com.IsConnected) { - log.Trace("Esp32> " + message); - Com.SendMsg(message); + Com.Publish(topic, message); } else { log.Warn("Not connected! Could not send message: " + message); } } - protected virtual void ParseMessage(string message) { } + protected virtual void ParseMessage(string fromTopic, string message) { } public virtual void Refresh() { } diff --git a/ADIS_Csharp/RobotLib/DevSplitFlap/DevSplitFlap.cs b/ADIS_Csharp/RobotLib/DevSplitFlap/DevSplitFlap.cs new file mode 100644 index 0000000..8ba2e59 --- /dev/null +++ b/ADIS_Csharp/RobotLib/DevSplitFlap/DevSplitFlap.cs @@ -0,0 +1,58 @@ +using RobotLib.Communication; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace RobotLib.DevSplitFlap +{ + internal class DevSplitFlap : DevBase + { + private const string TOPIC_INITALL = "/splitFlap/cmd/init/"; + private const string TOPIC_CONFIGURE_SF = "/splitFlap/config/setup/"; + private const string TOPIC_DISPLAY = "/splitFlap/cmd/display/"; + + public event EventHandler SplitFlapDisplayChanged; + + public DevSplitFlap(IPublisherSubscriber com) : base(com, new List() { TOPIC_DISPLAY }) { } + + public void InitializeAllSplitflaps() + { + base.SendMessage(TOPIC_INITALL, ""); + } + + public void ConfigureSplitflap(int setupId, int hardwareId) + { + string payload = JsonSerializer.Serialize(new Dictionary() { { "setupId", setupId.ToString() }, { "hardwareId", hardwareId.ToString() } }); + base.SendMessage(TOPIC_CONFIGURE_SF, payload); + } + + public void Display(string message) + { + string payload = JsonSerializer.Serialize(new Dictionary() { { "message", message } }); + base.SendMessage(TOPIC_DISPLAY, payload); + } + + protected override void ParseMessage(string fromTopic, string message) + { + if(fromTopic == TOPIC_DISPLAY) + { + string parsedMessage = (string)GetValueFromMesage("message", message); + if (parsedMessage == null) parsedMessage = "?"; + SplitFlapDisplayEventArgs eventArgs = new(parsedMessage); + SplitFlapDisplayChanged?.Invoke(this, eventArgs); + } + } + + private object GetValueFromMesage(string parameter,string message) + { + var data = JsonSerializer.Deserialize>(message); + object value = null; + data.TryGetValue(parameter, out value); + return value; + } + } +} diff --git a/ADIS_Csharp/RobotLib/DevSplitFlap/SplitFlapDisplayEventArgs.cs b/ADIS_Csharp/RobotLib/DevSplitFlap/SplitFlapDisplayEventArgs.cs new file mode 100644 index 0000000..fc9e242 --- /dev/null +++ b/ADIS_Csharp/RobotLib/DevSplitFlap/SplitFlapDisplayEventArgs.cs @@ -0,0 +1,12 @@ +namespace RobotLib.DevSplitFlap +{ + public class SplitFlapDisplayEventArgs + { + public string DisplayMessage { get; } + + public SplitFlapDisplayEventArgs(string displayMessage) + { + DisplayMessage = displayMessage; + } + } +} \ No newline at end of file