12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- 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<PolarAccDataLog>
- {
- 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<PolarAccDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
- {
- throw new System.NotImplementedException(); //TODO
- }
- private void OnDestroy()
- {
- sub?.Dispose();
- }
- }
- }
|