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.
 
 

43 lines
1.6 KiB

using RobotLib.Communication;
using RobotLib.SplitFlap;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace RobotLib.Status {
public class DevStatus: DevBase {
private const string TOPIC_STATUS_MODE = "/mobile/status/mode/";
private const string TOPIC_STATUS_PRESENT = "/mobile/status/present/";
public event EventHandler<StatusEventArgs> StatusChanged;
public event EventHandler<PresentEventArgs> PresentChanged;
public DevStatus(IPublisherSubscriber com) : base(com, new List<string>() { TOPIC_STATUS_MODE, TOPIC_STATUS_PRESENT }) { }
protected override void ParseMessage(string fromTopic, string message) {
if (fromTopic == TOPIC_STATUS_MODE) {
StatusEventArgs eventArgs = new(message);
StatusChanged?.Invoke(this, eventArgs);
}else if(fromTopic == TOPIC_STATUS_PRESENT) {
Int16 presentCnt = 0;
if (Int16.TryParse(message, out presentCnt)) {
PresentEventArgs presentEventArgs = new(presentCnt);
PresentChanged?.Invoke(this, presentEventArgs);
} else {
log.Error($"Invalid payload received {message}");
}
}
}
private T GetValueFromMesage<T>(string parameter, string message) {
var data = JsonSerializer.Deserialize<Dictionary<string, T>>(message);
data.TryGetValue(parameter, out T value);
return value;
}
}
}