github.com/simpleiot/simpleiot@v0.18.3/data/notification.go (about) 1 package data 2 3 import ( 4 "github.com/simpleiot/simpleiot/internal/pb" 5 "google.golang.org/protobuf/proto" 6 ) 7 8 // Notification represents a message sent by a node 9 type Notification struct { 10 ID string `json:"id"` 11 Parent string `json:"parent"` 12 SourceNode string `json:"sourceNode"` 13 Subject string `json:"subject"` 14 Message string `json:"message"` 15 } 16 17 // ToPb converts to protobuf data 18 func (n *Notification) ToPb() ([]byte, error) { 19 pbNot := pb.Notification{ 20 Id: n.ID, 21 Parent: n.Parent, 22 SourceNode: n.SourceNode, 23 Subject: n.Subject, 24 Msg: n.Message, 25 } 26 27 return proto.Marshal(&pbNot) 28 } 29 30 // PbDecodeNotification converts a protobuf to notification data structure 31 func PbDecodeNotification(data []byte) (Notification, error) { 32 pbNot := &pb.Notification{} 33 34 err := proto.Unmarshal(data, pbNot) 35 if err != nil { 36 return Notification{}, err 37 } 38 39 return Notification{ 40 ID: pbNot.Id, 41 Parent: pbNot.Parent, 42 SourceNode: pbNot.SourceNode, 43 Subject: pbNot.Subject, 44 Message: pbNot.Msg, 45 }, nil 46 }