public byte[] Send(IDeviceMessage message) { Connect(); var str_message = message.ToString(); message_queue = new List <byte>(); try { byte[] buffer = message.GetSendBuffer(); OnMessageSent?.Invoke(message.ToString().Substring(2)); if (_stream != null) { _stream.Write(buffer, 0, buffer.Length); _stream.Flush(); var task = BeginReceiveTask(); if (!task.Wait(_settings.Timeout)) { throw new MessageException("Device did not respond within the timeout."); } return(message_queue.ToArray()); } else { throw new MessageException("Device not connected."); } } finally { Disconnect(); } }
public byte[] Send(IDeviceMessage message) { OnMessageSent?.Invoke(message.ToString()); try { string payload = Convert.ToBase64String(message.GetSendBuffer()); return(Task.Run(async() => { _client = HttpWebRequest.Create(string.Format("http://{0}:{1}?{2}", _settings.IpAddress, _settings.Port, payload)); var response = await _client.GetResponseAsync(); var buffer = new List <byte>(); using (var sr = new StreamReader(response.GetResponseStream())) { var rec_buffer = await sr.ReadToEndAsync(); foreach (char c in rec_buffer) { buffer.Add((byte)c); } } return buffer.ToArray(); }).Result); } catch (Exception exc) { throw new MessageException("Failed to send message. Check inner exception for more details.", exc); } }
public byte[] Send(IDeviceMessage message) { byte[] buffer = message.GetSendBuffer(); Connect(); try { for (int i = 0; i < 3; i++) { _stream.WriteAsync(buffer, 0, buffer.Length).Wait(); var rvalue = _stream.GetTerminalResponseAsync(); if (rvalue != null) { byte lrc = rvalue[rvalue.Length - 1]; // Should be the LRC if (lrc != TerminalUtilities.CalculateLRC(rvalue)) { SendControlCode(ControlCodes.NAK); } else { SendControlCode(ControlCodes.ACK); return(rvalue); } } } throw new MessageException("Terminal did not respond in the given timeout."); } catch (Exception exc) { throw new MessageException(exc.Message, exc); } finally { OnMessageSent?.Invoke(message.ToString()); Disconnect(); } }
public byte[] Send(IDeviceMessage message) { if (OnMessageSent != null) { OnMessageSent(message.ToString()); } try { string payload = Convert.ToBase64String(message.GetSendBuffer()); _client = HttpWebRequest.Create(string.Format("http://{0}:{1}?{2}", _settings.IpAddress, _settings.Port, payload)); var response = (HttpWebResponse)_client.GetResponse(); var buffer = new List <byte>(); using (var sr = new StreamReader(response.GetResponseStream())) { var rec_buffer = sr.ReadToEnd(); foreach (char c in rec_buffer) { buffer.Add((byte)c); } } return(buffer.ToArray()); } catch (Exception exc) { throw new HpsMessageException("Failed to send message. Check inner exception for more details.", exc); } }
public byte[] Send(IDeviceMessage message) { byte[] buffer = message.GetSendBuffer(); ControlCodes?code = null; OnControlCodeReceived += (rec_code) => { code = rec_code; _await.Set(); }; byte[] rvalue = null; OnMessageReceived += (rec_buffer) => { rvalue = rec_buffer; _await.Set(); }; for (int i = 0; i < 3; i++) { _stream.Write(buffer, 0, buffer.Length); if (OnMessageSent != null) { OnMessageSent(message.ToString()); } _await.WaitOne(1000); if (!code.HasValue) { throw new HpsMessageException("Terminal did not respond in the given timeout."); } if (code == ControlCodes.NAK) { continue; } else if (code == ControlCodes.EOT) { throw new HpsMessageException("Terminal returned EOT for the current message."); } else if (code == ControlCodes.ACK) { _await.WaitOne(_settings.TimeOut); break; } else { throw new HpsMessageException(string.Format("Unknown message received: {0}", code)); } } return(rvalue); }
public byte[] Send(IDeviceMessage message) { OnMessageSent?.Invoke(message.ToString()); try { return(Task.Run(async() => { GatewayResponse serviceResponse = await StageTransactionAsync(message); if (serviceResponse.StatusCode == HttpStatusCode.OK) { var root = ElementTree.Parse(serviceResponse.RawResponse).Get("CreateTransactionResponse"); var errors = root.GetAll("Message"); if (errors.Length > 0) { var sb = new StringBuilder(); foreach (var error in root.GetAll("Message")) { sb.AppendLine(error.GetValue <string>("Information")); } throw new MessageException(sb.ToString()); } string transportKey = root.GetValue <string>("TransportKey"); string validationKey = root.GetValue <string>("ValidationKey"); return await InitializeTransactionAsync(transportKey); } else { throw new MessageException(serviceResponse.StatusCode.ToString()); } }).Result); } catch (Exception exc) { throw new MessageException("Failed to send message. Check inner exception for more details.", exc); } }
public byte[] Send(IDeviceMessage message) { byte[] buffer = message.GetSendBuffer(); var readyReceived = false; byte[] responseMessage = null; Connect(); try { var task = _stream.WriteAsync(buffer, 0, buffer.Length); if (!task.Wait(_settings.Timeout)) { throw new MessageException("Terminal did not respond in the given timeout."); } do { var rvalue = _stream.GetTerminalResponseAsync(); if (rvalue != null) { var msgValue = GetResponseMessageType(rvalue); switch (msgValue) { case UpaMessageType.Ack: break; case UpaMessageType.Nak: break; case UpaMessageType.Ready: readyReceived = true; break; case UpaMessageType.Busy: break; case UpaMessageType.TimeOut: break; case UpaMessageType.Msg: responseMessage = TrimResponse(rvalue); if (IsNonReadyResponse(responseMessage)) { readyReceived = true; // since reboot doesn't return READY } SendAckMessageToDevice(); break; default: throw new Exception("Message field value is unknown in API response."); } } else { // Reset the connection before the next attempt Disconnect(); Connect(); } } while (!readyReceived); return(responseMessage); } catch (Exception exc) { throw new MessageException(exc.Message, exc); } finally { OnMessageSent?.Invoke(message.ToString()); Disconnect(); } }