add another type of device that I made custom
authorjweigele <jweigele@local>
Wed, 12 Jul 2023 07:47:37 +0000 (00:47 -0700)
committerjweigele <jweigele@local>
Wed, 12 Jul 2023 07:47:37 +0000 (00:47 -0700)
 * it's different from my _current_ diy devices in that the firmware is
   programmed from the ground up, so the exposes/cluster/peripherals may
   all change - keep that in mind! but, seems to work for now

reprocess/main.go

index bd97a61c9b2659a978335c0eaa953816132c87fa..9d4d80532c0b7d9e689c13ada7ef4d05d58f9801 100644 (file)
@@ -126,6 +126,11 @@ type powerdevice struct {
        powerList           []powertime
 }
 
+type hexdevice struct {
+       *zigdevice
+       location string
+}
+
 func (dev *powerdevice) filter() {
        // if this gets excessive we can pass it down the chain
        now := time.Now().UTC()
@@ -198,6 +203,13 @@ func newpowerdevice(friendlyName, powerName string, queryNeeded bool) *powerdevi
        }
 }
 
+func newhexdevice(friendlyName, location string) *hexdevice {
+       return &hexdevice{
+               zigdevice: newdevice(friendlyName),
+               location:  location,
+       }
+}
+
 func handleTemp(obj map[string]interface{}) {
        logger.V(1).Info("Temperature received", "obj", obj)
        // these should both probably be checked for existence and type but oh well
@@ -216,6 +228,36 @@ func handleTemp(obj map[string]interface{}) {
 
 }
 
+func processPM25(location string, 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["pm25"] = pm25
+       results, err := aqi.Calculate(aqi.PM25{Concentration: pm25})
+       if err != nil {
+               logger.Error(nil, "Unable to calculate AQI, ignoring this delivery")
+               return
+       }
+
+       aqi := results.AQI
+       dataMap["aqi"] = aqi
+
+       sendThis := helper.RabbitSend{Data: dataMap, RoutingKey: "aqi", IncludeDate: true}
+       sendChannel <- sendThis
+       // we don't want to have to loop back around on rabbit processing, so just set the gauge here
+       now := time.Now().UTC()
+       // do the label update here
+       pm25Gauge.With(prometheus.Labels{"location": location}).Set(pm25)
+       aqiGauge.With(prometheus.Labels{"location": location}).Set(aqi)
+
+       expireMutex.Lock()
+       defer expireMutex.Unlock()
+
+       aqiExpire[location] = now
+       pm25Expire[location] = now
+
+}
+
 func (dev *diydevice) handleDIY(obj map[string]interface{}, sendChannel chan helper.RabbitSend) {
        logger.V(1).Info("DIY data received", "obj", obj)
        _, isAction := obj["action"]
@@ -259,31 +301,7 @@ func (dev *diydevice) handleDIY(obj map[string]interface{}, sendChannel chan hel
                        } else if senseType == "pm25" {
                                location := data["location"].(string)
                                pm25 := data["pm25"].(float64)
-                               dataMap["location"] = location
-                               dataMap["pm25"] = pm25
-                               results, err := aqi.Calculate(aqi.PM25{Concentration: pm25})
-                               if err != nil {
-                                       logger.Error(nil, "Unable to calculate AQI, ignoring this delivery")
-                                       return
-                               }
-
-                               aqi := results.AQI
-                               dataMap["aqi"] = aqi
-
-                               sendThis := helper.RabbitSend{Data: dataMap, RoutingKey: "aqi", IncludeDate: true}
-                               sendChannel <- sendThis
-                               // we don't want to have to loop back around on rabbit processing, so just set the gauge here
-                               now := time.Now().UTC()
-                               // do the label update here
-                               pm25Gauge.With(prometheus.Labels{"location": location}).Set(pm25)
-                               aqiGauge.With(prometheus.Labels{"location": location}).Set(aqi)
-
-                               expireMutex.Lock()
-                               defer expireMutex.Unlock()
-
-                               aqiExpire[location] = now
-                               pm25Expire[location] = now
-
+                               processPM25(location, pm25, sendChannel)
                        } else {
                                logger.Info("Sense type not detected, ignoring")
                        }
@@ -325,9 +343,36 @@ func (dev *powerdevice) handlePower(obj map[string]interface{}, sendChannel chan
 
 }
 
+func (dev *hexdevice) handleHex(obj map[string]interface{}, sendChannel chan helper.RabbitSend) {
+       //now := time.Now().UTC()
+       logger.V(1).Info("Hex data received", "obj", obj)
+       pm25, ok := obj["pm25"].(float64)
+       if !ok {
+               logger.Error(nil, "contact not found in data, ignoring", "obj", obj)
+               return
+       }
+       processPM25(dev.location, pm25, sendChannel)
+
+       temperature, ok := obj["temperature"].(float64)
+       if !ok {
+               logger.Error(nil, "contact not found in data, ignoring", "obj", obj)
+               return
+       }
+
+       dataMap := make(map[string]interface{}, 0)
+       // these should all exist properly
+       dataMap["fahrenheit"] = temperature*9/5 + 32
+       dataMap["celsius"] = temperature
+       dataMap["location"] = dev.location
+       logger.V(2).Info("Sending reprocessed temperature")
+       // hardcoded temp routingKey for this type of measurement
+       sendThis := helper.RabbitSend{Data: dataMap, RoutingKey: "temp", IncludeDate: true}
+       sendChannel <- sendThis
+}
+
 func (dev *doordevice) handleDoor(obj map[string]interface{}, sendChannel chan helper.RabbitSend) {
        //now := time.Now().UTC()
-       logger.V(0).Info("Door data received", "obj", obj)
+       logger.V(1).Info("Door data received", "obj", obj)
        doorOpened := false
        doorTampered := false
 
@@ -429,6 +474,10 @@ func readLoop(channel chan helper.RabbitSend, devices []device, rabbit helper.Ra
                                case *doordevice:
                                        logger.V(2).Info("Door device, sending to handle", "device", device)
                                        device.(*doordevice).handleDoor(item, channel)
+                               case *hexdevice:
+                                       logger.V(2).Info("Hex device, sending to handle", "device", device)
+                                       device.(*hexdevice).handleHex(item, channel)
+
                                default:
                                        logger.Info("Found a device we can't classify", "device", device, "type", t)
                                }
@@ -564,6 +613,9 @@ func main() {
        devices = append(devices, newdoordevice("front_door"))
        devices = append(devices, newdoordevice("data_door"))
 
+       // hex devices (super custom)
+       devices = append(devices, newhexdevice("0xfffe7e6af7f95560", "Downstairs Test"))
+
        //currentTemp, err := fetchTemp(weatherStation)
        channel := make(chan helper.RabbitSend)