mirror of
https://github.com/Mukheem/TwinTurbine.git
synced 2025-05-11 14:00:34 +02:00
added API script
This commit is contained in:
parent
5161538571
commit
aa449b37c8
Assets/SharedSpatialAnchors/Scripts
97
Assets/SharedSpatialAnchors/Scripts/API.cs
Normal file
97
Assets/SharedSpatialAnchors/Scripts/API.cs
Normal file
@ -0,0 +1,97 @@
|
||||
using OVRSimpleJSON;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
// Creating the data structure according to the expected Json
|
||||
[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 float latestWD;
|
||||
|
||||
void Start()
|
||||
{
|
||||
//TestFromJsonToData();
|
||||
|
||||
StartCoroutine(GetText());
|
||||
}
|
||||
|
||||
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);
|
||||
ExtractDataFromJson(www.downloadHandler.text);
|
||||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
Debug.Log("Total number of data points: " + dataPoints.Count);
|
||||
|
||||
// Find the value of WD in the latest hour
|
||||
for (int i = 0; i < dataPoints.Count; i++)
|
||||
{
|
||||
DataPoint point = dataPoints[i];
|
||||
if (point.name == "wd")
|
||||
{
|
||||
latestWD = point.values[0];
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/SharedSpatialAnchors/Scripts/API.cs.meta
Normal file
11
Assets/SharedSpatialAnchors/Scripts/API.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a631e17a18402642aaf0bbbe6b66cf3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
x
Reference in New Issue
Block a user