75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
|
"github.com/influxdata/influxdb-client-go/v2/api/write"
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
)
|
|
|
|
func ShellyConverter(d amqp.Delivery) ([]*write.Point, error) {
|
|
log.Printf(" [x] %s: %s", d.RoutingKey, d.Body)
|
|
parts := strings.Split(d.RoutingKey, ".")
|
|
field := parts[len(parts)-1]
|
|
device := strings.Join(parts[1:len(parts)-1], ".")
|
|
|
|
// try to cast to float64
|
|
float_value, err := strconv.ParseFloat(string(d.Body), 64)
|
|
|
|
var values map[string]interface{}
|
|
|
|
if err == nil {
|
|
values = map[string]interface{}{
|
|
field: float_value,
|
|
}
|
|
} else {
|
|
values = map[string]interface{}{
|
|
field: string(d.Body),
|
|
}
|
|
}
|
|
|
|
return []*write.Point{influxdb2.NewPoint(
|
|
"shelly",
|
|
map[string]string{
|
|
"device": device,
|
|
},
|
|
values,
|
|
time.Now(),
|
|
)}, nil
|
|
}
|
|
|
|
func TasmotaConverter(d amqp.Delivery) ([]*write.Point, error) {
|
|
log.Printf(" [ ] %s: %s", d.RoutingKey, d.Body)
|
|
parts := strings.Split(d.RoutingKey, ".")
|
|
device := strings.Join(parts[1:len(parts)-1], ".")
|
|
|
|
// read body as json
|
|
var values map[string]interface{}
|
|
err := json.Unmarshal(d.Body, &values)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var sensor_data map[string]interface{} = values["AM2301"].(map[string]interface{})
|
|
|
|
// get relevant values
|
|
relevant_values := map[string]interface{}{
|
|
"Temperature": sensor_data["Temperature"],
|
|
"Humidity": sensor_data["Humidity"],
|
|
"DewPoint": sensor_data["DewPoint"],
|
|
}
|
|
|
|
return []*write.Point{influxdb2.NewPoint(
|
|
"tasmota",
|
|
map[string]string{
|
|
"device": device,
|
|
},
|
|
relevant_values,
|
|
time.Now(),
|
|
)}, nil
|
|
}
|