SenML package is an implementation of RFC8428 - Sensor Measurement Lists (SenML) in Go.
It provides fully compliant data model and functionalities for:
- Validation of various SenML fields
- Normalization
- SenML Units
- SenML Media Types
- Encoding/Decoding (codec package)
Documentation and various usage examples are availabe as Go Docs: senml, codec
go get github.com/farshidtz/senml/v2
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>
}