diff --git a/InfluxDataService.cs b/InfluxDataService.cs index cee9e33..8cb925d 100644 --- a/InfluxDataService.cs +++ b/InfluxDataService.cs @@ -51,4 +51,38 @@ public class InfluxDataService return 0f; // Fallback value if query fails or no data is found } + + public async Task 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 + } } \ No newline at end of file diff --git a/MatrixRenderer.cs b/MatrixRenderer.cs index d2e949c..b339e22 100644 --- a/MatrixRenderer.cs +++ b/MatrixRenderer.cs @@ -13,7 +13,7 @@ public class MatrixRenderer } // - public async Task DrawAndSendFrameAsync(float currentTemp) + public async Task DrawAndSendFrameAsync(float currentTemp, int currentWater) //public async Task DrawAndSendFrameAsync(string currentTemp) { // 1. Create a 64x64 canvas @@ -41,7 +41,8 @@ public class MatrixRenderer // DrawText now takes the string, X, Y, the font, and the paint //canvas.DrawText($"{currentTemp:F1}c", 4, 32, textFont, textPaint); // DrawText now takes the string, X, Y, alignment, the font, and the paint - canvas.DrawText($"{currentTemp:F1}c", 4, 32, SKTextAlign.Left, textFont, textPaint); + canvas.DrawText($"{currentTemp:F1}c", 4, 12, SKTextAlign.Left, textFont, textPaint); + canvas.DrawText($"{currentWater:F1}%", 4, 32, SKTextAlign.Left, textFont, textPaint); // 4. Convert 32-bit RGBA to 24-bit RGB for WLED byte[] rgbaBytes = bitmap.Bytes; diff --git a/Program.cs b/Program.cs index 0605054..39d9bd6 100644 --- a/Program.cs +++ b/Program.cs @@ -7,7 +7,7 @@ var renderer = new MatrixRenderer("192.168.0.150"); // Your WLED IP // Replace these with your actual InfluxDB v2 credentials // If Influx is on the same Pi, you can use the Pi's local IP and port 8086 var influx = new InfluxDataService( - url: "http://192.168.0.76:8086", + url: "http://parsons.cc:8086", token: ":", org: "-", bucket: "sensor_data" @@ -16,6 +16,7 @@ var influx = new InfluxDataService( Console.WriteLine("Starting DDP stream with live InfluxDB data..."); float currentTemp = 1f; +int currentWaterLevel = 100; Stopwatch dbTimer = Stopwatch.StartNew(); while (true) @@ -24,12 +25,15 @@ while (true) if (dbTimer.ElapsedMilliseconds > 5000 || currentTemp == 0f) { currentTemp = await influx.GetLatestTemperatureAsync(); + currentWaterLevel = await influx.GetLatestWaterAsync(); Console.WriteLine($"Fetched new temp: {currentTemp}c"); + Console.WriteLine($"Fetched new water: {currentWaterLevel}%"); dbTimer.Restart(); } // Keep pushing frames to WLED at 30 FPS - await renderer.DrawAndSendFrameAsync(currentTemp); + await renderer.DrawAndSendFrameAsync(currentTemp, currentWaterLevel); + await Task.Delay(1000 / 30); } \ No newline at end of file