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()
_, 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 {
// 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"))