gateway.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package platform
  2. import (
  3. "errors"
  4. "strings"
  5. "github.com/yuguorong/go/log"
  6. bus "github.com/ammeter/Bus"
  7. "github.com/ammeter/cloudserver"
  8. "github.com/ammeter/config"
  9. drviers "github.com/ammeter/drivers"
  10. )
  11. /*
  12. gZdomIS9Hz3d7HxvcoNx
  13. data [{"accessToken":"AZM7QSPRqsV4CUxIGldn",
  14. "id":"1ec23004b88a020bf52b38d8579115b",
  15. "mqttServer":"test-sbuilding.pacom.cn:1885",
  16. "name":"FGGW10-202110020001"}]
  17. */
  18. var debugName = true
  19. const (
  20. DEF_VERDOR = "GW4G"
  21. DEF_INF_NAME = "eth0"
  22. )
  23. type Gateway struct {
  24. ID int
  25. Mqttcfg bus.QMqtt
  26. Name string
  27. Uid string
  28. modelList map[string]string
  29. tsQuery int32
  30. NorthRoute interface{} `json:"-" gorm:"-"`
  31. DeviceList map[string]drviers.IDevice
  32. }
  33. //https://test-admin.pacom.cn/platform/dev/get-sair-list
  34. func (gw *Gateway) SyncCloudModelDevice(url string, model interface{}) error {
  35. var err = errors.New("Nil model?")
  36. if model != nil {
  37. prof := config.GetSysConfig().GetProfile("remote_config", cloudserver.GetCloudConfig())
  38. cs := cloudserver.GetCloudServer(prof)
  39. param := map[string]string{
  40. "gwDevId": gw.Uid,
  41. }
  42. err = cs.GetClientData(url, model, &param)
  43. log.Info(model)
  44. }
  45. return err
  46. }
  47. type netinfo struct {
  48. name string
  49. uuid string
  50. prority int
  51. }
  52. func priotyInf(ni *netinfo, name string) bool {
  53. name = strings.ToUpper(name)
  54. ifnames := []string{"WLAN", "ETH", "ENS", "ENO", "本地连接"}
  55. for i := 0; i < ni.prority && i < len(ifnames); i++ {
  56. if strings.Contains(name, ifnames[i]) {
  57. ni.name = name
  58. ni.prority = i
  59. return true
  60. }
  61. }
  62. return false
  63. }
  64. func (gw *Gateway) InitGateway(modelList map[string]string) {
  65. //gw.GetGatewayName()
  66. gw.DeviceList = make(map[string]drviers.IDevice)
  67. gw.modelList = modelList
  68. }
  69. func (gw *Gateway) Remove(uid string) {
  70. for _, dev := range gw.DeviceList {
  71. dev.Close()
  72. }
  73. gw.DeviceList = nil
  74. gw.modelList = nil
  75. gw.Mqttcfg = bus.QMqtt{}
  76. }
  77. //
  78. func (gw *Gateway) ApplyProfile(uid string) {
  79. // name := "ammeter"
  80. // drvU := drviers.Install("SAIR10", nil)
  81. for name, url := range gw.modelList { //model name + model cloud api url
  82. drv := drviers.Install(name, nil) //If already installed ,return installed instance.
  83. model := drv.GetModel() //model: cloud information of device, create exact device absctrace from model
  84. if url != "" {
  85. if gw.SyncCloudModelDevice(url, model) != nil {
  86. model = config.GetSysConfig().GetProfile("gateway/"+name+"/amlist", model) //Cloud failed ,using local saved model
  87. } else {
  88. config.GetSysConfig().SetProfile("gateway/"+name+"/amlist", model) //update local model
  89. }
  90. }
  91. if sz, devlist := drv.CreateDevice(model, gw.Mqttcfg.Uid); sz > 0 {
  92. for _, dev := range devlist {
  93. gw.DeviceList[dev.Name()] = dev
  94. dev.SetRoute(nil, "ThingsBoards", "mqtt", "v1/gateway/telemetry", &gw.Mqttcfg)
  95. dev.Open()
  96. }
  97. }
  98. }
  99. }
  100. func InitGateway(mqttcfg *bus.QMqtt, modelList map[string]string) *Gateway {
  101. gw := &Gateway{
  102. Uid: mqttcfg.Uid,
  103. Mqttcfg: *mqttcfg,
  104. }
  105. gw.InitGateway(modelList)
  106. return gw
  107. }