senml

package module
v0.0.0-...-e20f7e9 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: EUPL-1.2 Imports: 10 Imported by: 10

README

Package senml

godoc cirrus ci goreportcard gocover

Package senml implements the SenML specification, used for sending simple sensor measurements encoded in JSON, CBOR or XML.

The goal of this package is to not only support the specification, but to also make it easy to work with within Go.

Examples

Encoding:

package main

import (
	"fmt"
	"time"

	"github.com/silkeh/senml"
)

func main() {
	now := time.Now()
	list := []senml.Measurement{
		senml.NewValue("sensor:temperature", 23.5, senml.Celsius, now, 0),
		senml.NewValue("sensor:humidity", 33.7, senml.RelativeHumidityPercent, now, 0),
	}

	data, err := senml.EncodeJSON(list)
	if err != nil {
		fmt.Print("Error encoding to JSON:", err)
	}

	fmt.Printf("%s\n", data)
}

Decoding:

package main

import (
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	json := []byte(`[{"bn":"sensor:","bt":1555487588,"bu":"Cel","n":"temperature","v":23.5},{"n":"humidity","u":"%RH","v":33.7}]`)

	list, err := senml.DecodeJSON(json)
	if err != nil {
		fmt.Print("Error encoding to JSON:", err)
	}

	for _, m := range list {
		fmt.Printf("%#v\n", m)
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var AutoTime = true

AutoTime toggles the automatic setting of zero timestamps to now. Disabling this option results in timestamps relative to zero time when no exact time is given.

Functions

func EncodeCBOR

func EncodeCBOR(list []Measurement) (b []byte, err error)

EncodeCBOR encodes a list of measurements into CBOR.

func EncodeJSON

func EncodeJSON(list []Measurement) ([]byte, error)

EncodeJSON encodes a list of measurements into JSON.

func EncodeXML

func EncodeXML(list []Measurement) (b []byte, err error)

EncodeXML encodes a list of measurements into XML.

Types

type Attributes

type Attributes struct {
	Name       string
	Unit       Unit
	Time       time.Time
	UpdateTime time.Duration
}

Attributes contains the properties common to a measurement.

func (*Attributes) Attrs

func (m *Attributes) Attrs() *Attributes

Attrs returns a pointer to the measurement of the Measurement value.

func (*Attributes) Equal

func (m *Attributes) Equal(s *Attributes) bool

Equal returns true if the given attribute values are equal.

func (*Attributes) Record

func (m *Attributes) Record() Record

Record returns a SenML record representing the value.

type Boolean

type Boolean struct {
	Attributes
	Value bool
}

Boolean represents a boolean measurement value. It implements Measurement.

func NewBoolean

func NewBoolean(name string, value bool, unit Unit, time time.Time, updateTime time.Duration) *Boolean

NewBoolean returns a new Boolean value with the corresponding value and attributes.

func (*Boolean) Equal

func (v *Boolean) Equal(ml Measurement) bool

Equal returns true if the given Measurement value is equal.

func (*Boolean) Record

func (v *Boolean) Record() Record

Record returns a SenML record representing the value.

type Data

type Data struct {
	Attributes
	Value []byte
}

Data represents a measurement value returning binary data. It implements Measurement.

func NewData

func NewData(name string, value []byte, unit Unit, time time.Time, updateTime time.Duration) *Data

NewData returns a new Data value with the corresponding value and attributes.

func (*Data) Equal

func (v *Data) Equal(ml Measurement) bool

Equal returns true if the given Measurement value is equal.

func (*Data) Record

func (v *Data) Record() Record

Record returns a SenML record representing the value.

type Decimal

type Decimal struct {
	// contains filtered or unexported fields
}

Decimal represents a number in the form of a mantissa and a base-10 scaling factor (m*10^e). It implements the decimal fraction from RFC8949 section 3.4.4.

func NewDecimal

func NewDecimal(e, m int64) Decimal

NewDecimal returns a decimal fraction that represents m*10^e.

func (Decimal) Compare

func (f Decimal) Compare(g Decimal) int

Compare returns

-1 if f is less than g,
 0 if f equals g,
+1 if f is greater than g.
Example
package main

import (
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	numbers := []senml.Decimal{
		senml.NewDecimal(0, 0),
		senml.NewDecimal(-1, 2),
		senml.NewDecimal(1, 3),
	}

	for _, a := range numbers {
		for _, b := range numbers {
			switch a.Compare(b) {
			case -1:
				fmt.Printf("%e < %e\n", a, b)
			case 0:
				fmt.Printf("%e = %e\n", a, b)
			case 1:
				fmt.Printf("%e > %e\n", a, b)
			}

		}
	}

}
Output:
0e+0 = 0e+0
0e+0 < 2e-1
0e+0 < 3e+1
2e-1 > 0e+0
2e-1 = 2e-1
2e-1 < 3e+1
3e+1 > 0e+0
3e+1 > 2e-1
3e+1 = 3e+1

func (Decimal) Equal

func (f Decimal) Equal(g Decimal) bool

Equal returns true if the value in f is the same as g.

Example
package main

import (
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	numbers := []senml.Decimal{
		senml.NewDecimal(1, 1),
		senml.NewDecimal(-1, 2),
		senml.NewDecimal(0, 10),
	}

	for _, a := range numbers {
		for _, b := range numbers {
			fmt.Printf("%e == %e ? %t\n", a, b, a.Equal(b))
		}
	}

}
Output:
1e+1 == 1e+1 ? true
1e+1 == 2e-1 ? false
1e+1 == 10e+0 ? true
2e-1 == 1e+1 ? false
2e-1 == 2e-1 ? true
2e-1 == 10e+0 ? false
10e+0 == 1e+1 ? true
10e+0 == 2e-1 ? false
10e+0 == 10e+0 ? true

func (Decimal) Float

func (f Decimal) Float() *big.Float

Float returns the big.Float equivalent of the decimal fraction. The result is infinity when the value exceeds the maximum value of a big.Float, and zero when it is smaller than can be represented.

Example
package main

import (
	"fmt"
	"math"

	"github.com/silkeh/senml"
)

func main() {
	numbers := []senml.Decimal{
		senml.NewDecimal(0, 1),
		senml.NewDecimal(-1, 11),
		// Largest valid positive number:
		senml.NewDecimal(646456974, 8808063447118036156),
		// Smallest valid positive non-zero number:
		senml.NewDecimal(-646457012, 2838308228594544048),
		// Largest valid negative number:
		senml.NewDecimal(-646457012, -2838308228594544048),
		// Smallest valid negative number:
		senml.NewDecimal(646456974, -8808063447118036156),
		// Too large
		senml.NewDecimal(math.MaxInt32, 1),
		// Too small
		senml.NewDecimal(-math.MaxInt32, 1),
	}

	for _, n := range numbers {
		dm, de := n.MantExp()
		fm := n.Float()
		fe := fm.MantExp(fm)

		fmt.Printf("%d * 10^%d =~ %g * 2^%d\n", dm, de, fm, fe)
	}

}
Output:
1 * 10^0 =~ 0.5 * 2^1
11 * 10^-1 =~ 0.5499999999999998779 * 2^1
8808063447118036156 * 10^646456974 =~ 0.9999999999999999999 * 2^2147483647
2838308228594544048 * 10^-646457012 =~ 0.5000000000000000001 * 2^-2147483648
-2838308228594544048 * 10^-646457012 =~ -0.5000000000000000001 * 2^-2147483648
-8808063447118036156 * 10^646456974 =~ -0.9999999999999999999 * 2^2147483647
1 * 10^2147483647 =~ +Inf * 2^0
1 * 10^-2147483647 =~ 0 * 2^0

func (Decimal) Float64

func (f Decimal) Float64() float64

Float64 returns the float64 equivalent of the decimal fraction. The result is infinity when the value exceeds the maximum value of a float64, and zero when it is smaller than can be represented.

Example
package main

import (
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	numbers := []senml.Decimal{
		senml.NewDecimal(0, 1),
		senml.NewDecimal(-1, 11),
		// Largest valid positive number:
		senml.NewDecimal(292, 17976931348623157),
		// Smallest valid positive non-zero number:
		senml.NewDecimal(-323, 1),
		// Largest valid negative number:
		senml.NewDecimal(-323, -1),
		// Smallest valid negative number:
		senml.NewDecimal(292, -17976931348623157),
		// Too large
		senml.NewDecimal(400, 1),
		// Too small
		senml.NewDecimal(-400, 1),
	}

	for _, n := range numbers {
		fmt.Printf("%g =~ %g\n", n, n.Float64())
	}

}
Output:
1e+0 =~ 1
11e-1 =~ 1.1
17976931348623157e+292 =~ 1.7976931348623157e+308
1e-323 =~ 1e-323
-1e-323 =~ -1e-323
-17976931348623157e+292 =~ -1.7976931348623157e+308
1e+400 =~ +Inf
1e-400 =~ 0

func (Decimal) Format

func (f Decimal) Format(s fmt.State, verb rune)

Format converts the decimal fraction to a string in the same format as floating-point values. It supports the following formatting directives:

%e, %g, %s  scientific notation, eg: 1234e+10
%E, %G      scientific notation with a capital, eg: 1234E+10

Other formats, such as %b and %x are not implemented. It implements fmt.Formatter.

Example
package main

import (
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	formatters := []string{
		"%e", "%E", "%g", "%G",
		"%+e", "%+E", "%+g", "%+G",
		"%9e", "%09e", "%-9e|",
		"%o", "%s",
	}

	for _, f := range formatters {
		fmt.Printf("%-5s  "+f+"\n", f, senml.NewDecimal(20, 1234))
	}

}
Output:
%e     1234e+20
%E     1234E+20
%g     1234e+20
%G     1234E+20
%+e    +1234e+20
%+E    +1234E+20
%+g    +1234e+20
%+G    +1234E+20
%9e     1234e+20
%09e   01234e+20
%-9e|  1234e+20 |
%o     %!o(senml.Decimal=1234e+20)
%s     1234e+20

func (Decimal) Int

func (f Decimal) Int() *big.Int

Int returns the big.Int representation of the decimal fraction. The result is zero for very small positive values and minus one for very small negative values. This function is *very* slow for large decimal fractions, which also result in very large integers.

Example
package main

import (
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	numbers := []senml.Decimal{
		senml.NewDecimal(0, 1),
		senml.NewDecimal(-1, 11),
		senml.NewDecimal(24, 1),
		// Too small to be represented:
		senml.NewDecimal(-1, 1),
		senml.NewDecimal(-1, -1),
	}

	for _, n := range numbers {
		fmt.Printf("%s =~ %d\n", n, n.Int())
	}

}
Output:
1e+0 =~ 1
11e-1 =~ 1
1e+24 =~ 1000000000000000000000000
1e-1 =~ 0
-1e-1 =~ -1

func (Decimal) Int64

func (f Decimal) Int64() int64

Int64 returns the int64 representation of the decimal fraction. The result is:

  • truncated (rounded down) for all normal values.
  • zero for very small positive values.
  • minus one for very small negative values.
  • math.MaxInt64 for very large positive values.
  • math.MinInt64 for very large negative values.
Example
package main

import (
	"fmt"
	"math"

	"github.com/silkeh/senml"
)

func main() {
	numbers := []senml.Decimal{
		senml.NewDecimal(0, 1),
		senml.NewDecimal(-1, 11),
		senml.NewDecimal(-18, math.MaxInt64),
		senml.NewDecimal(-18, math.MinInt64),
		senml.NewDecimal(100, 0),
		// Too small to be represented:
		senml.NewDecimal(-1, 1),
		senml.NewDecimal(-1, -1),
		senml.NewDecimal(-19, 1),
		senml.NewDecimal(-19, -1),
		// Too large to be represented:
		senml.NewDecimal(1, 922337203685477581),
		senml.NewDecimal(1, -922337203685477581),
		senml.NewDecimal(19, 1),
		senml.NewDecimal(19, -1),
	}

	for _, n := range numbers {
		fmt.Printf("%e =~ %d\n", n, n.Int64())
	}

}
Output:
1e+0 =~ 1
11e-1 =~ 1
9223372036854775807e-18 =~ 9
-9223372036854775808e-18 =~ -9
0e+100 =~ 0
1e-1 =~ 0
-1e-1 =~ -1
1e-19 =~ 0
-1e-19 =~ -1
922337203685477581e+1 =~ 9223372036854775807
-922337203685477581e+1 =~ -9223372036854775808
1e+19 =~ 9223372036854775807
-1e+19 =~ -9223372036854775808

func (Decimal) IsInt

func (f Decimal) IsInt() bool

IsInt returns true if the decimal fraction can be represented by a big.Int.

Example
package main

import (
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	numbers := []senml.Decimal{
		senml.NewDecimal(0, 1),
		senml.NewDecimal(-1, 11),
		senml.NewDecimal(10, 11),
	}

	for _, n := range numbers {
		fmt.Printf("%e is int: %t\n", n, n.IsInt())
	}

}
Output:
1e+0 is int: true
11e-1 is int: false
11e+10 is int: true

func (Decimal) IsInt64

func (f Decimal) IsInt64() bool

IsInt64 returns true if the decimal fraction can be represented by an int64.

Example
package main

import (
	"fmt"
	"math"

	"github.com/silkeh/senml"
)

func main() {
	numbers := []senml.Decimal{
		senml.NewDecimal(0, 1),
		senml.NewDecimal(-1, 11),
		senml.NewDecimal(18, 9),
		senml.NewDecimal(17, 93),
		senml.NewDecimal(0, math.MaxInt64),
	}

	for _, n := range numbers {
		fmt.Printf("%e is int64: %t\n", n, n.IsInt64())
	}

}
Output:
1e+0 is int64: true
11e-1 is int64: false
9e+18 is int64: true
93e+17 is int64: false
9223372036854775807e+0 is int64: true

func (Decimal) MantExp

func (f Decimal) MantExp() (m int64, e int64)

MantExp returns the mantissa and the base-10 exponent of the decimal fraction.

func (Decimal) MarshalCBOR

func (f Decimal) MarshalCBOR() ([]byte, error)

MarshalCBOR implements cbor.Marshaler.

Example
f := senml.NewDecimal(-2, 27315)

data, _ := cbor.Marshal(f)
fmt.Printf("% 02x\n", data)
Output:
c4 82 21 19 6a b3

func (Decimal) MarshalJSON

func (f Decimal) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	f := senml.NewDecimal(-2, 27315)

	data, _ := json.Marshal(f)
	fmt.Println(string(data))
}
Output:
27315e-2

func (Decimal) MarshalText

func (f Decimal) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

Example
package main

import (
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	f := senml.NewDecimal(-2, 27315)

	data, _ := f.MarshalText()
	fmt.Println(string(data))
}
Output:
27315e-2

func (Decimal) String

func (f Decimal) String() string

String returns the string representation fo the decimal fraction.

func (Decimal) Uint64

func (f Decimal) Uint64() uint64

Uint64 returns the int64 representation of the decimal fraction. The result is:

  • truncated (rounded down) for all normal values.
  • zero for very small positive values.
  • math.MaxUint64 for very large positive values.
Example
package main

import (
	"fmt"
	"math"

	"github.com/silkeh/senml"
)

func main() {
	numbers := []senml.Decimal{
		senml.NewDecimal(0, 1),
		senml.NewDecimal(-1, 11),
		senml.NewDecimal(-18, math.MaxInt64),
		senml.NewDecimal(-18, math.MinInt64),
		senml.NewDecimal(100, 0),
		// Too small to be represented:
		senml.NewDecimal(-1, 1),
		senml.NewDecimal(-19, 1),
		// Too large to be represented:
		senml.NewDecimal(1, 922337203685477581),
		senml.NewDecimal(21, 1),
	}

	for _, n := range numbers {
		fmt.Printf("%e =~ %d\n", n, n.Uint64())
	}

}
Output:
1e+0 =~ 1
11e-1 =~ 1
9223372036854775807e-18 =~ 9
-9223372036854775808e-18 =~ 0
0e+100 =~ 0
1e-1 =~ 0
1e-19 =~ 0
922337203685477581e+1 =~ 9223372036854775810
1e+21 =~ 18446744073709551615

func (*Decimal) UnmarshalCBOR

func (f *Decimal) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements [Unmarshaler].

Example
var f senml.Decimal

cbor.Unmarshal([]byte{0xC4, 0x82, 0x21, 0x19, 0x6a, 0xb3}, &f)

fmt.Println(f)
Output:
27315e-2

func (*Decimal) UnmarshalJSON

func (f *Decimal) UnmarshalJSON(data []byte) error

UnmarshalJSON implements encoding/json.Unmarshaler.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	var f senml.Decimal

	json.Unmarshal([]byte("27315e-2"), &f)

	fmt.Println(f)
}
Output:
27315e-2

func (*Decimal) UnmarshalText

func (f *Decimal) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

Example
package main

import (
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	var f senml.Decimal

	f.UnmarshalText([]byte("27315e-2"))

	fmt.Println(f)
}
Output:
27315e-2

type Measurement

type Measurement interface {
	// Attrs returns a pointer to the measurement of the Measurement value.
	Attrs() *Attributes

	// Equal returns true if the given Measurement value is equal.
	Equal(Measurement) bool

	// Record returns a SenML record representing the value.
	Record() Record
}

Measurement represents a single SenML measurement value. This interface is meant to represent the various Measurement values, see: Value, Sum, String, Boolean and Data.

func Decode

func Decode(records []Record) (list []Measurement, err error)

Decode decodes a list of Measurement records into measurement values.

func DecodeCBOR

func DecodeCBOR(c []byte) ([]Measurement, error)

DecodeCBOR decodes a list of measurements from CBOR.

func DecodeJSON

func DecodeJSON(j []byte) ([]Measurement, error)

DecodeJSON decodes a list of measurements from JSON.

func DecodeXML

func DecodeXML(x []byte) ([]Measurement, error)

DecodeXML decodes a list of measurements from XML.

type Numeric

type Numeric interface{}

Numeric represents a numeric value. This can be any integer or floating point type (including json.Number), or a Decimal fraction. Numeric is equal to the empty interface, but using it for anything other than those types will result in a panic.

type Record

type Record struct {
	XMLName      xml.Name `json:"-" xml:"senml" cbor:"-"`
	BaseName     string   `json:"bn,omitempty" xml:"bn,attr,omitempty" cbor:"-2,keyasint,omitempty"`
	BaseTime     Numeric  `json:"bt,omitempty" xml:"bt,attr,omitempty" cbor:"-3,keyasint,omitempty"`
	BaseUnit     string   `json:"bu,omitempty" xml:"bu,attr,omitempty" cbor:"-4,keyasint,omitempty"`
	BaseValue    Numeric  `json:"bv,omitempty" xml:"bv,attr,omitempty" cbor:"-5,keyasint,omitempty"`
	BaseSum      Numeric  `json:"bs,omitempty" xml:"bs,attr,omitempty" cbor:"-6,keyasint,omitempty"`
	BaseVersion  int      `json:"bver,omitempty" xml:"bver,attr,omitempty" cbor:"-1,keyasint,omitempty"`
	Name         string   `json:"n,omitempty" xml:"n,attr,omitempty" cbor:"0,keyasint,omitempty"`
	Unit         string   `json:"u,omitempty" xml:"u,attr,omitempty" cbor:"1,keyasint,omitempty"`
	Value        Numeric  `json:"v,omitempty" xml:"v,attr,omitempty" cbor:"2,keyasint,omitempty"`
	StringValue  string   `json:"vs,omitempty" xml:"vs,attr,omitempty" cbor:"3,keyasint,omitempty"`
	BooleanValue *bool    `json:"vb,omitempty" xml:"vb,attr,omitempty" cbor:"4,keyasint,omitempty"`
	DataValue    []byte   `json:"vd,omitempty" xml:"vd,attr,omitempty" cbor:"8,keyasint,omitempty"`
	Sum          Numeric  `json:"s,omitempty" xml:"s,attr,omitempty" cbor:"5,keyasint,omitempty"`
	Time         Numeric  `json:"t,omitempty" xml:"t,attr,omitempty" cbor:"6,keyasint,omitempty"`
	UpdateTime   Numeric  `json:"ut,omitempty" xml:"ut,attr,omitempty" cbor:"7,keyasint,omitempty"`
}

Record represents a SenML record. This type is used as an intermediary between the Measurement values and the actual encoding. All SenML attributes are supported in this record.

func Encode

func Encode(list []Measurement) (records []Record)

Encode encodes a list of measurements to corresponding Measurement records.

type String

type String struct {
	Attributes
	Value string
}

String represents a string measurement value. It implements Measurement.

func NewString

func NewString(name string, value string, unit Unit, time time.Time, updateTime time.Duration) *String

NewString returns a new String value with the corresponding value and attributes.

func (*String) Equal

func (v *String) Equal(ml Measurement) bool

Equal returns true if the given Measurement value is equal.

func (*String) Record

func (v *String) Record() Record

Record returns a SenML record representing the value.

type Sum

type Sum struct {
	Attributes
	Value float64
}

Sum represents an integrated floating point measurement value. It implements Measurement.

func NewSum

func NewSum(name string, sum float64, unit Unit, time time.Time, updateTime time.Duration) *Sum

NewSum returns a new Sum value with the corresponding value and attributes.

func (*Sum) Equal

func (v *Sum) Equal(ml Measurement) bool

Equal returns true if the given Measurement value is equal.

func (*Sum) Record

func (v *Sum) Record() Record

Record returns a SenML record representing the value.

type Unit

type Unit string

Unit represents a SenML defined unit

const (
	// RFC8428
	None                    Unit = ""
	Meter                   Unit = "m"
	Kilogram                Unit = "kg"
	Gram                    Unit = "g" // not recommended
	Second                  Unit = "s"
	Ampere                  Unit = "A"
	Kelvin                  Unit = "K"
	Candela                 Unit = "cd"
	Mole                    Unit = "mol"
	Hertz                   Unit = "Hz"
	Radian                  Unit = "rad"
	Steradian               Unit = "sr"
	Newton                  Unit = "N"
	Pascal                  Unit = "Pa"
	Joule                   Unit = "J"
	Watt                    Unit = "W"
	Coulomb                 Unit = "C"
	Volt                    Unit = "V"
	Farad                   Unit = "F"
	Ohm                     Unit = "Ohm"
	Siemens                 Unit = "S"
	Weber                   Unit = "Wb"
	Tesla                   Unit = "T"
	Henry                   Unit = "H"
	Celsius                 Unit = "Cel"
	Lumen                   Unit = "lm"
	Lux                     Unit = "lx"
	Becquerel               Unit = "Bq"
	Gray                    Unit = "Gy"
	Sievert                 Unit = "Sv"
	Katal                   Unit = "kat"
	SquareMeter             Unit = "m2"
	CubicMeter              Unit = "m3"
	Liter                   Unit = "l" // not recommended
	MeterPerSecond          Unit = "m/s"
	MeterPerSquareSecond    Unit = "m/s2"
	CubicMeterPerSecond     Unit = "m3/s"
	LiterPerSecond          Unit = "l/s" // not recommended
	WattPerSquareMeter      Unit = "W/m2"
	CandelaPerSquareMeter   Unit = "cd/m2"
	Bit                     Unit = "bit"
	BitPerSecond            Unit = "bit/s"
	Latitude                Unit = "lat"
	Longitude               Unit = "lon"
	PH                      Unit = "pH"
	Decibel                 Unit = "dB"
	DBW                     Unit = "dBW"
	Bel                     Unit = "Bspl" // not recommended
	Count                   Unit = "count"
	Ratio                   Unit = "/"
	Ratio2                  Unit = "%" // not recommended
	RelativeHumidityPercent Unit = "%RH"
	RemainingBatteryPercent Unit = "%EL"
	RemainingBatterySeconds Unit = "EL"
	Rate                    Unit = "1/s"
	RPM                     Unit = "1/min"    // not recommended
	HeartRate               Unit = "beat/min" // not recommended
	HeartBeats              Unit = "beats"    // not recommended
	Conductivity            Unit = "S/m"

	// RFC8798
	Byte                     Unit = "B"
	VoltAmpere               Unit = "VA"
	VoltAmpereSecond         Unit = "VAs"
	VoltAmpereReactive       Unit = "var"
	VoltAmpereReactiveSecond Unit = "vars"
	JoulePerMeter            Unit = "J/m"
	KilogramPerCubicMeter    Unit = "kg/m3"
	Degree                   Unit = "deg" // not recommended

	// ISO 7027-1:2016
	NephelometricTurbidityUnit Unit = "NTU"

	// Secondary units (RFC8798)
	Millisecond            Unit = "ms"
	Minute                 Unit = "min"
	Hour                   Unit = "h"
	Megahertz              Unit = "MHz"
	Kilowatt               Unit = "kW"
	KilovoltAmpere         Unit = "kVA"
	Kilovar                Unit = "kvar"
	AmpereHour             Unit = "Ah"
	WattHour               Unit = "Wh"
	KilowattHour           Unit = "kWh"
	VarHour                Unit = "varh"
	KilovarHour            Unit = "kvarh"
	KilovoltAmpereHour     Unit = "kVAh"
	WattHourPerKilometer   Unit = "Wh/km"
	Kibibyte               Unit = "KiB"
	Gigabyte               Unit = "GB"
	MegabitPerSecond       Unit = "Mbit/s"
	BytePerSecond          Unit = "B/s"
	MegabytePerSecond      Unit = "MB/s"
	Millivolt              Unit = "mV"
	Milliampere            Unit = "mA"
	DecibelMilliwatt       Unit = "dBm"
	MicrogramPerCubicMeter Unit = "ug/m3"
	MillimeterPerHour      Unit = "mm/h"
	MeterPerHour           Unit = "m/h"
	PartsPerMillion        Unit = "ppm"
	Percent                Unit = "/100"
	Permille               Unit = "/1000"
	Hectopascal            Unit = "hPa"
	Millimeter             Unit = "mm"
	Centimeter             Unit = "cm"
	Kilometer              Unit = "km"
	KilometerPerHour       Unit = "km/h"

	// Secondary units (CoRE-1)
	PartsPerBillion  Unit = "ppb"
	PartsPerTrillion Unit = "ppt"
	VoltAmpereHour   Unit = "VAh"
	Milligram        Unit = "mg/l"
	Microgram        Unit = "ug/l"
	GramPerLiter     Unit = "g/l"
)

SenML unit definitions

func (Unit) String

func (u Unit) String() string

String returns the string value of a Unit

type Value

type Value struct {
	Attributes
	Value float64
}

Value represents a floating point measurement value. It implements Measurement.

func NewValue

func NewValue(name string, value float64, unit Unit, time time.Time, updateTime time.Duration) *Value

NewValue returns a new Value with the corresponding value and attributes.

func (*Value) Equal

func (v *Value) Equal(ml Measurement) bool

Equal returns true if the given Measurement value is equal.

func (*Value) Record

func (v *Value) Record() Record

Record returns a SenML record representing the value.

type XMLDocument

type XMLDocument struct {
	XMLName      xml.Name `xml:"sensml" name:"urn:ietf:params:xml:ns:senml"`
	XMLNamespace string   `xml:"xmlns,attr"`
	Records      []Record `xml:"senml"`
}

XMLDocument represents an XML senml document. It contains a list of senml measurement values in the form or records.

func (*XMLDocument) UnmarshalXML

func (d *XMLDocument) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements xml.Unmarshaler.

Jump to

Keyboard shortcuts

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