platform.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package platform
  2. import (
  3. "reflect"
  4. "time"
  5. "github.com/yuguorong/go/log"
  6. bus "github.com/ammeter/Bus"
  7. "github.com/ammeter/cloudserver"
  8. "github.com/ammeter/config"
  9. )
  10. const (
  11. DEF_REQ_AMGW_URL = "/platform/dev/get-4G-gateway-list"
  12. REQ_AMMETER_URL = "/platform/dev/get-ammeter-list"
  13. REQ_AIR_URL = "/platform/dev/get-sair-list"
  14. DEF_FTP_PORT = 10010
  15. )
  16. type PaPlatform struct {
  17. gwUrl string
  18. GatewayList map[string]*Gateway
  19. modelList map[string]string
  20. }
  21. func (p *PaPlatform) SyncCloudGWConfig() *[]bus.QMqtt {
  22. if p.gwUrl == "" {
  23. p.gwUrl = DEF_REQ_AMGW_URL
  24. }
  25. mqttList := []bus.QMqtt{}
  26. prof := config.GetSysConfig().GetProfile("remote_config", cloudserver.GetCloudConfig())
  27. cs := cloudserver.GetCloudServer(prof)
  28. err := cs.GetClientData(p.gwUrl, &mqttList, nil)
  29. if err != nil {
  30. return nil
  31. }
  32. config.GetSysConfig().SetValue("mqtt_list", &mqttList)
  33. config.GetSysConfig().Save()
  34. return &mqttList
  35. }
  36. func (p *PaPlatform) LoadGatewayProfile() {
  37. gwMqttList := &[]bus.QMqtt{}
  38. config.GetSysConfig().GetValue("mqtt_list", gwMqttList)
  39. if gwrmt := p.SyncCloudGWConfig(); gwrmt != nil {
  40. gwMqttList = gwrmt
  41. config.GetSysConfig().SetValue("mqtt_list", gwMqttList)
  42. }
  43. for _, mq := range *gwMqttList {
  44. gw, has := p.GatewayList[mq.Uid]
  45. if !has {
  46. gw = InitGateway(&mq, p.modelList)
  47. p.GatewayList[gw.Uid] = gw
  48. }
  49. gw.ApplyProfile(mq.Uid)
  50. }
  51. }
  52. func (p *PaPlatform) ListDevices() interface{} {
  53. devmap := make(map[string]interface{})
  54. for _, gw := range p.GatewayList {
  55. mgw := make(map[string]interface{})
  56. for n, dev := range gw.DeviceList {
  57. mgw[n] = dev.ListDevices()
  58. }
  59. mgw["ID"] = gw.Uid
  60. mgw["URL"] = gw.Mqttcfg.MqttUrl
  61. devmap[gw.Mqttcfg.Name] = mgw
  62. }
  63. return devmap
  64. }
  65. func (p *PaPlatform) Init() {
  66. p.GatewayList = make(map[string]*Gateway)
  67. p.LoadModles()
  68. p.LoadGatewayProfile()
  69. }
  70. func (p *PaPlatform) SetModel(sname string, surl string) {
  71. if p.modelList == nil {
  72. p.modelList = make(map[string]string)
  73. }
  74. p.modelList[sname] = surl
  75. }
  76. func (p *PaPlatform) SaveModel() {
  77. config.GetSysConfig().SetProfile("model_list", &p.modelList)
  78. }
  79. func (p *PaPlatform) LoadModles() {
  80. p.modelList = make(map[string]string)
  81. list := config.GetSysConfig().GetProfile("model_list", &p.modelList)
  82. if list != nil {
  83. p.modelList = *list.(*map[string]string)
  84. }
  85. }
  86. func (p *PaPlatform) SetGatewayUrl(url string) {
  87. p.gwUrl = url
  88. }
  89. func StartServer(mainloop chan interface{}) {
  90. p := PaPlatform{}
  91. p.Init()
  92. ticksAutoFresh := time.NewTicker(time.Duration(time.Minute * 10))
  93. for loop := true; loop; {
  94. select {
  95. case msg := <-mainloop:
  96. if reflect.TypeOf(msg).Kind() == reflect.String {
  97. switch msg.(string) {
  98. case "UpdateDevice":
  99. p.LoadGatewayProfile()
  100. case "ListDevices":
  101. chn := <-mainloop
  102. chn.(chan interface{}) <- p.ListDevices()
  103. case "exit":
  104. loop = false
  105. }
  106. }
  107. case <-ticksAutoFresh.C:
  108. p.LoadGatewayProfile()
  109. }
  110. }
  111. log.Info("exit now")
  112. }