package test import ( "fmt" "git.sxidc.com/go-framework/baize" "git.sxidc.com/go-framework/baize/framework/core/application" "git.sxidc.com/go-framework/baize/framework/core/domain" "git.sxidc.com/go-framework/baize/framework/core/infrastructure" "git.sxidc.com/go-framework/baize/framework/core/mqtt_api" "git.sxidc.com/go-framework/baize/framework/core/mqtt_api/mqtt_request" "git.sxidc.com/go-framework/baize/framework/core/mqtt_api/mqtt_response" "git.sxidc.com/go-framework/baize/framework/mqtt_binding" "testing" ) type Hello struct { What string `json:"what"` Reply string `json:"reply"` } func TestMqttBinding(t *testing.T) { app := baize.NewApplication(application.Config{ ApiConfig: application.ApiConfig{ UrlPrefix: "test", Port: "10080", }, InfrastructureConfig: application.InfrastructureConfig{}, MqttApiConfig: &application.MqttApiConfig{ TopicPrefix: "test", LogSkipPaths: []string{"test/version"}, MqttConfig: application.MqttConfig{ UserName: "admin", Password: "mtyzxhc123", Address: "localhost:1883", ClientID: "test", KeepAliveSec: 60, PingTimeoutSec: 60, WriteTimeoutSec: 60, }, }, }) defer baize.DestroyApplication(app) err := app.StartMqttApi() if err != nil { t.Fatal(err) } defer func(app *application.App) { err := app.FinishMqttApi() if err != nil { t.Fatal(err) } }(app) app.MqttApi().Router().AddGlobalHandlers(func(c *mqtt_api.Context) { fmt.Println("Global") }) mqttBinder := mqtt_binding.NewBinder(app.MqttApi().Router(), app.Infrastructure()) mqtt_binding.Bind(mqttBinder, &mqtt_binding.BindItem[any]{ Topic: "/version", SendResponseFunc: mqtt_response.SendMsgResponse, ServiceFunc: func(c *mqtt_api.Context, params any, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) { fmt.Println("Version") return nil, nil }, }, func(c *mqtt_api.Context, i *infrastructure.Infrastructure) { fmt.Println("Version Middleware") }) mqtt_binding.Bind(mqttBinder, &mqtt_binding.BindItem[map[string]any]{ Topic: "/hello", SendResponseFunc: mqtt_response.SendMapResponse, RequestParams: &Hello{}, ResponseIdentifierFunc: func(c *mqtt_api.Context, params any) (string, error) { req, err := mqtt_request.ToConcrete[*Hello](params) if err != nil { return "", err } return req.Reply, nil }, ServiceFunc: func(c *mqtt_api.Context, params any, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) { req, err := mqtt_request.ToConcrete[*Hello](params) if err != nil { return make(map[string]any), err } return map[string]any{"message": "Hello " + req.What}, nil }, }, func(c *mqtt_api.Context, i *infrastructure.Infrastructure) { fmt.Println("Hello Middleware") }) for { } }