2024-05-25 13:45:40 +02:00

130 lines
3.8 KiB
C#

using OVRSimpleJSON;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
[Serializable]
public class DataPoint
{
public string name;
public string levelType;
public int level;
public string unit;
public List<float> values;
}
[Serializable]
public class DataPerHour
{
public string validTime;
public List<DataPoint> parameters;
}
[Serializable]
public class ApiResponse
{
public string approvedTime;
public string referenceTime;
public string geometry;
public List<DataPerHour> timeSeries;
}
public class API : MonoBehaviour
{
public TextMeshProUGUI voltageValue;
public TextMeshProUGUI windDirValue;
public TextMeshProUGUI temperatureValue;
public TextMeshProUGUI loc;
public GameObject webSocketController;
public TurbineController turbineController; // Reference to the TurbineController to control rotation
private WebSocketController webSocketControllerScript;
private float LatestT;
private float latestWD;
private string unit;
void Start()
{
webSocketControllerScript = webSocketController.GetComponent<WebSocketController>();
//TestFromJsonToData(); // Uncomment this to test JSON parsing independently
EmergencyButtonClick();
}
public void OnButtonClick()
{
StartCoroutine(GetText());
}
public void EmergencyButtonClick()
{
loc.SetText("----");
windDirValue.SetText("----");
temperatureValue.SetText("----");
}
IEnumerator GetText()
{
UnityWebRequest www = UnityWebRequest.Get("https://opendata-download-metanalys.smhi.se/api/category/mesan2g/version/1/geotype/point/lon/17.94/lat/59.40/data.json");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Received data: " + www.downloadHandler.text); // Log the JSON data received
ExtractDataFromJson(www.downloadHandler.text);
}
}
public void ExtractDataFromJson(string json)
{
ApiResponse response = JsonUtility.FromJson<ApiResponse>(json);
//Debug.Log("Api response worked!!!!");
// Access the latest hour
DataPerHour timeResponse = response.timeSeries[0];
List<DataPoint> dataPoints = timeResponse.parameters;
loc.SetText("Kista(Lat:59.4067 Long:17.9452)"); // Set the location display
voltageValue.text = webSocketControllerScript.voltageValue.ToString(); // Update voltage value display
for (int i = 0; i < dataPoints.Count; i++)
{
DataPoint point = dataPoints[i];
if (point.name == "wd") // Handling wind direction
{
latestWD = point.values[0];
windDirValue.SetText(latestWD.ToString() + " degrees");
//turbineController.SetRotationSpeed(latestWD); // Adjust turbine rotation based on wind direction
webSocketControllerScript.ws.Send(latestWD.ToString() + ": take input");
}
if (point.name == "t") // Handling temperature
{
LatestT = point.values[0];
unit = point.unit;
temperatureValue.SetText(LatestT.ToString() + " " + unit);
}
}
}
public void TestFromJsonToData()
{
string testJson = "{\r\n \"name\": \"t\",\r\n \"levelType\": \"hl\",\r\n \"level\": 2,\r\n \"unit\": \"Cel\",\r\n \"values\": [\r\n 21.3\r\n ]\r\n }";
DataPoint dataTurbine = JsonUtility.FromJson<DataPoint>(testJson);
//Debug.Log(dataTurbine.name);
//Debug.Log(dataTurbine.values[0]);
}
}