-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
81 lines (66 loc) · 3.41 KB
/
Program.cs
File metadata and controls
81 lines (66 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using com.clusterrr.TuyaNet;
using System.Text.Json;
namespace SolarSmartTest
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Avvio SolarSmart System - Esecuzione Singola (GitHub Actions)...");
// 1. LEGGI I SEGRETI DALLE VARIABILI DI AMBIENTE
string accessId = Environment.GetEnvironmentVariable("TUYA_ACCESS_ID");
string accessSecret = Environment.GetEnvironmentVariable("TUYA_ACCESS_SECRET");
string deviceId = Environment.GetEnvironmentVariable("TUYA_DEVICE_ID");
if (string.IsNullOrEmpty(accessId) || string.IsNullOrEmpty(accessSecret) || string.IsNullOrEmpty(deviceId))
{
Console.WriteLine("ERRORE: Variabili di ambiente mancanti. Interruzione.");
return;
}
// Inizializza l'API Tuya
var api = new TuyaApi(region: TuyaApi.Region.CentralEurope, accessId: accessId, apiSecret: accessSecret);
try
{
int radiation = await GetLocarnoRadiationJson();
Console.WriteLine($"Meteo Attuale -> Radiazione: {radiation} W/m²");
// 3. LOGICA E INVIO COMANDO
bool shouldBeOn = (radiation >= 500);
string commandJson = "{\"commands\":[{\"code\":\"switch_1\",\"value\":" + shouldBeOn.ToString().ToLower() + "}]}";
if (shouldBeOn)
Console.WriteLine("Condizioni OTTIMALI. Invio comando di ACCENSIONE...");
else
Console.WriteLine("Sole insufficiente. Invio comando di SPEGNIMENTO...");
// Invia a Tuya
var response = await api.RequestAsync(TuyaApi.Method.POST, $"/v1.0/devices/{deviceId}/commands", commandJson);
Console.WriteLine("Risposta Tuya: " + response);
}
catch (Exception ex)
{
Console.WriteLine($"Errore fatale: {ex.Message}");
}
Console.WriteLine("Esecuzione terminata.");
}
static async Task<int> GetLocarnoRadiationJson()
{
string url = "https://data.geo.admin.ch/ch.meteoschweiz.messwerte-globalstrahlung-10min/ch.meteoschweiz.messwerte-globalstrahlung-10min_it.json";
using var client = new HttpClient();
string jsonString = await client.GetStringAsync(url);
// Analizziamo il JSON senza creare classi complesse (usiamo JsonDocument)
using JsonDocument doc = JsonDocument.Parse(jsonString);
JsonElement root = doc.RootElement;
// Cerchiamo la "Feature" che ha id = "OTL"
var locarnoFeature = root.GetProperty("features")
.EnumerateArray()
.FirstOrDefault(f => f.GetProperty("id").GetString() == "OTL");
if (locarnoFeature.ValueKind != JsonValueKind.Undefined)
{
// Prendiamo il valore dalla proprietà "value" dentro "properties"
// Nota: MeteoSvizzera lo fornisce come numero intero o decimale
int valore = locarnoFeature.GetProperty("properties")
.GetProperty("value")
.GetInt32();
return valore;
}
throw new Exception("Stazione OTL non trovata nel JSON.");
}
}
}