diff --git a/bridge b/bridge index 320d445..43838a3 100755 Binary files a/bridge and b/bridge differ diff --git a/decoder b/decoder index 34ca632..3814f59 100755 Binary files a/decoder and b/decoder differ diff --git a/internal/app/decoder/app.go b/internal/app/decoder/app.go index defc355..d521435 100644 --- a/internal/app/decoder/app.go +++ b/internal/app/decoder/app.go @@ -2,7 +2,6 @@ package decoder import ( "context" - "fmt" "log/slog" "sync" "time" @@ -84,7 +83,6 @@ func (a *DecoderApp) Run(ctx context.Context) { continue } case msg := <-a.ChRaw: - fmt.Println("msg: ", msg) decoder.ProcessIncoming(msg, a.AppState, a.KafkaManager.GetWriter("alertbeacons"), a.ParserRegistry) case msg := <-a.ChParser: switch msg.ID { diff --git a/internal/app/server/events.go b/internal/app/server/events.go index 033e521..d1b4af2 100644 --- a/internal/app/server/events.go +++ b/internal/app/server/events.go @@ -23,11 +23,11 @@ func RunEventLoop(ctx context.Context, a *ServerApp) { case <-ctx.Done(): return case msg := <-a.ChHealthLocation: - slog.Info("location health", "health", msg) + a.AppState.UpdateLocationHealth(msg) case msg := <-a.ChHealthDecoder: - slog.Info("decoder health", "health", msg) + a.AppState.UpdateDecoderHealth(msg) case msg := <-a.ChHealthBridge: - slog.Info("bridge health", "health", msg) + a.AppState.UpdateBridgeHealth(msg) case msg := <-a.ChLoc: switch msg.Method { case "Standard": diff --git a/internal/app/server/routes.go b/internal/app/server/routes.go index 730c1f1..0db3df8 100644 --- a/internal/app/server/routes.go +++ b/internal/app/server/routes.go @@ -59,6 +59,8 @@ func (a *ServerApp) RegisterRoutes() http.Handler { // Tracks r.HandleFunc("/reslevis/getTracks/{id}", controller.TracksListController(a.DB, a.ctx)).Methods("GET") + r.HandleFunc("/reslevis/health", controller.HealthController(a.AppState, a.ctx)).Methods("GET") + chain := middleware.Recovery(middleware.Logging(middleware.RequestID(middleware.CORS(nil, nil, nil)(r)))) return chain } diff --git a/internal/pkg/common/appcontext/context.go b/internal/pkg/common/appcontext/context.go index 1fd6a53..cdf3331 100644 --- a/internal/pkg/common/appcontext/context.go +++ b/internal/pkg/common/appcontext/context.go @@ -4,6 +4,7 @@ import ( "fmt" "log/slog" "os" + "sync" "time" "github.com/AFASystems/presence/internal/pkg/kafkaclient" @@ -18,6 +19,7 @@ type AppState struct { beaconsLookup BeaconsLookup health Health startTime time.Time + mu sync.RWMutex } func getEnv(key, def string) string { @@ -52,7 +54,6 @@ func NewAppState() *AppState { Lookup: make(map[string]string), }, health: Health{ - ID: "1", Location: LocationHealth{ BaseHealth: BaseHealth{ Uptime: 0, @@ -81,6 +82,30 @@ func NewAppState() *AppState { } } +func (m *AppState) GetHealth() Health { + m.mu.RLock() + defer m.mu.RUnlock() + return m.health +} + +func (m *AppState) UpdateLocationHealth(h LocationHealth) { + m.mu.Lock() + m.health.Location = h + m.mu.Unlock() +} + +func (m *AppState) UpdateDecoderHealth(h DecoderHealth) { + m.mu.Lock() + m.health.Decoder = h + m.mu.Unlock() +} + +func (m *AppState) UpdateBridgeHealth(h BridgeHealth) { + m.mu.Lock() + m.health.Bridge = h + m.mu.Unlock() +} + func (m *AppState) GetLocationHealth(k *kafkaclient.KafkaManager) ([]byte, error) { m.health.Location.GetUptime(m.startTime) m.health.Location.GetActiveReaders(k) diff --git a/internal/pkg/common/appcontext/health.go b/internal/pkg/common/appcontext/health.go index 6852a77..92ab6fb 100644 --- a/internal/pkg/common/appcontext/health.go +++ b/internal/pkg/common/appcontext/health.go @@ -29,7 +29,6 @@ type BridgeHealth struct { } type Health struct { - ID string `json:"id" gorm:"primaryKey"` Location LocationHealth `gorm:"embedded;embeddedPrefix:loc_"` Decoder DecoderHealth `gorm:"embedded;embeddedPrefix:dec_"` Bridge BridgeHealth `gorm:"embedded;embeddedPrefix:brg_"` diff --git a/internal/pkg/common/utils/beacons.go b/internal/pkg/common/utils/beacons.go index df75e6c..3945491 100644 --- a/internal/pkg/common/utils/beacons.go +++ b/internal/pkg/common/utils/beacons.go @@ -1,6 +1,8 @@ package utils import ( + "fmt" + "github.com/AFASystems/presence/internal/pkg/common/appcontext" "github.com/AFASystems/presence/internal/pkg/model" ) @@ -38,16 +40,18 @@ func RemoveFlagBytes(b []byte) []byte { } // Generate event based on the Beacon type -func LoopADStructures(b []byte, i [][2]int, id string, parserRegistry *model.ParserRegistry) appcontext.BeaconEvent { +func LoopADStructures(b []byte, i [][2]int, id string, parserRegistry *model.ParserRegistry, beacon string) appcontext.BeaconEvent { be := appcontext.BeaconEvent{} for _, r := range i { ad := b[r[0]:r[1]] if !isValidADStructure(ad) { + fmt.Println("invalid ad structure: ", beacon) break } for name, parser := range parserRegistry.ParserList { if parser.CanParse(ad) { event, ok := parser.Parse(name, ad) + fmt.Println("parser can parse: ", name) if ok { event.ID = id event.Name = id @@ -55,6 +59,7 @@ func LoopADStructures(b []byte, i [][2]int, id string, parserRegistry *model.Par } } } + // fmt.Println("no parser can parse: ", beacon) } return be diff --git a/internal/pkg/database/database.go b/internal/pkg/database/database.go index 4d0b515..70aee1e 100644 --- a/internal/pkg/database/database.go +++ b/internal/pkg/database/database.go @@ -26,7 +26,7 @@ func Connect(cfg *config.Config) (*gorm.DB, error) { return nil, err } - if err := db.AutoMigrate(&model.Gateway{}, model.Zone{}, model.TrackerZones{}, model.Tracker{}, model.Config{}, appcontext.Settings{}, model.Tracks{}, &model.Alert{}, appcontext.Health{}); err != nil { + if err := db.AutoMigrate(&model.Gateway{}, model.Zone{}, model.TrackerZones{}, model.Tracker{}, model.Config{}, appcontext.Settings{}, model.Tracks{}, &model.Alert{}); err != nil { return nil, err } diff --git a/internal/pkg/decoder/process.go b/internal/pkg/decoder/process.go index fdc2192..b2bfadc 100644 --- a/internal/pkg/decoder/process.go +++ b/internal/pkg/decoder/process.go @@ -30,8 +30,6 @@ func DecodeBeacon(adv appcontext.BeaconAdvertisement, appState *appcontext.AppSt return nil } - fmt.Println("beacon: ", beacon) - b, err := hex.DecodeString(beacon) if err != nil { return err @@ -39,7 +37,7 @@ func DecodeBeacon(adv appcontext.BeaconAdvertisement, appState *appcontext.AppSt b = utils.RemoveFlagBytes(b) indices := utils.ParseADFast(b) - event := utils.LoopADStructures(b, indices, id, registry) + event := utils.LoopADStructures(b, indices, id, registry, beacon) if event.ID == "" { return nil diff --git a/location b/location index 4f6c3dd..49222f3 100755 Binary files a/location and b/location differ diff --git a/server b/server index f9b9632..6468ab4 100755 Binary files a/server and b/server differ