Add back in pm25 gauge, and additionally do pm10 as well
authorjweigele <jweigele@local>
Tue, 22 Aug 2023 16:49:09 +0000 (09:49 -0700)
committerjweigele <jweigele@local>
Tue, 22 Aug 2023 16:49:09 +0000 (09:49 -0700)
reprocess/main.go

index 1701f0afdd9299878028080f25d917ca70db0b38..d342c782627182a5ade93a62d20e0d2788b34117 100644 (file)
@@ -42,6 +42,15 @@ var (
                },
                []string{"ePDUOutletStatusIndex", "ePDUOutletStatusOutletName"},
        )
+
+       pm10Gauge = promauto.NewGaugeVec(
+               prometheus.GaugeOpts{
+                       Name: "airsensor_pm10",
+                       Help: "Used to measure air PM1.0 concentration in µg/m³",
+               },
+               []string{"location"},
+       )
+
        pm25Gauge = promauto.NewGaugeVec(
                prometheus.GaugeOpts{
                        Name: "airsensor_pm25",
@@ -84,6 +93,7 @@ var (
 
        tempExpire   = make(map[string]time.Time)
        powerExpire  = make(map[string]time.Time)
+       pm10Expire   = make(map[string]time.Time)
        pm25Expire   = make(map[string]time.Time)
        aqiExpire    = make(map[string]time.Time)
        uptimeExpire = make(map[string]time.Time)
@@ -253,22 +263,20 @@ func handleTemp(obj map[string]interface{}) {
 
 func handleAQI(obj map[string]interface{}) {
        now := time.Now().UTC()
-       // do the label update here
-       //pm25Gauge.With(prometheus.Labels{"location": location}).Set(pm25)
        aqiGauge.With(prometheus.Labels{"location": obj["location"].(string)}).Set(obj["aqi"].(float64))
 
        expireMutex.Lock()
        defer expireMutex.Unlock()
 
        aqiExpire[obj["location"].(string)] = now
-       //pm25Expire[location] = now
 
 }
 
-func processPM25(location string, pm25 float64, sendChannel chan helper.RabbitSend) {
+func processPM25(location string, pm10 float64, pm25 float64, sendChannel chan helper.RabbitSend) {
        // should have the senseType setup correctly now, if we're still here
        dataMap := make(map[string]interface{}, 0)
        dataMap["location"] = location
+       dataMap["pm10"] = pm10
        dataMap["pm25"] = pm25
        results, err := aqi.Calculate(aqi.PM25{Concentration: pm25})
        if err != nil {
@@ -279,6 +287,13 @@ func processPM25(location string, pm25 float64, sendChannel chan helper.RabbitSe
        aqi := results.AQI
        dataMap["aqi"] = aqi
 
+       pm10Gauge.With(prometheus.Labels{"location": location}).Set(pm10)
+       pm25Gauge.With(prometheus.Labels{"location": location}).Set(pm25)
+       now := time.Now().UTC()
+       expireMutex.Lock()
+       defer expireMutex.Unlock()
+       pm10Expire[location] = now
+       pm25Expire[location] = now
        sendThis := helper.RabbitSend{Data: dataMap, RoutingKey: "aqi", IncludeDate: true}
        sendChannel <- sendThis
 
@@ -339,7 +354,8 @@ func (dev *diydevice) handleDIY(obj map[string]interface{}, sendChannel chan hel
                        } else if senseType == "pm25" {
                                location := data["location"].(string)
                                pm25 := data["pm25"].(float64)
-                               processPM25(location, pm25, sendChannel)
+                               pm10 := data["pm10"].(float64)
+                               processPM25(location, pm10, pm25, sendChannel)
                        } else {
                                logger.Info("Sense type not detected, ignoring")
                        }
@@ -385,8 +401,15 @@ func hexCommon(location string, friendlyName string, obj map[string]interface{},
        pm25, ok := obj["pm25"].(float64)
        if !ok {
                logger.V(3).Info("pm25 not found in data, ignoring", "obj", obj)
-       } else {
-               processPM25(location, pm25, sendChannel)
+       }
+
+       pm10, ok := obj["pm10"].(float64)
+       if !ok {
+               logger.V(3).Info("pm10 not found in data, ignoring", "obj", obj)
+       }
+
+       if pm10 != 0.0 || pm25 != 0.0 {
+               processPM25(location, pm10, pm25, sendChannel)
        }
 
        temperature, ok := obj["temperature"].(float64)
@@ -639,6 +662,7 @@ func expireStaleMetric(expireMap map[string]time.Time, expireGauge *prometheus.G
 func expireStaleMetrics() {
        expireStaleMetric(tempExpire, tempGauge)
        expireStaleMetric(powerExpire, powerGauge)
+       expireStaleMetric(pm10Expire, pm10Gauge)
        expireStaleMetric(pm25Expire, pm25Gauge)
        expireStaleMetric(aqiExpire, aqiGauge)
        expireStaleMetric(uptimeExpire, uptimeGauge)