platform.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package platform
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "time"
  6. "github.com/yuguorong/go/log"
  7. bus "github.com/ammeter/Bus"
  8. "github.com/ammeter/cloudserver"
  9. "github.com/ammeter/config"
  10. drviers "github.com/ammeter/drivers"
  11. "github.com/ammeter/util"
  12. )
  13. const (
  14. DEF_REQ_AMGW_URL = "/platform/dev/get-4G-gateway-list"
  15. REQ_AMMETER_URL = "/platform/dev/get-ammeter-list"
  16. REQ_AIR_URL = "/platform/dev/get-sair-list"
  17. DEF_FTP_PORT = 10010
  18. )
  19. type GatewayConfig struct {
  20. bus.QMqtt
  21. Tenants string `json:"userName"`
  22. }
  23. type PaPlatform struct {
  24. gwUrl string
  25. jsGwList string
  26. jsDevList map[string]string
  27. GatewayList map[string]*Gateway
  28. modelList map[string]string
  29. }
  30. func (p *PaPlatform) LoadSyncGWConfig() *[]GatewayConfig {
  31. if p.gwUrl == "" {
  32. p.gwUrl = DEF_REQ_AMGW_URL
  33. }
  34. mqttList := []GatewayConfig{}
  35. prof := config.GetSysConfig().GetProfile("remote_config", cloudserver.GetCloudConfig())
  36. cs := cloudserver.GetCloudServer(prof)
  37. if err := cs.GetClientData(p.gwUrl, &mqttList, nil); err == nil {
  38. for i := 0; i < len(mqttList); {
  39. if mqttList[i].QMqtt.User == "" || mqttList[i].QMqtt.MqttUrl == "" || mqttList[i].QMqtt.Uid == "" || mqttList[i].QMqtt.Name == "" {
  40. mqttList = append(mqttList[:i], mqttList[i+1:]...)
  41. } else {
  42. i++
  43. }
  44. }
  45. jmq, _ := json.Marshal(&mqttList)
  46. if string(jmq) != p.jsGwList {
  47. config.GetDB().Save(&mqttList)
  48. p.jsGwList = string(jmq)
  49. //need check different
  50. }
  51. } else {
  52. config.GetDB().Table(&mqttList)
  53. }
  54. return &mqttList
  55. }
  56. func (p *PaPlatform) LoadSyncAllDevices() {
  57. for name, url := range p.modelList {
  58. drv := drviers.Install(name, nil)
  59. model := drv.GetModel()
  60. if url != "" && model != nil {
  61. prof := config.GetSysConfig().GetProfile("remote_config", cloudserver.GetCloudConfig())
  62. cs := cloudserver.GetCloudServer(prof)
  63. param := map[string]string{
  64. "gwDevId": "",
  65. }
  66. bupdate := true
  67. err := cs.GetClientData(url, model, &param)
  68. if err == nil {
  69. jdev, _ := json.Marshal(model)
  70. if js, has := p.jsDevList[name]; has {
  71. bupdate = (js != string(jdev))
  72. }
  73. p.jsDevList[name] = string(jdev)
  74. if bupdate {
  75. config.GetDB().Delete(drv.GetModel()) //clear first
  76. config.GetDB().CreateTbl(model)
  77. config.GetDB().Save(model)
  78. }
  79. }
  80. }
  81. }
  82. }
  83. func (p *PaPlatform) LoadGatewayProfile() {
  84. tscur := time.Now().UnixMilli()
  85. p.LoadSyncAllDevices()
  86. gwMqttList := p.LoadSyncGWConfig()
  87. for _, mq := range *gwMqttList {
  88. gw, has := p.GatewayList[mq.Uid]
  89. if !has {
  90. gw = InitGateway(&mq.QMqtt, mq.Tenants, p.modelList)
  91. p.GatewayList[gw.Uid] = gw
  92. }
  93. gw.InstallDrivers(mq.Uid)
  94. gw.tsUpdate = tscur
  95. }
  96. for name, gw := range p.GatewayList {
  97. if gw.tsUpdate != tscur {
  98. gw.Remove(gw.Uid)
  99. delete(p.GatewayList, name)
  100. }
  101. }
  102. }
  103. func (p *PaPlatform) ListDevices() interface{} {
  104. devmap := make(map[string]interface{})
  105. for _, gw := range p.GatewayList {
  106. mgw := make(map[string]interface{})
  107. for _, drv := range gw.drvList {
  108. for n, dev := range drv.GetDeviceList() {
  109. mgw[n] = dev.ListDevices()
  110. }
  111. }
  112. //mgw["ID"] = gw.Uid
  113. mgw["Tenants"] = gw.Name
  114. mgw["URL"] = gw.Mqttcfg.MqttUrl
  115. devmap[gw.Mqttcfg.Name] = mgw
  116. }
  117. return devmap
  118. }
  119. func (p *PaPlatform) Init() {
  120. p.GatewayList = make(map[string]*Gateway)
  121. p.jsDevList = make(map[string]string)
  122. p.LoadModles()
  123. p.LoadGatewayProfile()
  124. }
  125. func (p *PaPlatform) SetModel(sname string, surl string) {
  126. if p.modelList == nil {
  127. p.modelList = make(map[string]string)
  128. }
  129. p.modelList[sname] = surl
  130. }
  131. func (p *PaPlatform) SaveModel() {
  132. config.GetSysConfig().SetProfile("model_list", &p.modelList)
  133. }
  134. func (p *PaPlatform) LoadModles() {
  135. p.modelList = make(map[string]string)
  136. list := config.GetSysConfig().GetProfile("model_list", &p.modelList)
  137. if list != nil {
  138. p.modelList = *list.(*map[string]string)
  139. }
  140. }
  141. func (p *PaPlatform) SetGatewayUrl(url string) {
  142. p.gwUrl = url
  143. }
  144. func StartServer() {
  145. p := PaPlatform{}
  146. p.Init()
  147. msgAPI := util.GetMsgHandler("API")
  148. ticksAutoFresh := time.NewTicker(time.Duration(time.Minute * 60))
  149. for loop := true; loop; {
  150. select {
  151. case msg := <-msgAPI:
  152. if reflect.TypeOf(msg).Kind() == reflect.String {
  153. switch msg.(string) {
  154. case "UpdateDevice":
  155. p.LoadGatewayProfile()
  156. case "ListDevices":
  157. chn := <-msgAPI
  158. chn.(chan interface{}) <- p.ListDevices()
  159. case "exit":
  160. loop = false
  161. }
  162. }
  163. case <-ticksAutoFresh.C:
  164. log.Info("Auto refresh devices")
  165. p.LoadGatewayProfile()
  166. }
  167. }
  168. log.Info("exit now")
  169. }
  170. func init() {
  171. config.GetDB().CreateTbl(&GatewayConfig{})
  172. }