topic.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package protocol
  2. import (
  3. "errors"
  4. "strings"
  5. )
  6. /*
  7. 物理型topic:
  8. $thing/up/status/${productID}/${deviceName} 发布 属性上报
  9. $thing/down/status/${productID}/${deviceName} 订阅 属性下发与属性上报响应
  10. $thing/up/event/${productID}/${deviceName} 发布 事件上报
  11. $thing/down/event/${productID}/${deviceName} 订阅 事件上报响应
  12. $thing/up/command/${productID}/${deviceName} 发布 设备响应行为执行结果
  13. $thing/down/command/${productID}/${deviceName} 订阅 应用调用设备行为
  14. 系统级topic:
  15. $ota/report/${productID}/${deviceName} 发布 固件升级消息上行
  16. $ota/update/${productID}/${deviceName} 订阅 固件升级消息下行
  17. $broadcast/rxd/${productID}/${deviceName} 订阅 广播消息下行
  18. $shadow/operation/up/{productID}/${deviceName} 发布 设备影子消息上行
  19. $shadow/operation/down/{productID}/${deviceName} 订阅 设备影子消息下行
  20. $rrpc/txd/{productID}/${deviceName}/${MessageId} 发布 RRPC消息上行,MessageId为RRPC消息ID
  21. $rrpc/rxd/{productID}/${deviceName}/+ 订阅 RRPC消息下行
  22. $sys/operation/up/{productID}/${deviceName} 发布 系统topic:ntp服务消息上行
  23. $sys/operation/down/{productID}/${deviceName}/+ 订阅 系统topic:ntp服务消息下行
  24. log topic
  25. $log/up/operation/${productID}/${deviceName} //设备查询是否需要上传调试日志及日志级别,上行
  26. $log/down/operation/${productID}/${deviceName}
  27. $log/up/report/${productID}/${deviceName} //设备上传调试日志内容,上行
  28. $log/down/report/${productID}/${deviceName}
  29. $log/down/update/${productID}/${deviceName} //服务器端下发调试日志配置,下行
  30. 自定义topic:
  31. ${productID}/${deviceName}/control 订阅 编辑删除
  32. ${productID}/${deviceName}/data 订阅和发布 编辑删除
  33. ${productID}/${deviceName}/event 发布
  34. ${productID}/${deviceName}/xxxxx 订阅和发布 //自定义 暂不做支持
  35. */
  36. const (
  37. TopicHeadThing = "$thing"
  38. Thing = "thing"
  39. TopicHeadOta = "$ota"
  40. Ota = "ota"
  41. TopicHeadConfig = "$config"
  42. Config = "config"
  43. TopicHeadLog = "$log"
  44. Log = "log"
  45. TopicHeadShadow = "$shadow"
  46. Shadow = "shadow"
  47. TopicHeadGateway = "$gateway"
  48. Gateway = "gateway"
  49. TopicHeadExt = "$ext"
  50. Ext = "ext"
  51. TopicHeadEvent = "$event"
  52. EventTopic = "event"
  53. TopicHeadInfo = "$info"
  54. Info = "info"
  55. )
  56. type Direction int
  57. const (
  58. Unknown Direction = iota //设备通信流向:未知
  59. Up //设备通信流向:上行
  60. Down //设备通信流向:下行
  61. )
  62. type TopicInfo struct {
  63. ProductKey string
  64. DeviceCode string
  65. Direction Direction
  66. Types []string
  67. TopicHead string
  68. }
  69. func GetTopicInfo(topic string) (topicInfo *TopicInfo, err error) {
  70. keys := strings.Split(topic, "/")
  71. return parseTopic(keys)
  72. }
  73. func parseTopic(topics []string) (topicInfo *TopicInfo, err error) {
  74. if len(topics) < 2 {
  75. return nil, errors.New("topic is err")
  76. }
  77. switch topics[0] {
  78. case TopicHeadThing, TopicHeadOta, TopicHeadShadow, TopicHeadLog, TopicHeadConfig, TopicHeadGateway, TopicHeadExt:
  79. return parseLast(topics)
  80. default: //自定义消息
  81. return parsePose(0, topics)
  82. }
  83. }
  84. func parsePose(productPos int, topics []string) (topicInfo *TopicInfo, err error) {
  85. return nil, errors.New("topic is err")
  86. //先不考虑自定义消息
  87. //if len(topics) < (productPos + 2) {
  88. // return nil, errors.Parameter.AddDetail("topic is err")
  89. //}
  90. //return &TopicInfo{
  91. // ProductID: topics[productPos],
  92. // DeviceName: topics[productPos+1],
  93. // TopicHead: topics[0],
  94. //}, err
  95. }
  96. func parseLast(topics []string) (topicInfo *TopicInfo, err error) {
  97. if len(topics) < 4 {
  98. return nil, errors.New("topic is err")
  99. }
  100. return &TopicInfo{
  101. ProductKey: topics[len(topics)-2],
  102. DeviceCode: topics[len(topics)-1],
  103. Direction: getDirection(topics[1]),
  104. Types: topics[2 : len(topics)-2],
  105. TopicHead: topics[0],
  106. }, err
  107. }
  108. func GetCommandTopic(deviceCode, productKey string) string {
  109. topic := strings.Join([]string{TopicHeadThing, "down", "command", productKey, deviceCode}, "/")
  110. return topic
  111. }
  112. func getDirection(dir string) Direction {
  113. switch dir {
  114. case "up":
  115. return Up
  116. case "down":
  117. return Down
  118. default:
  119. return Unknown
  120. }
  121. }