topic.go 4.1 KB

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