mqtt_binding.go 2.9 KB

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