mqtt_binding.go 2.9 KB

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