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.

95 lines
2.3 KiB

  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. "strconv"
  8. "time"
  9. )
  10. func CheckNewDevices() {
  11. go func() {
  12. // Wait a bit before the first to let bridges connect.
  13. time.Sleep(time.Second * 5)
  14. err := checkNewDevices()
  15. if err != nil {
  16. log.Println("Failed to sync lights:", err)
  17. }
  18. for range time.NewTicker(time.Second * 30).C {
  19. err := checkNewDevices()
  20. if err != nil {
  21. log.Println("Failed to sync lights:", err)
  22. }
  23. }
  24. }()
  25. }
  26. func checkNewDevices() error {
  27. ctx, cancel := context.WithTimeout(context.Background(), time.Second*27)
  28. defer cancel()
  29. bridges, err := config.BridgeRepository().FetchAll(ctx)
  30. if err != nil {
  31. return err
  32. }
  33. for _, bridge := range bridges {
  34. driver, err := config.DriverProvider().Provide(bridge.Driver)
  35. if err != nil {
  36. log.Println("Unknown/unsupported driver:", bridge.Driver)
  37. continue
  38. }
  39. savedDevices, err := config.DeviceRepository().FetchByReference(ctx, models.RKBridgeID, strconv.Itoa(bridge.ID))
  40. if err != nil {
  41. log.Println("Failed to list devices from db:", err)
  42. continue
  43. }
  44. driverDevices, err := driver.ListDevices(ctx, bridge)
  45. if err != nil {
  46. log.Println("Failed to list devices from driver:", err)
  47. continue
  48. }
  49. foundNewDevices := false
  50. SaveLoop:
  51. for _, driverDevice := range driverDevices {
  52. for _, savedDevice := range savedDevices {
  53. if savedDevice.InternalID == driverDevice.InternalID {
  54. continue SaveLoop
  55. }
  56. }
  57. log.Println("Saving new device", driverDevice.InternalID)
  58. err := config.DeviceRepository().Save(ctx, &driverDevice, models.SMState|models.SMProperties|models.SMTags)
  59. if err != nil {
  60. log.Println("Failed to save device:", err)
  61. continue
  62. }
  63. foundNewDevices = true
  64. }
  65. // If new devices were found, publish them so that the driver can be set up.
  66. if foundNewDevices {
  67. savedDevices, err := config.DeviceRepository().FetchByReference(ctx, models.RKBridgeID, strconv.Itoa(bridge.ID))
  68. if err != nil {
  69. log.Println("Failed to fetch devices from db second time:", err)
  70. continue
  71. }
  72. err = driver.Publish(ctx, bridge, savedDevices)
  73. if err != nil {
  74. log.Println("Failed to list devices from db:", err)
  75. continue
  76. }
  77. }
  78. }
  79. return nil
  80. }