home_data/gatherer/main.go

65 lines
1.4 KiB
Go
Raw Normal View History

2023-02-11 20:32:51 +00:00
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() {
2023-02-16 19:22:28 +00:00
amqpConn := AMQPConnection{}
amqpConn.Init()
err := amqpConn.Connect()
defer amqpConn.Close()
failOnError(err, "Failed to connect to RabbitMQ")
2023-02-11 20:32:51 +00:00
// read token from /token/read_write_token.txt file
token_file := "/tokens/read_write.txt"
2023-02-16 19:22:28 +00:00
token_bytes, err := ioutil.ReadFile(token_file)
2023-02-11 20:32:51 +00:00
failOnError(err, "Failed to read token file")
2023-02-16 19:22:28 +00:00
influx_token := strings.TrimSpace(string(token_bytes))
2023-02-11 20:32:51 +00:00
fmt.Println("InfluxDB token:")
fmt.Println(strings.TrimSpace(string(influx_token)))
2023-02-16 19:22:28 +00:00
influx := InfluxWriter{}
influx.Init(
os.Getenv("INFLUX_URL"),
strings.TrimSpace(string(influx_token)),
os.Getenv("INFLUX_ORG"),
os.Getenv("INFLUX_BUCKET"),
)
2023-02-11 20:32:51 +00:00
2023-02-16 19:22:28 +00:00
// create exchange
err = amqpConn.channel.ExchangeDeclare(
2023-02-11 20:32:51 +00:00
"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{}
2023-02-16 20:12:15 +00:00
shellPipe := new(Piper)
2023-02-16 20:06:24 +00:00
shellPipe.Start(&influx, &amqpConn, "shelly", "shellies.#", ShellyConverter)
2023-02-11 20:32:51 +00:00
2023-02-16 20:12:15 +00:00
tasmotaPipe := new(Piper)
2023-02-16 20:06:24 +00:00
tasmotaPipe.Start(&influx, &amqpConn, "tasmota", "tele.#.SENSOR", TasmotaConverter)
2023-02-11 20:32:51 +00:00
log.Printf(" [*] Waiting for logs. To exit press CTRL+C")
<-forever
}