home_data/gatherer/main.go

67 lines
1.5 KiB
Go

package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
func failOnError(err error, msg string) {
if err != nil {
log.Panicf("%s: %s", msg, err)
}
}
func main() {
amqpConn := AMQPConnection{}
amqpConn.Init()
err := amqpConn.Connect()
defer amqpConn.Close()
failOnError(err, "Failed to connect to RabbitMQ")
// read token from /token/read_write_token.txt file
token_file := "/tokens/read_write.txt"
token_bytes, err := ioutil.ReadFile(token_file)
failOnError(err, "Failed to read token file")
influx_token := strings.TrimSpace(string(token_bytes))
fmt.Println("InfluxDB token:")
fmt.Println(strings.TrimSpace(string(influx_token)))
influx := InfluxWriter{}
influx.Init(
os.Getenv("INFLUX_URL"),
strings.TrimSpace(string(influx_token)),
os.Getenv("INFLUX_ORG"),
os.Getenv("INFLUX_BUCKET"),
)
// create exchange
err = amqpConn.channel.ExchangeDeclare(
"amq.topic", // name
"topic", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare an exchange")
var forever chan struct{}
shellPipe := Piper{}
shellPipe.Init(&influx, amqpConn, "shelly", "shellies.#")
go shellPipe.RunPipe(ShellyConverter)
tasmotaPipe := Piper{}
tasmotaPipe.Init(&influx, amqpConn, "tasmota", "tele.#.SENSOR")
go tasmotaPipe.RunPipe(TasmotaConverter)
log.Printf(" [*] Waiting for logs. To exit press CTRL+C")
<-forever
}