mqtt_binding.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. var group string
  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. Retained bool // 是否保留响应
  32. ResponseFunc response.SendFunc[O] // 响应泛型函数
  33. BusinessFunc BusinessFunc[I, O] // 业务泛型函数
  34. OptionalBindingFunc BindingFunc[O] // 可选的绑定函数
  35. }
  36. func (item *BindItem[I, O]) bind(r *router.Router, handlers ...router.Handler) {
  37. if utils.IsStringEmpty(item.Topic) {
  38. panic("需要指定主题")
  39. }
  40. if item.ResponseFunc == nil {
  41. panic("需要指定响应函数")
  42. }
  43. var inputCheckModel I
  44. inputType := reflect.TypeOf(inputCheckModel)
  45. if inputType != nil {
  46. if inputType.Kind() == reflect.Pointer {
  47. panic("输入对象不能使用指针类型")
  48. }
  49. if inputType.Kind() != reflect.Struct {
  50. panic("输入对象必须是结构")
  51. }
  52. }
  53. // 给单个路由增加中间件
  54. handlers = append(handlers, func(routerItem *router.Item, data []byte) {
  55. var inputModel I
  56. // 请求的结构类型不为any
  57. if inputType != nil {
  58. // 将请求数据解析到inputModel中
  59. if item.OptionalBindingFunc != nil {
  60. ok := item.OptionalBindingFunc(mqttClientInstance, routerItem, &inputModel, item.ResponseFunc)
  61. if !ok {
  62. return
  63. }
  64. } else {
  65. ok := request.BindingJson(mqttClientInstance, routerItem, &inputModel, item.ResponseFunc)
  66. if !ok {
  67. return
  68. }
  69. }
  70. }
  71. // 执行业务函数
  72. if item.BusinessFunc != nil {
  73. outputModel, err := item.BusinessFunc(mqttClientInstance, inputModel)
  74. item.ResponseFunc(mqttClientInstance, routerItem, outputModel, err)
  75. return
  76. }
  77. })
  78. // 所有的函数加入到执行链中
  79. routerItem, err := router.NewItem(r.Group+item.Topic, item.Qos, handlers)
  80. if err != nil {
  81. panic("创建路由条目失败: " + err.Error())
  82. return
  83. }
  84. err = r.AddItem(routerItem)
  85. if err != nil {
  86. panic("添加路由条目失败: " + err.Error())
  87. return
  88. }
  89. }