public async Task <bool> SetCiscoIpRegAsync(CyclopsRegisters reg, byte data, CancellationToken cancellationToken = default) { // return await SetCiscoIpRegAsync(reg, new[] { data }, cancellationToken); if (reg.Register.Size != 1) { throw new ArgumentOutOfRangeException(reg.Name, $"requires {reg.Register.Size} bytes while data is 1 byte."); } var policyWrapResults = await _policyWrap.ExecuteAndCaptureAsync(async (context, ct) => { ct.ThrowIfCancellationRequested(); SetVendorCommandParameters(reg.Register.Page, reg.Register.Address, reg.Register.Size); WriteByte(VendorCommand.Page2, VendorCommand.Buffer.Address, data); var success = await ExecuteVendorCmdAsync(VendorCommand.Command.CiscoCocoaWriteCipPage, 1000, 1000); if (!success) { throw new Exception("Failed to execute Vendor Command CiscoCocoaWriteCipPage"); } return(true); }, new Dictionary <string, object>() { { $"{GetCaller()}", $"{reg.Name}({reg.Page}.{reg.Address}:{data})" } }, cancellationToken : cancellationToken); if (policyWrapResults.Outcome == OutcomeType.Failure) { throw policyWrapResults.FinalException; } return(policyWrapResults.Outcome == OutcomeType.Successful); }
//=========================== CYCLOPS public async Task <int> GetRegAsync(CyclopsRegisters reg, CancellationToken cancellationToken = default) { var policyWrapResults = await _policyWrap.ExecuteAndCaptureAsync(async (context, ct) => { ct.ThrowIfCancellationRequested(); SetVendorCommandParameters(reg.Register); var executeVendorCmdResult = await ExecuteVendorCmdAsync( VendorCommand.Command.CiscoCocoaReadCipPage, DefaultTimeOutMS, DefaultStepMS).ConfigureAwait(false); if (!executeVendorCmdResult) { throw new Exception("Failed to execute Vendor Command CiscoCocoaReadCipPage"); } var data = await ReadAsync(VendorCommand.Page2, VendorCommand.Buffer.Address, reg.Register.Size) .ConfigureAwait(false); var res = ConvertData(reg, data); return((int)res); }, new Dictionary <string, object>() { { $"{GetCaller()}", $"{reg.Name}({reg.Page}.{reg.Address})" } }, cancellationToken : cancellationToken); if (policyWrapResults.Outcome == OutcomeType.Failure) { throw policyWrapResults.FinalException; } return(policyWrapResults.Result); }
//public async Task<bool> SetCiscoIpRegAsync(CyclopsRegisters reg, byte[] data, CancellationToken cancellationToken = default) //{ // var policyResult = await Policy.Handle<Exception>().RetryAsync(3, onRetry: async (exception, retryCount) => // { // Debug.WriteLine($"ReadReg error ({reg.Name} : retry {retryCount} : {exception.Message})"); // await Task.Delay(2000); // }).ExecuteAndCaptureAsync(async () => // { // SetVendorCommandParameters(reg.Register.Page, reg.Register.Address, reg.Register.Size); // WriteByte(VendorCommand.Page2, VendorCommand.Buffer.Address, data[0]); // var success = await ExecuteVendorCmdAsync(VendorCommand.Command.CiscoCocoaWriteCipPage, 1000, 1000); // if (!success) throw new Exception("Failed to execute Vendor Command CiscoCocoaWriteCipPage"); // return true; // }); // return policyResult.Result; //} public async Task <double> GetValueAsync(CyclopsRegisters reg, CancellationToken cancellationToken = default) { var result = await GetRegAsync(reg, cancellationToken).ConfigureAwait(false); if (reg.Register.Type.Equals(DataType.Dec) | reg.Register.Type.Equals(DataType.DecWord)) { return(result * reg.Register.Scale); } throw new ArgumentException($"Register {reg.Name} with data type : {reg.Type} does not support scaling."); //if ((Math.Abs(reg.Register.Scale) < 1.0E-10) | reg.Register.Type.Equals(DataType.HexWord) | // reg.Register.Type.Equals(DataType.Int32) | reg.Register.Type.Equals(DataType.Binary)) //{ // // var r = await Task.Run(() => readReg(reg, timeOut)); // var r = await GetRegAsync(reg, cancellationToken); // return r; //} //// var s = await Task.Run(() => readReg(reg, timeOut)); //var s = await GetRegAsync(reg, cancellationToken); //var ss = s * reg.Register.Scale; //return ss; }