You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.2 KiB
59 lines
1.2 KiB
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")
|
|
}
|
|
}
|