SEBWIN-487 added battery log event for power grid status

This commit is contained in:
anhefti 2021-11-10 12:02:01 +01:00
parent 34f25d51e8
commit c478b3b4a2

View file

@ -38,6 +38,7 @@ namespace SafeExamBrowser.Server
private FileSystem fileSystem; private FileSystem fileSystem;
private string connectionToken; private string connectionToken;
private int currentPowerSupplyValue; private int currentPowerSupplyValue;
private bool connectedToPowergrid;
private int currentWlanValue; private int currentWlanValue;
private string examId; private string examId;
private int handNotificationId; private int handNotificationId;
@ -455,26 +456,22 @@ namespace SafeExamBrowser.Server
try try
{ {
var value = Convert.ToInt32(status.BatteryCharge * 100); var value = Convert.ToInt32(status.BatteryCharge * 100);
var connected = status.IsOnline;
if (value != currentPowerSupplyValue) if (value != currentPowerSupplyValue)
{ {
var authorization = ("Authorization", $"Bearer {oauth2Token}");
var chargeInfo = $"{status.BatteryChargeStatus} at {value}%"; 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 gridInfo = $"{(status.IsOnline ? "connected to" : "disconnected from")} the power grid";
var token = ("SEBConnectionToken", connectionToken); var text = $"<battery> {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}";
var json = new JObject SendPowerSupplyStatus(text, value);
{
["type"] = LogLevel.Info.ToLogType(),
["timestamp"] = DateTime.Now.ToUnixTimestamp(),
["text"] = $"<battery> {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}",
["numericValue"] = value
};
var content = json.ToString();
TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token);
currentPowerSupplyValue = value; currentPowerSupplyValue = value;
} }
else if (connected != connectedToPowergrid)
{
var text = $"<battery> Device has been {(connected ? "connected to" : "disconnected from")} power grid";
SendPowerSupplyStatus(text, value);
connectedToPowergrid = connected;
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -482,6 +479,23 @@ namespace SafeExamBrowser.Server
} }
} }
private void SendPowerSupplyStatus(string text, int value)
{
var authorization = ("Authorization", $"Bearer {oauth2Token}");
var contentType = "application/json;charset=UTF-8";
var token = ("SEBConnectionToken", connectionToken);
var json = new JObject
{
["type"] = LogLevel.Info.ToLogType(),
["timestamp"] = DateTime.Now.ToUnixTimestamp(),
["text"] = text,
["numericValue"] = value
};
var content = json.ToString();
TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token);
}
private void WirelessAdapter_NetworksChanged() private void WirelessAdapter_NetworksChanged()
{ {
const int NOT_CONNECTED = -1; const int NOT_CONNECTED = -1;