mqtt_binding.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package mqtt_binding
  2. import (
  3. "errors"
  4. "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client"
  5. "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client/router"
  6. "git.sxidc.com/go-tools/api_binding/mqtt_binding/request"
  7. "git.sxidc.com/go-tools/api_binding/mqtt_binding/response"
  8. "git.sxidc.com/go-tools/api_binding/utils"
  9. "reflect"
  10. )
  11. type BusinessFunc[I any, O any] func(c *mqtt_client.MqttClient, inputModel I) (O, error)
  12. type BindingFunc[O any] func(c *mqtt_client.MqttClient, item *router.Item, request any, sendFunc response.SendFunc[O]) bool
  13. type Binding struct {
  14. router *router.Router
  15. }
  16. // NewBinding 创建版本对应的binding
  17. func NewBinding(apiVersion string, handlers ...router.Handler) *Binding {
  18. group := topicPrefix
  19. if utils.IsStringNotEmpty(apiVersion) && apiVersion != "root" {
  20. group += "/" + apiVersion
  21. }
  22. r := mqttClientInstance.GetRouter(group, handlers)
  23. return &Binding{router: r}
  24. }
  25. func Bind[I any, O any](b *Binding, item *BindItem[I, O], handlers ...router.Handler) {
  26. item.bind(b.router, handlers...)
  27. }
  28. func (binding *Binding) AddHandler(topic string, handlers ...router.Handler) error {
  29. if utils.IsStringEmpty(topic) {
  30. return errors.New("没有传递主题")
  31. }
  32. if handlers == nil || len(handlers) == 0 {
  33. return errors.New("没有传递处理函数")
  34. }
  35. return binding.router.AddTopic(topic, handlers...)
  36. }
  37. // BindItem 路由条目结构
  38. type BindItem[I any, O any] struct {
  39. Topic string // 请求路径
  40. ResponseFunc response.SendFunc[O] // 响应泛型函数
  41. BusinessFunc BusinessFunc[I, O] // 业务泛型函数
  42. OptionalBindingFunc BindingFunc[O] // 可选的绑定函数
  43. }
  44. func (item *BindItem[I, O]) bind(r *router.Router, handlers ...router.Handler) {
  45. if utils.IsStringEmpty(item.Topic) {
  46. panic("需要指定主题")
  47. }
  48. if item.ResponseFunc == nil {
  49. panic("需要指定响应函数")
  50. }
  51. var inputCheckModel I
  52. inputType := reflect.TypeOf(inputCheckModel)
  53. if inputType != nil {
  54. if inputType.Kind() == reflect.Pointer {
  55. panic("输入对象不能使用指针类型")
  56. }
  57. if inputType.Kind() != reflect.Struct {
  58. panic("输入对象必须是结构")
  59. }
  60. }
  61. // 给单个路由增加中间件
  62. handlers = append(handlers, func(routerItem *router.Item, data []byte) {
  63. var inputModel I
  64. // 请求的结构类型不为any
  65. if inputType != nil {
  66. // 将请求数据解析到inputModel中
  67. if item.OptionalBindingFunc != nil {
  68. ok := item.OptionalBindingFunc(mqttClientInstance, routerItem, &inputModel, item.ResponseFunc)
  69. if !ok {
  70. return
  71. }
  72. } else {
  73. ok := request.BindingJson(mqttClientInstance, routerItem, &inputModel, item.ResponseFunc)
  74. if !ok {
  75. return
  76. }
  77. }
  78. }
  79. // 执行业务函数
  80. if item.BusinessFunc != nil {
  81. outputModel, err := item.BusinessFunc(mqttClientInstance, inputModel)
  82. item.ResponseFunc(mqttClientInstance, routerItem, outputModel, err)
  83. return
  84. }
  85. })
  86. // 所有的函数加入到执行链中
  87. routerItem, err := router.NewItem(r.Group+item.Topic, handlers)
  88. if err != nil {
  89. panic("创建路由条目失败: " + err.Error())
  90. return
  91. }
  92. err = r.AddItem(routerItem)
  93. if err != nil {
  94. panic("添加路由条目失败: " + err.Error())
  95. return
  96. }
  97. }