T-DEV-811/AR/Assets/TrashDataExtractor.cs

76 lines
2.2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Linq;
using models;
using TMPro;
using UnityEngine;
public enum TrashType
{
Temperature, // T/H
Distance, // D
Poids // W
}
public class TrashDataExtractor : MonoBehaviour
{
[SerializeField] public TrashType type = TrashType.Distance;
[SerializeField] public TextMeshPro valueText;
[SerializeField] public TextMeshPro dateText;
[SerializeField] public TextMeshPro trashText;
[SerializeField] public float timeBetweenUpdate = 2f;
private float currentCountdown;
void Start()
{
this.currentCountdown = this.timeBetweenUpdate;
}
void UpdateText()
{
String unit = "T/H";
if (this.type == TrashType.Distance)
{
unit = "D";
} else if (this.type == TrashType.Poids)
{
unit = "W";
}
if (PocketBaseDataProvider.trashes.Count <= 0 || PocketBaseDataProvider.trashes[0].data.Count <= 0)
{
return;
}
var trashesFiltered = PocketBaseDataProvider.trashes.Where((trash => trash.data.Count > 0 && trash.data[0].unit == unit)).ToList();
if (trashesFiltered.Count <= 0 || trashesFiltered[0].data.Count <= 0)
{
return; // No trashes found
}
Data lastData = trashesFiltered[0].data[0];
foreach (var data in trashesFiltered[0].data)
{
if (DateTime.Parse(lastData.updated) < DateTime.Parse(data.updated))
{
lastData = data;
}
}
this.trashText.text = "Trash n° " + trashesFiltered[0].id;
this.dateText.text = "Mis à jour le " + DateTime.Parse(lastData.updated).ToString("G");
this.valueText.text = lastData.value + " " + lastData.unit;
if (this.type == TrashType.Temperature)
{
String[] v = lastData.value.Split(";");
this.valueText.text = v[0] + " °C - " + v[1] + "% d'humidité";
}
}
void Update()
{
this.currentCountdown -= Time.deltaTime;
if (this.currentCountdown <= 0)
{
this.currentCountdown = this.timeBetweenUpdate;
this.UpdateText();
}
}
}