|
|
|
@ -3,13 +3,15 @@ using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Diagnostics; |
|
|
|
using System.Diagnostics; |
|
|
|
using System.Text.Json; |
|
|
|
using System.Text.Json; |
|
|
|
|
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
|
|
|
|
namespace RobotLib.Battery |
|
|
|
namespace RobotLib.Battery |
|
|
|
{ |
|
|
|
{ |
|
|
|
public class DevBattery : DevBase |
|
|
|
public class DevBattery : DevBase |
|
|
|
{ |
|
|
|
{ |
|
|
|
private float voltage; |
|
|
|
private float voltage; |
|
|
|
private int secondsSinceLastResponse = 0; |
|
|
|
private int lastResponseS = 1000; // seconds since last response |
|
|
|
|
|
|
|
private const int offlineTresholdS = 11; // treshold when to assess a robot as offline (no response for x sec) |
|
|
|
private Stopwatch stopwatchLastResponse = new(); |
|
|
|
private Stopwatch stopwatchLastResponse = new(); |
|
|
|
|
|
|
|
|
|
|
|
private const string TOPIC_ROBO_REQ_BATTERY = "/both/cmd/battery/get_volt"; |
|
|
|
private const string TOPIC_ROBO_REQ_BATTERY = "/both/cmd/battery/get_volt"; |
|
|
|
@ -17,7 +19,7 @@ namespace RobotLib.Battery |
|
|
|
private const string TOPIC_STAT_RESP_BATTERY = "/stationary/state/battery/voltage"; |
|
|
|
private const string TOPIC_STAT_RESP_BATTERY = "/stationary/state/battery/voltage"; |
|
|
|
|
|
|
|
|
|
|
|
public event EventHandler<BatteryEventArgs> BatteryChanged; |
|
|
|
public event EventHandler<BatteryEventArgs> BatteryChanged; |
|
|
|
public event EventHandler<int> SecondsSinceLastResponseUpdate; |
|
|
|
public event EventHandler<LastResponseUpdateEventArgs> LastResponseUpdate; |
|
|
|
|
|
|
|
|
|
|
|
public float Voltage |
|
|
|
public float Voltage |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -36,11 +38,22 @@ namespace RobotLib.Battery |
|
|
|
|
|
|
|
|
|
|
|
public int SecondsSinceLastResponse |
|
|
|
public int SecondsSinceLastResponse |
|
|
|
{ |
|
|
|
{ |
|
|
|
get { return secondsSinceLastResponse; } |
|
|
|
get { return lastResponseS; } |
|
|
|
private set |
|
|
|
private set |
|
|
|
{ |
|
|
|
{ |
|
|
|
secondsSinceLastResponse = value; |
|
|
|
var lastVal = lastResponseS; // last stored value |
|
|
|
SecondsSinceLastResponseUpdate?.Invoke(this, secondsSinceLastResponse); |
|
|
|
lastResponseS = value; // update stored value |
|
|
|
|
|
|
|
// new value is smaller than online time and old value is larger or equal => newly connected |
|
|
|
|
|
|
|
if (value < offlineTresholdS && lastVal >= offlineTresholdS) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LastResponseUpdate?.Invoke(this, new LastResponseUpdateEventArgs(true, value)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// old value is larger than online time and new value is smaller => newly disconnected |
|
|
|
|
|
|
|
else if(value >= offlineTresholdS && lastVal < offlineTresholdS) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Task.Run(() => this.Voltage = float.NaN); // set voltage to NaN, now unknown |
|
|
|
|
|
|
|
LastResponseUpdate?.Invoke(this, new LastResponseUpdateEventArgs(false, value)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -57,22 +70,27 @@ namespace RobotLib.Battery |
|
|
|
return new List<string>() { TOPIC_ROBO_RESP_BATTERY }; |
|
|
|
return new List<string>() { TOPIC_ROBO_RESP_BATTERY }; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public void RequestBatteryVoltage() |
|
|
|
|
|
|
|
|
|
|
|
public void UpdateLastResponse() |
|
|
|
{ |
|
|
|
{ |
|
|
|
base.SendMessage(TOPIC_ROBO_REQ_BATTERY, true.ToString()); |
|
|
|
|
|
|
|
if (this.stopwatchLastResponse.IsRunning) |
|
|
|
if (this.stopwatchLastResponse.IsRunning) |
|
|
|
{ |
|
|
|
{ |
|
|
|
SecondsSinceLastResponse = this.stopwatchLastResponse.Elapsed.Seconds; |
|
|
|
this.SecondsSinceLastResponse = this.stopwatchLastResponse.Elapsed.Seconds; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void RequestBatteryVoltage() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
base.SendMessage(TOPIC_ROBO_REQ_BATTERY, true.ToString()); |
|
|
|
|
|
|
|
this.UpdateLastResponse(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected override void ParseMessage(string fromTopic, string message) |
|
|
|
protected override void ParseMessage(string fromTopic, string message) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (fromTopic == TOPIC_ROBO_RESP_BATTERY || fromTopic == TOPIC_STAT_RESP_BATTERY) |
|
|
|
if (fromTopic == TOPIC_ROBO_RESP_BATTERY || fromTopic == TOPIC_STAT_RESP_BATTERY) |
|
|
|
{ |
|
|
|
{ |
|
|
|
this.stopwatchLastResponse.Stop(); |
|
|
|
this.stopwatchLastResponse.Restart(); |
|
|
|
this.stopwatchLastResponse.Start(); |
|
|
|
this.SecondsSinceLastResponse = 0; |
|
|
|
SecondsSinceLastResponse = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var parsedString = GetValueFromMesage<string>("voltage", message); |
|
|
|
var parsedString = GetValueFromMesage<string>("voltage", message); |
|
|
|
if (parsedString == null) parsedString = "?"; |
|
|
|
if (parsedString == null) parsedString = "?"; |
|
|
|
|