senml

package module
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2024 License: Apache-2.0 Imports: 5 Imported by: 12

README

SenML

coverage go report card license

This repository contains a lightweight implementation of RFC 8428 Sensor Measurement Lists (SenML)

Codec

The following formats are supported:

  • JSON
  • XML
  • CBOR

Normalization

Normalized (resolved) SenML Pack consists of resolved SenML Records. A SenML Record is referred to as "resolved" if it does not contain any base values, i.e., labels starting with the character "b", except for Base Version fields, and has no relative times.*

Validation

Valid SenML Record is the record with valid all the required fields and exactly one value field. Base values, if present, must be valid, as well. The Pack is valid if all the Records are valid and have the same Base Version. All SenML Records in a Pack must have the same version number. This is typically done by adding a Base Version field to only the first Record in the Pack or by using the default value.*

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrVersionChange indicates that records with different BaseVersion are present in Pack.
	ErrVersionChange = errors.New("version change")

	// ErrUnsupportedFormat indicates the wrong message format (format other than JSON, XML or CBOR).
	ErrUnsupportedFormat = errors.New("unsupported format")

	// ErrEmptyName indicates empty record name.
	ErrEmptyName = errors.New("empty name")

	// ErrBadChar indicates invalid char or that char is not allowed at the given position.
	ErrBadChar = errors.New("invalid char")

	// ErrTooManyValues indicates that there is more than one value field.
	ErrTooManyValues = errors.New("more than one value in the record")

	// ErrNoValues indicates that there is no value nor sum field present.
	ErrNoValues = errors.New("no value or sum field found")
)

Functions

func Encode

func Encode(p Pack, format Format) ([]byte, error)

Encode takes a SenML Pack and encodes it using the given format.

func Validate

func Validate(p Pack) error

Validate validates SenML records.

Types

type Format

type Format int

Format represents SenML message format.

const (
	JSON Format = 1 + iota
	XML
	CBOR
)

Supported formats are JSON, XML, and CBOR.

type Pack

type Pack struct {
	XMLName *bool    `json:"-" xml:"sensml"`
	Xmlns   string   `json:"-" xml:"xmlns,attr"`
	Records []Record `xml:"senml"`
}

Pack consists of SenML records array.

func Decode

func Decode(msg []byte, format Format) (Pack, error)

Decode takes a SenML message in the given format and parses it and decodes it into the returned SenML record.

func Normalize

func Normalize(p Pack) (Pack, error)

Normalize removes all the base values and expands records values with the base items. The base fields apply to the entries in the Record and also to all Records after it up to, but not including, the next Record that has that same base field.

func (*Pack) Len

func (p *Pack) Len() int

Implement sort.Interface so that resolved recods can easily be sorted.

func (*Pack) Less

func (p *Pack) Less(i, j int) bool

func (*Pack) Swap

func (p *Pack) Swap(i, j int)

type Record

type Record struct {
	XMLName     *bool    `json:"-" xml:"senml" cbor:"-"`
	Link        string   `json:"l,omitempty" xml:"l,attr,omitempty" cbor:"-"`
	BaseName    string   `json:"bn,omitempty" xml:"bn,attr,omitempty" cbor:"-2,keyasint,omitempty"`
	BaseTime    float64  `json:"bt,omitempty" xml:"bt,attr,omitempty" cbor:"-3,keyasint,omitempty"`
	BaseUnit    string   `json:"bu,omitempty" xml:"bu,attr,omitempty" cbor:"-4,keyasint,omitempty"`
	BaseVersion uint     `json:"bver,omitempty" xml:"bver,attr,omitempty" cbor:"-1,keyasint,omitempty"`
	BaseValue   float64  `json:"bv,omitempty" xml:"bv,attr,omitempty" cbor:"-5,keyasint,omitempty"`
	BaseSum     float64  `json:"bs,omitempty" xml:"bs,attr,omitempty" cbor:"-6,keyasint,omitempty"`
	Name        string   `json:"n,omitempty" xml:"n,attr,omitempty" cbor:"0,keyasint,omitempty"`
	Unit        string   `json:"u,omitempty" xml:"u,attr,omitempty" cbor:"1,keyasint,omitempty"`
	Time        float64  `json:"t,omitempty" xml:"t,attr,omitempty" cbor:"6,keyasint,omitempty"`
	UpdateTime  float64  `json:"ut,omitempty" xml:"ut,attr,omitempty" cbor:"7,keyasint,omitempty"`
	Value       *float64 `json:"v,omitempty" xml:"v,attr,omitempty" cbor:"2,keyasint,omitempty"`
	StringValue *string  `json:"vs,omitempty" xml:"vs,attr,omitempty" cbor:"3,keyasint,omitempty"`
	DataValue   *string  `json:"vd,omitempty" xml:"vd,attr,omitempty" cbor:"8,keyasint,omitempty"`
	BoolValue   *bool    `json:"vb,omitempty" xml:"vb,attr,omitempty" cbor:"4,keyasint,omitempty"`
	Sum         *float64 `json:"s,omitempty" xml:"s,attr,omitempty" cbor:"5,keyasint,omitempty"`
}

Record represents one senML record.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL