Added temp and water

This commit is contained in:
2026-08-08 01:05:40 +01:00
parent f8d9727a38
commit 838e253ab1
3 changed files with 43 additions and 4 deletions

View File

@@ -51,4 +51,38 @@ public class InfluxDataService
return 0f; // Fallback value if query fails or no data is found
}
public async Task<int> GetLatestWaterAsync()
{
// Connect to the InfluxDB container
using var client = new InfluxDBClient(_url, _token);
var queryApi = client.GetQueryApi();
// This is a standard Flux query to get the last recorded value in the last 15 mins.
// You will need to change "_measurement" and "_field" to match your setup.
string flux = $@"
from(bucket: ""{_bucket}"")
|> range(start: -15m)
|> filter(fn: (r) => r[""_measurement""] == ""Motorhome_Water"")
|> filter(fn: (r) => r[""_field""] == ""W_Percentage"")
|> last()";
try
{
var tables = await queryApi.QueryAsync(flux, _org);
// Check if we actually got data back
if (tables != null && tables.Any() && tables[0].Records.Any())
{
var record = tables[0].Records[0];
return Convert.ToInt32(record.GetValue());
}
}
catch (Exception ex)
{
Console.WriteLine($"Error querying InfluxDB: {ex.Message}");
}
return 100; // Fallback value if query fails or no data is found
}
}