2024-08-17 01:46:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2024-08-17 01:54:33 +00:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2024-08-17 01:46:22 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Prometheus;
|
2024-08-17 03:00:06 +00:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Linq;
|
2024-08-17 01:46:22 +00:00
|
|
|
|
|
2024-08-17 01:54:33 +00:00
|
|
|
|
namespace PrometheusExporterEdenic
|
2024-08-17 01:46:22 +00:00
|
|
|
|
{
|
2024-08-17 23:57:42 +00:00
|
|
|
|
public class Device
|
|
|
|
|
{
|
|
|
|
|
public string id { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string label { get; set; }
|
|
|
|
|
// Add other properties as needed
|
|
|
|
|
}
|
2024-08-17 03:00:06 +00:00
|
|
|
|
public class ApiClient
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpClient _client;
|
|
|
|
|
private readonly string _authToken;
|
2024-08-17 23:57:42 +00:00
|
|
|
|
private readonly string _orgid;
|
|
|
|
|
private readonly string _apimainurl = "https://api.edenic.io/api/v1/device";
|
2024-08-18 00:55:16 +00:00
|
|
|
|
private string _devicename;
|
|
|
|
|
public string DeviceId { get; private set; }
|
|
|
|
|
|
|
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
{
|
|
|
|
|
DeviceId = await GetDeviceIdByName(_devicename);
|
|
|
|
|
}
|
2024-08-17 03:00:06 +00:00
|
|
|
|
|
2024-08-18 00:55:16 +00:00
|
|
|
|
public ApiClient(string authToken, string orgID, string devicename)
|
2024-08-17 03:00:06 +00:00
|
|
|
|
{
|
|
|
|
|
_client = new HttpClient();
|
|
|
|
|
_authToken = authToken;
|
2024-08-17 23:57:42 +00:00
|
|
|
|
_orgid = orgID;
|
2024-08-18 00:55:16 +00:00
|
|
|
|
_devicename = devicename;
|
2024-08-17 03:00:06 +00:00
|
|
|
|
_client.DefaultRequestHeaders.Authorization =
|
|
|
|
|
new AuthenticationHeaderValue(_authToken);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 23:57:42 +00:00
|
|
|
|
public async Task<dynamic> MakeApiRequest<T>(string path)
|
2024-08-17 03:00:06 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-17 23:57:42 +00:00
|
|
|
|
HttpResponseMessage response = await _client.GetAsync($"{_apimainurl}/{_orgid}{path}");
|
2024-08-17 03:00:06 +00:00
|
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
string content = await response.Content.ReadAsStringAsync();
|
2024-08-17 23:57:42 +00:00
|
|
|
|
return JsonSerializer.Deserialize<dynamic>(content); }
|
2024-08-17 03:00:06 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-17 23:57:42 +00:00
|
|
|
|
|
|
|
|
|
public async Task<string> GetDeviceIdByName(string name)
|
|
|
|
|
{
|
2024-08-18 00:55:16 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
dynamic result = await MakeApiRequest<object>("");
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-08-17 23:57:42 +00:00
|
|
|
|
}
|
2024-08-17 03:00:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 01:46:22 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
2024-08-17 03:00:06 +00:00
|
|
|
|
|
2024-08-17 01:46:22 +00:00
|
|
|
|
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");
|
2024-08-17 03:00:06 +00:00
|
|
|
|
|
2024-08-18 00:55:16 +00:00
|
|
|
|
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);
|
2024-08-17 23:57:42 +00:00
|
|
|
|
|
2024-08-17 01:46:22 +00:00
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
2024-08-18 00:55:16 +00:00
|
|
|
|
await client.InitializeAsync();
|
|
|
|
|
Console.WriteLine($"Device ID: {client.DeviceId}");
|
2024-08-17 01:46:22 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2024-08-17 23:57:42 +00:00
|
|
|
|
ph.Set(1);
|
|
|
|
|
temperature.Set(1);
|
|
|
|
|
ec.Set(1);
|
|
|
|
|
await Task.Delay(15000);
|
2024-08-17 01:46:22 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|