规则链

This commit is contained in:
XM-GO
2023-04-18 16:29:26 +08:00
parent db5a45590a
commit 5dfa341083
46 changed files with 980 additions and 381 deletions

View File

@@ -3,20 +3,39 @@ package message
import (
"encoding/json"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"time"
)
//消息类型
const (
EventConnectType = "connect"
EventDisConnectType = "disconnect"
EventUpEventType = "event"
EventAlarmType = "alarm"
EventTelemetryType = "telemetry"
EventAttributesType = "attributes"
)
// 数据类型Originator
const (
DEVICE = "DEVICE"
GATEWAY = "GATEWAY"
)
// Message ...
type Message interface {
GetId() string
GetTs() time.Time
GetOriginator() string
GetUserId() string
GetType() string
GetMsg() map[string]interface{}
GetMsgLogs() []MesLog
GetMetadata() Metadata
GetAllMap() map[string]interface{} //msg 和 Metadata的合并
SetType(string)
SetMsg(map[string]interface{})
SetOriginator(string)
SetUserId(string)
SetMetadata(Metadata)
MarshalBinary() ([]byte, error)
}
@@ -29,58 +48,69 @@ type Metadata interface {
GetValues() map[string]interface{}
}
// Predefined message types
const (
EventConnectType = "connect"
EventDisConnectType = "disconnect"
EventUpEventType = "event"
EventAlarmType = "alarm"
EventTelemetryType = "telemetry"
EventAttributesType = "attributes"
)
// NewMessage ...
func NewMessage() Message {
return &defaultMessage{
id: uuid.New().String(),
ts: time.Now(),
msg: map[string]interface{}{},
mesLog: make([]MesLog, 0),
Id: uuid.New().String(),
Ts: time.Now(),
Msg: map[string]interface{}{},
}
}
type defaultMessage struct {
id string //uuid
ts time.Time //时间戳
msgType string //消息类型, attributes参数telemetry遥测Connect连接事件
originator string //数据发布者 设备 规则链
userId string //客户Id UUID
deviceId string //设备Id UUID
msg map[string]interface{} //数据 数据结构JSON 设备原始数据 msg
metadata Metadata //消息的元数据 包括,设备名称,设备类型,命名空间,时间戳等
mesLog []MesLog
Id string //uuid 消息Id
Ts time.Time //时间戳
MsgType string //消息类型, attributes参数telemetry遥测Connect连接事件
Originator string //数据发布者 设备 规则链
UserId string //客户Id UUID 设备发布人
Msg map[string]interface{} //数据 数据结构JSON 设备原始数据 msg
Metadata Metadata //消息的元数据 包括,设备名称,设备类型,命名空间,时间戳等
}
// NewMessageWithDetail ...
func NewMessageWithDetail(originator string, messageType string, msg map[string]interface{}, metadata Metadata) Message {
func NewMessageWithDetail(userId, originator string, messageType string, msg map[string]interface{}, metadata Metadata) Message {
return &defaultMessage{
originator: originator,
msgType: messageType,
msg: msg,
metadata: metadata,
mesLog: make([]MesLog, 0),
Id: uuid.New().String(),
Ts: time.Now(),
UserId: userId,
Originator: originator,
MsgType: messageType,
Msg: msg,
Metadata: metadata,
}
}
func (t *defaultMessage) GetOriginator() string { return t.originator }
func (t *defaultMessage) GetType() string { return t.msgType }
func (t *defaultMessage) GetMsg() map[string]interface{} { return t.msg }
func (t *defaultMessage) GetMsgLogs() []MesLog { return t.mesLog }
func (t *defaultMessage) GetMetadata() Metadata { return t.metadata }
func (t *defaultMessage) SetType(msgType string) { t.msgType = msgType }
func (t *defaultMessage) SetMsg(msg map[string]interface{}) { t.msg = msg }
func (t *defaultMessage) SetOriginator(originator string) { t.originator = originator }
func (t *defaultMessage) SetMetadata(metadata Metadata) { t.metadata = metadata }
func (t *defaultMessage) GetId() string { return t.Id }
func (t *defaultMessage) GetTs() time.Time { return t.Ts }
func (t *defaultMessage) GetOriginator() string { return t.Originator }
func (t *defaultMessage) GetUserId() string { return t.UserId }
func (t *defaultMessage) GetType() string { return t.MsgType }
func (t *defaultMessage) GetMsg() map[string]interface{} { return t.Msg }
func (t *defaultMessage) GetMetadata() Metadata { return t.Metadata }
func (t *defaultMessage) GetAllMap() map[string]interface{} {
data := make(map[string]interface{})
for msgKey, msgValue := range t.GetMsg() {
for metaKey, metaValue := range t.GetMetadata().GetValues() {
if msgKey == metaKey {
data[msgKey] = metaValue
} else {
if _, ok := data[msgKey]; !ok {
data[msgKey] = msgValue
}
if _, ok := data[metaKey]; !ok {
data[metaKey] = metaValue
}
}
}
}
return data
}
func (t *defaultMessage) SetType(msgType string) { t.MsgType = msgType }
func (t *defaultMessage) SetMsg(msg map[string]interface{}) { t.Msg = msg }
func (t *defaultMessage) SetOriginator(originator string) { t.Originator = originator }
func (t *defaultMessage) SetUserId(userId string) { t.UserId = userId }
func (t *defaultMessage) SetMetadata(metadata Metadata) { t.Metadata = metadata }
func (t *defaultMessage) MarshalBinary() ([]byte, error) {
return json.Marshal(t)
}
@@ -112,7 +142,7 @@ func (t *defaultMetadata) Keys() []string {
func (t *defaultMetadata) GetKeyValue(key string) interface{} {
if _, found := t.values[key]; !found {
logrus.Fatalf("no key '%s' in metadata", key)
return nil
}
return t.values[key]
}
@@ -127,11 +157,3 @@ func (t *defaultMetadata) GetValues() map[string]interface{} {
func (t *defaultMetadata) SetValues(values map[string]interface{}) {
t.values = values
}
type MesLog struct {
NodeName string `json:"nodeName"`
startTime time.Time `json:"startTime"`
endTime time.Time `json:"endTime"`
result string `json:"result"`
Remark string `json:"remark"`
}