public object Get(double frame, string key) { // Get a value from logframe in particular frame and key if (Samples.ContainsKey(frame)) { TelemetrySample sample = Samples[frame]; if (ParameterCache.ContainsKey(key)) { if (sample.Data.ContainsKey(ParameterCache[key].id) && sample.Data[ParameterCache[key].id].ContainsKey(ParameterCache[key].parameter)) { return(sample.Data[ParameterCache[key].id][ParameterCache[key].parameter]); } else { return(0.0); } } else { string[] saKey = key.Split(".".ToCharArray()); string cls = saKey[0]; string obj = saKey[1]; // Look up the instance ID List <int> ids = Instances.GetKeysByValue(cls); if (ids.Count >= 1) { List <int> parameters = Properties[cls].GetKeysByValue(obj); if (parameters.Count >= 1) { ParameterLookUp plk = new ParameterLookUp { id = ids[0], parameter = parameters[0] }; ParameterCache.Add(key, plk); return(sample.Data[ids[0]][parameters[0]]); } else { return(0.0); } } else { return(0.0); } } } else { return(0); } }
/**** PLOTTER FUNCTIONS ****/ void GraphFill() { double timeMin = 100000, timeMax = 0; if (_logReader != null) { foreach (PlotterGraph graph in cPlotter.Graphs) { foreach (PlotterCurves curve in graph.Curves) { curve.Data.Clear(); } } lock (_logReader.Samples) { //double px = Double.MinValue, py= Double.MinValue, pz = Double.MinValue, pt = -1; //Triton.Maths.Filter spdFilter = new Triton.Maths.Filter(25); foreach (KeyValuePair <double, TelemetrySample> sp in _logReader.Samples) { try { timeMin = Math.Min(sp.Key / 1000.0, timeMin); timeMax = Math.Max(sp.Key / 1000.0, timeMax); TelemetrySample sample = sp.Value; // TODO: Make this work from the configuration data (or files) cPlotter.Graphs[0].Curves[0].Data.Add(sample.Time / 1000.0, _logReader.GetDouble(sample, "Driver.RPM")); cPlotter.Graphs[1].Curves[0].Data.Add(sample.Time / 1000.0, _logReader.GetDouble(sample, "Driver.Speed") * 3.6); cPlotter.Graphs[2].Curves[0].Data.Add(sample.Time / 1000.0, _logReader.GetDouble(sample, "Player.Pedals_Throttle") * 100); cPlotter.Graphs[2].Curves[1].Data.Add(sample.Time / 1000.0, _logReader.GetDouble(sample, "Player.Pedals_Brake") * 100); /*double x = (double)sample.Data[3][7]; * double y = (double)sample.Data[3][8]; * double z = (double)sample.Data[3][9]; * if (pt > 0) * { * double dx = x - px, dy = y - py, dz = z - pz, spd = 0; * dx /= (sample.Time - pt) / 1000; * dy /= (sample.Time - pt) / 1000; * dz /= (sample.Time - pt) / 1000; * if(Math.Abs(dx)>200) dx = 0; * if(Math.Abs(dy)>200) dy = 0; * if(Math.Abs(dz)>200) dz = 0; * //cPlotter.Graphs[2].Curves[0].Data.Add(sample.Time / 1000.0, (double)dx); * //cPlotter.Graphs[2].Curves[1].Data.Add(sample.Time / 1000.0, (double)dz); * spd = Math.Sqrt(dx*dx+dy*dy+dz*dz); * spdFilter.Add(spd); * cPlotter.Graphs[2].Curves[2].Data.Add(sample.Time / 1000.0, spdFilter.Average*3.6); * } * * pt = sample.Time; * px = x; * py = y; * pz = z;*/ //cPlotter.Graphs[2].Curves[0].Data.Add(sample.Time / 1000.0, (double)sample.Data[3][10] * 100); //cPlotter.Graphs[2].Curves[1].Data.Add(sample.Time / 1000.0, (double)sample.Data[3][11] * 100); } catch (Exception ex) { Debug.WriteLine("Data conversion in TelemetryViewer error!"); break; } } } } _timeLineStart = timeMin; _timeLineEnd = timeMax; DrawPlotbounds(); }
public double GetDouble(TelemetrySample frame, string key) { return(Convert.ToDouble(Get(frame.Time, key))); }
public string GetString(TelemetrySample frame, string key) { return(Get(frame.Time, key).ToString()); }
private void ReadWorker(object n) { lock (this.Data) { _DataPointer = 0; _ReadStage = 1; for (int i = 0; i < data.Length - 8; i++) { if (data[i] == '$' && data[i + 1] == '#') { ushort PacketID = BitConverter.ToUInt16(data, i + 2); ushort Type = BitConverter.ToUInt16(data, i + 4); ushort InstanceID = BitConverter.ToUInt16(data, i + 6); ushort PacketSize = BitConverter.ToUInt16(data, i + 8); byte[] PacketData = new byte[PacketSize]; ByteMethods.memcpy(PacketData, data, PacketSize, 0, i + 10); TelemetryPacket packet = new TelemetryPacket { Data = PacketData, ID = PacketID, Type = (TelemetryLogPacket)Type, InstanceID = InstanceID, Size = PacketSize }; Packets.Add(packet); _DataPointer = i + PacketSize; i += PacketSize; } } _DataPointer = data.Length; _ReadStage = 2; // parse packets TelemetrySample Sample = new TelemetrySample(); Instances = new Dictionary <int, string>(); Properties = new Dictionary <string, Dictionary <int, string> >(); Types = new Dictionary <string, Dictionary <int, int> >(); string last_type = ""; int last_type_id = 0; bool first_packet = true; double time = -1.0; foreach (TelemetryPacket packet in Packets) { _PacketPointer++; switch (packet.Type) { case TelemetryLogPacket.InstanceName: last_type = ASCIIEncoding.ASCII.GetString(packet.Data).Replace('\0', ' ').Trim(); last_type_id = packet.InstanceID; Instances.Add(last_type_id, last_type); Types.Add(last_type, new Dictionary <int, int>()); Properties.Add(last_type, new Dictionary <int, string>()); break; case TelemetryLogPacket.InstanceMember: int id = BitConverter.ToInt16(packet.Data, 2); if (!Sample.Data.ContainsKey(last_type_id)) { Sample.Data.Add(last_type_id, new Dictionary <int, object>()); } if (Sample.Data[last_type_id].ContainsKey(id) == false) { Sample.Data[last_type_id].Add(id, 0); } int type_id = BitConverter.ToInt16(packet.Data, 0); if (Types[last_type].ContainsKey(id) == false) { Types[last_type].Add(id, type_id); byte[] member_name = new byte[packet.Data.Length - 4]; ByteMethods.memcpy(member_name, packet.Data, packet.Data.Length - 4, 0, 4); string key = ASCIIEncoding.ASCII.GetString(member_name).Replace('\0', ' ').Trim(); Properties[last_type].Add(id, key); } break; case TelemetryLogPacket.Data: int instance_id = BitConverter.ToInt16(packet.Data, 0); int last_typeid = 0; for (int i = 2; i < packet.Data.Length;) { int id_type = BitConverter.ToInt32(packet.Data, i); object v = 0; Dictionary <int, int> TypeArray = Types[Instances[instance_id]]; i += 4; if (TypeArray.ContainsKey(id_type)) { switch (TypeArray[id_type]) { case 0: //double v = BitConverter.ToDouble(packet.Data, i); i += 8; break; case 1: //float v = BitConverter.ToSingle(packet.Data, i); i += 4; break; case 2: // int32 v = BitConverter.ToInt32(packet.Data, i); i += 4; break; case 3: // int16 v = BitConverter.ToInt16(packet.Data, i); i += 2; break; case 4: // int8 v = packet.Data[i]; i += 1; break; case 5: // uint32 v = BitConverter.ToUInt32(packet.Data, i); i += 4; break; case 6: // uint16 v = BitConverter.ToUInt16(packet.Data, i); i += 2; break; case 7: // uint8 v = packet.Data[i]; i += 1; break; case 8: // string int k = i; for (; k < packet.Data.Length; k++) { if (packet.Data[k] == 0) { break; } } v = ASCIIEncoding.ASCII.GetString(packet.Data, i, k - i); i = k + 1; break; case 9: // bool v = ((packet.Data[i] == 1) ? true : false); i += 1; break; default: v = 0; break; } Sample.Data[instance_id][id_type] = (object)v; last_typeid = id_type; } } break; case TelemetryLogPacket.Time: // Store previous samples. if (time != -1.0 && this.Data.ContainsKey(time) == false) { this.Data.Add(time, Sample.Clone()); this.Data[time].Time = time; } time = BitConverter.ToDouble(packet.Data, 0); break; } } // Store last sample. if (time != -1.0 && this.Data.ContainsKey(time) == false) { this.Data.Add(time, Sample.Clone()); this.Data[time].Time = time; } _ReadStage = 3; // Set the telemetry track. if (Telemetry.m.Active_Session) { return; } else if (Telemetry.m.Sims != null) { // TODO: Urgent; add simulator type to log file. Telemetry.m.Track_Load(Telemetry.m.Sims.Get("rfactor"), GetString(time, "Session.CircuitName")); } } }