platform.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. devmap["Version"] = config.GetSysConfig().GetValue("GatwayVersion", "0.0.0.1")
  106. for _, gw := range p.GatewayList {
  107. mgw := make(map[string]interface{})
  108. for _, drv := range gw.drvList {
  109. for n, dev := range drv.GetDeviceList() {
  110. mgw[n] = dev.ListDevices()
  111. }
  112. }
  113. //mgw["ID"] = gw.Uid
  114. mgw["Tenants"] = gw.Name
  115. mgw["URL"] = gw.Mqttcfg.MqttUrl
  116. devmap[gw.Mqttcfg.Name] = mgw
  117. }
  118. return devmap
  119. }
  120. func (p *PaPlatform) Init() {
  121. p.GatewayList = make(map[string]*Gateway)
  122. p.jsDevList = make(map[string]string)
  123. p.LoadModles()
  124. p.LoadGatewayProfile()
  125. }
  126. func (p *PaPlatform) SetModel(sname string, surl string) {
  127. if p.modelList == nil {
  128. p.modelList = make(map[string]string)
  129. }
  130. p.modelList[sname] = surl
  131. }
  132. func (p *PaPlatform) SaveModel() {
  133. config.GetSysConfig().SetProfile("model_list", &p.modelList)
  134. }
  135. func (p *PaPlatform) LoadModles() {
  136. p.modelList = make(map[string]string)
  137. list := config.GetSysConfig().GetProfile("model_list", &p.modelList)
  138. if list != nil {
  139. p.modelList = *list.(*map[string]string)
  140. }
  141. }
  142. func (p *PaPlatform) SetGatewayUrl(url string) {
  143. p.gwUrl = url
  144. }
  145. func StartServer() {
  146. p := PaPlatform{}
  147. p.Init()
  148. msgAPI := util.GetMsgHandler("API")
  149. ticksAutoFresh := time.NewTicker(time.Duration(time.Minute * 60))
  150. for loop := true; loop; {
  151. select {
  152. case msg := <-msgAPI:
  153. if reflect.TypeOf(msg).Kind() == reflect.String {
  154. switch msg.(string) {
  155. case "UpdateDevice":
  156. p.LoadGatewayProfile()
  157. case "ListDevices":
  158. chn := <-msgAPI
  159. chn.(chan interface{}) <- p.ListDevices()
  160. case "exit":
  161. loop = false
  162. }
  163. }
  164. case <-ticksAutoFresh.C:
  165. log.Info("Auto refresh devices")
  166. p.LoadGatewayProfile()
  167. }
  168. }
  169. log.Info("exit now")
  170. }
  171. func init() {
  172. config.GetDB().CreateTbl(&GatewayConfig{})
  173. }