Couple bugfixes
authorjweigele <jweigele@local>
Wed, 14 Dec 2022 02:41:32 +0000 (18:41 -0800)
committerjweigele <jweigele@local>
Wed, 14 Dec 2022 02:42:03 +0000 (18:42 -0800)
 * stairway_pico was added twice in reprocess, fixed
 * Being unable to process action as a map (which sometimes happens in
   zigbee) resulted in a panic in reprocess from time to time, just
printed and continued for now, though more changes might be needed
elsewhere
 * Very large gaping hole in the pico relayData code - it basically just
   set the RGB values then turned around and set them all to 0 (lol!).
So uhh, I added back in the } else { part of that block and now it seems
fine.

lights/main.go
reprocess/main.go

index 9c2e499cd8adc9cce87ec46c448cfdf2a7cd5e72..3f0e95f7f6f1c710ff30ae28ee090bcb5c252ad7 100644 (file)
@@ -449,6 +449,7 @@ func (relayData PicoRelayData) getRelayData(red, green, blue float64) (map[strin
                actionMap["red"] = red
                actionMap["green"] = green
                actionMap["blue"] = blue
+       } else {
                actionMap["red"] = 0.0
                actionMap["green"] = 0.0
                actionMap["blue"] = 0.0
index 8a36cc0181334f68fd248ea139b29d782eda71be..077031cab3364daaee420ae2c00a17e9a6cb6494 100644 (file)
@@ -147,6 +147,7 @@ func newpowerdevice(friendlyName, powerName string, queryNeeded bool) *powerdevi
 
 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
        celsius := obj["celsius"].(float64)
        location := obj["location"].(string)
        now := time.Now().UTC()
@@ -165,42 +166,46 @@ func handleDIY(obj map[string]interface{}, sendChannel chan helper.RabbitSend) {
        _, isAction := obj["action"]
        if isAction {
                // this should also already be decoded for us
-               data := obj["action"].(map[string]interface{})
-               var senseType string
+               data, castSuccess := obj["action"].(map[string]interface{})
+               if castSuccess {
+                       var senseType string
 
-               _, isTemp := data["celsius"]
-               if isTemp {
-                       senseType = "temperature"
-               } else {
-                       _, hasType := data["type"]
-                       if hasType {
-                               senseType = data["type"].(string)
+                       _, isTemp := data["celsius"]
+                       if isTemp {
+                               senseType = "temperature"
                        } else {
-                               logger.Info("Did not find any sense type, just returning")
-                               return
+                               _, hasType := data["type"]
+                               if hasType {
+                                       senseType = data["type"].(string)
+                               } else {
+                                       logger.Info("Did not find any sense type, just returning")
+                                       return
+                               }
+                       }
+                       // should have the senseType setup correctly now, if we're still here
+                       dataMap := make(map[string]interface{}, 0)
+
+                       if senseType == "temperature" {
+                               // these should all exist properly
+                               dataMap["fahrenheit"] = data["celsius"].(float64)*9/5 + 32
+                               dataMap["celsius"] = data["celsius"].(float64)
+                               dataMap["location"] = data["location"].(string)
+                               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
+                       } else if senseType == "motion" {
+                               dataMap["motion_detected"] = data["state"].(bool)
+                               // copied from above, but it might change later so shrug
+                               dataMap["location"] = data["location"].(string)
+                               logger.V(2).Info("Sending reprocessed motion")
+                               sendThis := helper.RabbitSend{Data: dataMap, RoutingKey: "motion", IncludeDate: true}
+                               sendChannel <- sendThis
+                       } else {
+                               logger.Info("Sense type not detected, ignoring")
                        }
-               }
-               // should have the senseType setup correctly now, if we're still here
-               dataMap := make(map[string]interface{}, 0)
-
-               if senseType == "temperature" {
-                       // these should all exist properly
-                       dataMap["fahrenheit"] = data["celsius"].(float64)*9/5 + 32
-                       dataMap["celsius"] = data["celsius"].(float64)
-                       dataMap["location"] = data["location"].(string)
-                       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
-               } else if senseType == "motion" {
-                       dataMap["motion_detected"] = data["state"].(bool)
-                       // copied from above, but it might change later so shrug
-                       dataMap["location"] = data["location"].(string)
-                       logger.V(2).Info("Sending reprocessed motion")
-                       sendThis := helper.RabbitSend{Data: dataMap, RoutingKey: "motion", IncludeDate: true}
-                       sendChannel <- sendThis
                } else {
-                       logger.Info("Sense type not detected, ignoring")
+                       logger.Error(nil, "Unable to cast action key to map ignoring this delivery", "action", obj["action"])
                }
 
        } else {
@@ -389,7 +394,6 @@ func main() {
        // DIY devices
        devices = append(devices, newdiydevice("office_pico"))
        devices = append(devices, newdiydevice("stairway_pico"))
-       devices = append(devices, newdiydevice("stairway_pico"))
        devices = append(devices, newdiydevice("downstairs_stairway_pico"))
        devices = append(devices, newdiydevice("upstairs_pico"))
        devices = append(devices, newdiydevice("data_closet_pico"))