mqtt_binding_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package api_binding
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.sxidc.com/go-tools/api_binding/mqtt_binding"
  6. "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client"
  7. "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client/router"
  8. "git.sxidc.com/go-tools/api_binding/mqtt_binding/response"
  9. mqtt "github.com/eclipse/paho.mqtt.golang"
  10. "testing"
  11. "time"
  12. )
  13. type CallerIdentifier struct {
  14. CallerIdentifier string `json:"callerIdentifier" validate:"required"`
  15. }
  16. func (callerIdentifier *CallerIdentifier) Identifier() string {
  17. return callerIdentifier.CallerIdentifier
  18. }
  19. var bindingCallerIdentifier = &CallerIdentifier{CallerIdentifier: ""}
  20. var itemCallerIdentifier = &CallerIdentifier{CallerIdentifier: ""}
  21. func TestMqttBinding(t *testing.T) {
  22. err := mqtt_binding.Init("test_prefix", &mqtt_client.MqttClientOptions{
  23. UserName: "admin",
  24. Password: "mtyzxhc",
  25. Address: "tcp://127.0.0.1:1883",
  26. ClientID: "test-binding",
  27. KeepAliveSec: 5,
  28. PingTimeoutSec: 5,
  29. })
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. defer mqtt_binding.Destroy()
  34. testBinding := mqtt_binding.NewBinding("test", bindingCallerIdentifier, func(item *router.Item, data []byte) {
  35. fmt.Println("Global Middleware!!!")
  36. item.Next()
  37. })
  38. mqtt_binding.Bind(testBinding, &mqtt_binding.BindItem[struct {
  39. Time string `json:"time"`
  40. }, map[string]interface{}]{
  41. Topic: "/test-topic",
  42. ResponseIdentifier: itemCallerIdentifier,
  43. ResponseFunc: response.SendMapResponse,
  44. BusinessFunc: func(c *mqtt_client.MqttClient, inputModel struct {
  45. Time string `json:"time"`
  46. }) (map[string]interface{}, error) {
  47. fmt.Printf("Received: %v\n", inputModel)
  48. return map[string]interface{}{
  49. "result": "pong",
  50. }, nil
  51. },
  52. }, func(item *router.Item, data []byte) {
  53. fmt.Println("Binding Middleware!!!")
  54. item.Next()
  55. })
  56. waitChan := make(chan []byte)
  57. mqttClient := mqtt.NewClient(mqtt.NewClientOptions().
  58. SetAutoReconnect(true).
  59. SetUsername("admin").
  60. SetPassword("mtyzxhc").
  61. AddBroker("tcp://127.0.0.1:1883").
  62. SetClientID("test-client").
  63. SetKeepAlive(5*time.Second).
  64. SetPingTimeout(5*time.Second).
  65. SetWill("test-client/will", "dead", 2, true).
  66. SetOnConnectHandler(func(client mqtt.Client) {
  67. replyTopic := "test_prefix/test/test-topic/reply"
  68. if itemCallerIdentifier.Identifier() != "" {
  69. replyTopic = "test_prefix/test/test-topic/" + itemCallerIdentifier.Identifier() + "/reply"
  70. } else if bindingCallerIdentifier.Identifier() != "" {
  71. replyTopic = "test_prefix/test/test-topic/" + bindingCallerIdentifier.Identifier() + "/reply"
  72. }
  73. token := client.Subscribe(replyTopic, 2, func(client mqtt.Client, message mqtt.Message) {
  74. waitChan <- message.Payload()
  75. })
  76. if token.Wait(); token.Error() != nil {
  77. fmt.Println(token.Error())
  78. return
  79. }
  80. sendMap := map[string]any{
  81. "time": time.Now().Format(time.DateTime),
  82. }
  83. if itemCallerIdentifier.Identifier() != "" {
  84. sendMap["callerIdentifier"] = itemCallerIdentifier.Identifier()
  85. } else if bindingCallerIdentifier.Identifier() != "" {
  86. sendMap["callerIdentifier"] = bindingCallerIdentifier.Identifier()
  87. }
  88. sendJson, err := json.Marshal(sendMap)
  89. if token.Wait(); token.Error() != nil {
  90. fmt.Println(err)
  91. return
  92. }
  93. token = client.Publish("test_prefix/test/test-topic", 2, false, sendJson)
  94. if token.Wait(); token.Error() != nil {
  95. fmt.Println(token.Error())
  96. return
  97. }
  98. }))
  99. token := mqttClient.Connect()
  100. if token.Wait(); token.Error() != nil {
  101. fmt.Println(token.Error())
  102. return
  103. }
  104. defer mqttClient.Disconnect(250)
  105. data := <-waitChan
  106. respMap := make(map[string]interface{})
  107. err = json.Unmarshal(data, &respMap)
  108. if err != nil {
  109. fmt.Println(err)
  110. return
  111. }
  112. pong, ok := respMap["result"]
  113. if !ok {
  114. fmt.Println("响应错误")
  115. return
  116. }
  117. if pong != "pong" {
  118. fmt.Println("响应错误")
  119. return
  120. }
  121. fmt.Println(respMap)
  122. }