From 22118aa4f600e1b477ecb102260dadd207464ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Bustarret?= Date: Mon, 2 Jul 2018 18:33:07 +0200 Subject: [PATCH] Add units (#1) --- doc.go | 2 +- senml.go | 7 ++-- senml_test.go | 6 ++-- units.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 units.go diff --git a/doc.go b/doc.go index 6b36838..98c044d 100644 --- a/doc.go +++ b/doc.go @@ -1,2 +1,2 @@ -// The senml package implements the Sensor Markup Language, as defined by https://tools.ietf.org/html/draft-ietf-core-senml-16. +// Package senml implements the Sensor Markup Language, as defined by https://tools.ietf.org/html/draft-ietf-core-senml-16. package senml diff --git a/senml.go b/senml.go index 52801d4..fb8c233 100644 --- a/senml.go +++ b/senml.go @@ -28,14 +28,14 @@ func (p Pack) Equals(p2 Pack) bool { type Record struct { BaseName string `json:"bn,omitempty" xml:"bn,attr,omitempty"` BaseTime float64 `json:"bt,omitempty" xml:"bt,attr,omitempty"` - BaseUnit string `json:"bu,omitempty" xml:"bu,attr,omitempty"` + BaseUnit Unit `json:"bu,omitempty" xml:"bu,attr,omitempty"` BaseValue *float64 `json:"bv,omitempty" xml:"bv,attr,omitempty"` BaseSum *float64 `json:"bs,omitempty" xml:"bs,attr,omitempty"` Version int `json:"bver,omitempty" xml:"bver,attr,omitempty"` Name string `json:"n,omitempty" xml:"n,attr,omitempty"` - Unit string `json:"u,omitempty" xml:"u,attr,omitempty"` + Unit Unit `json:"u,omitempty" xml:"u,attr,omitempty"` Time float64 `json:"t,omitempty" xml:"t,attr,omitempty"` UpdateTime float64 `json:"ut,omitempty" xml:"ut,attr,omitempty"` @@ -103,7 +103,8 @@ func (r *Record) Equals(r2 *Record) bool { // Normalize resolves the SenML Records, as explained in https://tools.ietf.org/html/draft-ietf-core-senml-16#section-4.6. // All base items are removed, and records are sorted in chronological order. func (p Pack) Normalize() Pack { - var bname, bunit string + var bname string + var bunit Unit var btime, bval, bsum float64 var bver int diff --git a/senml_test.go b/senml_test.go index f4cfeba..4e6ee83 100644 --- a/senml_test.go +++ b/senml_test.go @@ -84,7 +84,7 @@ func TestJSON(t *testing.T) { }{ { src: Pack{ - {Name: "urn:dev:ow:10e2073a01080063", Unit: "Cel", Value: fptr(23.1)}, + {Name: "urn:dev:ow:10e2073a01080063", Unit: Celsius, Value: fptr(23.1)}, }, json: `[{"n":"urn:dev:ow:10e2073a01080063","u":"Cel","v":23.1}]`, }, @@ -106,13 +106,13 @@ func TestXML(t *testing.T) { }{ { src: Pack{ - {Name: "urn:dev:ow:10e2073a01080063", Unit: "Cel", Value: fptr(23.1)}, + {Name: "urn:dev:ow:10e2073a01080063", Unit: Celsius, Value: fptr(23.1)}, }, xml: ``, }, { src: Pack{ - {BaseName: "urn:dev:ow:10e2073a01080063", BaseTime: 1.276020076001e+09, BaseUnit: "A", Version: 5, Name: "voltage", Unit: "V", Value: fptr(120.1)}, + {BaseName: "urn:dev:ow:10e2073a01080063", BaseTime: 1.276020076001e+09, BaseUnit: Ampere, Version: 5, Name: "voltage", Unit: Volt, Value: fptr(120.1)}, {Name: "current", Time: -5, Value: fptr(1.2)}, {Name: "current", Time: -4, Value: fptr(1.3)}, {Name: "current", Time: -3, Value: fptr(1.4)}, diff --git a/units.go b/units.go new file mode 100644 index 0000000..d0d759f --- /dev/null +++ b/units.go @@ -0,0 +1,91 @@ +package senml + +// Unit is the unit for a measurement value. +type Unit string + +const ( + Meter Unit = "m" + Kilogram Unit = "kg" + 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" + // degrees Celsius + Celsius Unit = "Cel" + Lumen Unit = "lm" + Lux Unit = "lux" + Becquerel Unit = "Bq" + Gray Unit = "Gy" + Sievert Unit = "Sv" + Katal Unit = "kat" + // square meter (area) + SquareMeter Unit = "m2" + // cubic meter (volume) + CubicMeter Unit = "m3" + // liter (volume) + Liter Unit = "l" + // meter per second (velocity) + MeterPerSecond Unit = "m/s" + // meter per square second (acceleration) + MeterPerSquareSecond Unit = "m/s2" + // cubic meter per second (flow rate) + CubicMeterPerSecond Unit = "m3/s" + // liter per second (flow rate) + LiterPerSecond Unit = "l/s" + // watt per square meter (irradiance) + WattPerSquareMeter Unit = "W/m2" + // candela per square meter (luminance) + CandelaPerSquareMeter Unit = "cd/m2" + // bit (information content) + Bit Unit = "bit" + // bit per second (data rate) + BitPerSecond Unit = "bit/s" + // degrees latitude + DegreesLatitude Unit = "lat" + // degrees longitude + DegreesLongitude Unit = "lon" + // pH value (acidity; logarithmic quantity) + PH Unit = "pH" + // decibel (logarithmic quantity) + Decibel Unit = "dB" + // decibel relative to 1 W (power level) + Decibel1W Unit = "dBW" + // bel (sound pressure level; logarithmic quantity) + Bel Unit = "Bspl" + // 1 (counter value) + Count Unit = "count" + // 1 (Ratio e.g., value of a switch) + Switch Unit = "/" + // percentage + Percentage Unit = "%" + // percentage (Relative Humidity) + RelativeHumidity Unit = "%RH" + // percentage (remaining battery energy level) + EnergyLevel Unit = "%EL" + // seconds (remaining battery energy level) + EnergyRemaining Unit = "EL" + // 1 per second (event rate) + EventRate Unit = "1/s" + // 1 per minute (Heart rate in beats per minute) + BeatsPerMinute Unit = "beat/min" + // 1 (Cumulative number of heart beats) + Beats Unit = "beats" + // siemens per meter (conductivity) + SiemensPerMeter Unit = "S/m" +)