public static async Task RunSpecialStatusMessageAsync(IChannelWrapper channel, ClientOptions options) { var client = CreateClient <TestService.TestServiceClient>(channel); var echoStatus = new EchoStatus { Code = 2, Message = "\t\ntest with whitespace\r\nand Unicode BMP ☺ and non-BMP 😈\t\n" }; try { await client.UnaryCallAsync(new SimpleRequest { ResponseStatus = echoStatus }); Assert.Fail(); } catch (RpcException e) { Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); Assert.AreEqual(echoStatus.Message, e.Status.Detail); } }
public EchoStatus TrySendReceive(byte[] message, bool retryOnNak, byte receiveMessageId, InsteonMessageType?receiveMessageType, out Dictionary <PropertyKey, int> properties) { properties = null; WaitItem item = new WaitItem(receiveMessageId, receiveMessageType); lock (waitList) waitList.Add(item); EchoStatus status = TrySend(message, retryOnNak); if (status == EchoStatus.ACK) { if (item.Message == null) { item.MessageEvent.WaitOne(Constants.sendReceiveTimeout); } if (item.Message != null) { properties = item.Message.Properties; } else { logger.ErrorFormat("Did not receive expected message reply; SentMessage='{0}', ExpectedReceiveMessageId={1:X2}, Timeout={2}ms", Utilities.ByteArrayToString(message), receiveMessageId, Constants.sendReceiveTimeout); } } lock (waitList) waitList.Remove(item); return(status); }
private static async Task RunSpecialStatusMessageAsync(TestService.TestServiceClient client) { Console.WriteLine("running special_status_message"); var echoStatus = new EchoStatus { Code = 2, Message = "\t\ntest with whitespace\r\nand Unicode BMP ☺ and non-BMP 😈\t\n" }; try { await client.UnaryCallAsync(new SimpleRequest { ResponseStatus = echoStatus }); Assert.Fail(); } catch (RpcException e) { Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); Assert.AreEqual(echoStatus.Message, e.Status.Detail); } Console.WriteLine("Passed!"); }
private static void EnsureEchoStatus(EchoStatus responseStatus, ServerCallContext context) { if (responseStatus != null) { var statusCode = (StatusCode)responseStatus.Code; context.Status = new Status(statusCode, responseStatus.Message); } }
private bool TryGetLinksInternal(out InsteonDeviceLinkRecord[] links) { links = null; List <InsteonDeviceLinkRecord> list = new List <InsteonDeviceLinkRecord>(); Dictionary <PropertyKey, int> properties; EchoStatus status = EchoStatus.None; Log.WriteLine("Controller {0} GetLinks", Address.ToString()); byte[] message1 = { 0x69 }; status = network.Messenger.TrySendReceive(message1, false, 0x57, out properties); if (status == EchoStatus.NAK) { links = new InsteonDeviceLinkRecord[0]; // empty link table Log.WriteLine("Controller {0} GetLinks returned no links, empty link table", Address.ToString()); return(true); } else if (status == EchoStatus.ACK) { if (properties == null) { Log.WriteLine("ERROR: Controller {0} null properties object", Address.ToString()); return(false); } list.Add(new InsteonDeviceLinkRecord(properties)); } else { return(false); // echo was not ACK or NAK } Log.WriteLine("Controller {0} GetLinks", Address.ToString()); byte[] message2 = { 0x6A }; status = network.Messenger.TrySendReceive(message2, false, 0x57, out properties); while (status == EchoStatus.ACK) { if (properties == null) { Log.WriteLine("ERROR: Controller {0} null properties object", Address.ToString()); return(false); } list.Add(new InsteonDeviceLinkRecord(properties)); status = network.Messenger.TrySendReceive(message2, false, 0x57, out properties); } if (status != EchoStatus.NAK) { return(false); // echo was not ACK or NAK } links = list.ToArray(); Log.WriteLine("Controller {0} GetLinks returned {1} links", Address.ToString(), links.Length); return(true); }
public EchoStatus TrySendEchoCommand(byte[] message, bool retryOnNak, int echoLength, out Dictionary <PropertyKey, int> properties) { echoMessage = null; echoCommand = true; EchoStatus status = TrySend(message, retryOnNak, echoLength); echoCommand = false; properties = echoMessage?.Properties; echoMessage = null; return(status); }
public static async Task RunStatusCodeAndMessageAsync(IChannelWrapper channel, ClientOptions options) { var client = CreateClient <TestService.TestServiceClient>(channel); var echoStatus = new EchoStatus { Code = 2, Message = "test status message" }; { // step 1: test unary call var request = new SimpleRequest { ResponseStatus = echoStatus }; var e = await ExceptionAssert.ThrowsAsync <RpcException>(async() => await client.UnaryCallAsync(request)); Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); Assert.AreEqual(echoStatus.Message, e.Status.Detail); } // We want to test a unary call in gRPC-Web but skip the unsupported full duplex call. #if !NETSTANDARD2_1 { // step 2: test full duplex call var request = new StreamingOutputCallRequest { ResponseStatus = echoStatus }; var call = client.FullDuplexCall(); await call.RequestStream.WriteAsync(request); await call.RequestStream.CompleteAsync(); try { // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock. await call.ResponseStream.ToListAsync(); Assert.Fail(); } catch (RpcException e) { Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); Assert.AreEqual(echoStatus.Message, e.Status.Detail); } } #endif }
public EchoStatus Send(byte[] message, bool retryOnNak, int echoLength) { if (port == null) { throw new InvalidOperationException(); } port.SetNotify(null); EchoStatus status = EchoStatus.None; try { ProcessData(); // process any pending data before sending a new command var input = new byte[message.Length + 1]; input[0] = Constants.MessageStartByte; message.CopyTo(input, 1); int retry = -1; while (retry++ < Constants.sendMessageRetries) { if (retry <= 0) { logger.DebugFormat("TX: {0}", Utilities.ByteArrayToString(input)); } else { Thread.Sleep(retry * Constants.sendMessageWaitTime); logger.DebugFormat("TX: {0} - RETRY {1} of {2}", Utilities.ByteArrayToString(input), retry, Constants.sendMessageRetries); } port.Write(input); status = ProcessEcho(echoLength + 2); // +1 for leading 02 byte, +1 for trailing ACK/NAK byte if (status == EchoStatus.ACK) { return(status); } if (status == EchoStatus.NAK && !retryOnNak) { return(status); } } logger.DebugFormat("Send failed after {0} retries", Constants.sendMessageRetries); return(status); } finally { port.SetNotify(DataAvailable); } }
public static async Task RunStatusCodeAndMessageAsync(TestService.TestServiceClient client) { Console.WriteLine("running status_code_and_message"); var echoStatus = new EchoStatus { Code = 2, Message = "test status message" }; { // step 1: test unary call var request = new SimpleRequest { ResponseStatus = echoStatus }; var e = Assert.Throws <RpcException>(() => client.UnaryCall(request)); Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); Assert.AreEqual(echoStatus.Message, e.Status.Detail); } { // step 2: test full duplex call var request = new StreamingOutputCallRequest { ResponseStatus = echoStatus }; var call = client.FullDuplexCall(); await call.RequestStream.WriteAsync(request); await call.RequestStream.CompleteAsync(); try { // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock. await call.ResponseStream.ToListAsync(); Assert.Fail(); } catch (RpcException e) { Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); Assert.AreEqual(echoStatus.Message, e.Status.Detail); } } Console.WriteLine("Passed!"); }
private bool TryCommandInternal(InsteonDeviceCommands command, byte value) { byte[] message = GetStandardMessage(Address, (byte)command, value); Log.WriteLine("Device {0} Command(command:{1}, value:{2:X2})", Address.ToString(), command.ToString(), value); EchoStatus status = network.Messenger.TrySend(message); if (status == EchoStatus.ACK) { ackTimer.Change(Constants.deviceAckTimeout, Timeout.Infinite); // start ACK timeout timer return(true); } else { ClearPendingCommand(); return(false); } }
public static void Echo(string text, EchoStatus status = EchoStatus.Undefined) { if (status == EchoStatus.Undefined || status == EchoStatus.Ordinary) { Output.WriteLine(text); } else { ConsoleColor oldColor = Output.TextColor; ConsoleColor newColor; switch (status) { case EchoStatus.Information: newColor = ConsoleColor.Green; break; case EchoStatus.Attention: newColor = ConsoleColor.Yellow; break; case EchoStatus.Important: case EchoStatus.VeryImportant: newColor = ConsoleColor.Red; break; default: newColor = oldColor; break; } try { Output.TextColor = newColor; Output.WriteLine(text); } finally { Output.TextColor = oldColor; } } }
public bool VerifyConnection() { if (!bridge.IsConnected) { return(false); } byte[] message = { 0x60 }; Dictionary <PropertyKey, int> properties; EchoStatus status = TrySendEchoCommand(message, true, 7, out properties); if (status == EchoStatus.ACK || status == EchoStatus.NAK) { return(true); } Log.WriteLine("ERROR: Verify connection failed"); network.OnDisconnected(); return(false); }
public bool VerifyConnection() { if (!bridge.IsConnected) { return(false); } byte[] message = { (byte)InsteonModemSerialCommand.GetImInfo }; Dictionary <PropertyKey, int> properties; EchoStatus status = TrySendEchoCommand(message, true, 7, out properties); if (status == EchoStatus.ACK || status == EchoStatus.NAK) { return(true); } logger.ErrorFormat("Verify connection failed"); network.OnDisconnected(); return(false); }
public EchoStatus TrySend(byte[] message, bool retryOnNak, int echoLength) { EchoStatus status = EchoStatus.None; lock (bridge) { sentMessage = message; try { status = bridge.Send(message, retryOnNak, echoLength); } catch (InvalidOperationException) { logger.ErrorFormat("Bridge send command fatal error"); } catch (IOException) { logger.ErrorFormat("Bridge send command fatal error"); } catch (Exception ex) { logger.ErrorFormat("Unexpected failure... {0}", ex.Message); if (Debugger.IsAttached) { throw; } } finally { sentMessage = null; } } if (status == EchoStatus.None) { logger.ErrorFormat("No response from serial port"); network.OnDisconnected(); } return(status); }
/// <summary> /// Gets a value that indicates the on-level of the device. /// </summary> /// <returns> /// A value indicating the on-level of the device. For a dimmer a value between 0 and 255 will be returned. For a non-dimmer a value 0 or 255 will be returned. /// </returns> /// <remarks> /// This is a blocking method that sends an INSTEON message to the target device and waits for a reply, or until the device command times out. /// </remarks> public bool TryGetOnLevel(out byte value) { InsteonDeviceCommands command = InsteonDeviceCommands.StatusRequest; WaitAndSetPendingCommand(command, 0); Log.WriteLine("Device {0} GetOnLevel", Address.ToString()); byte[] message = GetStandardMessage(Address, (byte)command, 0); Dictionary <PropertyKey, int> properties; EchoStatus status = network.Messenger.TrySendReceive(message, true, 0x50, out properties); // on-level returned in cmd2 of ACK if (status == EchoStatus.ACK && properties != null) { value = (byte)properties[PropertyKey.Cmd2]; Log.WriteLine("Device {0} GetOnLevel returning {1:X2}", Address.ToString(), value); return(true); } else { ClearPendingCommand(); value = 0; return(false); } }
public void Echo(string str, EchoStatus status = EchoStatus.Undefined) { if (!_isContentEchoed) { if (!IsHeaderWritten("Content-type")) { Header("Content-type", "text/html"); } if (!IsHeaderWritten("Content-encoding")) { Header("Content-encoding", Encoding.BodyName); } oscript.Output.WriteLine(); _isContentEchoed = true; } if (str != "") { Output(str); oscript.Output.WriteLine(); } }
public static async Task RunStatusCodeAndMessageAsync(TestService.TestServiceClient client) { Console.WriteLine("running status_code_and_message"); var echoStatus = new EchoStatus { Code = 2, Message = "test status message" }; { // step 1: test unary call var request = new SimpleRequest { ResponseStatus = echoStatus }; var e = Assert.Throws<RpcException>(() => client.UnaryCall(request)); Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); Assert.AreEqual(echoStatus.Message, e.Status.Detail); } { // step 2: test full duplex call var request = new StreamingOutputCallRequest { ResponseStatus = echoStatus }; var call = client.FullDuplexCall(); await call.RequestStream.WriteAsync(request); await call.RequestStream.CompleteAsync(); try { // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock. await call.ResponseStream.ToListAsync(); Assert.Fail(); } catch (RpcException e) { Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); Assert.AreEqual(echoStatus.Message, e.Status.Detail); } } Console.WriteLine("Passed!"); }
public void Echo(string str, EchoStatus status = EchoStatus.Undefined) { _output.AppendText(str + '\n'); _output.ScrollToEnd(); System.Windows.Forms.Application.DoEvents(); }
void InsteonNetworkBridge.IMessageProcessor.SetEchoStatus(EchoStatus status) { }
public void Echo(string text, EchoStatus status = EchoStatus.Undefined) { ConsoleHostImpl.Echo(text, status); }
void IMessageProcessor.SetEchoStatus(EchoStatus status) { }