Browse Source

增加配网校验

lijian 2 years ago
parent
commit
81964baabf

+ 1 - 0
pkg/klink/klink.go

@@ -11,6 +11,7 @@ const (
 	ReportFirmwareAction PacketAction = "reportFirmware" // 设备上报固件信息
 	DevLoginAction       PacketAction = "devLogin"       // 子设备上线
 	DevLogoutAction      PacketAction = "devLogout"      // 子设备下线
+	DevNetConfigAction   PacketAction = "devNetConfig"   // 设备配网信息
 )
 
 // DevLogin 子设备上线

+ 14 - 0
pkg/models/device_net_config.go

@@ -0,0 +1,14 @@
+package models
+
+import "github.com/jinzhu/gorm"
+
+// DeviceNetConfig device net config hash
+// device is a product instance, which is managed by our platform
+type DeviceNetConfig struct {
+	gorm.Model
+	// universal device identifier, generated from vendorid-productid-deviceserial
+	DeviceIdentifier string `gorm:"column:device_identifier;index;size:200"`
+	// device secret which is auto generated by the platform
+	MD5    string `gorm:"column:md5;size:32;not null"`
+	Status int    `gorm:"column:status"` // 状态(1:未确认2:确认)
+}

+ 1 - 0
pkg/mysql/migrate.go

@@ -46,6 +46,7 @@ func MigrateDatabase(dbhost, dbport, dbname, dbuser, dbpass string) error {
 		&models.Relation{},
 		&models.RuleNode{},
 		&models.RuleChain{},
+		&models.DeviceNetConfig{},
 	).Error
 	if err != nil {
 		fmt.Printf("%s", err.Error())

+ 10 - 0
pkg/rpcs/device_net_config.go

@@ -0,0 +1,10 @@
+package rpcs
+
+type ArgsCheckDeviceNetConfig struct {
+	Md5        string `json:"md5"`
+	DeviceCode string `json:"device_code"`
+}
+
+type ReplyCheckDeviceNetConfig struct {
+	Result bool `json:"result"`
+}

+ 20 - 5
services/apiprovider/actions.go

@@ -297,13 +297,9 @@ func AppAuth(req *http.Request, r render.Render) {
 		return
 	}
 
-	span, ctx := opentracing.StartSpanFromContext(context.Background(), "AppAuth")
-	defer span.Finish()
-	ext.SpanKindRPCClient.Set(span)
-
 	app := &models.Application{}
 
-	err = server.RPCCallByName(ctx, rpcs.RegistryServerName, "Registry.FindApplicationByAppKey", ruleReq, app)
+	err = server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.FindApplicationByAppKey", ruleReq, app)
 	if err != nil {
 
 		r.JSON(http.StatusOK, renderError(ErrWrongSecret, errors.New("invalid secret key")))
@@ -326,3 +322,22 @@ func AppAuth(req *http.Request, r render.Render) {
 	})
 	return
 }
+
+func CheckDeviceNetConfig(req *http.Request, r render.Render) {
+	var params *rpcs.ArgsCheckDeviceNetConfig
+	decoder := json.NewDecoder(req.Body)
+	err := decoder.Decode(&params)
+	if err != nil {
+		r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
+		return
+	}
+	var reply rpcs.ReplyCheckDeviceNetConfig
+	err = server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.CheckDeviceNetConfig", params, &reply)
+	if err != nil {
+		r.JSON(http.StatusOK, renderError(ErrWrongSecret, errors.New("invalid secret key")))
+		return
+	}
+	r.JSON(http.StatusOK, Common{
+		Result: reply.Result,
+	})
+}

+ 1 - 19
services/apiprovider/router.go

@@ -61,28 +61,10 @@ func route(m *martini.ClassicMartini) {
 		r.Post("/devices/:identifier/rules",
 			ApplicationAuthOnDeviceIdentifer, CheckDeviceIdentifier,
 			AddRule)
+		r.Get("/devices/check_net_config", CheckDeviceNetConfig)
 
 	}, ValidateTokenMiddleware)
 
 	m.Post("/application/auth", AppAuth)
-	// // user login
-	// m.Post("/api/v1/login", binding.Bind(models.LoginRequest{}),
-	// 	UserLogin)
-	// // user register
-	// m.Post("/api/v1/reg", binding.Bind(models.Reqrequest{}),
-	// 	UserRegister)
-
-	// // user api group
-	// // jwt check and pass UserToken
-	// m.Group("/api/v1/user", func(r martini.Router) {
-	// 	// user modify password api
-	// 	r.Post("/modifypass", ModifyPassword)
-	// 	// user add a product
-	// 	r.Post("/product", binding.Bind(models.Product{}), SaveProduct)
-	// 	// delete a product
-	// 	r.Delete("/product", binding.Bind(models.Product{}), DeleteProduct)
-	// 	// get products
-	// 	r.Get("/product", GetProducts)
-	// }, handler.Serve)
 
 }

+ 22 - 4
services/mqttaccess/mqtt_provider.go

@@ -3,6 +3,7 @@ package main
 import (
 	"github.com/gogf/gf/encoding/gjson"
 	"sparrow/pkg/klink"
+	"sparrow/pkg/models"
 	"sparrow/pkg/rpcs"
 	"sparrow/pkg/server"
 )
@@ -80,10 +81,12 @@ func (mp *MQTTProvider) OnDeviceMessage(deviceid, vendorId string, msgtype strin
 			switch act {
 			case klink.DevSendAction:
 				processReportStatus(deviceid, vendorId, message)
-				//case klink.DevLoginAction:
-				//	_ = processDevLogin(deviceid)
-				//case klink.DevLogoutAction:
-				//	_ = processDevLogout(deviceid)
+			//case klink.DevLoginAction:
+			//	_ = processDevLogin(deviceid)
+			//case klink.DevLogoutAction:
+			//	_ = processDevLogout(deviceid)
+			case klink.DevNetConfigAction:
+				_ = processDevNetConfig(deviceid, message.GetString("md5"))
 			}
 		}
 	case "e":
@@ -145,3 +148,18 @@ func processDevLogout(subDeviceId string) error {
 
 	return err
 }
+
+// 处理设备配网信息
+func processDevNetConfig(deviceCode, md5 string) error {
+	args := &models.DeviceNetConfig{
+		DeviceIdentifier: deviceCode,
+		MD5:              md5,
+		Status:           1,
+	}
+	reply := rpcs.ReplyCheckDeviceNetConfig{}
+	err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.CreateDeviceNetConfig", args, &reply)
+	if err != nil {
+		server.Log.Errorf("set device:%s net config info error:%v", deviceCode, err)
+	}
+	return nil
+}

+ 31 - 0
services/registry/registry.go

@@ -507,3 +507,34 @@ func (r *Registry) CreateEvent(args *models.Event, reply *rpcs.ReplyEmptyResult)
 	}
 	return db.Save(args).Error
 }
+
+// CreateDeviceNetConfig 创建设备配网信息
+func (r *Registry) CreateDeviceNetConfig(args *models.DeviceNetConfig, reply *rpcs.ReplyEmptyResult) error {
+	db, err := getDB()
+	if err != nil {
+		return err
+	}
+	return db.Save(args).Error
+}
+
+// CheckDeviceNetConfig 检验设备配网是否成功
+func (r *Registry) CheckDeviceNetConfig(args *rpcs.ArgsCheckDeviceNetConfig, reply *rpcs.ReplyCheckDeviceNetConfig) error {
+	if db, err := getDB(); err == nil {
+		where := &models.DeviceNetConfig{}
+		where.MD5 = args.Md5
+		where.DeviceIdentifier = args.DeviceCode
+		var count int
+		err = db.Where(where).Count(&count).Error
+		if err != nil {
+			return err
+		}
+		if count > 0 {
+			reply.Result = true
+		} else {
+			reply.Result = false
+		}
+	} else {
+		return err
+	}
+	return nil
+}