|
|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
using RobotLib.Communication; |
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Diagnostics; |
|
|
|
|
using System.Text.Json; |
|
|
|
|
|
|
|
|
|
namespace RobotLib.Battery |
|
|
|
|
@ -8,11 +9,15 @@ namespace RobotLib.Battery |
|
|
|
|
public class DevBattery : DevBase |
|
|
|
|
{ |
|
|
|
|
private float voltage; |
|
|
|
|
private int secondsSinceLastResponse = 0; |
|
|
|
|
private Stopwatch stopwatchLastResponse = new(); |
|
|
|
|
|
|
|
|
|
private const string TOPIC_ROBO_REQ_BATTERY = "/mobile/cmd/battery/get_volt"; |
|
|
|
|
private const string TOPIC_ROBO_REQ_BATTERY = "/both/cmd/battery/get_volt"; |
|
|
|
|
private const string TOPIC_ROBO_RESP_BATTERY = "/mobile/state/battery/voltage"; |
|
|
|
|
private const string TOPIC_STAT_RESP_BATTERY = "/stationary/state/battery/voltage"; |
|
|
|
|
|
|
|
|
|
public event EventHandler<BatteryEventArgs> BatteryChanged; |
|
|
|
|
public event EventHandler<int> SecondsSinceLastResponseUpdate; |
|
|
|
|
|
|
|
|
|
public float Voltage |
|
|
|
|
{ |
|
|
|
|
@ -29,17 +34,46 @@ namespace RobotLib.Battery |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public DevBattery(IPublisherSubscriber com) : base(com, new List<string>() { TOPIC_ROBO_RESP_BATTERY }) { } |
|
|
|
|
public int SecondsSinceLastResponse |
|
|
|
|
{ |
|
|
|
|
get { return secondsSinceLastResponse; } |
|
|
|
|
private set |
|
|
|
|
{ |
|
|
|
|
secondsSinceLastResponse = value; |
|
|
|
|
SecondsSinceLastResponseUpdate?.Invoke(this, secondsSinceLastResponse); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public DevBattery(IPublisherSubscriber com, RobotMode mode) : base(com, GenerateListOfRelevantTopics(mode)) { } |
|
|
|
|
|
|
|
|
|
private static List<string> GenerateListOfRelevantTopics(RobotMode mode) |
|
|
|
|
{ |
|
|
|
|
if (mode == RobotMode.Stationary) |
|
|
|
|
{ |
|
|
|
|
return new List<string>() { TOPIC_STAT_RESP_BATTERY }; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
return new List<string>() { TOPIC_ROBO_RESP_BATTERY }; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
public void RequestBatteryVoltage() |
|
|
|
|
{ |
|
|
|
|
base.SendMessage(TOPIC_ROBO_REQ_BATTERY, true.ToString()); |
|
|
|
|
if (this.stopwatchLastResponse.IsRunning) |
|
|
|
|
{ |
|
|
|
|
SecondsSinceLastResponse = this.stopwatchLastResponse.Elapsed.Seconds; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected override void ParseMessage(string fromTopic, string message) |
|
|
|
|
{ |
|
|
|
|
if (fromTopic == TOPIC_ROBO_RESP_BATTERY) |
|
|
|
|
if (fromTopic == TOPIC_ROBO_RESP_BATTERY || fromTopic == TOPIC_STAT_RESP_BATTERY) |
|
|
|
|
{ |
|
|
|
|
this.stopwatchLastResponse.Stop(); |
|
|
|
|
this.stopwatchLastResponse.Start(); |
|
|
|
|
SecondsSinceLastResponse = 0; |
|
|
|
|
|
|
|
|
|
var parsedString = GetValueFromMesage<string>("voltage", message); |
|
|
|
|
if (parsedString == null) parsedString = "?"; |
|
|
|
|
|
|
|
|
|
|