platform.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package platform
  2. import (
  3. "github.com/yuguorong/go/log"
  4. bus "github.com/ammeter/Bus"
  5. "github.com/ammeter/cloudserver"
  6. "github.com/ammeter/config"
  7. )
  8. const (
  9. DEF_REQ_AMGW_URL = "/platform/dev/get-4G-gateway-list"
  10. REQ_AMMETER_URL = "/platform/dev/get-ammeter-list"
  11. REQ_AIR_URL = "/platform/dev/get-sair-list"
  12. DEF_FTP_PORT = 10010
  13. )
  14. type PaPlatform struct {
  15. gwUrl string
  16. GatewayList []*Gateway
  17. modelList map[string]string
  18. }
  19. func (p *PaPlatform) SyncCloudGWConfig() *[]bus.QMqtt {
  20. if p.gwUrl == "" {
  21. p.gwUrl = DEF_REQ_AMGW_URL
  22. }
  23. mqttList := []bus.QMqtt{}
  24. prof := config.GetSysConfig().GetProfile("remote_config", cloudserver.GetCloudConfig())
  25. cs := cloudserver.GetCloudServer(prof)
  26. err := cs.GetClientData(p.gwUrl, &mqttList, nil)
  27. if err != nil {
  28. return nil
  29. }
  30. config.GetSysConfig().SetValue("mqtt_list", &mqttList)
  31. config.GetSysConfig().Save()
  32. return &mqttList
  33. }
  34. func (p *PaPlatform) LoadGatewayProfile() {
  35. p.GatewayList = make([]*Gateway, 0)
  36. mqttList := &[]bus.QMqtt{}
  37. config.GetSysConfig().GetValue("mqtt_list", mqttList)
  38. if mqrmt := p.SyncCloudGWConfig(); mqrmt != nil {
  39. mqttList = mqrmt
  40. config.GetSysConfig().SetValue("mqtt_list", mqttList)
  41. }
  42. for _, mq := range *mqttList {
  43. gw := InitGateway(&mq, p.modelList)
  44. p.GatewayList = append(p.GatewayList, gw)
  45. gw.StartServer(mq.Uid)
  46. }
  47. }
  48. func (p *PaPlatform) SetModel(sname string, surl string) {
  49. if p.modelList == nil {
  50. p.modelList = make(map[string]string)
  51. }
  52. p.modelList[sname] = surl
  53. }
  54. func (p *PaPlatform) SaveModel() {
  55. config.GetSysConfig().SetProfile("model_list", &p.modelList)
  56. }
  57. func (p *PaPlatform) LoadModles() {
  58. p.modelList = make(map[string]string)
  59. list := config.GetSysConfig().GetProfile("model_list", &p.modelList)
  60. if list != nil {
  61. p.modelList = *list.(*map[string]string)
  62. }
  63. }
  64. func (p *PaPlatform) SetGatewayUrl(url string) {
  65. p.gwUrl = url
  66. }
  67. var mainloop chan interface{}
  68. func StartServer() {
  69. p := &PaPlatform{}
  70. p.LoadModles()
  71. p.LoadGatewayProfile()
  72. mainloop = make(chan interface{})
  73. v := <-mainloop
  74. log.Info("exit now", v)
  75. }