Przeglądaj źródła

1. Upgrade version number to 2.0.0.1
2. Fixed logic of gateway device list
3. Add hack of cloud query for 0 number of ammeter

yuguorong 3 lat temu
rodzic
commit
b9429e32a8
5 zmienionych plików z 47 dodań i 34 usunięć
  1. 4 1
      api/go.mod
  2. 2 0
      cmd/main.go
  3. 7 3
      drivers/ameter.go
  4. 33 30
      platform/gateway.go
  5. 1 0
      platform/platform.go

+ 4 - 1
api/go.mod

@@ -1,8 +1,11 @@
 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
 )

+ 2 - 0
cmd/main.go

@@ -16,6 +16,7 @@ const (
 	REQ_AMMETER_URL = "/platform/dev/get-ammeter-list"
 	REQ_AIR_URL     = "/platform/dev/get-sair-list"
 	DEF_FTP_PORT    = 10010
+	GATEWAY_VERSION = "2.0.0.1"
 )
 
 var Port string
@@ -40,6 +41,7 @@ func init() {
 func main() {
 
 	config.GetSysConfig().SetValue("Bus/DtuServer/Port", DEF_FTP_PORT)
+	//config.GetSysConfig().SetValue("GatwayVersion", GATEWAY_VERSION)
 	log.Info("process start...")
 	p := platform.PaPlatform{}
 	p.SetGatewayUrl("/platform/dev/get-4G-gateway-list")

+ 7 - 3
drivers/ameter.go

@@ -146,12 +146,16 @@ func (dev *AmMeterHub) AdjustTelemetry(mval interface{}, am *Ammeter) {
 	}
 
 	if val, has := vlist["TotalActivePower"]; has {
-		diff := val.(float64) - am.TotalEnergy
+		v := val.(float64)
+		if v < 0.001 {
+			v = 0.001 //hack for cloud query
+		}
+		diff := v - am.TotalEnergy
 		if am.DBStatus == 0 { //数据库清0
-			diff = 0
+			diff = 0.001 //hack for cloud query
 			am.DBStatus = 1
 		}
-		am.TotalEnergy = val.(float64)
+		am.TotalEnergy = v
 		vlist["ActivePowerIncrement"] = diff
 		if len(dev.persitList) == 0 {
 			dev.tmrPersist.Reset(POSTPONE_PERSIST_TIME)

+ 33 - 30
platform/gateway.go

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

+ 1 - 0
platform/platform.go

@@ -114,6 +114,7 @@ func (p *PaPlatform) LoadGatewayProfile() {
 func (p *PaPlatform) ListDevices() interface{} {
 
 	devmap := make(map[string]interface{})
+	devmap["Version"] = config.GetSysConfig().GetValue("GatwayVersion", "0.0.0.1")
 
 	for _, gw := range p.GatewayList {
 		mgw := make(map[string]interface{})