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.

47 lines
1.0 KiB

4 years ago
4 years ago
  1. package services
  2. import (
  3. "context"
  4. "git.aiterp.net/lucifer/new-server/app/config"
  5. "git.aiterp.net/lucifer/new-server/models"
  6. "log"
  7. )
  8. func StartPublisher() {
  9. ctx := context.Background()
  10. go func() {
  11. for devices := range config.PublishChannel {
  12. if len(devices) == 0 {
  13. continue
  14. }
  15. // Emergency solution! Please avoid!
  16. // Send devices not belonging to the first channel separately
  17. bridgeID := devices[0].BridgeID
  18. for _, device := range devices {
  19. if device.BridgeID != bridgeID {
  20. config.PublishChannel<-[]models.Device{device}
  21. }
  22. }
  23. bridge, err := config.BridgeRepository().Find(ctx, devices[0].BridgeID)
  24. if err != nil {
  25. log.Println("Publishing error (1): " + err.Error())
  26. continue
  27. }
  28. driver, err := config.DriverProvider().Provide(bridge.Driver)
  29. if err != nil {
  30. log.Println("Publishing error (2): " + err.Error())
  31. continue
  32. }
  33. err = driver.Publish(ctx, bridge, devices)
  34. if err != nil {
  35. log.Println("Publishing error (3): " + err.Error())
  36. continue
  37. }
  38. }
  39. }()
  40. }