فهرست منبع

Revert mistake gateway.go commit from b9429e32a828be6150db23632a2413e0bf52458d.

yuguorong 3 سال پیش
والد
کامیت
3c9fe97e6e
3فایلهای تغییر یافته به همراه32 افزوده شده و 38 حذف شده
  1. 1 4
      api/go.mod
  2. 1 1
      cmd/main.go
  3. 30 33
      platform/gateway.go

+ 1 - 4
api/go.mod

@@ -1,11 +1,8 @@
 module github.com/ammeter/api
 
 go 1.14
-
-replace github.com/ammeter/util => ../util
-
+replace "github.com/ammeter/util" => "../util"
 require (
-	github.com/ammeter/util v0.0.0-00010101000000-000000000000
 	github.com/gin-gonic/gin v1.7.7
 	github.com/yuguorong/go v0.0.0-20180604090527-bdc77568d726
 )

+ 1 - 1
cmd/main.go

@@ -41,7 +41,7 @@ func init() {
 func main() {
 
 	config.GetSysConfig().SetValue("Bus/DtuServer/Port", DEF_FTP_PORT)
-	//config.GetSysConfig().SetValue("GatwayVersion", GATEWAY_VERSION)
+	config.GetSysConfig().SetValue("GatwayVersion", GATEWAY_VERSION)
 	log.Info("process start...")
 	p := platform.PaPlatform{}
 	p.SetGatewayUrl("/platform/dev/get-4G-gateway-list")

+ 30 - 33
platform/gateway.go

@@ -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, &param)
-		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
 }