@@ -3,6 +3,7 @@ package senml
33import (
44 "encoding/xml"
55 "sort"
6+ "time"
67)
78
89// Pack defines a SenML pack (a list of Records).
@@ -24,82 +25,6 @@ func (p Pack) Equals(p2 Pack) bool {
2425 return true
2526}
2627
27- // Record is a SenML Record.
28- type Record struct {
29- BaseName string `json:"bn,omitempty" xml:"bn,attr,omitempty"`
30- BaseTime float64 `json:"bt,omitempty" xml:"bt,attr,omitempty"`
31- BaseUnit Unit `json:"bu,omitempty" xml:"bu,attr,omitempty"`
32- BaseValue * float64 `json:"bv,omitempty" xml:"bv,attr,omitempty"`
33- BaseSum * float64 `json:"bs,omitempty" xml:"bs,attr,omitempty"`
34-
35- Version int `json:"bver,omitempty" xml:"bver,attr,omitempty"`
36-
37- Name string `json:"n,omitempty" xml:"n,attr,omitempty"`
38- Unit Unit `json:"u,omitempty" xml:"u,attr,omitempty"`
39-
40- Time float64 `json:"t,omitempty" xml:"t,attr,omitempty"`
41- UpdateTime float64 `json:"ut,omitempty" xml:"ut,attr,omitempty"`
42-
43- Value * float64 `json:"v,omitempty" xml:"v,attr,omitempty"`
44- StringValue string `json:"vs,omitempty" xml:"vs,attr,omitempty"`
45- DataValue []byte `json:"vd,omitempty" xml:"vd,attr,omitempty"`
46- BoolValue * bool `json:"vb,omitempty" xml:"vb,attr,omitempty"`
47- Sum * float64 `json:"s,omitempty" xml:"s,attr,omitempty"`
48- }
49-
50- // Equals checks if two records are equal
51- func (r * Record ) Equals (r2 * Record ) bool {
52- if (r == nil && r2 != nil ) || (r != nil && r2 == nil ) {
53- return false
54- }
55- if r .BaseName != r2 .BaseName {
56- return false
57- }
58- if r .BaseTime != r2 .BaseTime {
59- return false
60- }
61- if r .BaseUnit != r2 .BaseUnit {
62- return false
63- }
64- if r .Version != r2 .Version {
65- return false
66- }
67- if r .Name != r2 .Name {
68- return false
69- }
70- if r .Unit != r2 .Unit {
71- return false
72- }
73- if r .Time != r2 .Time {
74- return false
75- }
76- if r .UpdateTime != r2 .UpdateTime {
77- return false
78- }
79-
80- if r .Value != nil && r2 .Value != nil && * r .Value == * r2 .Value {
81- return true
82- }
83- if r .StringValue != "" && r .StringValue == r2 .StringValue {
84- return true
85- }
86- if r .BoolValue != nil && r2 .BoolValue != nil && * r .BoolValue == * r2 .BoolValue {
87- return true
88- }
89- if r .Sum != nil && r2 .Sum != nil && * r .Sum == * r2 .Sum {
90- return true
91- }
92- if r .DataValue != nil && r2 .DataValue != nil && len (r .DataValue ) == len (r2 .DataValue ) {
93- for i := range r .DataValue {
94- if r .DataValue [i ] != r2 .DataValue [i ] {
95- return false
96- }
97- }
98- return true
99- }
100- return false
101- }
102-
10328// Normalize resolves the SenML Records, as explained in https://tools.ietf.org/html/draft-ietf-core-senml-16#section-4.6.
10429// All base items are removed, and records are sorted in chronological order.
10530func (p Pack ) Normalize () Pack {
@@ -159,6 +84,17 @@ func (p Pack) Normalize() Pack {
15984 return n
16085}
16186
87+ // NormalizeAt resolves the SenML Records, and replaces all relative times
88+ // by absolute times, based on the t reference time.
89+ func (p Pack ) NormalizeAt (t time.Time ) Pack {
90+ n := p .Normalize ()
91+ rt := Time (t )
92+ for i := range n {
93+ n [i ].Time = absoluteTime (n [i ].Time , rt )
94+ }
95+ return n
96+ }
97+
16298// Len implements sort.Interface.
16399func (p Pack ) Len () int {
164100 return len (p )
0 commit comments