"fmt"
"os"
"reflect"
+ "strconv"
"strings"
"sync"
"time"
[]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",
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)
}
+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)
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)
}
}
}
+ 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 {
expireStaleMetric(pm10Expire, pm10Gauge)
expireStaleMetric(pm25Expire, pm25Gauge)
expireStaleMetric(aqiExpire, aqiGauge)
+ expireStaleMetric(co2Expire, co2Gauge)
+ expireStaleMetric(humidityExpire, humidityGauge)
expireStaleMetric(versionExpire, versionGauge)
expireStaleMetric(uptimeExpire, uptimeGauge)
expireStaleMetric(internalTempExpire, internalTempGauge)