Skip to content

Commit

Permalink
Add units (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbus authored Jul 2, 2018
1 parent 7469e05 commit 22118aa
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions senml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions senml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}]`,
},
Expand All @@ -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: `<sensml xmlns="urn:ietf:params:xml:ns:senml"><senml n="urn:dev:ow:10e2073a01080063" u="Cel" v="23.1"></senml></sensml>`,
},
{
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)},
Expand Down
91 changes: 91 additions & 0 deletions units.go
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 22118aa

Please sign in to comment.