github.com/simpleiot/simpleiot@v0.18.3/client/msg.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/nats-io/nats.go"
     9  	"github.com/simpleiot/simpleiot/data"
    10  )
    11  
    12  // DecodeNodePointsMsg decodes NATS message into node ID and points
    13  func DecodeNodePointsMsg(msg *nats.Msg) (string, []data.Point, error) {
    14  	chunks := strings.Split(msg.Subject, ".")
    15  	if len(chunks) < 2 {
    16  		return "", []data.Point{}, fmt.Errorf("Invalid NodePoints subject: %v", msg.Subject)
    17  	}
    18  	nodeID := chunks[1]
    19  	points, err := data.PbDecodePoints(msg.Data)
    20  	if err != nil {
    21  		log.Println("Error decoding Pb points:", err)
    22  		return "", []data.Point{}, fmt.Errorf("Error decoding Pb points: %w", err)
    23  	}
    24  
    25  	return nodeID, points, nil
    26  }
    27  
    28  // DecodeEdgePointsMsg decodes NATS message into node ID and points
    29  // returns nodeID, parentID, points, error
    30  func DecodeEdgePointsMsg(msg *nats.Msg) (string, string, []data.Point, error) {
    31  	chunks := strings.Split(msg.Subject, ".")
    32  	if len(chunks) < 3 {
    33  		return "", "", []data.Point{}, fmt.Errorf("Invalid EdgePoints subject: %v", msg.Subject)
    34  	}
    35  	nodeID := chunks[1]
    36  	parentID := chunks[2]
    37  	points, err := data.PbDecodePoints(msg.Data)
    38  	if err != nil {
    39  		log.Println("Error decoding Pb points:", err)
    40  		return "", "", []data.Point{}, fmt.Errorf("Error decoding Pb points: %w", err)
    41  	}
    42  
    43  	return nodeID, parentID, points, nil
    44  }
    45  
    46  // DecodeUpNodePointsMsg decodes NATS message into node ID and points
    47  // returns upID, nodeID, points, error
    48  func DecodeUpNodePointsMsg(msg *nats.Msg) (string, string, []data.Point, error) {
    49  	chunks := strings.Split(msg.Subject, ".")
    50  	if len(chunks) < 3 {
    51  		return "", "", []data.Point{}, fmt.Errorf("Invalid UpNode subject: %v", msg.Subject)
    52  	}
    53  	upID := chunks[1]
    54  	nodeID := chunks[2]
    55  	points, err := data.PbDecodePoints(msg.Data)
    56  	if err != nil {
    57  		log.Println("Error decoding Pb points:", err)
    58  		return "", "", []data.Point{}, fmt.Errorf("Error decoding Pb points: %w", err)
    59  	}
    60  
    61  	return upID, nodeID, points, nil
    62  }
    63  
    64  // DecodeUpEdgePointsMsg decodes NATS message into node ID and points
    65  // returns upID, nodeID, parentID, points, error
    66  func DecodeUpEdgePointsMsg(msg *nats.Msg) (string, string, string, []data.Point, error) {
    67  	chunks := strings.Split(msg.Subject, ".")
    68  	if len(chunks) < 4 {
    69  		return "", "", "", []data.Point{}, fmt.Errorf("Invalid UpEdge subject: %v", msg.Subject)
    70  	}
    71  	upID := chunks[1]
    72  	nodeID := chunks[2]
    73  	parentID := chunks[3]
    74  	points, err := data.PbDecodePoints(msg.Data)
    75  	if err != nil {
    76  		log.Println("Error decoding Pb points:", err)
    77  		return "", "", "", []data.Point{}, fmt.Errorf("Error decoding Pb points: %w", err)
    78  	}
    79  
    80  	return upID, nodeID, parentID, points, nil
    81  }