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.

104 lines
2.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "git.aiterp.net/lucifer/new-server/app/config"
  6. "git.aiterp.net/lucifer/new-server/models"
  7. "log"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. func StartEventHandler() {
  13. config.EventChannel<-models.Event{Name: "LuciferStarted"}
  14. go func() {
  15. for event := range config.EventChannel {
  16. handleEvent(event)
  17. }
  18. }()
  19. // Generate TimeChanged event
  20. go func() {
  21. drift := time.Now().Add(time.Minute).Truncate(time.Minute).Sub(time.Now())
  22. time.Sleep(drift + time.Millisecond * 5)
  23. for range time.NewTicker(time.Minute).C {
  24. config.EventChannel <- models.Event{Name: "TimeChanged"}
  25. }
  26. }()
  27. }
  28. var loc, _ = time.LoadLocation("Europe/Oslo")
  29. var ctx = context.Background()
  30. var X = false
  31. func handleEvent(event models.Event) {
  32. if !event.HasPayload("hour") {
  33. event.AddPayload("hour", time.Now().In(loc).Format("15"))
  34. }
  35. if !event.HasPayload("minute") {
  36. event.AddPayload("minute", time.Now().In(loc).Format("04"))
  37. }
  38. err := handleSpecial(event)
  39. if err != nil {
  40. log.Printf("Special event handler error (%s): %v", event.Name, err)
  41. return
  42. }
  43. if !X {
  44. paramStrings := make([]string, 0, 8)
  45. for key, value := range event.Payload {
  46. paramStrings = append(paramStrings, fmt.Sprintf("%s=%s", key, value))
  47. }
  48. log.Printf("Unhandled event %s(%s)", event.Name, strings.Join(paramStrings, ", "))
  49. return
  50. }
  51. handlers, err := config.EventHandlerRepository().FetchAll(ctx)
  52. if err != nil {
  53. log.Printf("Error fetchin event halders: %d", err)
  54. return
  55. }
  56. for _, handler := range handlers {
  57. devices, err := config.DeviceRepository().FetchByReference(ctx, handler.TargetKind, handler.TargetValue)
  58. if err != nil {
  59. log.Printf("Error fetchin event halders: %d", err)
  60. return
  61. }
  62. if !handler.MatchesEvent(event, devices) {
  63. continue
  64. }
  65. }
  66. }
  67. func handleSpecial(event models.Event) error {
  68. switch event.Name {
  69. case models.ENBridgeConnected:
  70. bridgeId, _ := strconv.Atoi(event.Payload["bridgeId"])
  71. bridge, err := config.BridgeRepository().Find(ctx, bridgeId)
  72. if err != nil {
  73. return err
  74. }
  75. devices, err := config.DeviceRepository().FetchByReference(ctx, models.RKBridgeID, event.Payload["bridgeId"])
  76. if err != nil {
  77. return err
  78. }
  79. driver, err := config.DriverProvider().Provide(bridge.Driver)
  80. if err != nil {
  81. return err
  82. }
  83. return driver.Publish(ctx, bridge, devices)
  84. default:
  85. return nil
  86. }
  87. }