From 0149e09498f94e720f9f06d9ed8f05eee94ffd9f Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Tue, 13 Mar 2018 15:15:40 +0100 Subject: [PATCH] Add SetSuspended to simplify handling of status changes --- ATxCommon/Serializables/ServiceStatus.cs | 33 +++++++++++++++++++----- ATxService/AutoTx.cs | 20 +++----------- ATxTray/AutoTxTray.cs | 6 ++--- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/ATxCommon/Serializables/ServiceStatus.cs b/ATxCommon/Serializables/ServiceStatus.cs index a7082c0..5df0e42 100644 --- a/ATxCommon/Serializables/ServiceStatus.cs +++ b/ATxCommon/Serializables/ServiceStatus.cs @@ -20,7 +20,7 @@ namespace ATxCommon.Serializables private DateTime _lastAdminNotification; private DateTime _lastGraceNotification; - private string _limitReason; + private string _statusDescription; private string _currentTransferSrc; private string _currentTargetTmp; @@ -151,13 +151,13 @@ namespace ATxCommon.Serializables /// <summary> /// String indicating why the service is currently suspended (empty if not suspended). /// </summary> - public string LimitReason { - get => _limitReason; + public string StatusDescription { + get => _statusDescription; set { - if (_limitReason == value) return; + if (_statusDescription == value) return; - _limitReason = value; - Log.Trace("LimitReason was updated ({0}).", value); + _statusDescription = value; + Log.Trace("StatusDescription was updated ({0}).", value); Serialize(); } } @@ -286,6 +286,27 @@ namespace ATxCommon.Serializables Environment.MachineName, _currentTargetTmp); } + + /// <summary> + /// Helper to set the service state, logging a message if the state has changed. + /// </summary> + /// <param name="suspended">The target state for <see cref="ServiceSuspended"/>.</param> + /// <param name="description">The description to use for the log message as well as for + /// setting <see cref="StatusDescription"/>.</param> + public void SetSuspended(bool suspended, string description) { + if (description == _statusDescription && suspended == _serviceSuspended) + return; + + _serviceSuspended = suspended; + _statusDescription = description; + Serialize(); + + if (suspended) { + Log.Info("Service suspended. Reason(s): [{0}]", description); + } else { + Log.Info("Service resuming operation ({0}).", description); + } + } #region validate and report diff --git a/ATxService/AutoTx.cs b/ATxService/AutoTx.cs index eaec65b..b9f6469 100644 --- a/ATxService/AutoTx.cs +++ b/ATxService/AutoTx.cs @@ -574,32 +574,18 @@ namespace ATxService // all parameters within valid ranges, so set the state to "Running": if (suspendReasons.Count == 0) { - if (_status.ServiceSuspended) { - _status.ServiceSuspended = false; - _status.LimitReason = ""; // reset to force a message on next service suspend - Log.Info("Service resuming operation (all parameters in valid ranges)."); - } + _status.SetSuspended(false, "all parameters in valid ranges"); return; } // set state to "Running" if no-one is logged on: if (SystemChecks.NoUserIsLoggedOn()) { - if (_status.ServiceSuspended) { - _status.ServiceSuspended = false; - _status.LimitReason = ""; // reset to force a message on next service suspend - Log.Info("Service resuming operation (no user logged on)."); - } + _status.SetSuspended(false, "no user is currently logged on"); return; } // by reaching this point we know the service should be suspended: - _status.ServiceSuspended = true; - var limitReason = string.Join(", ", suspendReasons); - if (limitReason == _status.LimitReason) - return; - - Log.Info("Service suspended. Reason(s): [{0}]", limitReason); - _status.LimitReason = limitReason; + _status.SetSuspended(true, string.Join(", ", suspendReasons)); } /// <summary> diff --git a/ATxTray/AutoTxTray.cs b/ATxTray/AutoTxTray.cs index fe0b01f..eecbc91 100644 --- a/ATxTray/AutoTxTray.cs +++ b/ATxTray/AutoTxTray.cs @@ -459,18 +459,18 @@ namespace ATxTray private void UpdateServiceSuspendedState() { // first update the suspend reason as this can possibly change even if the service // never leaves the suspended state and we should still display the correct reason: - if (_serviceSuspendReason == _status.LimitReason && + if (_serviceSuspendReason == _status.StatusDescription && _serviceSuspended == _status.ServiceSuspended) return; _serviceSuspended = _status.ServiceSuspended; - _serviceSuspendReason = _status.LimitReason; + _serviceSuspendReason = _status.StatusDescription; if (_serviceSuspended) { _miSvcSuspended.Text = @"Service suspended, reason: " + _serviceSuspendReason; _miSvcSuspended.BackColor = Color.LightSalmon; /* _notifyIcon.ShowBalloonTip(500, "AutoTx Monitor", - "Service suspended: " + _status.LimitReason, ToolTipIcon.Warning); + "Service suspended: " + _status.StatusDescription, ToolTipIcon.Warning); */ } else { _miSvcSuspended.Text = @"No limits apply, service active."; -- GitLab