lijian 4 years ago
parent
commit
b188189ea6

+ 10 - 8
pkg/models/relation.go

@@ -1,13 +1,15 @@
 package models
 
+import "github.com/jinzhu/gorm"
+
 // Relation 规则节点关系
 type Relation struct {
-	RecordId string `gorm:"column:record_id;size:32;index"`
-	FromID            int
-	FromType          string
-	ToID              int
-	ToType            string
-	RelationTypeGroup string
-	RelationType      string
-	AdditionalInfo    string
+	gorm.Model
+	RecordId          string `gorm:"column:record_id;size:32;index"`
+	FromID            string `gorm:"column:from_id;size:32;index"`         // 起始节点ID
+	FromType          string `gorm:"column:from_type;size:200;"`           // 起始节点类型
+	ToID              string `gorm:"column:to_id;size:32;index"`           // 目标节点Id
+	ToType            string `gorm:"column:to_type;size:200"`              // 目标节点类型
+	RelationTypeGroup string `gorm:"column:relation_type_group;size:100;"` // 关系类型组
+	RelationType      string `gorm:"column:relation_type;size:100;"`       // 关系类型
 }

+ 3 - 3
pkg/models/rulechain.go

@@ -7,13 +7,13 @@ import (
 // RuleChain 规则链
 type RuleChain struct {
 	gorm.Model
-	RecordId string `gorm:"column:record_id;size:32;index"`
+	RecordId        string `gorm:"column:record_id;size:32;index"`
 	AdditionalInfo  string `gorm:"column:additional_info"`    //节点属性信息
 	Configuration   string `gorm:"column:configuration"`      //配置信息
 	Name            string `gorm:"column:name"`               //名称
-	FirstRuleNodeID int    `gorm:"column:first_rule_node_id"` //第一个节点的ID
+	FirstRuleNodeID string `gorm:"column:first_rule_node_id"` //第一个节点的ID
 	Root            bool   `gorm:"column:root"`               //是否为root chain
 	DebugModel      bool   `gorm:"column:debug_model"`        //调试模式
 	Intro           string `gorm:"column:intro"`              //描述
-	VendorID        int    `gorm:"column:vendor_id"`          //厂商ID
+	VendorID        string `gorm:"column:vendor_id"`          //厂商ID
 }

+ 8 - 8
pkg/models/rulenode.go

@@ -7,12 +7,12 @@ import (
 // RuleNode 规则链中的节点
 type RuleNode struct {
 	gorm.Model
-	RecordId string `gorm:"column:record_id;size:32;index"`
-	RuleChainID    int    //关联rulechain id
-	AdditionalInfo string //附加属性信息
-	Configuration  string //配置信息
-	Type           string //类型
-	Name           string //名称
-	DebugModel     bool   //调试模式
-	Intro          string //描述
+	RecordId       string `gorm:"column:record_id;size:32;index"`
+	RuleChainID    string `gorm:"column:rule_chain_id;size:32;index"` //关联rulechain id
+	AdditionalInfo string `gorm:"column:additional_info;size:255;"`   //附加属性信息
+	Configuration  string `gorm:"column:configuration;size:1024;"`    //配置信息
+	Type           string `gorm:"column:type;size:200"`               //类型
+	Name           string `gorm:"column:name;size:200"`               //名称
+	DebugModel     bool   `gorm:"column:debug_model"`                 //调试模式
+	Intro          string `gorm:"column:intro;size:1024"`             //描述
 }

+ 3 - 0
pkg/mysql/migrate.go

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

+ 1 - 0
pkg/ruleEngine/event_service.go

@@ -2,6 +2,7 @@ package ruleEngine
 
 import "sparrow/pkg/models"
 
+// EventService 事件服务
 type EventService interface {
 	// 保存事件
 	Save(data *models.Event) error

+ 39 - 15
services/registry/registry.go

@@ -376,37 +376,61 @@ func (r *Registry) FindDeviceByIdentifier(identifier string, reply *models.Devic
 	return nil
 }
 
-// FindDeviceById will find the device with given id
-func (r *Registry) FindDeviceByRecordId(args *rpcs.ArgsDeviceAuth, reply *models.Device) error {
+func (r *Registry) FindDeviceById(args uint64, reply *models.Device) error {
 	db, err := getDB()
 	if err != nil {
 		return err
 	}
-	d := &models.Device{}
-	d.RecordId = args.DeviceID
-	err = db.Where(d).First(reply).Error
-
-	if err != nil {
-		return err
+	cache := getCache()
+	key := fmt.Sprintf("Device:%v", args)
+	if v, ok := cache.Get(key);ok {
+		device := v.(*models.Device)
+		setDevice(reply, device)
+	} else {
+		d := &models.Device{}
+		d.ID = uint(args)
+		err = db.Where(d).First(reply).Error
+		if err != nil {
+			return err
+		}
+		var storage models.Device
+		storage = *reply
+		cache.Set(key, &storage)
 	}
+
+
 	return nil
 }
 
-func (r *Registry) FindDeviceById(args uint64, reply *models.Device) error {
+// FindDeviceById will find the device with given id
+func (r *Registry) FindDeviceByRecordId(args *rpcs.ArgsDeviceAuth, reply *models.Device) error {
 	db, err := getDB()
 	if err != nil {
 		return err
 	}
-	d := &models.Device{}
-	d.ID = uint(args)
-	err = db.Where(d).First(reply).Error
-
-	if err != nil {
-		return err
+	cache := getCache()
+	key := fmt.Sprintf("Device:%v", args.DeviceID)
+	if v, ok := cache.Get(key);ok {
+		device := v.(*models.Device)
+		setDevice(reply, device)
+	} else {
+		d := &models.Device{}
+		d.RecordId = args.DeviceID
+		err = db.Where(d).First(reply).Error
+		if err != nil {
+			return err
+		}
+		var storage models.Device
+		storage = *reply
+		cache.Set(key, &storage)
 	}
+
+
 	return nil
 }
 
+
+
 // ValidateDevice will validate a device key and return the model if success.
 func (r *Registry) ValidateDevice(key string, device *models.Device) error {
 	id, err := r.keygen.DecodeIDFromRandomKey(key)

BIN
tests/device/device


+ 0 - 19
tests/httpserver/main.go

@@ -1,19 +0,0 @@
-package main
-
-import (
-	"fmt"
-	"io/ioutil"
-	"net/http"
-)
-
-func main() {
-	http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
-		b, err := ioutil.ReadAll(r.Body)
-		if err != nil {
-			panic(err)
-		}
-		fmt.Println(string(b))
-	})
-
-	http.ListenAndServe("127.0.0.1:8899", nil)
-}