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

76 lines
2.0 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using CandyCoded.env;
using models;
using services;
using TMPro;
using UnityEngine;
public enum TrashType
{
Temperature,
Distance,
Gyro
}
public class PocketBaseDataRepository : MonoBehaviour
{
[SerializeField] public String userToken;
[SerializeField] public Trash[] trashes;
[SerializeField] public TrashType type = TrashType.Distance;
[SerializeField] public TextMeshPro textToUpdate;
[SerializeField] public float timeBetweenUpdate = 10f;
private float currentCountdown;
void Start()
{
this.currentCountdown = this.timeBetweenUpdate;
env.TryParseEnvironmentVariable("API_USER", out string user);
env.TryParseEnvironmentVariable("API_PASSWORD", out string password);
LoginResponse res = UserService.Login(new LoginRequest(user, password));
this.userToken = res.token;
this.UpdateData();
}
void UpdateData()
{
TrashList list = TrashService.ListTrash(this.userToken);
foreach (Trash trash in list.items)
{
DataList trashData = TrashService.ListTrashData(userToken, trash.id);
trash.data = trashData.items;
}
this.trashes = list.items;
this.UpdateText();
}
void UpdateText()
{
if (this.trashes.Length <= 0 || this.textToUpdate == null)
{
return;
}
this.textToUpdate.text = "" + this.trashes[0].data[0].value + " " + this.trashes[0].data[0].unit;
}
void Update()
{
this.currentCountdown -= Time.deltaTime;
if (this.currentCountdown <= 0)
{
this.currentCountdown = this.timeBetweenUpdate;
this.UpdateData();
}
}
private void LateUpdate()
{
this.textToUpdate.transform.LookAt(Camera.main.transform.position);
this.textToUpdate.transform.rotation = Quaternion.LookRotation(this.textToUpdate.transform.position - Camera.main.transform.position);
}
}