-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistoricallocation.go
101 lines (83 loc) · 3.11 KB
/
historicallocation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package core
import (
"encoding/json"
"errors"
"time"
)
// HistoricalLocation in sensorthings represents the current and previous locations of a thing including time
type HistoricalLocation struct {
BaseEntity
Time string `json:"time,omitempty"`
NavThing string `json:"[email protected],omitempty"`
NavLocations string `json:"[email protected],omitempty"`
Thing *Thing `json:"Thing,omitempty"`
Locations []*Location `json:"Locations,omitempty"`
}
func (h *HistoricalLocation) ClearNav() {
h.NavSelf = ""
h.NavThing = ""
h.NavLocations = ""
}
// GetEntityType returns the EntityType for HistoricalLocation
func (h HistoricalLocation) GetEntityType() EntityType {
return EntityTypeHistoricalLocation
}
// GetPropertyNames returns the available properties for a HistoricalLocation
func (h *HistoricalLocation) GetPropertyNames() []string {
return []string{"id", "time"}
}
// ParseEntity tries to parse the given json byte array into the current entity
func (h *HistoricalLocation) ParseEntity(data []byte) error {
hl := &h
err := json.Unmarshal(data, hl)
if err != nil {
err = errors.New("Unable to parse HistoricalLocation")
}
return err
}
// ContainsMandatoryParams checks if all mandatory params for a HistoricalLocation are available before posting
func (h *HistoricalLocation) ContainsMandatoryParams() (bool, []error) {
err := []error{}
if len(h.Time) == 0 {
h.Time = time.Now().UTC().Format(time.RFC3339Nano)
}
CheckMandatoryParam(&err, h.Time, h.GetEntityType(), "time")
CheckMandatoryParam(&err, h.Thing, h.GetEntityType(), "Thing")
if h.Thing != nil {
CheckMandatoryParam(&err, h.Thing.ID, h.GetEntityType(), "Thing.ID")
}
CheckMandatoryParam(&err, h.Locations, h.GetEntityType(), "Location")
if len(h.Locations) == 0 {
err = append(err, errors.New("Missing location"))
} else {
CheckMandatoryParam(&err, h.Locations[0].ID, h.GetEntityType(), "Location.ID")
}
if len(err) != 0 {
return false, err
}
return true, nil
}
// SetAllLinks sets the self link and relational links
func (h *HistoricalLocation) SetAllLinks(externalURL string) {
h.SetSelfLink(externalURL)
h.SetLinks(externalURL)
if h.Thing != nil {
h.Thing.SetAllLinks(externalURL)
}
for _, l := range h.Locations {
l.SetAllLinks(externalURL)
}
}
// SetSelfLink sets the self link for the entity
func (h *HistoricalLocation) SetSelfLink(externalURL string) {
h.NavSelf = CreateEntitySelfLink(externalURL, EntityLinkHistoricalLocations.ToString(), h.ID)
}
// SetLinks sets the entity specific navigation links, empty string if linked(expanded) data is not nil
func (h *HistoricalLocation) SetLinks(externalURL string) {
h.NavThing = CreateEntityLink(h.Thing == nil, externalURL, EntityLinkHistoricalLocations.ToString(), EntityTypeThing.ToString(), h.ID)
h.NavLocations = CreateEntityLink(h.Locations == nil, externalURL, EntityLinkHistoricalLocations.ToString(), EntityLinkLocations.ToString(), h.ID)
}
// GetSupportedEncoding returns the supported encoding tye for this entity
func (h HistoricalLocation) GetSupportedEncoding() map[int]EncodingType {
return map[int]EncodingType{}
}