codec

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package codec provides various encoding/decoding functions for SenML Packs: http://github.com/farshidtz/senml

Index

Examples

Constants

View Source
const DefaultCSVHeader = "Time,Update Time,Name,Unit,Value,String Value,Boolean Value,Data Value,Sum"

DefaultCSVHeader is the default (currently fixed) CSV header

Variables

This section is empty.

Functions

func Decode

func Decode(mediaType string, b []byte, options ...Option) (senml.Pack, error)

Decode is a convenient function to call the decoding functions using the corresponding media type

Example
input := `[{"bn":"room1/temp","u":"Cel","t":1276020076.305,"v":23.5},{"u":"Cel","t":1276020091.305,"v":23.6}]`

// decode JSON
pack, err := Decode(senml.MediaTypeSenmlJSON, []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
}
Output:

func DecodeCBOR

func DecodeCBOR(b []byte, _ ...Option) (senml.Pack, error)

DecodeCBOR takes a SenML pack in CBOR bytes and decodes it into a Pack. The options are ignored.

func DecodeCSV

func DecodeCSV(b []byte, options ...Option) (senml.Pack, error)

DecodeCSV takes a SenML pack in CSV bytes and decodes it into a Pack

Example
input := `Time,Update Time,Name,Unit,Value,String Value,Boolean Value,Data Value,Sum
1276020000,0,room1/air_quality,,,good,,,
1276020100,0,room1/air_quality,,,excellent,,,`

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

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

func DecodeJSON

func DecodeJSON(b []byte, _ ...Option) (senml.Pack, error)

DecodeJSON takes a SenML pack in JSON bytes and decodes it into a Pack. The options are ignored.

Example
input := `[{"bn":"room1/temp","u":"Cel","t":1276020076.305,"v":23.5},{"u":"Cel","t":1276020091.305,"v":23.6}]`

// decode JSON
pack, err := 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
}
Output:

func DecodeXML

func DecodeXML(b []byte, _ ...Option) (senml.Pack, error)

DecodeXML takes a SenML pack in XML bytes and decodes it into a Pack. The options are ignored.

Example
input := `<sensml xmlns="urn:ietf:params:xml:ns:senml"><senml bn="dev123" bt="-45.67" bu="degC" bver="5" n="temp" u="degC" t="-1" ut="10" v="22.1" s="0"></senml><senml n="room" t="-1" vs="kitchen"></senml><senml n="data" vd="abc"></senml><senml n="ok" vb="true"></senml></sensml>`

// decode XML
pack, err := DecodeXML([]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
}
Output:

func Encode

func Encode(mediaType string, p senml.Pack, options ...Option) ([]byte, error)

Encode is a convenient function to call the encoding functions using the corresponding media type

Example
v := 23.1
var p senml.Pack = []senml.Record{
	{Value: &v, Unit: "Cel", Name: "urn:dev:ow:10e2073a01080063"},
}

dataOut, err := Encode(senml.MediaTypeSenmlJSON, p)
if err != nil {
	panic(err) // handle the error
}
fmt.Printf("%s", dataOut)
Output:

[{"n":"urn:dev:ow:10e2073a01080063","u":"Cel","v":23.1}]

func EncodeCBOR

func EncodeCBOR(p senml.Pack, _ ...Option) ([]byte, error)

EncodeCBOR serializes the SenML pack into CBOR bytes. The options are ignored.

Example
v := 23.1
var p senml.Pack = []senml.Record{
	{Value: &v, Unit: "Cel", Name: "urn:dev:ow:10e2073a01080063"},
}

dataOut, err := EncodeCBOR(p)
if err != nil {
	panic(err) // handle the error
}
fmt.Printf("%v", dataOut)
Output:

[129 163 0 120 27 117 114 110 58 100 101 118 58 111 119 58 49 48 101 50 48 55 51 97 48 49 48 56 48 48 54 51 1 99 67 101 108 2 251 64 55 25 153 153 153 153 154]
Example (Hex)

Output Diagnostic: http://cbor.me/?bytes=81a300781b75726e3a6465763a6f773a31306532303733613031303830303633016343656c02fb403719999999999a

v := 23.1
var p senml.Pack = []senml.Record{
	{Value: &v, Unit: "Cel", Name: "urn:dev:ow:10e2073a01080063"},
}

dataOut, err := EncodeCBOR(p)
if err != nil {
	panic(err) // handle the error
}
fmt.Printf(hex.EncodeToString(dataOut))
Output:

81a300781b75726e3a6465763a6f773a31306532303733613031303830303633016343656c02fb403719999999999a

func EncodeCSV

func EncodeCSV(p senml.Pack, options ...Option) ([]byte, error)

EncodeCSV serializes the SenML pack into CSV bytes

Example
value := 22.1
var pack senml.Pack = []senml.Record{
	{Time: 1276020000, Name: "air_quality", StringValue: "good", BaseName: "room1/"},
	{Time: 1276020100, Name: "air_quality", StringValue: "excellent"},
	{Time: 1276020100, Name: "temp", Value: &value, Unit: senml.UnitCelsius},
}

// encode to CSV (format: name,excel-time,value,unit)
csvBytes, err := EncodeCSV(pack, SetDefaultHeader)
if err != nil {
	panic(err) // handle the error
}
fmt.Printf("%s\n", csvBytes)
Output:

Time,Update Time,Name,Unit,Value,String Value,Boolean Value,Data Value,Sum
1276020000,0,room1/air_quality,,,good,,,
1276020100,0,room1/air_quality,,,excellent,,,
1276020100,0,room1/temp,Cel,22.1,,,,

func EncodeJSON

func EncodeJSON(p senml.Pack, options ...Option) ([]byte, error)

EncodeJSON serializes the SenML pack into JSON bytes

Example
value := 22.1
var pack senml.Pack = []senml.Record{
	{Time: 1276020000, Name: "air_quality", StringValue: "good", BaseName: "room1/"},
	{Time: 1276020100, Name: "air_quality", StringValue: "excellent"},
	{Time: 1276020100, Name: "temp", Value: &value, Unit: senml.UnitCelsius},
}

pack.Normalize() // optional

dataOut, err := EncodeJSON(pack, SetPrettyPrint)
if err != nil {
	panic(err) // handle the error
}
fmt.Printf("%s", dataOut)
Output:

[
  {"n":"room1/air_quality","t":1276020000,"vs":"good"},
  {"n":"room1/air_quality","t":1276020100,"vs":"excellent"},
  {"n":"room1/temp","u":"Cel","t":1276020100,"v":22.1}
]

func EncodeXML

func EncodeXML(p senml.Pack, options ...Option) ([]byte, error)

EncodeXML serializes the SenML pack into XML bytes

Example
var pack senml.Pack = []senml.Record{
	{Time: 1276020000, Name: "room1/temp_label", StringValue: "hot"},
	{Time: 1276020100, Name: "room1/temp_label", StringValue: "cool"},
}

// encode to Pretty XML
xmlBytes, err := EncodeXML(pack, SetPrettyPrint)
if err != nil {
	panic(err) // handle the error
}
fmt.Printf("%s\n", xmlBytes)
Output:

<sensml xmlns="urn:ietf:params:xml:ns:senml">
  <senml n="room1/temp_label" t="1.27602e+09" vs="hot"></senml>
  <senml n="room1/temp_label" t="1.2760201e+09" vs="cool"></senml>
</sensml>

func ReadCSV

func ReadCSV(r io.Reader, options ...Option) (senml.Pack, error)

ReadCSV reads from the given reader to construct and returns a Pack

func SetDefaultHeader

func SetDefaultHeader(o *codecOptions)

SetDefaultHeader enables header for CSV encoding/decoding

Example
var p senml.Pack = []senml.Record{
	{Time: 946684700, Name: "lamp/brightness", StringValue: "100", Unit: senml.UnitLumen},
	{Time: 946684800, Name: "lamp/brightness", StringValue: "500", Unit: senml.UnitLumen},
}

dataOut, err := EncodeCSV(p, SetDefaultHeader)
if err != nil {
	panic(err) // handle the error
}
fmt.Printf("%s", dataOut)
Output:

Time,Update Time,Name,Unit,Value,String Value,Boolean Value,Data Value,Sum
946684700,0,lamp/brightness,lm,,100,,,
946684800,0,lamp/brightness,lm,,500,,,

func SetPrettyPrint

func SetPrettyPrint(o *codecOptions)

SetPrettyPrint enables indentation for JSON and XML encoding

Example
var p senml.Pack = []senml.Record{
	{Time: 946684700, Name: "lamp/brightness", StringValue: "100", Unit: senml.UnitLumen},
	{Time: 946684800, Name: "lamp/brightness", StringValue: "500", Unit: senml.UnitLumen},
}

dataOut, err := EncodeJSON(p, SetPrettyPrint)
if err != nil {
	panic(err) // handle the error
}
fmt.Printf("%s", dataOut)
Output:

[
  {"n":"lamp/brightness","u":"lm","t":946684700,"vs":"100"},
  {"n":"lamp/brightness","u":"lm","t":946684800,"vs":"500"}
]

func WriteCSV

func WriteCSV(p senml.Pack, w io.Writer, options ...Option) error

WriteCSV serializes and writes the Pack on the given writer

Example
var pack senml.Pack = []senml.Record{
	{Time: 1276020000, Name: "room1/air_quality", StringValue: "good"},
	{Time: 1276020100, Name: "room1/air_quality", StringValue: "excellent"},
}

var writer io.Writer = os.Stdout // write to stdout
err := WriteCSV(pack, writer, SetDefaultHeader)
if err != nil {
	panic(err) // handle the error
}
Output:

Time,Update Time,Name,Unit,Value,String Value,Boolean Value,Data Value,Sum
1276020000,0,room1/air_quality,,,good,,,
1276020100,0,room1/air_quality,,,excellent,,,

Types

type Decoder

type Decoder func(b []byte, options ...Option) (senml.Pack, error)

Decoder is the decoding function type

type Encoder

type Encoder func(p senml.Pack, options ...Option) ([]byte, error)

Encoder is the encoding function type

type Option

type Option func(*codecOptions)

Option is the function type for setting codec options

type Reader

type Reader func(r io.Reader, options ...Option) (senml.Pack, error)

Reader is the reading function type

type Writer

type Writer func(p senml.Pack, w io.Writer, options ...Option) error

Write is the writing function type

Jump to

Keyboard shortcuts

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