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.
58 lines
2.1 KiB
58 lines
2.1 KiB
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<SplitFlapDisplayEventArgs> SplitFlapDisplayChanged;
|
|
|
|
public DevSplitFlap(IPublisherSubscriber com) : base(com, new List<string>() { TOPIC_DISPLAY }) { }
|
|
|
|
public void InitializeAllSplitflaps()
|
|
{
|
|
base.SendMessage(TOPIC_INITALL, "");
|
|
}
|
|
|
|
public void ConfigureSplitflap(int setupId, int hardwareId)
|
|
{
|
|
string payload = JsonSerializer.Serialize(new Dictionary<string, string>() { { "setupId", setupId.ToString() }, { "hardwareId", hardwareId.ToString() } });
|
|
base.SendMessage(TOPIC_CONFIGURE_SF, payload);
|
|
}
|
|
|
|
public void Display(string message)
|
|
{
|
|
string payload = JsonSerializer.Serialize(new Dictionary<string, string>() { { "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<Dictionary<string, object>>(message);
|
|
object value = null;
|
|
data.TryGetValue(parameter, out value);
|
|
return value;
|
|
}
|
|
}
|
|
}
|
|
|