private KartKraftData parseResponse(KartKraft.Frame resp) { KartKraftData outData = new KartKraftData(); try { outData.AccelerationX = resp.Motion.Value.AccelerationX; outData.AccelerationY = resp.Motion.Value.AccelerationY; outData.AccelerationZ = resp.Motion.Value.AccelerationZ; outData.Pitch = resp.Motion.Value.Pitch; outData.Roll = resp.Motion.Value.Roll; outData.Yaw = resp.Motion.Value.Yaw; outData.RPM = resp.Dash.Value.Rpm; outData.Speed = resp.Dash.Value.Speed; }catch (Exception e) { // sometimes the response object can be null, so we catch them here. // LogError(e.ToString()); // nothing wo log, only null values } return(outData); }
/// <summary> /// Connect to the TCP Server of No Limits 2 /// </summary> private void Run() { //TODO KartKraftData lastTelemetryData = new KartKraftData(); Stopwatch sw = new Stopwatch(); sw.Start(); UdpClient socket = new UdpClient(); socket.ExclusiveAddressUse = false; socket.Client.Bind(new IPEndPoint(IPAddress.Any, PORTNUM)); Log("Listener thread started (IP: " + IP + ":" + PORTNUM.ToString() + ") DCSTelemetryProvider.Thread"); while (!isStopped) { try { // get data from game, if (socket.Available == 0) { if (sw.ElapsedMilliseconds > 500) { IsRunning = false; IsConnected = false; //Thread.Sleep(1000); } else { Thread.Sleep(1); } continue; } else { IsConnected = true; } // DCS approach.... // Byte[] received = socket.Receive(ref _senderIP); // string resp = Encoding.UTF8.GetString(received); //LogDebug(resp); //KK Approach Byte[] received = socket.Receive(ref _senderIP); ByteBuffer b = new ByteBuffer(received); KartKraft.Frame frame = KartKraft.Frame.GetRootAsFrame(b); KartKraftData telemetryData = parseResponse(frame); IsRunning = true; //TODO TelemetryEventArgs args = new TelemetryEventArgs( new KartKraftTelemetryInfo(telemetryData, lastTelemetryData)); RaiseEvent(OnTelemetryUpdate, args); lastTelemetryData = telemetryData; sw.Restart(); //Thread.Sleep(SamplePeriod); } catch (Exception e) { LogError(e.ToString()); IsConnected = false; IsRunning = false; Thread.Sleep(1000); } } IsConnected = false; IsRunning = false; try { socket.Close(); } catch (Exception) { } Log("Listener thread stopped, KartKraftTelemetryProvider.Thread"); }