|
|
|
@@ -3,6 +3,7 @@ package bridge |
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"log/slog" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
@@ -13,15 +14,10 @@ import ( |
|
|
|
"github.com/segmentio/kafka-go" |
|
|
|
) |
|
|
|
|
|
|
|
// BeaconLookup provides MAC->ID lookup (e.g. AppState). |
|
|
|
type BeaconLookup interface { |
|
|
|
BeaconExists(mac string) (id string, ok bool) |
|
|
|
} |
|
|
|
|
|
|
|
// HandleMQTTMessage processes an MQTT message: parses JSON array of RawReading or CSV. |
|
|
|
// For JSON, converts each reading to BeaconAdvertisement and writes to the writer if MAC is in lookup. |
|
|
|
// Hostname is derived from topic (e.g. "publish_out/gateway1" -> "gateway1"). Safe if topic has no "/". |
|
|
|
func HandleMQTTMessage(topic string, payload []byte, lookup BeaconLookup, writer *kafka.Writer) { |
|
|
|
func HandleMQTTMessage(topic string, payload []byte, appState *appcontext.AppState, writer *kafka.Writer) { |
|
|
|
parts := strings.SplitN(topic, "/", 2) |
|
|
|
hostname := "" |
|
|
|
if len(parts) >= 2 { |
|
|
|
@@ -39,10 +35,11 @@ func HandleMQTTMessage(topic string, payload []byte, lookup BeaconLookup, writer |
|
|
|
if reading.Type == "Gateway" { |
|
|
|
continue |
|
|
|
} |
|
|
|
id, ok := lookup.BeaconExists(reading.MAC) |
|
|
|
id, ok := appState.BeaconExists(reading.MAC) |
|
|
|
if !ok { |
|
|
|
continue |
|
|
|
} |
|
|
|
fmt.Println("beacon found: ", id) |
|
|
|
adv := appcontext.BeaconAdvertisement{ |
|
|
|
ID: id, |
|
|
|
Hostname: hostname, |
|
|
|
|