PM.BLL/PMPollingService.cs
/// <summary>
/// Listener for the poll timer interval
/// </summary>
/// <param name="stateInfo">The state of the timer, containing location</param>
Listener for the poll timer interval
Listener for the poll timer interval
private void HandlePollTimer(object? stateInfo)
        {
            if (stateInfo == null)
            {
                _logger.LogWarning("State must be provided to [{MethodName}]. No action is being taken for this interval.", nameof(HandlePollTimer));
                return;
            }
            if (stateInfo is not PollState pollState)
            {
                _logger.LogWarning("Object must be of type [{ObjectType}]. No action is being taken for this interval.", typeof(PollState));
                return;
            }
            ICommandList commands = _commandListFactory.Create();
            string cacheKey = pollState.SerialNumber;
            ObjectCache workoutStateCache = MemoryCache.Default;
            WorkoutState? workoutState;
            lock (_workoutStateLock) 
            { 
                workoutState = workoutStateCache[cacheKey] as WorkoutState?;
                if (workoutState == null)
                {
                    CacheItemPolicy policy = new()
                    {
                        AbsoluteExpiration = DateTime.UtcNow.AddSeconds(WorkoutStateCheckInterval),
                        UpdateCallback = UpdateWorkoutState
                    };
                    int retryCount = 0;
                    bool retryWorkoutState = false;
                    do
                    {
                        workoutState = GetWorkoutState(pollState.SerialNumber);
                        if (workoutState == null)
                        {
                            retryWorkoutState = retryCount++ < 2;
                        }
                        else
                        {
                            retryWorkoutState = false;
                        }
                    } while (retryWorkoutState);
                    if (workoutState == null)
                    {
                        throw new InvalidOperationException("WorkoutState was null after retries.");
                    }
                    workoutStateCache.Set(cacheKey, workoutState, policy);
                }
                else
                {
                    workoutState = workoutStateCache[cacheKey] as WorkoutState?;
                }
            }
            if (workoutState == null)
            {
                _logger.LogWarning("Workout state could not be found. Polling not performed.");
                return;
            }
            int workoutStateValue = (int) workoutState;
            if (workoutStateValue < 1 || workoutStateValue > 9)
            {
            }
            IEnumerable<PollInterval> pollIntervals = CollectPollIntervals(pollState.Iterations++);
            if (pollIntervals.Any()) 
            { 
                ICommandList? result = ExecutePoll(pollState.SerialNumber, pollIntervals);
                UpdateData(pollState.SerialNumber, result);
                if (pollState.Iterations > 100)
                {
                    EventArgs args = new PollEventArgs
                    {
                        Data = _data[pollState.SerialNumber],
                        SerialNumber = pollState.SerialNumber
                    };
                    PollReturned?.Invoke(this, args);
                    pollState.Iterations = 0;
                }
            }
        }
private void HandlePollTimer ( object ? stateInfo ) { if ( stateInfo == null ) { _logger . LogWarning ( " " , nameof ( HandlePollTimer ) ) ; return ; } if ( stateInfo is not PollState pollState ) { _logger . LogWarning ( " " , typeof ( PollState ) ) ; return ; } ICommandList commands = _commandListFactory . Create ( ) ; string cacheKey = pollState . SerialNumber ; ObjectCache workoutStateCache = MemoryCache . Default ; WorkoutState ? workoutState ; lock ( _workoutStateLock ) { workoutState = workoutStateCache [ cacheKey ] as WorkoutState ? ; if ( workoutState == null ) { CacheItemPolicy policy = new ( ) { AbsoluteExpiration = DateTime . UtcNow . AddSeconds ( WorkoutStateCheckInterval ) , UpdateCallback = UpdateWorkoutState } ; int retryCount = 0 ; bool retryWorkoutState = false ; do { workoutState = GetWorkoutState ( pollState . SerialNumber ) ; if ( workoutState == null ) { retryWorkoutState = retryCount ++ < 2 ; } else { retryWorkoutState = false ; } } while ( retryWorkoutState ) ; if ( workoutState == null ) { throw new InvalidOperationException ( " " ) ; } workoutStateCache . Set ( cacheKey , workoutState , policy ) ; } else { workoutState = workoutStateCache [ cacheKey ] as WorkoutState ? ; } } if ( workoutState == null ) { _logger . LogWarning ( " " ) ; return ; } int workoutStateValue = ( int ) workoutState ; if ( workoutStateValue < 1 || workoutStateValue > 9 ) { } IEnumerable < PollInterval > pollIntervals = CollectPollIntervals ( pollState . Iterations ++ ) ; if ( pollIntervals . Any ( ) ) { ICommandList ? result = ExecutePoll ( pollState . SerialNumber , pollIntervals ) ; UpdateData ( pollState . SerialNumber , result ) ; if ( pollState . Iterations > 100 ) { EventArgs args = new PollEventArgs { Data = _data [ pollState . SerialNumber ] , SerialNumber = pollState . SerialNumber } ; PollReturned ? . Invoke ( this , args ) ; pollState . Iterations = 0 ; } } }
Listener for the poll timer interval
Listener for the poll timer interval
// TODO: Uncomment this // return; // Collect the intervals to execute for this iteration // Execute poll // Update data // Check if 1 second has been reached // Fire event with the poll data // Reset the count
stateInfo
object?
The state of the timer, containing location
The state of the timer containing location