async Task updateAwailablePortsLoop(CancellationToken cancellation) { await ThreadingUtils.ContinueAtDedicatedThread(cancellation); var period = new PeriodDelay(1000); while (true) { try { using (await Locker.AcquireAsync()) { var devices = getAllDevices(); PortNames = devices.Length.Range().Select(i => PORT_PREFIX + i); } } catch (OperationCanceledException) { throw; } catch (Exception ex) { Logger.LogError(null, $"Ошибка обновления списка устройств", ex); } await period.WaitTimeLeftAsync(cancellation); } }
async void openStateCheckingAsyncLoop() { await ThreadingUtils.ContinueAtDedicatedThread(CancellationToken.None); var period = new PeriodDelay(1000); while (true) { if (_openedDevice != null) { try { var devices = getAllDevices(); var exists = devices.FirstOrDefault(d => d.LocId == _openedDevice.LocId && d.SerialNumber == _openedDevice.SerialNumber && d.Description == _openedDevice.Description) != null; if (!exists) { Close(); } } catch (Exception ex) { Logger.LogError(null, "Ошибка ", ex); } } await period.WaitTimeLeftAsync(); } }
async void DeviceStatusVM_StatusAcquired(DeviceStatusInfo StatusInfo) { if (_statusUpdateDelay.TimeLeft > 0) { return; } else { await _statusUpdateDelay.WaitTimeLeftAsync(); // Restart } await Application.Current.Dispatcher.BeginInvoke(new Action(() => { using (Flags.EventSuppressingMode) { Flags.Clear(); Flags.AddRange(StatusInfo.CurrentStatuses); } using (Info.EventSuppressingMode) { Info.Clear(); if (StatusInfo.SerialNumber.HasValue) { Info.Add(new KeyValuePair <string, string>("Серийный номер", StatusInfo.SerialNumber.Value.ToStringInvariant())); } var flags = Convert .ToString(StatusInfo.Status.Bits, 2) .PadLeft(StatusInfo.Status.NumOfBits, '0') .Reverse() .GroupBy(4) .Select(g => g.Aggregate()) .Aggregate(" ") .Reverse() .Aggregate(); #warning hardcoded statuses if (StatusInfo.Status.NumOfBits == 16) { Info.Add(new KeyValuePair <string, string>("Статус", flags)); } else if (StatusInfo.Status.NumOfBits == 32) { Info.Add(new KeyValuePair <string, string>( $"Статус 31÷15", flags.Take(flags.Length / 2).Aggregate())); Info.Add(new KeyValuePair <string, string>( "Статус 15÷00", flags.TakeFromEnd(flags.Length / 2).Aggregate())); } else { throw new NotSupportedException(); } } }), System.Windows.Threading.DispatcherPriority.Normal); }
public override async Task <BurnResult> BurnAsync(Command request, IEnumerable <IDataEntity> entities, DeviceOperationScope scope, AsyncOperationInfo cancellation) { var baseResult = await base.BurnAsync(request, entities, scope, cancellation); if (baseResult.Status == BurnStatus.OK && request.GetInfo().WriteResponse == Attributes.WriteResponse.RICH) { var richResponse = new RichResponse(baseResult.ResponseSections[SalachovProtocol.BODY_SECTION_KEY]); if (richResponse.Type == RichResponseType.OPERATION_STATUS) { if (richResponse.StatusResponseData != null) { switch (richResponse.StatusResponseData.Status) { case LastWriteRequestStatus.EXECUTED: return(baseResult); case LastWriteRequestStatus.ERROR: return(new BurnResult(BurnStatus.ERROR_ON_DEVICE, baseResult.Response, baseResult.ResponseSections)); case LastWriteRequestStatus.PROCESSING: { var result = await waitTillReadyAsync(); return(new BurnResult(result, baseResult.Response, baseResult.ResponseSections)); } case LastWriteRequestStatus.BUSY: { var result = await waitTillReadyAsync(); if (result == BurnStatus.OK) { return(await BurnAsync(request, entities, scope, cancellation)); } } break; default: throw new NotSupportedException(); } } } } return(baseResult); async Task <BurnStatus> waitTillReadyAsync() { var isCheckSupported = SupportedCommands.Contains(Command.PING); if (isCheckSupported) { var period = new PeriodDelay(PING_SEND_PERIOD); while (true) { await period.WaitTimeLeftAsync(cancellation); var pingResult = await ReadAsync(Command.PING, scope, cancellation); if (pingResult.Status == ReadStatus.OK) { var status = new RichResponse(pingResult.AnswerSections[SalachovProtocol.BODY_SECTION_KEY]).StatusResponseData; if (status.Status.IsOneOf(LastWriteRequestStatus.BUSY, LastWriteRequestStatus.PROCESSING)) { continue; } else if (status.Status == LastWriteRequestStatus.EXECUTED) { return(BurnStatus.OK); } else { return(BurnStatus.COULD_NOT_COMPLETE_WAIT_OPERATION); } } else { return(BurnStatus.COULD_NOT_COMPLETE_WAIT_OPERATION); } } } else { return(BurnStatus.COULD_NOT_COMPLETE_WAIT_OPERATION); } } }