public async Task <bool> ExecuteVendorCmdAsync(VendorCommand.Command command, int timeoutMs, int stepMS) { var policyResult = await Policy.Handle <Exception>().RetryAsync(3, onRetry: async(exception, retryCount) => { Debug.WriteLine($"ExecuteVendorCmdAsync error (retry {retryCount} : {exception.Message})"); await Task.Delay(2000); }).ExecuteAndCaptureAsync(async() => { WriteModuleMfrPassword(); if (VendorCommandStatus != VendorCommand.Status.Idle) { throw new Exception($"Cannot execute command 0x{command.Value:0X}, vendor status is not idle"); } WriteByte(VendorCommand.Page1, VendorCommand.Address, command.Value); // write the command var status = VendorCommandStatus; // read initial status Debug.WriteLine(status.Name); while (status == VendorCommand.Status.Idle || status == VendorCommand.Status.InProgress) { if (timeoutMs <= 0) { throw new Exception($"Timeout waiting for vendor command to complete: 0x{command.Value:0X}."); } timeoutMs -= stepMS; await Task.Delay(stepMS); status = VendorCommandStatus; Debug.WriteLine($"status {status.Value}: {status.Name}"); } WriteByte(VendorCommand.Page1, VendorCommand.StatusAddress, VendorCommand.Status.Idle.Value); // set status to idle if (status != VendorCommand.Status.Complete) { throw new Exception( $"Vendor command : 0x{command.Value:0X} failed with status 0x{status.Value:0X}."); } ClearModuleMfrPassword(); return(true); }); return(policyResult.Result); }
public async Task <bool> ExecuteVendorCmdAsync(VendorCommand.Command command, TimeSpan timeout, TimeSpan step) => await ExecuteVendorCmdAsync(command, (int)timeout.TotalMilliseconds, (int)step.TotalMilliseconds);
public async Task <bool> ExecuteVendorCmdAsync(VendorCommand.Command command) => await ExecuteVendorCmdAsync(command, DefaultTimeOutMS, DefaultStepMS);