/// <summary>
        /// Closes the next event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        internal void CloseNextEvent(object sender, AlarmEventArg e)
        {
            //un-box the sender
            EventGenerator currentGenerator = sender as EventGenerator;

            // find the next event to close
            int next = this._instanceGenerator.Next(_maxKey);

            while (!OpenAlarms.ContainsKey(next))
            {
                next = this._instanceGenerator.Next(_maxKey);
            }

            // remove from the collection of open events
            int value;

            OpenAlarms.TryRemove(next, out value);

            // queue the action
            currentGenerator?.OnActionQueued(new QueuedEventArg()
            {
                QueuedAction = () => { CloseAlarm(value, e.EventDateTime); }
            });

            //switch back to opening events
            this.OnSwitchingModes(EventArgs.Empty);
        }
        /// <summary>
        /// Opens the next event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        internal void OpenNextEvent(object sender, AlarmEventArg e)
        {
            // un-box the sender
            EventGenerator currentGenerator = sender as EventGenerator;

            // get the next threshold and type - don't open events already open
            int next = this._instanceGenerator.Next(TotalThresholds);

            while (OpenAlarms.ContainsKey(next))
            {
                next = this._instanceGenerator.Next(TotalThresholds);
            }
            if (e.EventType == EventGenerator.EventType.Alarm)
            {
                while (!this.IsAlarm(next))
                {
                    next = this._instanceGenerator.Next(TotalThresholds);
                }
            }
            else
            {
                while (!this.IsWarning(next))
                {
                    next = this._instanceGenerator.Next(TotalThresholds);
                }
            }

            // queue the action
            currentGenerator?.OnActionQueued(new QueuedEventArg()
            {
                QueuedAction = () => { OpenAlarm(next, e.EventDateTime); }
            });

            // toggle the mode - opening or closing if we have more than 25 open events
            if (OpenAlarms.Count > 25)
            {
                this.OnSwitchingModes(EventArgs.Empty);
            }
        }
        private void OnAlarmClosed(AlarmEventArg e)
        {
            var handler = this.AlarmCloseEventRaised;

            handler?.Invoke(this, e);
        }