Documentation
¶
Overview ¶
Package senml is an implementation of RFC8428 - Sensor Measurement Lists (SenML): https://tools.ietf.org/html/rfc8428
The sub-package codec provides various encoding/decoding functions for senml.Pack. The documentation for codec package is available at: https://pkg.go.dev/github.com/farshidtz/senml/v2/codec
Example (DecodeValidateNormalizeEncode) ¶
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 JSON dataOut, err := codec.EncodeJSON(pack, codec.SetPrettyPrint) if err != nil { panic(err) // handle the error } fmt.Printf("%s", dataOut) }
Output: [ {"n":"room1/temp","u":"Cel","t":1276020076,"v":23.5}, {"n":"room1/temp","u":"Cel","t":1276020091,"v":23.6} ]
Index ¶
Examples ¶
Constants ¶
const ( MediaTypeSenmlJSON = "application/senml+json" MediaTypeSenmlCBOR = "application/senml+cbor" MediaTypeSenmlXML = "application/senml+xml" MediaTypeSenmlEXI = "application/senml-exi" // Custom types MediaTypeCustomSenmlCSV = "text/vnd.senml.v2+csv" )
Sensor Measurement Lists (SenML) Media Types
const ( MediaTypeSensmlJSON = "application/sensml+json" MediaTypeSensmlCBOR = "application/sensml+cbor" MediaTypeSensmlXML = "application/sensml+xml" MediaTypeSensmlEXI = "application/sensml-exi" // Custom types MediaTypeCustomSensmlCSV = "text/vnd.sensml.v2+csv" )
Sensor Streaming Measurement Lists (SenSML) Media Types
const ( UnitMeter = "m" // meter UnitKilogram = "k" // kilogram UnitGram = "g" // gram - NOT RECOMMENDED UnitSecond = "s" // second UnitAmpere = "A" // ampere UnitKelvin = "K" // kelvin UnitCandela = "cd" // candela UnitMole = "mol" // mole UnitHertz = "Hz" // hertz UnitRadian = "rad" // radian UnitSteradian = "sr" // steradian UnitNewton = "N" // newton UnitPascal = "Pa" // pascal UnitJoule = "J" // joule UnitWatt = "watt" // watt UnitCoulomb = "C" // coulomb UnitVolt = "V" // volt UnitFarad = "F" // farad UnitOhm = "Ohm" // ohm UnitSiemens = "S" // siemens UnitWeber = "Wb" // weber UnitTesla = "T" // tesla UnitHenry = "H" // henry UnitCelsius = "Cel" // degrees Celsius UnitLumen = "lm" // lumen UnitLux = "lx" // lux UnitBecquerel = "Bq" // becquerel UnitGray = "Gy" // gray UnitSievert = "Sv" // sievert UnitKatal = "kat" // katal UnitSquareMeter = "m2" // square meter (area) UnitCubicMeter = "m3" // cubic meter (volume) UnitLiter = "l" // liter (volume) - NOT RECOMMENDED UnitMeterPerSecond = "m/s" // meter per second (velocity) UnitMeterPerSquareSecond = "m/s2" // meter per square second (acceleration) UnitCubicMeterPerSecond = "m3/s" // cubic meter per second (flow rate) UnitLiterPerSecond = "l/s" // liter per second (flow rate) - NOT RECOMMENDED UnitWattPerSquareMeter = "W/m2" // watt per square meter (irradiance) UnitCandelaPerSquareMeter = "cd/m2" // candela per square meter (luminance) UnitBit = "bit" // bit (information content) UnitBitPerSecond = "bit/s" // bit per second (data rate) UnitLat = "lat" // degrees latitude UnitLon = "lon" // degrees longitude UnitpHValue = "pH" // pH value (acidity; logarithmic quantity) UnitDecibel = "dB" // decibel (logarithmic quantity) UnitDecibelWatt = "dBW" // decibel relative to 1 W (power level) UnitBel = "Bspl" // bel (sound pressure level; logarithmic quantity) - NOT RECOMMENDED UnitCount = "count" // 1 (counter value) UnitRatio = "/" // 1 (ratio, e.g., value of a switch) UnitAbsoluteRatio = "%" // 1 (ratio, e.g., value of a switch) - NOT RECOMMENDED UnitRelativeHumidity = "%RH" // percentage (relative humidity) UnitEnergyLevelPercentage = "%EL" // percentage (remaining battery energy level) UnitEnergyLevelSeconds = "EL" // seconds (remaining battery energy level UnitEventRateOnePerSecond = "1/s" // 1 per second (event rate) UnitEventRateOnePerMinute = "1/min" // 1 per minute (event rate, "rpm") - NOT RECOMMENDED UnitHeartRateBeatsPerMinute = "beat/min" // 1 per minute (heart rate in beats per minute) - NOT RECOMMENDED UnitHeartBeats = "beats" // 1 (cumulative number of heart beats) - NOT RECOMMENDED UnitSiemensPerMeter = "S/m" // siemens per meter (conductivity) )
SenML Units Registry: https://tools.ietf.org/html/rfc8428#section-12.1
const DefaultBaseVersion = 10
DefaultBaseVersion is the default version of the SenML data model based on: https://tools.ietf.org/html/rfc8428
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Pack ¶
type Pack []Record
Pack is a SenML Pack which is one or more SenML Records in an array structure.
func (Pack) Normalize ¶
func (p Pack) Normalize()
Normalize converts the SenML Pack to to the resolved format according to: https://tools.ietf.org/html/rfc8428#section-4.6
Normalize must be called on a validated pack only.
Example ¶
package main import ( "fmt" "github.com/farshidtz/senml/v2/codec" ) func main() { input := `[{"bn":"room1/temp","u":"Cel","t":1276020076.305,"v":23.5},{"u":"Cel","t":1276020091.305,"v":23.6}]` // decode JSON pack, err := codec.DecodeJSON([]byte(input)) if err != nil { panic(err) // handle the error } pack.Normalize() dataOut, err := codec.EncodeJSON(pack, codec.SetPrettyPrint) if err != nil { panic(err) // handle the error } fmt.Printf("%s", dataOut) }
Output: [ {"n":"room1/temp","u":"Cel","t":1276020076.305,"v":23.5}, {"n":"room1/temp","u":"Cel","t":1276020091.305,"v":23.6} ]
func (Pack) Validate ¶
Validate tests if the SenML Pack is valid
Example ¶
package main import ( "fmt" "github.com/farshidtz/senml/v2/codec" ) func main() { input := `[{"bn":"room1/ temp","t":1270000040,"v":23.5},{"t":1270000050,"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 { fmt.Println(err) // handle the error } }
Output: invalid name: must begin with alphanumeric and contain alphanumeric or one of - : . / _
type Record ¶
type Record struct { XMLName *bool `json:"_,omitempty" xml:"senml"` // BaseName is a string that is prepended to the names found in the entries. BaseName string `json:"bn,omitempty" xml:"bn,attr,omitempty" cbor:"-2,keyasint,omitempty"` // BaseTime is added to the time found in an entry. BaseTime float64 `json:"bt,omitempty" xml:"bt,attr,omitempty" cbor:"-3,keyasint,omitempty"` // BaseUnit is assumed for all entries, unless otherwise indicated. BaseUnit string `json:"bu,omitempty" xml:"bu,attr,omitempty" cbor:"-4,keyasint,omitempty"` // BaseVersion is a positive integer and defaults to 10 if not present. BaseVersion *int `json:"bver,omitempty" xml:"bver,attr,omitempty" cbor:"-1,keyasint,omitempty"` // BaseValue is added to the value found in an entry, similar to BaseTime. BaseValue *float64 `json:"bv,omitempty" xml:"bv,attr,omitempty" cbor:"-5,keyasint,omitempty"` // BaseSum is added to the sum found in an entry, similar to BaseTime. BaseSum *float64 `json:"bs,omitempty" xml:"bs,attr,omitempty" cbor:"-6,keyasint,omitempty"` // Name of the sensor or parameter. Name string `json:"n,omitempty" xml:"n,attr,omitempty" cbor:"0,keyasint,omitempty"` // Unit for a measurement value. Unit string `json:"u,omitempty" xml:"u,attr,omitempty" cbor:"1,keyasint,omitempty"` // Time in seconds when the value was recorded. Time float64 `json:"t,omitempty" xml:"t,attr,omitempty" cbor:"6,keyasint,omitempty"` // UpdateTime is the maximum seconds before there is an updated reading for a measurement. UpdateTime float64 `json:"ut,omitempty" xml:"ut,attr,omitempty" cbor:"7,keyasint,omitempty"` // Value is the float value of the entry. Value *float64 `json:"v,omitempty" xml:"v,attr,omitempty" cbor:"2,keyasint,omitempty"` // StringValue is the string value of the entry. StringValue string `json:"vs,omitempty" xml:"vs,attr,omitempty" cbor:"3,keyasint,omitempty"` // DataValue is a base64-encoded string value of the entry with the URL-safe alphabet. DataValue string `json:"vd,omitempty" xml:"vd,attr,omitempty" cbor:"8,keyasint,omitempty"` // BoolValue is the boolean value of the entry. BoolValue *bool `json:"vb,omitempty" xml:"vb,attr,omitempty" cbor:"4,keyasint,omitempty"` // Sum is the integrated sum of the float values over time. Sum *float64 `json:"s,omitempty" xml:"s,attr,omitempty" cbor:"5,keyasint,omitempty"` }
Record is a SenML Record which is one measurement or configuration instance in time presented using the SenML data model.