public byte[] ToByteArray() { WTAPI packet = this; int num = Marshal.SizeOf <WTAPI>(packet); byte[] array = new byte[num]; IntPtr intPtr = Marshal.AllocHGlobal(num); Marshal.StructureToPtr <WTAPI>(packet, intPtr, false); Marshal.Copy(intPtr, array, 0, num); Marshal.FreeHGlobal(intPtr); return(array); }
public WTTelemetryInfo(WTAPI telemetryData, WTAPI lastTelemetryData) { _telemetryData = telemetryData; _lastTelemetryData = lastTelemetryData; }
/// <summary> /// The thread funktion to poll the telemetry data and send TelemetryUpdated events. /// </summary> private void Run() { WTAPI lastTelemetryData = new WTAPI(); Session session = new Session(); WebClient wc = new WebClient(); Stopwatch sw = new Stopwatch(); Stopwatch rollSmoothSW = new Stopwatch(); sw.Start(); rollSmoothSW.Start(); while (!isStopped) { try { string json = wc.DownloadString(indicatorsURL); Dictionary <string, object> indicators = JsonConvert.DeserializeObject <Dictionary <string, object> >(json); // json = wc.DownloadString(stateURL); // Dictionary<string, object> state = JsonConvert.DeserializeObject<Dictionary<string, object>>(json); float deg2rad = (float)Math.PI / 180.0f; bool valid = (bool)indicators["valid"]; if (valid) { float pitch = (float)(double)indicators["aviahorizon_pitch"]; float roll = (float)(double)indicators["aviahorizon_roll"]; float yaw = (float)(double)indicators["compass"] * deg2rad; float speed = (float)(double)indicators["speed"]; telemetryData.Pitch = pitch; float absRoll = Math.Abs(roll); if (absRoll > 130.0f) { rollSmoothSW.Restart(); } float smoothTime = 300.0f; if (rollSmoothSW.ElapsedMilliseconds > smoothTime) { telemetryData.Roll = LoopAngle(roll, 90.0f); } else { float minLerp = 0.0001f; float maxLerp = 1.0f; float lerp = minLerp + ((rollSmoothSW.ElapsedMilliseconds / smoothTime) * (maxLerp - minLerp)); lerp = 0.02f; telemetryData.Roll = Lerp(telemetryData.Roll, LoopAngle(roll, 90.0f), lerp); } pitch *= deg2rad; roll *= deg2rad; Vector3 fwd = new Vector3( (float)Math.Cos(yaw) * (float)Math.Cos(pitch), (float)Math.Sin(yaw) * (float)Math.Cos(pitch), (float)Math.Sin(pitch)); fwd = Vector3.Normalize(fwd); float temp = fwd.Y; fwd.Y = fwd.Z; fwd.Z = temp; Vector3 velocity = fwd * speed; velocity = Vector3.Lerp(lastVelocity, velocity, 0.5f); Vector3 acceleration = velocity - lastVelocity; lastVelocity = velocity; Quaternion orientationQ = Quaternion.CreateFromAxisAngle(fwd, roll); Matrix4x4 orientation = Matrix4x4.CreateFromQuaternion(orientationQ); Matrix4x4 orientationInv = new Matrix4x4(); Matrix4x4.Invert(orientation, out orientationInv); Vector3 localAcceleration = Vector3.Transform(acceleration, orientationInv); telemetryData.AccG[0] = acceleration.X; telemetryData.AccG[1] = acceleration.Y; telemetryData.AccG[2] = acceleration.Z; // otherwise we are connected IsConnected = true; IsRunning = true; sw.Restart(); TelemetryEventArgs args = new TelemetryEventArgs( new WTTelemetryInfo(telemetryData, lastTelemetryData)); RaiseEvent(OnTelemetryUpdate, args); lastTelemetryData = telemetryData; } else if (sw.ElapsedMilliseconds > 500) { IsRunning = false; } } catch (Exception e) { LogError("WarThunderTelemetryProvider Exception while processing data", e); IsConnected = false; IsRunning = false; Thread.Sleep(1000); } } IsConnected = false; IsRunning = false; }