|
@@ -4,8 +4,6 @@ import (
|
|
|
"errors"
|
|
|
"strings"
|
|
|
|
|
|
- "github.com/yuguorong/go/log"
|
|
|
-
|
|
|
bus "github.com/ammeter/Bus"
|
|
|
"github.com/ammeter/cloudserver"
|
|
|
"github.com/ammeter/config"
|
|
@@ -27,14 +25,14 @@ const (
|
|
|
)
|
|
|
|
|
|
type Gateway struct {
|
|
|
- ID int
|
|
|
+ ID int `gorm:"not null"`
|
|
|
Mqttcfg bus.QMqtt
|
|
|
- Name string
|
|
|
- Uid string
|
|
|
+ Name string `gorm:"not null"`
|
|
|
+ Uid string `gorm:"not null"`
|
|
|
modelList map[string]string
|
|
|
- tsQuery int32
|
|
|
+ tsUpdate int64
|
|
|
NorthRoute interface{} `json:"-" gorm:"-"`
|
|
|
- DeviceList map[string]drviers.IDevice
|
|
|
+ drvList map[string]drviers.IDriver
|
|
|
}
|
|
|
|
|
|
//https://test-admin.pacom.cn/platform/dev/get-sair-list
|
|
@@ -48,7 +46,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
|
|
|
}
|
|
@@ -72,51 +70,50 @@ func priotyInf(ni *netinfo, name string) bool {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
-func (gw *Gateway) InitGateway(modelList map[string]string) {
|
|
|
+func (gw *Gateway) InitGateway() {
|
|
|
//gw.GetGatewayName()
|
|
|
- gw.DeviceList = make(map[string]drviers.IDevice)
|
|
|
- gw.modelList = modelList
|
|
|
|
|
|
}
|
|
|
|
|
|
func (gw *Gateway) Remove(uid string) {
|
|
|
- for _, dev := range gw.DeviceList {
|
|
|
- dev.Close()
|
|
|
+ for _, drv := range gw.drvList {
|
|
|
+ drv.Uninstall()
|
|
|
}
|
|
|
- gw.DeviceList = nil
|
|
|
+ gw.drvList = nil
|
|
|
gw.modelList = nil
|
|
|
gw.Mqttcfg = bus.QMqtt{}
|
|
|
}
|
|
|
|
|
|
//
|
|
|
-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
|
|
|
- }
|
|
|
+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
|
|
|
}
|
|
|
- if sz, devlist := drv.CreateDevice(model, gw.Mqttcfg.Uid); sz > 0 {
|
|
|
+ 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 {
|
|
|
for _, dev := range devlist {
|
|
|
- gw.DeviceList[dev.Name()] = dev
|
|
|
dev.SetRoute(nil, "ThingsBoards", "mqtt", "v1/gateway/telemetry", &gw.Mqttcfg)
|
|
|
- dev.Open()
|
|
|
+ dev.Open() //open specail Route here
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func InitGateway(mqttcfg *bus.QMqtt, modelList map[string]string) *Gateway {
|
|
|
+func InitGateway(mqttcfg *bus.QMqtt, extname string, models map[string]string) *Gateway {
|
|
|
gw := &Gateway{
|
|
|
- Uid: mqttcfg.Uid,
|
|
|
- Mqttcfg: *mqttcfg,
|
|
|
+ Uid: mqttcfg.Uid,
|
|
|
+ Mqttcfg: *mqttcfg,
|
|
|
+ tsUpdate: 0,
|
|
|
+ drvList: make(map[string]drviers.IDriver),
|
|
|
+ Name: extname,
|
|
|
+ modelList: models,
|
|
|
}
|
|
|
- gw.InitGateway(modelList)
|
|
|
+
|
|
|
return gw
|
|
|
}
|