diff --git a/ATxCommon/Serializables/ServiceStatus.cs b/ATxCommon/Serializables/ServiceStatus.cs index a7082c0aa9345ac4adc92c254b162478b43767c2..5df0e42f97ef1261d58aa3a6c1f953cb8eff457b 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 eaec65b4e13637498e4d22d377d5393608c3ce30..b9f646989fe5de9ca00c8caf0ffcef5e5933d41c 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 fe0b01fa528d7fc9172711f1c9440ef51cfe1e82..eecbc91b0b9f3e678574723f9b795dcf4cb412b9 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.";