|
@@ -6,25 +6,26 @@ import (
|
|
|
"git.sxidc.com/go-tools/api_binding/mqtt_binding"
|
|
|
"git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client"
|
|
|
"git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client/router"
|
|
|
+ "git.sxidc.com/go-tools/api_binding/mqtt_binding/request"
|
|
|
"git.sxidc.com/go-tools/api_binding/mqtt_binding/response"
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
"testing"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-type CallerIdentifier struct {
|
|
|
- CallerIdentifier string `json:"callerIdentifier" validate:"required"`
|
|
|
+type ResponseIdentifier struct {
|
|
|
+ ResponseIdentifier string `json:"responseIdentifier" validate:"required"`
|
|
|
}
|
|
|
|
|
|
-func (callerIdentifier *CallerIdentifier) Identifier() string {
|
|
|
- return callerIdentifier.CallerIdentifier
|
|
|
+func (responseIdentifier *ResponseIdentifier) Identifier() string {
|
|
|
+ return responseIdentifier.ResponseIdentifier
|
|
|
}
|
|
|
|
|
|
-var bindingCallerIdentifier = &CallerIdentifier{CallerIdentifier: "binding"}
|
|
|
-var itemCallerIdentifier = &CallerIdentifier{CallerIdentifier: "item"}
|
|
|
+var bindingResponseIdentifier = &request.CallerIdentifierRequest{CallerIdentifier: "binding"}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+var itemResponseIdentifier = &ResponseIdentifier{ResponseIdentifier: "item"}
|
|
|
+
|
|
|
+
|
|
|
|
|
|
func TestMqttBinding(t *testing.T) {
|
|
|
err := mqtt_binding.Init("test_prefix", &mqtt_client.MqttClientOptions{
|
|
@@ -41,7 +42,7 @@ func TestMqttBinding(t *testing.T) {
|
|
|
|
|
|
defer mqtt_binding.Destroy()
|
|
|
|
|
|
- testBinding := mqtt_binding.NewBinding("test", bindingCallerIdentifier, func(item *router.Item, data []byte) {
|
|
|
+ testBinding := mqtt_binding.NewBinding("test", bindingResponseIdentifier, func(item *router.Item, data []byte) {
|
|
|
fmt.Println("Global Middleware!!!")
|
|
|
item.Next()
|
|
|
})
|
|
@@ -49,7 +50,7 @@ func TestMqttBinding(t *testing.T) {
|
|
|
Time string `json:"time"`
|
|
|
}, map[string]interface{}]{
|
|
|
Topic: "/test-topic",
|
|
|
- ResponseIdentifier: itemCallerIdentifier,
|
|
|
+ ResponseIdentifier: &ResponseIdentifier{},
|
|
|
ResponseFunc: response.SendMapResponse,
|
|
|
BusinessFunc: func(c *mqtt_client.MqttClient, inputModel struct {
|
|
|
Time string `json:"time"`
|
|
@@ -78,12 +79,14 @@ func TestMqttBinding(t *testing.T) {
|
|
|
SetWill("test-client/will", "dead", 2, true).
|
|
|
SetOnConnectHandler(func(client mqtt.Client) {
|
|
|
replyTopic := "test_prefix/test/test-topic/reply"
|
|
|
- if itemCallerIdentifier != nil {
|
|
|
- replyTopic = "test_prefix/test/test-topic/" + itemCallerIdentifier.Identifier() + "/reply"
|
|
|
- } else if bindingCallerIdentifier != nil {
|
|
|
- replyTopic = "test_prefix/test/test-topic/" + bindingCallerIdentifier.Identifier() + "/reply"
|
|
|
+ if itemResponseIdentifier != nil {
|
|
|
+ replyTopic = "test_prefix/test/test-topic/" + itemResponseIdentifier.Identifier() + "/reply"
|
|
|
+ } else if bindingResponseIdentifier != nil {
|
|
|
+ replyTopic = "test_prefix/test/test-topic/" + bindingResponseIdentifier.Identifier() + "/reply"
|
|
|
}
|
|
|
|
|
|
+ fmt.Println("Reply Topic: " + replyTopic)
|
|
|
+
|
|
|
token := client.Subscribe(replyTopic, 2, func(client mqtt.Client, message mqtt.Message) {
|
|
|
waitChan <- message.Payload()
|
|
|
})
|
|
@@ -93,14 +96,13 @@ func TestMqttBinding(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
sendMap := map[string]any{
|
|
|
- "callerIdentifier": "test",
|
|
|
- "time": time.Now().Format(time.DateTime),
|
|
|
+ "time": time.Now().Format(time.DateTime),
|
|
|
}
|
|
|
|
|
|
- if itemCallerIdentifier.Identifier() != "" {
|
|
|
- sendMap["callerIdentifier"] = itemCallerIdentifier.Identifier()
|
|
|
- } else if bindingCallerIdentifier.Identifier() != "" {
|
|
|
- sendMap["callerIdentifier"] = bindingCallerIdentifier.Identifier()
|
|
|
+ if itemResponseIdentifier != nil {
|
|
|
+ sendMap["responseIdentifier"] = itemResponseIdentifier.Identifier()
|
|
|
+ } else if bindingResponseIdentifier != nil {
|
|
|
+ sendMap["callerIdentifier"] = bindingResponseIdentifier.Identifier()
|
|
|
}
|
|
|
|
|
|
sendJson, err := json.Marshal(sendMap)
|