Documentation ¶
Overview ¶
Package codec provides various encoding/decoding functions for SenML Packs: http://github.com/farshidtz/senml
Index ¶
- Constants
- func Decode(mediaType string, b []byte, options ...Option) (senml.Pack, error)
- func DecodeCBOR(b []byte, _ ...Option) (senml.Pack, error)
- func DecodeCSV(b []byte, options ...Option) (senml.Pack, error)
- func DecodeJSON(b []byte, _ ...Option) (senml.Pack, error)
- func DecodeXML(b []byte, _ ...Option) (senml.Pack, error)
- func Encode(mediaType string, p senml.Pack, options ...Option) ([]byte, error)
- func EncodeCBOR(p senml.Pack, _ ...Option) ([]byte, error)
- func EncodeCSV(p senml.Pack, options ...Option) ([]byte, error)
- func EncodeJSON(p senml.Pack, options ...Option) ([]byte, error)
- func EncodeXML(p senml.Pack, options ...Option) ([]byte, error)
- func ReadCSV(r io.Reader, options ...Option) (senml.Pack, error)
- func SetDefaultHeader(o *codecOptions)
- func SetPrettyPrint(o *codecOptions)
- func WriteCSV(p senml.Pack, w io.Writer, options ...Option) error
- type Decoder
- type Encoder
- type Option
- type Reader
- type Writer
Examples ¶
Constants ¶
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 ¶
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 ¶
DecodeCBOR takes a SenML pack in CBOR bytes and decodes it into a Pack. The options are ignored.
func DecodeCSV ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 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 ¶
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,,,