topic.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. )
  54. type Direction int
  55. const (
  56. Unknown Direction = iota //设备通信流向:未知
  57. Up //设备通信流向:上行
  58. Down //设备通信流向:下行
  59. )
  60. type TopicInfo struct {
  61. ProductKey string
  62. DeviceCode string
  63. Direction Direction
  64. Types []string
  65. TopicHead string
  66. }
  67. func GetTopicInfo(topic string) (topicInfo *TopicInfo, err error) {
  68. keys := strings.Split(topic, "/")
  69. return parseTopic(keys)
  70. }
  71. func parseTopic(topics []string) (topicInfo *TopicInfo, err error) {
  72. if len(topics) < 2 {
  73. return nil, errors.New("topic is err")
  74. }
  75. switch topics[0] {
  76. case TopicHeadThing, TopicHeadOta, TopicHeadShadow, TopicHeadLog, TopicHeadConfig, TopicHeadGateway, TopicHeadExt:
  77. return parseLast(topics)
  78. default: //自定义消息
  79. return parsePose(0, topics)
  80. }
  81. }
  82. func parsePose(productPos int, topics []string) (topicInfo *TopicInfo, err error) {
  83. return nil, errors.New("topic is err")
  84. //先不考虑自定义消息
  85. //if len(topics) < (productPos + 2) {
  86. // return nil, errors.Parameter.AddDetail("topic is err")
  87. //}
  88. //return &TopicInfo{
  89. // ProductID: topics[productPos],
  90. // DeviceName: topics[productPos+1],
  91. // TopicHead: topics[0],
  92. //}, err
  93. }
  94. func parseLast(topics []string) (topicInfo *TopicInfo, err error) {
  95. if len(topics) < 4 {
  96. return nil, errors.New("topic is err")
  97. }
  98. return &TopicInfo{
  99. ProductKey: topics[len(topics)-2],
  100. DeviceCode: topics[len(topics)-1],
  101. Direction: getDirection(topics[1]),
  102. Types: topics[2 : len(topics)-2],
  103. TopicHead: topics[0],
  104. }, err
  105. }
  106. func GetCommandTopic(deviceCode, productKey string) string {
  107. topic := strings.Join([]string{TopicHeadThing, "down", "command", productKey, deviceCode}, "/")
  108. return topic
  109. }
  110. func getDirection(dir string) Direction {
  111. switch dir {
  112. case "up":
  113. return Up
  114. case "down":
  115. return Down
  116. default:
  117. return Unknown
  118. }
  119. }