/// <summary> /// The thread funktion to poll the telemetry data and send TelemetryUpdated events. /// </summary> private void Run() { Stopwatch sw = new Stopwatch(); sw.Start(); while (!isStopped) { try { // check if we are connected, otherwise try to connect if (!IsConnected) { IsConnected = Connect(); continue; } NetworkStream stream = tcpClient.GetStream(); byte[] reqData = NL2API.CreateRequest(RequestId++); stream.Write(reqData, 0, reqData.Length); var reader = new BinaryReader(stream, Encoding.GetEncoding("iso-8859-1")); byte[] resData = readMessage(reader); NL2API telemetryData = new NL2API(); telemetryData.ParseResponse(resData); if (!telemetryData.Paused && telemetryData.Onboard && telemetryData.InPlay) { IsRunning = true; } if (IsRunning) { sw.Restart(); TelemetryEventArgs args = new TelemetryEventArgs( new NL2TelemetryInfo(telemetryData)); RaiseEvent(OnTelemetryUpdate, args); } else if (sw.ElapsedMilliseconds > 500) { IsRunning = false; } Thread.Sleep(SamplePeriod); } catch (Exception e) { LogError("NoLimits2TelemetryProvider Exception while processing data", e); IsConnected = false; IsRunning = false; Thread.Sleep(1000); } } IsConnected = false; IsRunning = false; // Close the connection try { if (tcpClient != null) { tcpClient.Close(); } } catch (Exception) { } }
/// <summary> /// Read the data from the stream /// </summary> /// <param name="input"></param> /// <returns></returns> private byte[] readMessage(BinaryReader input) { int prefix = input.Read(); if (prefix != (int)'N') { if (prefix != -1) { throw new Exception("Invalid message received"); } else { throw new Exception("No data from server"); } } int b1 = input.Read(); if (b1 == -1) { throw new Exception("No data from server"); } int b2 = input.Read(); if (b2 == -1) { throw new Exception("No data from server"); } int b3 = input.Read(); if (b3 == -1) { throw new Exception("No data from server"); } int b4 = input.Read(); if (b4 == -1) { throw new Exception("No data from server"); } int b5 = input.Read(); if (b5 == -1) { throw new Exception("No data from server"); } int b6 = input.Read(); if (b6 == -1) { throw new Exception("No data from server"); } int b7 = input.Read(); if (b7 == -1) { throw new Exception("No data from server"); } int b8 = input.Read(); if (b8 == -1) { throw new Exception("No data from server"); } int extraSize = NL2API.decodeUShort16((byte)b7, (byte)b8); byte[] bytes = new byte[10 + extraSize]; bytes[0] = (byte)prefix; bytes[1] = (byte)b1; bytes[2] = (byte)b2; bytes[3] = (byte)b3; bytes[4] = (byte)b4; bytes[5] = (byte)b5; bytes[6] = (byte)b6; bytes[7] = (byte)b7; bytes[8] = (byte)b8; for (int i = 0; i < extraSize; ++i) { int b = input.Read(); if (b == -1) { throw new Exception("No data from server"); } bytes[9 + i] = (byte)b; } int postfix = input.Read(); if (postfix != (int)'L') { if (postfix != -1) { throw new Exception("Invalid message received"); } else { throw new Exception("No data from server"); } } bytes[9 + extraSize] = (byte)postfix; return(bytes); }
public NL2TelemetryInfo(NL2API telemetryData) { this.telemetryData = telemetryData; }