mqtt_binding_test.go 3.0 KB

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