mqtt_binding_test.go 3.0 KB

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