package main import ( "fmt" "io/ioutil" "log" "os" "strings" "time" amqp "github.com/rabbitmq/amqp091-go" ) func failOnError(err error, msg string) { if err != nil { log.Panicf("%s: %s", msg, err) } } func create_and_connect_to_queue(ch *amqp.Channel, name string, routing_key string) <-chan amqp.Delivery { q, err := ch.QueueDeclare( name, // name false, // durable false, // delete when unused true, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") err = ch.QueueBind( q.Name, // queue name routing_key, // routing key "amq.topic", // exchange false, nil, ) failOnError(err, "Failed to bind a queue") msgs, err := ch.Consume( q.Name, // queue "", // consumer true, // auto ack false, // exclusive false, // no local false, // no wait nil, // args ) failOnError(err, "Failed to register a consumer") return msgs } func main() { // read token from /token/read_write_token.txt file token_file := "/tokens/read_write.txt" influx_token, err := ioutil.ReadFile(token_file) failOnError(err, "Failed to read token file") fmt.Println("InfluxDB token:") fmt.Println(strings.TrimSpace(string(influx_token))) var conn *amqp.Connection amqp_url := os.Getenv("AMQP_URL") for { conn, err = amqp.Dial(amqp_url) if err != nil { log.Printf("Failed to connect to RabbitMQ: %s", err) time.Sleep(5 * time.Second) } else { break } } defer conn.Close() ch, err := conn.Channel() failOnError(err, "Failed to open a channel") defer ch.Close() err = ch.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{} shelly_msgs := create_and_connect_to_queue(ch, "shelly", "shellies.#") tasmota_msgs := create_and_connect_to_queue(ch, "tasmota", "tele.#.SENSOR") go RunPipe( shelly_msgs, os.Getenv("INFLUX_URL"), strings.TrimSpace(string(influx_token)), os.Getenv("INFLUX_ORG"), os.Getenv("INFLUX_BUCKET"), ShellyConverter, ) go RunPipe( tasmota_msgs, os.Getenv("INFLUX_URL"), strings.TrimSpace(string(influx_token)), os.Getenv("INFLUX_ORG"), os.Getenv("INFLUX_BUCKET"), TasmotaConverter, ) log.Printf(" [*] Waiting for logs. To exit press CTRL+C") <-forever }