Handle.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package Handle
  2. import (
  3. "Yunlot/logs"
  4. "Yunlot/models/Device"
  5. "Yunlot/models/Product"
  6. "fmt"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "reflect"
  9. )
  10. func AnalysisMap(Device_r *Device.Device, ProductType_r Product.ProductType, ArticleSlide map[string]interface{}, JointTab string) {
  11. //JointTab += "."
  12. T_r := make(map[string]interface{})
  13. for key, value := range ArticleSlide {
  14. //fmt.Println(reflect.TypeOf(value).String())
  15. switch reflect.TypeOf(value).String() {
  16. case "map[string]interface {}":
  17. AnalysisMap(Device_r, ProductType_r, value.(map[string]interface{}), JointTab+key+".")
  18. break
  19. case "[]interface {}":
  20. for _, valuex := range value.([]interface{}) {
  21. if reflect.TypeOf(valuex).String() == "map[string]interface {}" {
  22. AnalysisMap(Device_r, ProductType_r, valuex.(map[string]interface{}), JointTab+key+".")
  23. }
  24. }
  25. break
  26. default:
  27. T_r[key] = value
  28. // 根 TAB
  29. if len(JointTab) == 0 {
  30. Device.Data_Add(Device_r.T_sn+"_"+key, &bson.M{key: value})
  31. }
  32. break
  33. }
  34. }
  35. if len(JointTab) == 0 {
  36. return
  37. }
  38. // 多级 TAB
  39. JointTab = JointTab[:len(JointTab)-1]
  40. fmt.Println(JointTab, "-------")
  41. bson_r := bson.M{}
  42. for key, value := range T_r {
  43. fmt.Println(key, "->:", value)
  44. bson_r[key] = value
  45. }
  46. // 消息转发
  47. //Relay(Device_r, ProductType_r, JointTab, ArticleSlide)
  48. // 数据存储
  49. Device.Data_Add(Device_r.T_sn+"_"+JointTab, &bson_r)
  50. }
  51. // 设备->平台
  52. func T(Device_r Device.Device, topic string, message []byte) {
  53. // 设备类型
  54. ProductType_r := Product.ProductType{T_ProductID: Device_r.T_ProductID}
  55. if !ProductType_r.Read() {
  56. logs.Println("MqttServer", Device_r.T_sn+"|"+Device_r.T_ProductID+" 设备类型找不到!")
  57. return
  58. }
  59. // 设备协议
  60. ProductProt_r := Product.ProductProt{Id: ProductType_r.T_prot}
  61. if !ProductProt_r.Read() {
  62. logs.Println("MqttServer", Device_r.T_sn+"|"+Device_r.T_ProductID+"-"+fmt.Sprintf("%d", ProductType_r.T_prot)+" 设备协议找不到!")
  63. return
  64. }
  65. //
  66. //// 根据库的存放路径加载库
  67. //p, err := plugin.Open(conf.Analysis_Dir + ProductProt_r.T_analysis)
  68. //if err != nil {
  69. // println(err)
  70. // panic(any(err))
  71. //}
  72. //
  73. //// 查找库导出信息
  74. //s, err := p.Lookup("T")
  75. //if err != nil {
  76. // println(err)
  77. // panic(any(err))
  78. //}
  79. //// 类型转换
  80. //f := s.(func(b []byte) []byte)(message)
  81. ////0:Mqtt 1:http 2:tcp 3:CoAP 4:websocket
  82. //switch ProductProt_r.T_mode {
  83. //case 0: //Mqtt
  84. // MqttServer.Mqtt_publish(topic, f) // 返回数据
  85. // break
  86. //case 1: //http
  87. //
  88. // break
  89. //case 2: //tcp
  90. //
  91. // break
  92. //case 3: //CoAP
  93. //
  94. // break
  95. //case 4: //websocket
  96. //
  97. // break
  98. //
  99. //}
  100. return
  101. }