|
@@ -4,6 +4,8 @@ import (
|
|
|
"errors"
|
|
|
"strings"
|
|
|
|
|
|
+ "github.com/yuguorong/go/log"
|
|
|
+
|
|
|
bus "github.com/ammeter/Bus"
|
|
|
"github.com/ammeter/cloudserver"
|
|
|
"github.com/ammeter/config"
|
|
@@ -25,14 +27,14 @@ const (
|
|
|
)
|
|
|
|
|
|
type Gateway struct {
|
|
|
- ID int `gorm:"not null"`
|
|
|
+ ID int
|
|
|
Mqttcfg bus.QMqtt
|
|
|
- Name string `gorm:"not null"`
|
|
|
- Uid string `gorm:"not null"`
|
|
|
+ Name string
|
|
|
+ Uid string
|
|
|
modelList map[string]string
|
|
|
- tsUpdate int64
|
|
|
+ tsQuery int32
|
|
|
NorthRoute interface{} `json:"-" gorm:"-"`
|
|
|
- drvList map[string]drviers.IDriver
|
|
|
+ DeviceList map[string]drviers.IDevice
|
|
|
}
|
|
|
|
|
|
//https://test-admin.pacom.cn/platform/dev/get-sair-list
|
|
@@ -46,7 +48,7 @@ func (gw *Gateway) SyncCloudModelDevice(url string, model interface{}) error {
|
|
|
"gwDevId": gw.Uid,
|
|
|
}
|
|
|
err = cs.GetClientData(url, model, ¶m)
|
|
|
- //log.Info(model)
|
|
|
+ log.Info(model)
|
|
|
}
|
|
|
return err
|
|
|
}
|
|
@@ -70,50 +72,51 @@ func priotyInf(ni *netinfo, name string) bool {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
-func (gw *Gateway) InitGateway() {
|
|
|
+func (gw *Gateway) InitGateway(modelList map[string]string) {
|
|
|
//gw.GetGatewayName()
|
|
|
+ gw.DeviceList = make(map[string]drviers.IDevice)
|
|
|
+ gw.modelList = modelList
|
|
|
|
|
|
}
|
|
|
|
|
|
func (gw *Gateway) Remove(uid string) {
|
|
|
- for _, drv := range gw.drvList {
|
|
|
- drv.Uninstall()
|
|
|
+ for _, dev := range gw.DeviceList {
|
|
|
+ dev.Close()
|
|
|
}
|
|
|
- gw.drvList = nil
|
|
|
+ gw.DeviceList = nil
|
|
|
gw.modelList = nil
|
|
|
gw.Mqttcfg = bus.QMqtt{}
|
|
|
}
|
|
|
|
|
|
//
|
|
|
-func (gw *Gateway) InstallDrivers(uid string) {
|
|
|
-
|
|
|
- //tsUpdate := time.Now().UnixMilli()
|
|
|
- for name, _ := range gw.modelList { //model name + model cloud api url
|
|
|
- drv, has := gw.drvList[name]
|
|
|
- if !has || drv == nil {
|
|
|
- drv = drviers.Install(name, nil) //If already installed ,return installed instance.
|
|
|
- gw.drvList[name] = drv
|
|
|
+func (gw *Gateway) ApplyProfile(uid string) {
|
|
|
+ // name := "ammeter"
|
|
|
+ // drvU := drviers.Install("SAIR10", nil)
|
|
|
+ for name, url := range gw.modelList { //model name + model cloud api url
|
|
|
+ drv := drviers.Install(name, nil) //If already installed ,return installed instance.
|
|
|
+ model := drv.GetModel() //model: cloud information of device, create exact device absctrace from model
|
|
|
+ if url != "" {
|
|
|
+ if gw.SyncCloudModelDevice(url, model) != nil {
|
|
|
+ model = config.GetSysConfig().GetProfile("gateway/"+name+"/amlist", model) //Cloud failed ,using local saved model
|
|
|
+ } else {
|
|
|
+ config.GetSysConfig().SetProfile("gateway/"+name+"/amlist", model) //update local model
|
|
|
+ }
|
|
|
}
|
|
|
- model := drv.GetModel() //model: cloud information of device, create exact device absctrace from model
|
|
|
- config.GetDB().Find(model, `gw_dev_id="`+gw.Uid+`"`)
|
|
|
- if sz, devlist := drv.CreateDevice(model); sz > 0 {
|
|
|
+ if sz, devlist := drv.CreateDevice(model, gw.Mqttcfg.Uid); sz > 0 {
|
|
|
for _, dev := range devlist {
|
|
|
+ gw.DeviceList[dev.Name()] = dev
|
|
|
dev.SetRoute(nil, "ThingsBoards", "mqtt", "v1/gateway/telemetry", &gw.Mqttcfg)
|
|
|
- dev.Open() //open specail Route here
|
|
|
+ dev.Open()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func InitGateway(mqttcfg *bus.QMqtt, extname string, models map[string]string) *Gateway {
|
|
|
+func InitGateway(mqttcfg *bus.QMqtt, modelList map[string]string) *Gateway {
|
|
|
gw := &Gateway{
|
|
|
- Uid: mqttcfg.Uid,
|
|
|
- Mqttcfg: *mqttcfg,
|
|
|
- tsUpdate: 0,
|
|
|
- drvList: make(map[string]drviers.IDriver),
|
|
|
- Name: extname,
|
|
|
- modelList: models,
|
|
|
+ Uid: mqttcfg.Uid,
|
|
|
+ Mqttcfg: *mqttcfg,
|
|
|
}
|
|
|
-
|
|
|
+ gw.InitGateway(modelList)
|
|
|
return gw
|
|
|
}
|