mqtt_binding_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Retained: true,
  37. ResponseFunc: response.SendMapResponse,
  38. BusinessFunc: func(c *mqtt_client.MqttClient, inputModel struct {
  39. Time string `json:"time"`
  40. }) (map[string]interface{}, error) {
  41. fmt.Printf("Received: %v\n", inputModel)
  42. return map[string]interface{}{
  43. "result": "pong",
  44. }, nil
  45. },
  46. OptionalBindingFunc: nil,
  47. }, func(item *router.Item, data []byte) {
  48. fmt.Println("Binding Middleware!!!")
  49. item.Next()
  50. })
  51. wg := sync.WaitGroup{}
  52. wg.Add(1)
  53. go func() {
  54. mqttClient := mqtt.NewClient(mqtt.NewClientOptions().
  55. SetAutoReconnect(true).
  56. SetUsername("admin").
  57. SetPassword("mtyzxhc").
  58. AddBroker("tcp://127.0.0.1:1883").
  59. SetClientID("test-client").
  60. SetKeepAlive(5*time.Second).
  61. SetPingTimeout(5*time.Second).
  62. SetWill("test-client/will", "dead", 2, true).
  63. SetOnConnectHandler(func(client mqtt.Client) {
  64. token := client.Subscribe("test_prefix/test/test-topic/reply", 2, func(client mqtt.Client, message mqtt.Message) {
  65. respMap := make(map[string]interface{})
  66. err = json.Unmarshal(message.Payload(), &respMap)
  67. if err != nil {
  68. fmt.Println(err)
  69. return
  70. }
  71. pong, ok := respMap["result"]
  72. if !ok {
  73. fmt.Println("响应错误")
  74. return
  75. }
  76. if pong != "pong" {
  77. fmt.Println("响应错误")
  78. return
  79. }
  80. })
  81. if token.Wait(); token.Error() != nil {
  82. fmt.Println(token.Error())
  83. return
  84. }
  85. sendMap := map[string]any{
  86. "time": time.Now().Format(time.DateTime),
  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(10000)
  105. time.Sleep(2 * time.Second)
  106. wg.Done()
  107. }()
  108. wg.Wait()
  109. }