-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththing.go
99 lines (82 loc) · 3.55 KB
/
thing.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
package core
import (
"encoding/json"
"errors"
)
// Thing in SensorThings represents a physical object in the real world. A Thing is a good starting
// point in which to start creating the SensorThings model structure. A Thing has a Location and one or
// more Datastreams to collect Observations. A minimal Thing can be created without a Location and Datastream
// and there are options to create a Things with a nested linked Location and Datastream.
type Thing struct {
BaseEntity
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Properties map[string]interface{} `json:"properties,omitempty"`
NavLocations string `json:"[email protected],omitempty"`
NavDatastreams string `json:"[email protected],omitempty"`
NavHistoricalLocations string `json:"[email protected],omitempty"`
Locations []*Location `json:"Locations,omitempty"`
Datastreams []*Datastream `json:"Datastreams,omitempty"`
HistoricalLocations []*HistoricalLocation `json:"HistoricalLocations,omitempty"`
}
func (t *Thing) ClearNav() {
t.NavSelf = ""
t.NavLocations = ""
t.NavDatastreams = ""
t.NavHistoricalLocations = ""
}
// GetEntityType returns the EntityType for Thing
func (t Thing) GetEntityType() EntityType {
return EntityTypeThing
}
// GetPropertyNames returns the available properties for a Thing
func (t *Thing) GetPropertyNames() []string {
return []string{"id", "name", "description", "properties"}
}
// ParseEntity tries to parse the given json byte array into the current entity
func (t *Thing) ParseEntity(data []byte) error {
thing := &t
err := json.Unmarshal(data, thing)
if err != nil {
return errors.New("Unable to parse Thing")
}
return nil
}
// ContainsMandatoryParams checks if all mandatory params for Thing are available before posting.
func (t *Thing) ContainsMandatoryParams() (bool, []error) {
err := []error{}
CheckMandatoryParam(&err, t.Name, t.GetEntityType(), "name")
CheckMandatoryParam(&err, t.Description, t.GetEntityType(), "description")
if len(err) != 0 {
return false, err
}
return true, nil
}
// SetAllLinks sets the self link and relational links
func (t *Thing) SetAllLinks(externalURL string) {
t.SetSelfLink(externalURL)
t.SetLinks(externalURL)
for _, l := range t.Locations {
l.SetAllLinks(externalURL)
}
for _, d := range t.Datastreams {
d.SetAllLinks(externalURL)
}
for _, hl := range t.HistoricalLocations {
hl.SetAllLinks(externalURL)
}
}
// SetSelfLink sets the self link for the entity
func (t *Thing) SetSelfLink(externalURL string) {
t.NavSelf = CreateEntitySelfLink(externalURL, EntityLinkThings.ToString(), t.ID)
}
// SetLinks sets the entity specific navigation links, empty string if linked(expanded) data is not nil
func (t *Thing) SetLinks(externalURL string) {
t.NavLocations = CreateEntityLink(t.Locations == nil, externalURL, EntityLinkThings.ToString(), EntityLinkLocations.ToString(), t.ID)
t.NavDatastreams = CreateEntityLink(t.Datastreams == nil, externalURL, EntityLinkThings.ToString(), EntityLinkDatastreams.ToString(), t.ID)
t.NavHistoricalLocations = CreateEntityLink(t.HistoricalLocations == nil, externalURL, EntityLinkThings.ToString(), EntityLinkHistoricalLocations.ToString(), t.ID)
}
// GetSupportedEncoding returns the supported encoding tye for this entity
func (t Thing) GetSupportedEncoding() map[int]EncodingType {
return map[int]EncodingType{}
}