12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- 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<PersonManager> personManagerLazy;
- string calibrationFileName;
- private void Awake()
- {
- personManagerLazy = new Lazy<PersonManager>(FindObjectOfType<PersonManager>);
- }
- BlockingCollection<Action> taskQueue;
- GameObject marker;
- void Start()
- {
- taskQueue = new BlockingCollection<Action>();
- calibrationFileName = Path.Combine(Configuration.Instance.OutputCalibrationFilesDirectory, $"{DateTime.Now:yyyy-dd-M_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<CalibrationMarkerBehavior>();
- 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());
- }
- }
- }
- }
|