|
|
|
@@ -134,23 +134,31 @@ func main() { |
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) |
|
|
|
defer stop() |
|
|
|
|
|
|
|
// define kafka reader |
|
|
|
// define kafka readers |
|
|
|
apiReader := appState.AddKafkaReader(cfg.KafkaURL, "apibeacons", "bridge-api") |
|
|
|
alertReader := appState.AddKafkaReader(cfg.KafkaURL, "alert", "bridge-alert") |
|
|
|
|
|
|
|
// define kafka writer |
|
|
|
writer := appState.AddKafkaWriter(cfg.KafkaURL, "rawbeacons") |
|
|
|
|
|
|
|
slog.Info("Bridge initialized, subscribed to kafka topics") |
|
|
|
|
|
|
|
chApi := make(chan model.ApiUpdate, 200) |
|
|
|
chAlert := make(chan model.Alert, 200) |
|
|
|
|
|
|
|
wg.Add(1) |
|
|
|
wg.Add(2) |
|
|
|
go kafkaclient.Consume(apiReader, chApi, ctx, &wg) |
|
|
|
go kafkaclient.Consume(alertReader, chAlert, ctx, &wg) |
|
|
|
|
|
|
|
opts := mqtt.NewClientOptions() |
|
|
|
opts.AddBroker(fmt.Sprintf("tcp://%s:%d", cfg.MQTTHost, 1883)) |
|
|
|
opts.SetClientID("go_mqtt_client") |
|
|
|
opts.SetUsername("emqx") |
|
|
|
opts.SetPassword("public") |
|
|
|
opts.SetAutoReconnect(true) |
|
|
|
opts.SetConnectRetry(true) |
|
|
|
opts.SetConnectRetryInterval(1 * time.Second) |
|
|
|
opts.SetMaxReconnectInterval(600 * time.Second) |
|
|
|
opts.SetCleanSession(false) |
|
|
|
|
|
|
|
opts.SetDefaultPublishHandler(func(c mqtt.Client, m mqtt.Message) { messagePubHandler(m, writer, appState) }) |
|
|
|
opts.OnConnect = connectHandler |
|
|
|
opts.OnConnectionLost = connectLostHandler |
|
|
|
@@ -184,6 +192,13 @@ eventloop: |
|
|
|
lMsg := fmt.Sprintf("Beacon removed from lookup: %s", id) |
|
|
|
slog.Info(lMsg) |
|
|
|
} |
|
|
|
case msg := <-chAlert: |
|
|
|
fmt.Printf("Alerts: %+v\n", msg) |
|
|
|
p, err := json.Marshal(msg) |
|
|
|
if err != nil { |
|
|
|
continue |
|
|
|
} |
|
|
|
client.Publish("/alerts", 0, true, p) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@@ -198,16 +213,6 @@ eventloop: |
|
|
|
slog.Info("Closing connection to MQTT broker") |
|
|
|
} |
|
|
|
|
|
|
|
func publish(client mqtt.Client) { |
|
|
|
num := 10 |
|
|
|
for i := 0; i < num; i++ { |
|
|
|
text := fmt.Sprintf("Message %d", i) |
|
|
|
token := client.Publish("topic/test", 0, false, text) |
|
|
|
token.Wait() |
|
|
|
time.Sleep(time.Second) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func sub(client mqtt.Client) { |
|
|
|
topic := "publish_out/#" |
|
|
|
token := client.Subscribe(topic, 1, nil) |
|
|
|
|