package services import ( "context" "git.aiterp.net/lucifer/new-server/app/config" "git.aiterp.net/lucifer/new-server/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() { drift := time.Now().Add(time.Minute).Truncate(time.Minute).Sub(time.Now()) time.Sleep(drift + time.Millisecond * 5) for range time.NewTicker(time.Minute).C { config.EventChannel <- models.Event{Name: "TimeChanged"} } }() } var loc, _ = time.LoadLocation("Europe/Oslo") var ctx = context.Background() var X = false func handleEvent(event models.Event) { if !event.HasPayload("hour") { event.AddPayload("hour", time.Now().In(loc).Format("15")) } if !event.HasPayload("minute") { event.AddPayload("minute", time.Now().In(loc).Format("04")) } if !X { log.Println("Unhandled event: " + event.Name) return } 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 } } }