123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package platform
- import (
- "reflect"
- "time"
- "github.com/yuguorong/go/log"
- bus "github.com/ammeter/Bus"
- "github.com/ammeter/cloudserver"
- "github.com/ammeter/config"
- )
- const (
- DEF_REQ_AMGW_URL = "/platform/dev/get-4G-gateway-list"
- REQ_AMMETER_URL = "/platform/dev/get-ammeter-list"
- REQ_AIR_URL = "/platform/dev/get-sair-list"
- DEF_FTP_PORT = 10010
- )
- type PaPlatform struct {
- gwUrl string
- GatewayList map[string]*Gateway
- modelList map[string]string
- }
- func (p *PaPlatform) SyncCloudGWConfig() *[]bus.QMqtt {
- if p.gwUrl == "" {
- p.gwUrl = DEF_REQ_AMGW_URL
- }
- mqttList := []bus.QMqtt{}
- prof := config.GetSysConfig().GetProfile("remote_config", cloudserver.GetCloudConfig())
- cs := cloudserver.GetCloudServer(prof)
- err := cs.GetClientData(p.gwUrl, &mqttList, nil)
- if err != nil {
- return nil
- }
- config.GetSysConfig().SetValue("mqtt_list", &mqttList)
- config.GetSysConfig().Save()
- return &mqttList
- }
- func (p *PaPlatform) LoadGatewayProfile() {
- gwMqttList := &[]bus.QMqtt{}
- config.GetSysConfig().GetValue("mqtt_list", gwMqttList)
- if gwrmt := p.SyncCloudGWConfig(); gwrmt != nil {
- gwMqttList = gwrmt
- config.GetSysConfig().SetValue("mqtt_list", gwMqttList)
- }
- for _, mq := range *gwMqttList {
- gw, has := p.GatewayList[mq.Uid]
- if !has {
- gw = InitGateway(&mq, p.modelList)
- p.GatewayList[gw.Uid] = gw
- }
- gw.ApplyProfile(mq.Uid)
- }
- }
- func (p *PaPlatform) ListDevices() interface{} {
- devmap := make(map[string]interface{})
- for _, gw := range p.GatewayList {
- mgw := make(map[string]interface{})
- for n, dev := range gw.DeviceList {
- mgw[n] = dev.ListDevices()
- }
- mgw["ID"] = gw.Uid
- mgw["URL"] = gw.Mqttcfg.MqttUrl
- devmap[gw.Mqttcfg.Name] = mgw
- }
- return devmap
- }
- func (p *PaPlatform) Init() {
- p.GatewayList = make(map[string]*Gateway)
- p.LoadModles()
- p.LoadGatewayProfile()
- }
- func (p *PaPlatform) SetModel(sname string, surl string) {
- if p.modelList == nil {
- p.modelList = make(map[string]string)
- }
- p.modelList[sname] = surl
- }
- func (p *PaPlatform) SaveModel() {
- config.GetSysConfig().SetProfile("model_list", &p.modelList)
- }
- func (p *PaPlatform) LoadModles() {
- p.modelList = make(map[string]string)
- list := config.GetSysConfig().GetProfile("model_list", &p.modelList)
- if list != nil {
- p.modelList = *list.(*map[string]string)
- }
- }
- func (p *PaPlatform) SetGatewayUrl(url string) {
- p.gwUrl = url
- }
- func StartServer(mainloop chan interface{}) {
- p := PaPlatform{}
- p.Init()
- ticksAutoFresh := time.NewTicker(time.Duration(time.Minute * 10))
- for loop := true; loop; {
- select {
- case msg := <-mainloop:
- if reflect.TypeOf(msg).Kind() == reflect.String {
- switch msg.(string) {
- case "UpdateDevice":
- p.LoadGatewayProfile()
- case "ListDevices":
- chn := <-mainloop
- chn.(chan interface{}) <- p.ListDevices()
- case "exit":
- loop = false
- }
- }
- case <-ticksAutoFresh.C:
- p.LoadGatewayProfile()
- }
- }
- log.Info("exit now")
- }
|