mqtt_binding_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package test
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-framework/baize"
  5. "git.sxidc.com/go-framework/baize/framework/core/application"
  6. "git.sxidc.com/go-framework/baize/framework/core/domain"
  7. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  9. "git.sxidc.com/go-framework/baize/framework/core/mqtt_api"
  10. "git.sxidc.com/go-framework/baize/framework/core/mqtt_api/mqtt_request"
  11. "git.sxidc.com/go-framework/baize/framework/core/mqtt_api/mqtt_response"
  12. "git.sxidc.com/go-framework/baize/framework/mqtt_binding"
  13. "testing"
  14. )
  15. type Hello struct {
  16. What string `json:"what" assign:"toField:what"`
  17. Reply string `json:"reply"`
  18. }
  19. type Entity struct {
  20. entity.Base
  21. What string
  22. }
  23. func (e Entity) DomainCNName() string {
  24. return "实体"
  25. }
  26. func (e Entity) DomainCamelName() string {
  27. return "Entity"
  28. }
  29. func (e Entity) GetFieldMap() map[string]string {
  30. return map[string]string{
  31. "what": "什么",
  32. }
  33. }
  34. func TestMqttBinding(t *testing.T) {
  35. app := baize.NewApplication(application.Config{
  36. ApiConfig: application.ApiConfig{
  37. UrlPrefix: "test",
  38. Port: "10080",
  39. },
  40. InfrastructureConfig: application.InfrastructureConfig{},
  41. MqttApiConfig: &application.MqttApiConfig{
  42. TopicPrefix: "test",
  43. LogSkipPaths: []string{"test/version"},
  44. MqttConfig: application.MqttConfig{
  45. UserName: "admin",
  46. Password: "mtyzxhc1231",
  47. Address: "emqx.sxidc.com:28739",
  48. ClientID: "test",
  49. KeepAliveSec: 60,
  50. PingTimeoutSec: 60,
  51. WriteTimeoutSec: 60,
  52. },
  53. },
  54. })
  55. defer baize.DestroyApplication(app)
  56. err := app.StartMqttApi()
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. defer func(app *application.App) {
  61. err := app.FinishMqttApi()
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. }(app)
  66. app.MqttApi().Router().AddGlobalHandlers(func(c *mqtt_api.Context) {
  67. fmt.Println("Global")
  68. })
  69. mqttBinder := mqtt_binding.NewBinder(app.MqttApi().Router(), app.Infrastructure())
  70. mqtt_binding.Bind(mqttBinder, &mqtt_binding.BindItem[any]{
  71. Topic: "/version",
  72. SendResponseFunc: mqtt_response.SendMsgResponse,
  73. ServiceFunc: func(c *mqtt_api.Context, params any, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  74. fmt.Println("Version")
  75. return nil, nil
  76. },
  77. }, func(c *mqtt_api.Context, i *infrastructure.Infrastructure) {
  78. fmt.Println("Version Middleware")
  79. c.Next()
  80. fmt.Println("Version Middleware After")
  81. })
  82. mqtt_binding.Bind(mqttBinder, &mqtt_binding.BindItem[map[string]any]{
  83. Topic: "/hello",
  84. SendResponseFunc: mqtt_response.SendMapResponse,
  85. RequestParams: &Hello{},
  86. ResponseIdentifierFunc: func(c *mqtt_api.Context, params any) (string, error) {
  87. req, err := mqtt_request.ToConcrete[*Hello](params)
  88. if err != nil {
  89. return "", err
  90. }
  91. return req.Reply, nil
  92. },
  93. Objects: []domain.Object{&Entity{}},
  94. ServiceFunc: func(c *mqtt_api.Context, params any, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  95. fmt.Println("Hello")
  96. e, err := domain.ToConcrete[*Entity](objects[0])
  97. if err != nil {
  98. return make(map[string]any), err
  99. }
  100. return map[string]any{"message": "Hello " + e.What}, nil
  101. },
  102. }, func(c *mqtt_api.Context, i *infrastructure.Infrastructure) {
  103. fmt.Println("Hello Middleware")
  104. c.Next()
  105. fmt.Println("Hello Middleware After")
  106. })
  107. for {
  108. }
  109. }