100 lines
3.6 KiB
C#
100 lines
3.6 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Globalization;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
|
||
namespace SmartRoom
|
||
{
|
||
/// <summary>
|
||
/// Externe Daten laut Architekturdiagramm: Wetterdaten (Außentemperatur, Wind, Sonne) und Luftqualität
|
||
/// von Open-Meteo (kostenlos, kein API-Key). Standardmäßig aus – im HUD unter "Verbindung" einschalten.
|
||
/// </summary>
|
||
public class WeatherService : MonoBehaviour
|
||
{
|
||
public bool useLiveWeather;
|
||
public float latitude = 52.52f; // Berlin – an Hochschulstandort anpassen
|
||
public float longitude = 13.405f;
|
||
[Tooltip("Aktualisierung in Echtzeit-Minuten")] public float refreshMinutes = 10f;
|
||
|
||
public RoomEnvironment env;
|
||
public string Status { get; private set; } = "aus";
|
||
|
||
Coroutine loop;
|
||
|
||
[Serializable] class WeatherCurrent { public float temperature_2m; public float wind_speed_10m; public float shortwave_radiation; }
|
||
[Serializable] class WeatherResponse { public WeatherCurrent current; }
|
||
[Serializable] class AirCurrent { public float european_aqi; }
|
||
[Serializable] class AirResponse { public AirCurrent current; }
|
||
|
||
void Start()
|
||
{
|
||
if (useLiveWeather) SetLive(true);
|
||
}
|
||
|
||
public void SetLive(bool on)
|
||
{
|
||
useLiveWeather = on;
|
||
if (on && loop == null) loop = StartCoroutine(Loop());
|
||
if (!on && loop != null)
|
||
{
|
||
StopCoroutine(loop);
|
||
loop = null;
|
||
Status = "aus";
|
||
}
|
||
}
|
||
|
||
public void RefreshNow()
|
||
{
|
||
if (useLiveWeather) StartCoroutine(Fetch());
|
||
}
|
||
|
||
IEnumerator Loop()
|
||
{
|
||
while (true)
|
||
{
|
||
yield return Fetch();
|
||
yield return new WaitForSecondsRealtime(refreshMinutes * 60f);
|
||
}
|
||
}
|
||
|
||
IEnumerator Fetch()
|
||
{
|
||
Status = "lädt …";
|
||
var ci = CultureInfo.InvariantCulture;
|
||
string lat = latitude.ToString(ci), lon = longitude.ToString(ci);
|
||
|
||
string url = $"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}" +
|
||
"¤t=temperature_2m,wind_speed_10m,shortwave_radiation&wind_speed_unit=ms";
|
||
using (var req = UnityWebRequest.Get(url))
|
||
{
|
||
yield return req.SendWebRequest();
|
||
if (req.result != UnityWebRequest.Result.Success)
|
||
{
|
||
Status = "Fehler: " + req.error;
|
||
yield break;
|
||
}
|
||
var r = JsonUtility.FromJson<WeatherResponse>(req.downloadHandler.text);
|
||
if (r != null && r.current != null)
|
||
{
|
||
env.outdoorTemp = r.current.temperature_2m;
|
||
env.windSpeed = r.current.wind_speed_10m;
|
||
env.solar = r.current.shortwave_radiation;
|
||
}
|
||
}
|
||
|
||
string aqUrl = $"https://air-quality-api.open-meteo.com/v1/air-quality?latitude={lat}&longitude={lon}¤t=european_aqi";
|
||
using (var req = UnityWebRequest.Get(aqUrl))
|
||
{
|
||
yield return req.SendWebRequest();
|
||
if (req.result == UnityWebRequest.Result.Success)
|
||
{
|
||
var r = JsonUtility.FromJson<AirResponse>(req.downloadHandler.text);
|
||
if (r != null && r.current != null) env.outdoorAqi = r.current.european_aqi;
|
||
}
|
||
}
|
||
Status = $"Open-Meteo · {DateTime.Now:HH:mm}";
|
||
}
|
||
}
|
||
}
|