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

     1  package client
     2  
     3  import "fmt"
     4  
     5  // create subject strings for various types of messages
     6  
     7  // SubjectNodePoints constructs a NATS subject for node points
     8  func SubjectNodePoints(nodeID string) string {
     9  	return fmt.Sprintf("p.%v", nodeID)
    10  }
    11  
    12  // SubjectEdgePoints constructs a NATS subject for edge points
    13  func SubjectEdgePoints(nodeID, parentID string) string {
    14  	return fmt.Sprintf("p.%v.%v", nodeID, parentID)
    15  }
    16  
    17  // SubjectNodeAllPoints provides subject for all points for any node
    18  func SubjectNodeAllPoints() string {
    19  	return "p.*"
    20  }
    21  
    22  // SubjectEdgeAllPoints provides subject for all edge points for any node
    23  func SubjectEdgeAllPoints() string {
    24  	return "p.*.*"
    25  }
    26  
    27  // SubjectNodeHRPoints constructs a NATS subject for high rate node points
    28  func SubjectNodeHRPoints(nodeID string) string {
    29  	return fmt.Sprintf("phr.%v", nodeID)
    30  }
    31  
    32  // Destination indicates the destination for generated points, including the
    33  // point type and key
    34  type Destination struct {
    35  	// NodeID indicating the destination for points; if not specified, the point
    36  	// destination is determined by the Parent field
    37  	NodeID string `point:"nodeID"`
    38  	// Parent is set if points should be sent to the parent node; otherwise,
    39  	// points are send to the origin node.
    40  	Parent bool `point:"parent"`
    41  	// HighRate indicates that points should be sent over the phrup NATS
    42  	// subject. If set, points are never sent to the origin node; rather, it is
    43  	// implied that points will be sent to the NodeID (if set) or the parent
    44  	// node.
    45  	HighRate bool `point:"highRate"`
    46  	// PointType indicates the point type for generated points
    47  	PointType string `point:"pointType"`
    48  	// PointKey indicates the point key for generated points
    49  	PointKey string `point:"pointKey"`
    50  }
    51  
    52  // Subject returns the NATS subject on which points for this Destination shall
    53  // be published
    54  func (sd Destination) Subject(originID string, parentID string) string {
    55  	if sd.HighRate {
    56  		// HighRate implies Parent
    57  		destID := parentID
    58  		if sd.NodeID != "" {
    59  			destID = sd.NodeID
    60  		}
    61  		return fmt.Sprintf("phrup.%v.%v", destID, originID)
    62  	}
    63  	destID := originID
    64  	if sd.NodeID != "" {
    65  		destID = sd.NodeID
    66  	} else if sd.Parent {
    67  		destID = parentID
    68  	}
    69  	return SubjectNodePoints(destID)
    70  }