SEBWIN-445: Ensured battery and WLAN data is only sent to server when values change.

This commit is contained in:
Damian Büchel 2020-12-10 17:42:50 +01:00
parent 86242911a2
commit 9b8379bdae

View file

@ -39,6 +39,8 @@ namespace SafeExamBrowser.Server
private AppConfig appConfig;
private CancellationTokenSource cancellationTokenSource;
private string connectionToken;
private int currentPowerSupplyValue;
private int currentWlanValue;
private string examId;
private HttpClient httpClient;
private ILogger logger;
@ -332,9 +334,13 @@ namespace SafeExamBrowser.Server
private void PowerSupply_StatusChanged(IPowerSupplyStatus status)
{
try
{
var value = Convert.ToInt32(status.BatteryCharge * 100);
if (value != currentPowerSupplyValue)
{
var authorization = ("Authorization", $"Bearer {oauth2Token}");
var chargeInfo = $"{status.BatteryChargeStatus} at {Convert.ToInt32(status.BatteryCharge * 100)}%";
var chargeInfo = $"{status.BatteryChargeStatus} at {value}%";
var contentType = "application/json;charset=UTF-8";
var gridInfo = $"{(status.IsOnline ? "connected to" : "disconnected from")} the power grid";
var token = ("SEBConnectionToken", connectionToken);
@ -343,11 +349,13 @@ namespace SafeExamBrowser.Server
["type"] = ToLogType(LogLevel.Info),
["timestamp"] = ToUnixTimestamp(DateTime.Now),
["text"] = $"<battery> {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}",
["numericValue"] = Convert.ToInt32(status.BatteryCharge * 100)
["numericValue"] = value
};
var content = json.ToString();
TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token);
currentPowerSupplyValue = value;
}
}
catch (Exception e)
{
@ -392,11 +400,16 @@ namespace SafeExamBrowser.Server
private void WirelessAdapter_NetworksChanged()
{
const int NOT_CONNECTED = -1;
try
{
var network = wirelessAdapter.GetNetworks().FirstOrDefault(n => n.Status == WirelessNetworkStatus.Connected);
if (network?.SignalStrength != currentWlanValue)
{
var authorization = ("Authorization", $"Bearer {oauth2Token}");
var contentType = "application/json;charset=UTF-8";
var network = wirelessAdapter.GetNetworks().FirstOrDefault(n => n.Status == WirelessNetworkStatus.Connected);
var token = ("SEBConnectionToken", connectionToken);
var json = new JObject { ["type"] = ToLogType(LogLevel.Info), ["timestamp"] = ToUnixTimestamp(DateTime.Now) };
@ -411,6 +424,9 @@ namespace SafeExamBrowser.Server
}
TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, json.ToString(), contentType, authorization, token);
currentWlanValue = network?.SignalStrength ?? NOT_CONNECTED;
}
}
catch (Exception e)
{