senml

package module
v2.0.0-alpha.6 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2020 License: MIT Imports: 3 Imported by: 1

README

SenML: Sensor Measurement Lists

GoDoc Build Status Go Report Card GitHub tag (latest SemVer) GitHub tag (latest SemVer pre-release)

SenML package is an implementation of RFC8428 - Sensor Measurement Lists (SenML) in Go.

Note: The 2nd version of this library is under development.

Install

go get github.com/farshidtz/senml/v2

Usage

package main

import (
	"fmt"
	"github.com/farshidtz/senml/v2"
)

func main() {
	jsonBytes := []byte(`[{"bn":"room1/temp","u":"Cel","t":1276020076.305,"v":23.5},{"u":"Cel","t":1276020091.305,"v":23.6}]`)

	// decode JSON
	pack, err := senml.Decode(jsonBytes, senml.JSON)
	if err != nil {
		panic(err) // don't panic, handle the error
	}

	// encode to pretty JSON
	senmlBytes, err := pack.Encode(senml.JSON, &senml.OutputOptions{PrettyPrint: true})
	if err != nil {
		panic(err) // don't panic, handle the error
	}
	fmt.Printf("%s\n", senmlBytes)
	/* Output:
	[
		{"bn":"room1/temp","u":"Cel","t":1276020076.305,"v":23.5},
		{"u":"Cel","t":1276020091.305,"v":23.6}
	]
	*/

	// encode to XML
	xmlBytes, err := pack.Encode(senml.XML, nil)
	if err != nil {
		panic(err) // don't panic, handle the error
	}
	fmt.Printf("%s\n", xmlBytes)
	/* Output:
	<sensml xmlns="urn:ietf:params:xml:ns:senml"><senml bn="room1/temp" u="Cel" t="1.276020076305e+09" v="23.5"></senml><senml u="Cel" t="1.276020091305e+09" v="23.6"></senml></sensml>
	*/

	// encode to CSV (format: name,excel-time,value,unit)
	csvBytes, err := pack.Encode(senml.CSV, nil)
	if err != nil {
		panic(err) // don't panic, handle the error
	}
	fmt.Printf("%s\n", csvBytes)
	/* Output:
	room1/temp,40337.750883,23.500000,Cel
	room1/temp,40337.751057,23.600000,Cel
	*/
}

Documentation

Index

Constants

View Source
const (
	MediaTypeSenmlJSON = "application/senml+json"
	MediaTypeSenmlCBOR = "application/senml+cbor"
	MediaTypeSenmlXML  = "application/senml+xml"
	MediaTypeSenmlEXI  = "application/senml-exi"
)

Sensor Measurement Lists (SenML) Media Types

View Source
const (
	MediaTypeSensmlJSON = "application/sensml+json"
	MediaTypeSensmlCBOR = "application/sensml+cbor"
	MediaTypeSensmlXML  = "application/sensml+xml"
	MediaTypeSensmlEXI  = "application/sensml-exi"
)

Sensor Streaming Measurement Lists (SenSML) Media Types

View Source
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

View Source
const DefaultBaseVersion = 10

Variables

This section is empty.

Functions

func ValidateName

func ValidateName(name string) error

ValidateName validates the SenML name

Types

type Format

type Format int

Format is the SenML encoding/decoding format

type Pack

type Pack []Record

Pack is a SenML Pack:

One or more SenML Records in an array structure.

func (Pack) Clone

func (p Pack) Clone() (clone Pack)

Clone returns a deep copy of p

func (Pack) Normalize

func (p Pack) Normalize()

Normalize removes all the base items adds them to corresponding record fields. It converts relative times to absolute times. Normalize must be called on a validated pack only. The Decode function performs this validation internally. A SenML Record is referred to as "resolved" if it does not contain

 any base values, i.e., labels starting with the character "b", except
 for Base Version fields (see below), and has no relative times.  To
 resolve the Records, the applicable base values of the SenML Pack (if
 any) are applied to the Record.  That is, for the base values in the
 Record or before the Record in the Pack, Name and Base Name are
 concatenated, the Base Time is added to the time of the Record, the
 Base Unit is applied to the Record if it did not contain a Unit, etc.
 In addition, the Records need to be in chronological order in the
 Pack.
Source: https://tools.ietf.org/html/rfc8428#section-4.6

func (Pack) Validate

func (p Pack) Validate() error

Validate tests if SenML is valid

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:

One measurement or configuration instance in time presented using the SenML data model.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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