mqtt_binding_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/infrastructure"
  8. "git.sxidc.com/go-framework/baize/framework/core/mqtt_api"
  9. "git.sxidc.com/go-framework/baize/framework/core/mqtt_api/mqtt_request"
  10. "git.sxidc.com/go-framework/baize/framework/core/mqtt_api/mqtt_response"
  11. "git.sxidc.com/go-framework/baize/framework/mqtt_binding"
  12. "testing"
  13. )
  14. type Hello struct {
  15. What string `json:"what"`
  16. Reply string `json:"reply"`
  17. }
  18. func TestMqttBinding(t *testing.T) {
  19. app := baize.NewApplication(application.Config{
  20. ApiConfig: application.ApiConfig{
  21. UrlPrefix: "test",
  22. Port: "10080",
  23. },
  24. InfrastructureConfig: application.InfrastructureConfig{},
  25. MqttApiConfig: &application.MqttApiConfig{
  26. TopicPrefix: "test",
  27. LogSkipPaths: []string{"test/version"},
  28. MqttConfig: application.MqttConfig{
  29. UserName: "admin",
  30. Password: "mtyzxhc123",
  31. Address: "localhost:1883",
  32. ClientID: "test",
  33. KeepAliveSec: 60,
  34. PingTimeoutSec: 60,
  35. WriteTimeoutSec: 60,
  36. },
  37. },
  38. })
  39. defer baize.DestroyApplication(app)
  40. err := app.StartMqttApi()
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. defer func(app *application.App) {
  45. err := app.FinishMqttApi()
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. }(app)
  50. app.MqttApi().Router().AddGlobalHandlers(func(c *mqtt_api.Context) {
  51. fmt.Println("Global")
  52. })
  53. mqttBinder := mqtt_binding.NewBinder(app.MqttApi().Router(), app.Infrastructure())
  54. mqtt_binding.Bind(mqttBinder, &mqtt_binding.BindItem[any]{
  55. Topic: "/version",
  56. SendResponseFunc: mqtt_response.SendMsgResponse,
  57. ServiceFunc: func(c *mqtt_api.Context, params any, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  58. fmt.Println("Version")
  59. return nil, nil
  60. },
  61. }, func(c *mqtt_api.Context, i *infrastructure.Infrastructure) {
  62. fmt.Println("Version Middleware")
  63. })
  64. mqtt_binding.Bind(mqttBinder, &mqtt_binding.BindItem[map[string]any]{
  65. Topic: "/hello",
  66. SendResponseFunc: mqtt_response.SendMapResponse,
  67. RequestParams: &Hello{},
  68. ResponseIdentifierFunc: func(c *mqtt_api.Context, params any) (string, error) {
  69. req, err := mqtt_request.ToConcrete[*Hello](params)
  70. if err != nil {
  71. return "", err
  72. }
  73. return req.Reply, nil
  74. },
  75. ServiceFunc: func(c *mqtt_api.Context, params any, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  76. req, err := mqtt_request.ToConcrete[*Hello](params)
  77. if err != nil {
  78. return make(map[string]any), err
  79. }
  80. return map[string]any{"message": "Hello " + req.What}, nil
  81. },
  82. }, func(c *mqtt_api.Context, i *infrastructure.Infrastructure) {
  83. fmt.Println("Hello Middleware")
  84. })
  85. for {
  86. }
  87. }