codec

package
v2.0.0-beta.1 Latest Latest
Warning

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

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

Documentation

Index

Examples

Constants

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

CSVHeader is the fixed CSV header

Variables

This section is empty.

Functions

func DecodeCBOR

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

DecodeCBOR takes a SenML pack in CBOR bytes and decodes it into a Pack

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
946684799,10,dev123temp,degC,22.1,,,,0
946684799,0,dev123room,degC,,kitchen,,,`

// 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
}

func DecodeJSON

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

DecodeJSON takes a SenML pack in JSON bytes and decodes it into a Pack

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
}

func DecodeXML

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

DecodeXML takes a SenML pack in XML bytes and decodes it into a Pack

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
}

func EncodeCBOR

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

EncodeCBOR serializes the SenML pack into CBOR bytes

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
var pack senml.Pack = []senml.Record{
	{Time: 1276020000, Name: "room1/temp_label", StringValue: "hot"},
	{Time: 1276020100, Name: "room1/temp_label", StringValue: "cool"},
}

// 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/temp_label,,,hot,,,
1276020100,0,room1/temp_label,,,cool,,,

func EncodeJSON

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

EncodeJSON serializes the SenML pack into JSON bytes

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

dataOut, err := EncodeJSON(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 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)

func SetDefaultHeader

func SetDefaultHeader(o *codecOptions)

SetDefaultHeader enables header for CSV encoding/decoding

func SetPrettyPrint

func SetPrettyPrint(o *codecOptions)

SetPrettyPrint enables indentation for JSON and XML encoding

func WriteCSV

func WriteCSV(p senml.Pack, w io.Writer, options ...Option) error
Example
var pack senml.Pack = []senml.Record{
	{Time: 1276020000, Name: "room1/temp_label", StringValue: "hot"},
	{Time: 1276020100, Name: "room1/temp_label", StringValue: "cool"},
}

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/temp_label,,,hot,,,
1276020100,0,room1/temp_label,,,cool,,,

Types

type Option

type Option func(*codecOptions)

Option is the function type for setting codec options

Jump to

Keyboard shortcuts

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