Add version parsing to esp32 devices
authorjweigele <jweigele@local>
Tue, 20 Feb 2024 01:08:59 +0000 (17:08 -0800)
committerjweigele <jweigele@local>
Tue, 20 Feb 2024 01:08:59 +0000 (17:08 -0800)
This simply uses a global topic bind and then sets gauges internally,
with approrpriate labels for each value. Expiration is similarly kinda
complicated.

mattersocket/mattersocket.go
reprocess/main.go

index 63dc817a6aa1a97ff37a770def4730cc85dfa7f1..5280c8af13d618dc97009e2d1af9c0dd89e0d46d 100644 (file)
@@ -42,7 +42,7 @@ type MatterSocket struct {
 }
 
 func (matter *MatterSocket) Init() {
-       matter.Address = "iotbridge:5580"
+       matter.Address = "matterbridge:5580"
        flag.Parse()
        log.SetFlags(0)
 
index fc6d0c0353eeececdd7162b75154637bdbce2023..2b42175114d519a791cc1d241cb282e14f19cb8c 100644 (file)
@@ -67,6 +67,14 @@ var (
                []string{"location"},
        )
 
+       versionGauge = promauto.NewGaugeVec(
+               prometheus.GaugeOpts{
+                       Name: "sensor_version",
+                       Help: "Various information to map a hardware sensor back to compiled version",
+               },
+               []string{"location", "app_version", "idf_version", "mac", "ext"},
+       )
+
        uptimeGauge = promauto.NewGaugeVec(
                prometheus.GaugeOpts{
                        Name: "sensor_uptime",
@@ -104,6 +112,7 @@ var (
        pm10Expire         = make(map[string]time.Time)
        pm25Expire         = make(map[string]time.Time)
        aqiExpire          = make(map[string]time.Time)
+       versionExpire      = make(map[string]time.Time)
        uptimeExpire       = make(map[string]time.Time)
        internalTempExpire = make(map[string]time.Time)
 
@@ -281,6 +290,26 @@ func handleAQI(obj map[string]interface{}) {
 
 }
 
+func handleVersion(obj map[string]interface{}) {
+       now := time.Now().UTC()
+       extAddr, ok := obj["ext"].(string)
+       if !ok {
+               extAddr = ""
+       }
+       versionGauge.With(prometheus.Labels{"location": obj["location"].(string),
+               "app_version": obj["app_version"].(string),
+               "idf_version": obj["idf_version"].(string),
+               "mac":         obj["mac"].(string),
+               "ext":         extAddr,
+       }).Set(1)
+
+       versionHash := labelValHash{labelValues: []string{obj["location"].(string), obj["app_version"].(string), obj["idf_version"].(string), obj["mac"].(string), extAddr}}
+       expireMutex.Lock()
+       defer expireMutex.Unlock()
+       versionExpire[versionHash.getHash()] = now
+
+}
+
 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)
@@ -568,6 +597,13 @@ func readLoop(channel chan helper.RabbitSend, devices []device, rabbit helper.Ra
                os.Exit(1)
        }
 
+       /// this one is hardcoded, for the generic sensor version info
+       err = helper.Bind("esp32.version_info", &rabbit)
+       if err != nil {
+               logger.Error(err, "unable to bind successfully to exchange", "routingKey", "esp32.version_info")
+               os.Exit(1)
+       }
+
        deliveries, err := helper.StartConsuming(rabbit)
        if err != nil {
                logger.Error(err, "unable to start consuming data from rabbit")
@@ -593,6 +629,8 @@ func readLoop(channel chan helper.RabbitSend, devices []device, rabbit helper.Ra
                        handleTemp(item)
                } else if delivery.RoutingKey == "aqi" {
                        handleAQI(item)
+               } else if delivery.RoutingKey == "esp32.version_info" {
+                       handleVersion(item)
                }
                for _, device := range devices {
                        if device.getRoutingKey() == delivery.RoutingKey {
@@ -708,6 +746,7 @@ func expireStaleMetrics() {
        expireStaleMetric(pm10Expire, pm10Gauge)
        expireStaleMetric(pm25Expire, pm25Gauge)
        expireStaleMetric(aqiExpire, aqiGauge)
+       expireStaleMetric(versionExpire, versionGauge)
        expireStaleMetric(uptimeExpire, uptimeGauge)
        expireStaleMetric(internalTempExpire, internalTempGauge)