mqtt_binding_test.go 3.9 KB

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