Skip to content

Sensor Measurement Lists (SenML) in Go

License

Notifications You must be signed in to change notification settings

farshidtz/senml

Repository files navigation

SenML: Sensor Measurement Lists

GoDoc Test Go Report Card

SenML package is an implementation of RFC8428 - Sensor Measurement Lists (SenML) in Go.

It provides fully compliant data model and functionalities for:

Documentation

Documentation and various usage examples are availabe as Go Docs: senml, codec

Usage

Install

go get github.com/farshidtz/senml/v2

Simple Example

More examples are available in the documentation.

Decode JSON bytes into a SenML Pack, validate, normalize, and encode it as pretty XML:

package main

import (
	"fmt"
	"github.com/farshidtz/senml/v2/codec"
)

func main() {
	input := `[{"bn":"room1/temp","u":"Cel","t":1276020076,"v":23.5},{"u":"Cel","t":1276020091,"v":23.6}]`

	// decode JSON
	pack, err := codec.DecodeJSON([]byte(input))
	if err != nil {
		panic(err) // handle the error
	}

	// validate the SenML Pack
	err = pack.Validate()
	if err != nil {
		panic(err) // handle the error
	}

	// normalize the SenML Pack
	pack.Normalize()

	// encode the normalized SenML Pack to XML
	dataOut, err := codec.EncodeXML(pack, codec.SetPrettyPrint)
	if err != nil {
		panic(err) // handle the error
	}
	fmt.Printf("%s", dataOut)
	// Output:
	// <sensml xmlns="urn:ietf:params:xml:ns:senml">
	//   <senml n="room1/temp" u="Cel" t="1.276020076e+09" v="23.5"></senml>
	//   <senml n="room1/temp" u="Cel" t="1.276020091e+09" v="23.6"></senml>
	// </sensml>
}

Go Playground