/
msg.go
44 lines (38 loc) · 1.22 KB
/
msg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package nats
import (
"errors"
"fmt"
"log"
"strings"
natsgo "github.com/nats-io/nats.go"
"github.com/simpleiot/simpleiot/data"
)
// DecodeNodePointsMsg decodes NATS message into node ID and points
func DecodeNodePointsMsg(msg *natsgo.Msg) (string, []data.Point, error) {
chunks := strings.Split(msg.Subject, ".")
if len(chunks) < 3 {
return "", []data.Point{}, errors.New("Error decoding node points subject")
}
nodeID := chunks[1]
points, err := data.PbDecodePoints(msg.Data)
if err != nil {
log.Println("Error decoding Pb points: ", err)
return "", []data.Point{}, fmt.Errorf("Error decoding Pb points: %w", err)
}
return nodeID, points, nil
}
// DecodeEdgePointsMsg decodes NATS message into node ID and points
func DecodeEdgePointsMsg(msg *natsgo.Msg) (string, string, []data.Point, error) {
chunks := strings.Split(msg.Subject, ".")
if len(chunks) < 4 {
return "", "", []data.Point{}, errors.New("Error decoding edge points subject")
}
nodeID := chunks[1]
parentID := chunks[2]
points, err := data.PbDecodePoints(msg.Data)
if err != nil {
log.Println("Error decoding Pb points: ", err)
return "", "", []data.Point{}, fmt.Errorf("Error decoding Pb points: %w", err)
}
return nodeID, parentID, points, nil
}