using Assets.StreetLight; using Assets.StreetLight.Scripts; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Unity.VisualScripting; using UnityEngine; namespace Assets { public class CalibrationRecorderBehavior : MonoBehaviour { PersonManager PersonManager => personManagerLazy.Value; Lazy personManagerLazy; string calibrationFileName; private void Awake() { personManagerLazy = new Lazy(FindObjectOfType); } BlockingCollection taskQueue; GameObject marker; void Start() { taskQueue = new BlockingCollection(); calibrationFileName = Path.Combine(Configuration.Instance.OutputCalibrationFilesDirectory, $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.csv"); if (!File.Exists(calibrationFileName)) { File.WriteAllLines(calibrationFileName, new string[] { "Time,WorldX,WorldY,WorldZ,UnityX,UnityY,UnityZ" }); } enabled = false; PersonManager.DetectionReady += PersonManager_DetectionReady; marker = GameObject.Find("CalibrationMarker"); var markerBehavior = FindObjectOfType(); markerBehavior.PathFinished += MarkerBehavior_PathFinished; } private void MarkerBehavior_PathFinished(object sender, EventArgs e) { enabled = false; } private void PersonManager_DetectionReady(object sender, EventArgs e) { enabled = true; } void Update() { var persons = PersonManager.Persons; if (PersonManager.Persons.Count == 1) { var person = PersonManager.Persons.Single(); var personPosition = person.WorldPosition; var markerPosition = marker.transform.position; taskQueue.Add(() => File.AppendAllLines(calibrationFileName, new string[] { FormattableString.Invariant($"{DateTime.Now:yyyy-MM-dd-HH-mm-ss-ff},{personPosition.x},{personPosition.y},{personPosition.z},{markerPosition.x},{markerPosition.y},{markerPosition.z}") })); Task.Run(() => taskQueue.Take().Invoke()); } } } }