From c478b3b4a2f96be272536e364efecc2cebfc903d Mon Sep 17 00:00:00 2001 From: anhefti Date: Wed, 10 Nov 2021 12:02:01 +0100 Subject: [PATCH] SEBWIN-487 added battery log event for power grid status --- SafeExamBrowser.Server/ServerProxy.cs | 40 ++++++++++++++++++--------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/SafeExamBrowser.Server/ServerProxy.cs b/SafeExamBrowser.Server/ServerProxy.cs index d56afe18..795e19d8 100644 --- a/SafeExamBrowser.Server/ServerProxy.cs +++ b/SafeExamBrowser.Server/ServerProxy.cs @@ -38,6 +38,7 @@ namespace SafeExamBrowser.Server private FileSystem fileSystem; private string connectionToken; private int currentPowerSupplyValue; + private bool connectedToPowergrid; private int currentWlanValue; private string examId; private int handNotificationId; @@ -455,26 +456,22 @@ namespace SafeExamBrowser.Server try { var value = Convert.ToInt32(status.BatteryCharge * 100); + var connected = status.IsOnline; if (value != currentPowerSupplyValue) { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); 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); - var json = new JObject - { - ["type"] = LogLevel.Info.ToLogType(), - ["timestamp"] = DateTime.Now.ToUnixTimestamp(), - ["text"] = $" {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}", - ["numericValue"] = value - }; - var content = json.ToString(); - - TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token); + var text = $" {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}"; + SendPowerSupplyStatus(text, value); currentPowerSupplyValue = value; } + else if (connected != connectedToPowergrid) + { + var text = $" Device has been {(connected ? "connected to" : "disconnected from")} power grid"; + SendPowerSupplyStatus(text, value); + connectedToPowergrid = connected; + } } 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() { const int NOT_CONNECTED = -1;