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 19:22:28 +00:00
|
|
|
shellPipe := Piper{}
|
2023-02-16 20:01:30 +00:00
|
|
|
shellPipe.Init(&influx, &amqpConn, "shelly", "shellies.#")
|
2023-02-16 19:22:28 +00:00
|
|
|
go shellPipe.RunPipe(ShellyConverter)
|
2023-02-11 20:32:51 +00:00
|
|
|
|
2023-02-16 19:22:28 +00:00
|
|
|
tasmotaPipe := Piper{}
|
2023-02-16 20:01:30 +00:00
|
|
|
tasmotaPipe.Init(&influx, &amqpConn, "tasmota", "tele.#.SENSOR")
|
2023-02-16 19:22:28 +00:00
|
|
|
go tasmotaPipe.RunPipe(TasmotaConverter)
|
2023-02-11 20:32:51 +00:00
|
|
|
|
|
|
|
log.Printf(" [*] Waiting for logs. To exit press CTRL+C")
|
|
|
|
<-forever
|
|
|
|
|
|
|
|
}
|