Done 🎉

This commit is contained in:
Michal 2024-08-18 02:18:59 +01:00
parent 7767455a38
commit 159be99cba
9 changed files with 52 additions and 25 deletions

View File

@ -28,6 +28,9 @@ namespace PrometheusExporterEdenic
private readonly string _apimainurl = "https://api.edenic.io/api/v1"; private readonly string _apimainurl = "https://api.edenic.io/api/v1";
private string _devicename; private string _devicename;
public string DeviceId { get; private set; } 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() public async Task InitializeAsync()
{ {
@ -101,37 +104,60 @@ namespace PrometheusExporterEdenic
return null; return null;
} }
} }
public async Task GetTelemetry()
public async Task<string> GetTelemetry()
{ {
try try
{ {
dynamic result = await MakeApiRequest<object>($"/telemetry/{DeviceId}"); dynamic result = await MakeApiRequest<object>($"/telemetry/{DeviceId}");
string jsonString = result.ToString(); // Convert dynamic to string string jsonString = result.ToString();
Console.WriteLine($"Raw JSON: {jsonString}"); // Debug output Console.WriteLine($"Raw JSON: {jsonString}"); // Debug output
return jsonString;
// var devices = JsonSerializer.Deserialize<List<Device>>(jsonString); using (JsonDocument doc = JsonDocument.Parse(jsonString))
// Console.WriteLine($"Deserialized {devices.Count} devices"); // Debug output {
// JsonElement root = doc.RootElement;
// var targetDevice = devices.FirstOrDefault(d => d.label == name);
// if (targetDevice != null) ph = ExtractValue(root, "ph");
// { temperature = ExtractValue(root, "temperature");
// Console.WriteLine($"Found device: {targetDevice.label}, ID: {targetDevice.id}"); // Debug output ec = ExtractValue(root, "electrical_conductivity");
// return targetDevice.id;
// } Console.WriteLine($"Extracted values - pH: {ph/100.0}, Temperature: {temperature/100.0}, EC: {ec/100.0}");
// else }
// {
// Console.WriteLine($"No device found with label: {name}"); // Debug output
// return null;
// }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error in GetDeviceIdByName: {ex.Message}"); // Debug output Console.WriteLine($"Error in GetTelemetry: {ex.Message}");
return null;
} }
} }
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 public class Program
@ -190,9 +216,10 @@ namespace PrometheusExporterEdenic
Console.WriteLine($"Device ID: {client.DeviceId}"); Console.WriteLine($"Device ID: {client.DeviceId}");
while (true) while (true)
{ {
ph.Set(1); await client.GetTelemetry();
temperature.Set(1); ph.Set(client.ph / 100.0); // Convert back to float
ec.Set(1); temperature.Set(client.temperature / 100.0); // Convert back to float
ec.Set(client.ec / 100.0); // Convert back to float
await Task.Delay(15000); await Task.Delay(15000);
} }
}); });

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("EdenicExporter")] [assembly: System.Reflection.AssemblyCompanyAttribute("EdenicExporter")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [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.AssemblyProductAttribute("EdenicExporter")]
[assembly: System.Reflection.AssemblyTitleAttribute("EdenicExporter")] [assembly: System.Reflection.AssemblyTitleAttribute("EdenicExporter")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
184710734eedccb7e8218a40f90b97508609f3208f7414d1463795baa0b9e857 ed15f81d353bd3dfcb791555499e7b649dbcdde6cc4a1e5c0b9fa874eb0cbeff