get device
This commit is contained in:
parent
f1be1e80b9
commit
9451138fe5
@ -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);
|
||||
|
Binary file not shown.
Binary file not shown.
@ -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")]
|
||||
|
@ -1 +1 @@
|
||||
8b3979b2670092c3f16be63c7eaee07a5d3524e94fa5f7a6b9b1b75cbc91ce4c
|
||||
119886f42dc5b610e5b763673658a8f8230bdd04e244e8c6f0c34a054a44ab6b
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user