Refactor Directories
This commit is contained in:
228
Program.cs
Normal file
228
Program.cs
Normal file
@@ -0,0 +1,228 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Prometheus;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Linq;
|
||||
|
||||
namespace PrometheusExporterEdenic
|
||||
{
|
||||
public class Device
|
||||
{
|
||||
public string id { get; set; }
|
||||
public string name { get; set; }
|
||||
public string label { get; set; }
|
||||
// Add other properties as needed
|
||||
}
|
||||
public class ApiClient
|
||||
{
|
||||
private readonly HttpClient _client;
|
||||
private readonly string _authToken;
|
||||
private readonly string _orgid;
|
||||
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()
|
||||
{
|
||||
DeviceId = await GetDeviceIdByName(_devicename);
|
||||
}
|
||||
|
||||
public ApiClient(string authToken, string orgID, string devicename)
|
||||
{
|
||||
_client = new HttpClient();
|
||||
_authToken = authToken;
|
||||
_orgid = orgID;
|
||||
_devicename = devicename;
|
||||
_client.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue(_authToken);
|
||||
}
|
||||
|
||||
public async Task<dynamic> MakeApiRequest<T>(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await _client.GetAsync($"{_apimainurl}{path}");
|
||||
Console.WriteLine($"Request send to: {_apimainurl}{path}");
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<dynamic>(content); }
|
||||
else
|
||||
{
|
||||
throw new HttpRequestException($"Error: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
Console.WriteLine($"Request exception: {e.Message}");
|
||||
throw;
|
||||
}
|
||||
catch (JsonException e)
|
||||
{
|
||||
Console.WriteLine($"JSON parsing exception: {e.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetDeviceIdByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
dynamic result = await MakeApiRequest<object>($"/device/{_orgid}");
|
||||
string jsonString = result.ToString(); // Convert dynamic to string
|
||||
Console.WriteLine($"Raw JSON: {jsonString}"); // Debug output
|
||||
|
||||
var devices = JsonSerializer.Deserialize<List<Device>>(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;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error in GetDeviceIdByName: {ex.Message}"); // Debug output
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public async Task GetTelemetry()
|
||||
{
|
||||
try
|
||||
{
|
||||
dynamic result = await MakeApiRequest<object>($"/telemetry/{DeviceId}");
|
||||
string jsonString = result.ToString();
|
||||
Console.WriteLine($"Raw JSON: {jsonString}"); // Debug output
|
||||
|
||||
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 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
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
|
||||
public class Startup
|
||||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseMetricServer();
|
||||
app.UseHttpMetrics();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapGet("/", async context =>
|
||||
{
|
||||
await context.Response.WriteAsync("Open /metrics");
|
||||
});
|
||||
});
|
||||
|
||||
var ph = Metrics.CreateGauge("edenic_ph", "Edenic ph");
|
||||
var temperature = Metrics.CreateGauge("edenic_temperature", "Edenic temperature");
|
||||
var ec = Metrics.CreateGauge("edenic_ec", "Edenic EC");
|
||||
|
||||
string EDENIC_API_TOKEN = Environment.GetEnvironmentVariable("EDENIC_API_TOKEN");
|
||||
string EDENIC_ORG_ID = Environment.GetEnvironmentVariable("EDENIC_ORG_ID");
|
||||
string EDENIC_DEVICENAME = Environment.GetEnvironmentVariable("EDENIC_DEVICENAME");
|
||||
ApiClient client = new ApiClient(EDENIC_API_TOKEN, EDENIC_ORG_ID, EDENIC_DEVICENAME);
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await client.InitializeAsync();
|
||||
Console.WriteLine($"Device ID: {client.DeviceId}");
|
||||
while (true)
|
||||
{
|
||||
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(60000);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user