using System; using System.Collections.Generic; using System.Globalization; using Sensors; using Sensors.Polar; using UniRx; using UnityEngine; namespace Logging { public readonly struct PolarAccDataLog { private readonly float timestamp; private readonly float accX; private readonly float accY; private readonly float accZ; public PolarAccDataLog(float timestamp, float accX, float accY, float accZ) { this.timestamp = timestamp; this.accX = accX; this.accY = accY; this.accZ = accZ; } public string[] Serialize() => new[] { timestamp.ToString("F4", CultureInfo.InvariantCulture), accX.ToString("F4", CultureInfo.InvariantCulture), accY.ToString("F4", CultureInfo.InvariantCulture), accZ.ToString("F4", CultureInfo.InvariantCulture), }; } public class PolarAccDataLogger : SensorDataLogger { public override string Key => "polar_acc_data"; private long startTimeAcc = -1; private TimeSync timeSync; private IDisposable sub; public override void Start() { base.Start(); timeSync = new TimeSync { StartTime = DateTime.Now, DifDataStreamStart = -1 }; var bikeSensorData = BikeSensorData.Instance; sub = bikeSensorData.RawAccData?.Subscribe(data => OnData(data)); } private void OnData(in AccData data) { if (timeSync.DifDataStreamStart < 0) { timeSync.DifDataStreamStart = (long) (DateTime.Now - timeSync.StartTime).TotalMilliseconds; } if (startTimeAcc < 0) { startTimeAcc = data.Timestamp; } if (BikeSensorData.Instance.PolarConfig == null) return; var internalTimestamp = (data.Timestamp - startTimeAcc) / 1000000; var timestamp = internalTimestamp + timeSync.DifDataStreamStart; foreach (var item in data.Values) { Log(new PolarAccDataLog(timestamp, item.x, item.y, item.z)); timestamp += 1000 / BikeSensorData.Instance.PolarConfig.Value.accSampleRate; } } public override void Log(PolarAccDataLog value) { Buffer.Add(value.Serialize()); } public override IEnumerable ReadLog(IEnumerable> lines) { throw new System.NotImplementedException(); //TODO } private void OnDestroy() { sub?.Dispose(); } } }