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.

144 lines
3.1 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
  1. package api
  2. import (
  3. "fmt"
  4. "git.aiterp.net/lucifer/new-server/app/config"
  5. "git.aiterp.net/lucifer/new-server/models"
  6. "github.com/gin-gonic/gin"
  7. "log"
  8. "time"
  9. )
  10. func Bridges(r gin.IRoutes) {
  11. r.GET("", handler(func(c *gin.Context) (interface{}, error) {
  12. return config.BridgeRepository().FetchAll(ctxOf(c))
  13. }))
  14. r.GET("/:id", handler(func(c *gin.Context) (interface{}, error) {
  15. return config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
  16. }))
  17. r.POST("", handler(func(c *gin.Context) (interface{}, error) {
  18. var body struct {
  19. Driver models.DriverKind `json:"driver"`
  20. Address string `json:"address"`
  21. Token string `json:"token"`
  22. ForceSave bool `json:"forceSave"`
  23. DryRun bool `json:"dryRun"`
  24. }
  25. err := parseBody(c, &body)
  26. if err != nil {
  27. return nil, err
  28. }
  29. driver, err := config.DriverProvider().Provide(body.Driver)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if body.ForceSave {
  34. bridge := models.Bridge{
  35. Name: fmt.Sprintf("Manually saved %s bridge (%s)", body.Driver, body.Address),
  36. Driver: body.Driver,
  37. Address: body.Address,
  38. Token: body.Token,
  39. }
  40. err := config.BridgeRepository().Save(ctxOf(c), &bridge)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return []models.Bridge{bridge}, nil
  45. }
  46. bridges, err := driver.SearchBridge(ctxOf(c), body.Address, body.DryRun)
  47. if err != nil {
  48. return nil, err
  49. }
  50. if !body.DryRun {
  51. for _, bridge := range bridges {
  52. err := config.BridgeRepository().Save(ctxOf(c), &bridge)
  53. if err != nil {
  54. return nil, err
  55. }
  56. log.Printf("Saved new bridge: %s (%s)", bridge.Name, bridge.Address)
  57. }
  58. }
  59. return bridges, nil
  60. }))
  61. r.PUT("/:id", handler(func(c *gin.Context) (interface{}, error) {
  62. var body struct {
  63. Name *string `json:"name"`
  64. }
  65. err := parseBody(c, &body)
  66. if err != nil {
  67. return nil, err
  68. }
  69. bridge, err := config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
  70. if err != nil {
  71. return nil, err
  72. }
  73. if body.Name != nil {
  74. bridge.Name = *body.Name
  75. }
  76. err = config.BridgeRepository().Save(ctxOf(c), &bridge)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return bridge, nil
  81. }))
  82. r.PUT("/:id/search", handler(func(c *gin.Context) (interface{}, error) {
  83. var body struct {
  84. TimeoutMS int64 `json:"timeoutMs"`
  85. }
  86. err := parseBody(c, &body)
  87. if err != nil {
  88. return nil, err
  89. }
  90. if body.TimeoutMS <= 0 {
  91. body.TimeoutMS = 30000
  92. }
  93. bridge, err := config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
  94. if err != nil {
  95. return nil, err
  96. }
  97. driver, err := config.DriverProvider().Provide(bridge.Driver)
  98. if err != nil {
  99. return nil, err
  100. }
  101. devices, err := driver.SearchDevices(ctxOf(c), bridge, time.Millisecond * time.Duration(body.TimeoutMS))
  102. if err != nil {
  103. return nil, err
  104. }
  105. return devices, nil
  106. }))
  107. r.DELETE("/:id", handler(func(c *gin.Context) (interface{}, error) {
  108. bridge, err := config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
  109. if err != nil {
  110. return nil, err
  111. }
  112. err = config.BridgeRepository().Delete(ctxOf(c), &bridge)
  113. if err != nil {
  114. return nil, err
  115. }
  116. return bridge, nil
  117. }))
  118. }