package services import ( "context" "git.aiterp.net/lucifer/server3/app/config" "git.aiterp.net/lucifer/server3/models" "log" "time" ) func StartEventHandler() { config.EventChannel<-models.Event{Name: "LuciferStarted"} go func() { for event := range config.EventChannel { handleEvent(event) } }() // Dispatch an HourChanged event at every hour go func() { for { now := time.Now() nextHour := now.Add(time.Hour).Truncate(time.Hour) time.Sleep(nextHour.Sub(now)) config.EventChannel <- models.Event{Name: "HourChanged"} } }() } var loc, _ = time.LoadLocation("Europe/Oslo") var ctx = context.Background() func handleEvent(event models.Event) { if !event.HasPayload("hour") { event.AddPayload("hour", time.Now().In(loc).Format("15")) } handlers, err := config.EventHandlerRepository().FetchAll(ctx) if err != nil { log.Printf("Error fetchin event halders: %d", err) return } for _, handler := range handlers { devices, err := config.DeviceRepository().FetchByReference(ctx, handler.TargetKind, handler.TargetValue) if err != nil { log.Printf("Error fetchin event halders: %d", err) return } if !handler.MatchesEvent(event, devices) { continue } panic("panik") } }