diff --git a/EdenicExporter/Program.cs b/EdenicExporter/Program.cs index f3c8f1c..c7d2e55 100644 --- a/EdenicExporter/Program.cs +++ b/EdenicExporter/Program.cs @@ -28,6 +28,9 @@ namespace PrometheusExporterEdenic private readonly string _apimainurl = "https://api.edenic.io/api/v1"; private string _devicename; public string DeviceId { get; private set; } + public int ph { get; private set; } + public int temperature { get; private set; } + public int ec { get; private set; } public async Task InitializeAsync() { @@ -101,37 +104,60 @@ namespace PrometheusExporterEdenic return null; } } - - public async Task GetTelemetry() + public async Task GetTelemetry() { try { dynamic result = await MakeApiRequest($"/telemetry/{DeviceId}"); - string jsonString = result.ToString(); // Convert dynamic to string + string jsonString = result.ToString(); Console.WriteLine($"Raw JSON: {jsonString}"); // Debug output - return jsonString; - // var devices = JsonSerializer.Deserialize>(jsonString); - // Console.WriteLine($"Deserialized {devices.Count} devices"); // Debug output - // - // var targetDevice = devices.FirstOrDefault(d => d.label == name); - // if (targetDevice != null) - // { - // Console.WriteLine($"Found device: {targetDevice.label}, ID: {targetDevice.id}"); // Debug output - // return targetDevice.id; - // } - // else - // { - // Console.WriteLine($"No device found with label: {name}"); // Debug output - // return null; - // } + using (JsonDocument doc = JsonDocument.Parse(jsonString)) + { + JsonElement root = doc.RootElement; + + ph = ExtractValue(root, "ph"); + temperature = ExtractValue(root, "temperature"); + ec = ExtractValue(root, "electrical_conductivity"); + + Console.WriteLine($"Extracted values - pH: {ph/100.0}, Temperature: {temperature/100.0}, EC: {ec/100.0}"); + } } catch (Exception ex) { - Console.WriteLine($"Error in GetDeviceIdByName: {ex.Message}"); // Debug output - return null; + Console.WriteLine($"Error in GetTelemetry: {ex.Message}"); } } + + private int ExtractValue(JsonElement root, string key) + { + if (root.TryGetProperty(key, out JsonElement property) && + property.GetArrayLength() > 0 && + property[0].TryGetProperty("value", out JsonElement valueElement)) + { + string valueString = valueElement.GetString(); + if (!string.IsNullOrEmpty(valueString)) + { + if (float.TryParse(valueString, out float floatValue)) + { + return (int)(floatValue * 100); // Convert to int, preserving two decimal places + } + else + { + Console.WriteLine($"Error: Unable to parse {key} value: {valueString}"); + } + } + else + { + Console.WriteLine($"Error: {key} value is empty"); + } + } + else + { + Console.WriteLine($"Error: {key} not found or invalid"); + } + return 0; // Default value + } } public class Program @@ -190,9 +216,10 @@ namespace PrometheusExporterEdenic Console.WriteLine($"Device ID: {client.DeviceId}"); while (true) { - ph.Set(1); - temperature.Set(1); - ec.Set(1); + await client.GetTelemetry(); + ph.Set(client.ph / 100.0); // Convert back to float + temperature.Set(client.temperature / 100.0); // Convert back to float + ec.Set(client.ec / 100.0); // Convert back to float await Task.Delay(15000); } }); diff --git a/EdenicExporter/bin/Debug/net8.0/EdenicExporter.dll b/EdenicExporter/bin/Debug/net8.0/EdenicExporter.dll index 87d5411..465a92b 100644 Binary files a/EdenicExporter/bin/Debug/net8.0/EdenicExporter.dll and b/EdenicExporter/bin/Debug/net8.0/EdenicExporter.dll differ diff --git a/EdenicExporter/bin/Debug/net8.0/EdenicExporter.pdb b/EdenicExporter/bin/Debug/net8.0/EdenicExporter.pdb index 5e2c6ca..cc9ee45 100644 Binary files a/EdenicExporter/bin/Debug/net8.0/EdenicExporter.pdb and b/EdenicExporter/bin/Debug/net8.0/EdenicExporter.pdb differ diff --git a/EdenicExporter/obj/Debug/net8.0/DotNet.Docker.AssemblyInfo.cs b/EdenicExporter/obj/Debug/net8.0/DotNet.Docker.AssemblyInfo.cs index 52b62f8..f6f595b 100644 --- a/EdenicExporter/obj/Debug/net8.0/DotNet.Docker.AssemblyInfo.cs +++ b/EdenicExporter/obj/Debug/net8.0/DotNet.Docker.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("EdenicExporter")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9451138fe5f22ab586beafbc14d8ed95103e23f5")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+7767455a38432d30f522887148dc80cf6d1c3258")] [assembly: System.Reflection.AssemblyProductAttribute("EdenicExporter")] [assembly: System.Reflection.AssemblyTitleAttribute("EdenicExporter")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/EdenicExporter/obj/Debug/net8.0/DotNet.Docker.AssemblyInfoInputs.cache b/EdenicExporter/obj/Debug/net8.0/DotNet.Docker.AssemblyInfoInputs.cache index 3a12010..fb16b0a 100644 --- a/EdenicExporter/obj/Debug/net8.0/DotNet.Docker.AssemblyInfoInputs.cache +++ b/EdenicExporter/obj/Debug/net8.0/DotNet.Docker.AssemblyInfoInputs.cache @@ -1 +1 @@ -184710734eedccb7e8218a40f90b97508609f3208f7414d1463795baa0b9e857 +ed15f81d353bd3dfcb791555499e7b649dbcdde6cc4a1e5c0b9fa874eb0cbeff diff --git a/EdenicExporter/obj/Debug/net8.0/EdenicExporter.dll b/EdenicExporter/obj/Debug/net8.0/EdenicExporter.dll index 87d5411..465a92b 100644 Binary files a/EdenicExporter/obj/Debug/net8.0/EdenicExporter.dll and b/EdenicExporter/obj/Debug/net8.0/EdenicExporter.dll differ diff --git a/EdenicExporter/obj/Debug/net8.0/EdenicExporter.pdb b/EdenicExporter/obj/Debug/net8.0/EdenicExporter.pdb index 5e2c6ca..cc9ee45 100644 Binary files a/EdenicExporter/obj/Debug/net8.0/EdenicExporter.pdb and b/EdenicExporter/obj/Debug/net8.0/EdenicExporter.pdb differ diff --git a/EdenicExporter/obj/Debug/net8.0/ref/EdenicExporter.dll b/EdenicExporter/obj/Debug/net8.0/ref/EdenicExporter.dll index acb1d27..d2c34a2 100644 Binary files a/EdenicExporter/obj/Debug/net8.0/ref/EdenicExporter.dll and b/EdenicExporter/obj/Debug/net8.0/ref/EdenicExporter.dll differ diff --git a/EdenicExporter/obj/Debug/net8.0/refint/EdenicExporter.dll b/EdenicExporter/obj/Debug/net8.0/refint/EdenicExporter.dll index acb1d27..d2c34a2 100644 Binary files a/EdenicExporter/obj/Debug/net8.0/refint/EdenicExporter.dll and b/EdenicExporter/obj/Debug/net8.0/refint/EdenicExporter.dll differ