private async Task UpdateDataAsync() { var isCommonParamsRequested = _isCommonParamsRequested == IsRequestedValue; var isPressureParamsRequested = _isPressureParamsRequested == IsRequestedValue; var isEcgParamsRequested = _isEcgParamsRequested == IsRequestedValue; if (!isCommonParamsRequested && !isPressureParamsRequested && !isEcgParamsRequested && !_isPumpingRequested) { return; } AssertConnection(); await _updateDataSyncSemaphore .WaitAsync() .ConfigureAwait(false); var ecgValue = new short[2]; try { var commonParams = await _mitar .GetCommonParams() .ConfigureAwait(false); var pressureParams = await _mitar .GetPressureParams() .ConfigureAwait(false); _pumpingStatus = await _mitar .GetPumpingStatus() .ConfigureAwait(false); if (_isPumpingRequested) { if (!_isPumpingStarted && _pumpingStatus == PumpingStatus.InProgress) { _isPumpingStarted = true; } if (_isPumpingStarted && (_pumpingStatus != PumpingStatus.InProgress)) { _isPumpingStarted = false; _isPumpingRequested = false; _pumpingReady.Set(); } } if (isCommonParamsRequested) { _lastCommonParams = commonParams; _commonParamsReady.Set(); } if (isPressureParamsRequested) { _lastPressureParams = pressureParams; _pressureParamsReady.Set(); } if (isEcgParamsRequested) { _ecgValues.Enqueue(ecgValue[0]); _ecgValues.Enqueue(ecgValue[1]); var currentTime = DateTime.UtcNow; if (currentTime > _startedEcgCollectingTime && currentTime - _startedEcgCollectingTime >= _ecgCollectionDuration) { _lastEcgParams = new PatientEcgParams(_ecgValues.ToArray()); while (_ecgValues.TryDequeue(out _)) { } _ecgParamsReady.Set(); } } } catch (Exception) { // чтобы не было deadlock'ов if (isCommonParamsRequested) { _commonParamsReady.Set(); } if (isPressureParamsRequested) { _commonParamsReady.Set(); } if (isEcgParamsRequested) { _ecgParamsReady.Set(); } throw; } finally { _updateDataSyncSemaphore.Release(); } }
public async Task <CycleProcessingContext> ProcessAsync([NotNull] CycleProcessingContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (!context.IsValid()) { _logger.Warning($"{GetType().Name}: действие не будет выполнено, т.к. в обработке сеанса возникли ошибки"); return(context); } var angleParams = context.TryGetAngleParam(); if (angleParams == null) { var forcedRequest = context.TryGetForcedDataCollectionRequest(); forcedRequest?.CommonParamsSemaphore.SetResult(true); return(context); } var isFree = await _mutex .WaitAsync(_blockWaitingTimeout) .ConfigureAwait(false); if (!isFree) { _logger?.Warning($"{GetType().Name}: предыдущий запрос еще выполняется. " + $"Новый запрос не будет выполнен, т.к. прошло больше {_blockWaitingTimeout.TotalMilliseconds} мс"); var forcedRequest = context.TryGetForcedDataCollectionRequest(); forcedRequest?.CommonParamsSemaphore.SetResult(true); return(context); } PatientCommonParams param = null; var sessionInfo = context.TryGetSessionProcessingInfo(); var cycleNumber = sessionInfo?.CurrentCycleNumber; var iterationInfo = context.TryGetIterationParams(); var iterationNumber = iterationInfo?.CurrentIteration; try { _logger?.Trace($"{GetType().Name}: запрос общих параметров пациента с таймаутом {_updatePatientParamTimeout.TotalMilliseconds} мс"); var timeoutPolicy = Policy.TimeoutAsync(_updatePatientParamTimeout); param = await timeoutPolicy .ExecuteAsync(_monitorController.GetPatientCommonParamsAsync) .ConfigureAwait(false); } catch (DeviceConnectionException e) { context.AddOrUpdate( new ExceptionCycleProcessingContextParams( new SessionProcessingException( SessionProcessingErrorCodes.InversionTableConnectionError, e.Message, e, cycleNumber, iterationNumber))); } catch (TimeoutRejectedException e) { context.AddOrUpdate( new ExceptionCycleProcessingContextParams( new SessionProcessingException( SessionProcessingErrorCodes.PatientCommonParamsRequestTimeout, "Получение общих параметров пациента прервано по таймауту", e, cycleNumber, iterationNumber))); } catch (Exception e) { context.AddOrUpdate( new ExceptionCycleProcessingContextParams( new SessionProcessingException( SessionProcessingErrorCodes.PatientCommonParamsRequestError, e.Message, e, cycleNumber, iterationNumber))); } finally { var forcedRequest = context.TryGetForcedDataCollectionRequest(); forcedRequest?.CommonParamsSemaphore.SetResult(true); _mutex.Release(); if (param == null) { param = new PatientCommonParams(-1, -1, -1); } } _logger?.Trace($"{GetType().Name}: текущие общие показатели: ЧСС - {param.HeartRate}, " + $"ЧСД - {param.RepsirationRate}, " + $"SPO2 - {param.Spo2}"); context.AddOrUpdate( new CommonPatientCycleProcessingContextParams( param.HeartRate, param.RepsirationRate, param.Spo2)); return(context); }