get device

This commit is contained in:
Michal 2024-08-18 01:55:16 +01:00
parent f1be1e80b9
commit 9451138fe5
9 changed files with 43 additions and 12 deletions

View File

@ -26,12 +26,20 @@ namespace PrometheusExporterEdenic
private readonly string _authToken;
private readonly string _orgid;
private readonly string _apimainurl = "https://api.edenic.io/api/v1/device";
private string _devicename;
public string DeviceId { get; private set; }
public async Task InitializeAsync()
{
DeviceId = await GetDeviceIdByName(_devicename);
}
public ApiClient(string authToken, string orgID)
public ApiClient(string authToken, string orgID, string devicename)
{
_client = new HttpClient();
_authToken = authToken;
_orgid = orgID;
_devicename = devicename;
_client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(_authToken);
}
@ -65,11 +73,32 @@ namespace PrometheusExporterEdenic
public async Task<string> GetDeviceIdByName(string name)
{
dynamic result = await MakeApiRequest<object>("");
var jsonString = JsonSerializer.Serialize(result);
var devices = JsonSerializer.Deserialize<List<Device>>(jsonString);
var targetDevice = devices.FirstOrDefault((Func<Device, bool>)(d => d.label == name));
return targetDevice?.id;
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;
}
}
}
@ -77,10 +106,6 @@ namespace PrometheusExporterEdenic
{
public static void Main(string[] args)
{
string EDENIC_API_TOKEN = Environment.GetEnvironmentVariable("EDENIC_API_TOKEN");
string EDENIC_ORG_ID = Environment.GetEnvironmentVariable("EDENIC_ORG_ID");
ApiClient client = new ApiClient(EDENIC_API_TOKEN, EDENIC_ORG_ID);
Console.WriteLine(client.GetDeviceIdByName("The First One"));
CreateHostBuilder(args).Build().Run();
}
@ -122,9 +147,15 @@ namespace PrometheusExporterEdenic
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)
{
ph.Set(1);

View File

@ -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+9c7d7928e96cee04d634ce311c4f039de4040cb2")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f1be1e80b9c9b26103bc6df1148538674cdf99b9")]
[assembly: System.Reflection.AssemblyProductAttribute("EdenicExporter")]
[assembly: System.Reflection.AssemblyTitleAttribute("EdenicExporter")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
8b3979b2670092c3f16be63c7eaee07a5d3524e94fa5f7a6b9b1b75cbc91ce4c
119886f42dc5b610e5b763673658a8f8230bdd04e244e8c6f0c34a054a44ab6b