add humidity tracking under reprocess
authorjweigele <jweigele@local>
Sat, 13 Apr 2024 03:33:42 +0000 (20:33 -0700)
committerjweigele <jweigele@local>
Sat, 13 Apr 2024 03:33:42 +0000 (20:33 -0700)
reprocess/main.go
wunder/main.go

index 2b42175114d519a791cc1d241cb282e14f19cb8c..ad571314c919f20feec9deca03fffab7e484db7e 100644 (file)
@@ -6,6 +6,7 @@ import (
        "fmt"
        "os"
        "reflect"
+       "strconv"
        "strings"
        "sync"
        "time"
@@ -67,6 +68,22 @@ var (
                []string{"location"},
        )
 
+       co2Gauge = promauto.NewGaugeVec(
+               prometheus.GaugeOpts{
+                       Name: "airsensor_co2",
+                       Help: "CO2 PPM concentration",
+               },
+               []string{"location"},
+       )
+
+       humidityGauge = promauto.NewGaugeVec(
+               prometheus.GaugeOpts{
+                       Name: "sensor_humidity",
+                       Help: "Relative humidity 0-100%",
+               },
+               []string{"location"},
+       )
+
        versionGauge = promauto.NewGaugeVec(
                prometheus.GaugeOpts{
                        Name: "sensor_version",
@@ -112,6 +129,8 @@ var (
        pm10Expire         = make(map[string]time.Time)
        pm25Expire         = make(map[string]time.Time)
        aqiExpire          = make(map[string]time.Time)
+       co2Expire          = make(map[string]time.Time)
+       humidityExpire     = make(map[string]time.Time)
        versionExpire      = make(map[string]time.Time)
        uptimeExpire       = make(map[string]time.Time)
        internalTempExpire = make(map[string]time.Time)
@@ -290,6 +309,26 @@ func handleAQI(obj map[string]interface{}) {
 
 }
 
+func handleCO2(location string, co2 float64) {
+       now := time.Now().UTC()
+       co2Gauge.With(prometheus.Labels{"location": location}).Set(co2)
+
+       expireMutex.Lock()
+       defer expireMutex.Unlock()
+
+       co2Expire[location] = now
+}
+
+func handleRH(location string, humidity float64) {
+       now := time.Now().UTC()
+       humidityGauge.With(prometheus.Labels{"location": location}).Set(humidity)
+
+       expireMutex.Lock()
+       defer expireMutex.Unlock()
+
+       humidityExpire[location] = now
+}
+
 func handleVersion(obj map[string]interface{}) {
        now := time.Now().UTC()
        extAddr, ok := obj["ext"].(string)
@@ -461,17 +500,17 @@ func hexCommon(location string, friendlyName string, obj map[string]interface{},
                sendChannel <- sendThis
        }
 
-       pm25, ok := obj["pm25"].(float64)
-       if !ok {
+       pm25, pm25ok := obj["pm25"].(float64)
+       if !pm25ok {
                logger.V(3).Info("pm25 not found in data, ignoring", "obj", obj)
        }
 
-       pm10, ok := obj["pm10"].(float64)
-       if !ok {
+       pm10, pm10ok := obj["pm10"].(float64)
+       if !pm10ok {
                logger.V(3).Info("pm10 not found in data, ignoring", "obj", obj)
        }
 
-       if pm10 != 0.0 || pm25 != 0.0 {
+       if pm10ok && pm25ok {
                processPM25(location, pm10, pm25, sendChannel)
        }
 
@@ -494,6 +533,19 @@ func hexCommon(location string, friendlyName string, obj map[string]interface{},
                }
        }
 
+       co2, ok := obj["co2"].(float64)
+       if ok {
+               handleCO2(location, co2)
+       }
+
+       rh, ok := obj["rh"].(string)
+       if ok {
+               rh_float, err := strconv.ParseFloat(rh, 64)
+               if err == nil {
+                       handleRH(location, rh_float)
+               }
+       }
+
        internal, ok := obj["internal"].(float64)
        if ok {
                if internal < -55.0 || internal > 125.0 {
@@ -746,6 +798,8 @@ func expireStaleMetrics() {
        expireStaleMetric(pm10Expire, pm10Gauge)
        expireStaleMetric(pm25Expire, pm25Gauge)
        expireStaleMetric(aqiExpire, aqiGauge)
+       expireStaleMetric(co2Expire, co2Gauge)
+       expireStaleMetric(humidityExpire, humidityGauge)
        expireStaleMetric(versionExpire, versionGauge)
        expireStaleMetric(uptimeExpire, uptimeGauge)
        expireStaleMetric(internalTempExpire, internalTempGauge)
index 42b44e0fe3e09f472b561b90ad00aa8f42b891a7..b34cd68188a3728a12c9f4b41b64b7a01ddf8b9c 100644 (file)
@@ -216,8 +216,8 @@ func main() {
                os.Exit(1)
        }
        // off, somehow?
-       //const weatherStation string = "KWASEATT2696"
-       const weatherStation string = "KWASEATT2613"
+       const weatherStation string = "KWASEATT2696"
+       //const weatherStation string = "KWASEATT2613"
        const sleepTime time.Duration = 30 * time.Second
 
        //currentTemp, err := fetchTemp(weatherStation)