-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
345 lines (300 loc) · 9.56 KB
/
entity.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package core
import (
"fmt"
"strings"
)
// EntityType holds the name and type of a SensorThings entity.
type EntityType string
// List of all EntityTypes.
const (
EntityTypeVersion EntityType = "Version"
EntityTypeThing EntityType = "Thing"
EntityTypeLocation EntityType = "Location"
EntityTypeHistoricalLocation EntityType = "HistoricalLocation"
EntityTypeDatastream EntityType = "Datastream"
EntityTypeSensor EntityType = "Sensor"
EntityTypeObservedProperty EntityType = "ObservedProperty"
EntityTypeObservation EntityType = "Observation"
EntityTypeFeatureOfInterest EntityType = "FeatureOfInterest"
EntityTypeThingToLocation EntityType = "ThingToLocation"
EntityTypeLocationToHistoricalLocation EntityType = "LocationToHistoricalLocation"
EntityTypeCreateObservations EntityType = "CreateObservations"
EntityTypeUnknown EntityType = "Unknown"
)
// GetEndpoint returns the single entity endpoint name
func (e EntityType) GetEndpoint() string {
switch e {
case EntityTypeThing:
return "Thing"
case EntityTypeLocation:
return "Location"
case EntityTypeHistoricalLocation:
return "HistoricalLocation"
case EntityTypeDatastream:
return "Datastream"
case EntityTypeSensor:
return "Sensor"
case EntityTypeObservedProperty:
return "ObservedProperty"
case EntityTypeFeatureOfInterest:
return "FeatureOfInterest"
case EntityTypeCreateObservations:
return "CreateObservations"
}
return ""
}
// EntityTypeList is a list for all known entity types
var EntityTypeList = []EntityType{EntityTypeThing,
EntityTypeLocation, EntityTypeHistoricalLocation,
EntityTypeDatastream, EntityTypeSensor,
EntityTypeObservedProperty, EntityTypeObservation,
EntityTypeFeatureOfInterest, EntityTypeUnknown,
EntityTypeCreateObservations,
}
// StringEntityMap is a map of strings that map a string to an EntityType
var StringEntityMap = map[string]EntityType{
"thing": EntityTypeThing, "things": EntityTypeThing,
"location": EntityTypeLocation, "locations": EntityTypeLocation,
"historicallocation": EntityTypeHistoricalLocation, "historicallocations": EntityTypeHistoricalLocation,
"datastream": EntityTypeDatastream, "datastreams": EntityTypeDatastream,
"sensor": EntityTypeSensor, "sensors": EntityTypeSensor,
"observedproperty": EntityTypeObservedProperty, "observedproperties": EntityTypeObservedProperty,
"observation": EntityTypeObservation, "observations": EntityTypeObservation,
"featureofinterest": EntityTypeFeatureOfInterest, "featuresofinterest": EntityTypeFeatureOfInterest,
}
// GetArrayEndpoint returns the (array) endpoint name for the current EntityType
func (e EntityType) GetArrayEndpoint() string {
switch e {
case EntityTypeThing:
return "Things"
case EntityTypeLocation:
return "Locations"
case EntityTypeHistoricalLocation:
return "HistoricalLocations"
case EntityTypeDatastream:
return "Datastreams"
case EntityTypeSensor:
return "Sensors"
case EntityTypeObservedProperty:
return "ObservedProperties"
case EntityTypeFeatureOfInterest:
return "FeaturesOfInterest"
}
return ""
}
// ToString return the string representation of the EntityType.
func (e EntityType) ToString() string {
return fmt.Sprintf("%s", e)
}
// EntityFromType returns an empty entity belonging to the type
// returns nil if type cannot be mapped to an entity
func EntityFromType(e EntityType) Entity {
switch e {
case EntityTypeThing:
return &Thing{}
case EntityTypeLocation:
return &Location{}
case EntityTypeHistoricalLocation:
return &HistoricalLocation{}
case EntityTypeDatastream:
return &Datastream{}
case EntityTypeSensor:
return &Sensor{}
case EntityTypeObservedProperty:
return &ObservedProperty{}
case EntityTypeObservation:
return &Observation{}
case EntityTypeFeatureOfInterest:
return &FeatureOfInterest{}
case EntityTypeCreateObservations:
return &CreateObservations{}
}
return nil
}
// EntityFromString returns an empty entity based on a string, returns error
// if string cannot be mapped to an entity
func EntityFromString(e string) (Entity, error) {
et, err := EntityTypeFromString(e)
if err != nil {
return nil, err
}
return EntityFromType(et), nil
}
// EntityTypeFromString returns the EntityType for a given string
// function is case-insensitive
func EntityTypeFromString(e string) (EntityType, error) {
val, ok := StringEntityMap[strings.ToLower(e)]
if !ok {
return EntityTypeUnknown, fmt.Errorf("Unknown entity %s", e)
}
return val, nil
}
// EntityLink holds the name and type of a SensorThings entity link.
type EntityLink string
// List of all EntityLinks.
const (
EntityLinkThings EntityLink = "Things"
EntityLinkLocations EntityLink = "Locations"
EntityLinkHistoricalLocations EntityLink = "HistoricalLocations"
EntityLinkDatastreams EntityLink = "Datastreams"
EntityLinkSensors EntityLink = "Sensors"
EntityLinkObservedProperties EntityLink = "ObservedProperties"
EntityLinkObservations EntityLink = "Observations"
EntityLinkFeatureOfInterests EntityLink = "FeaturesOfInterest"
EntityLinkCreateObservations EntityLink = "CreateObservations"
)
// BaseEntity is the entry point for an entity
type BaseEntity struct {
ID interface{} `json:"@iot.id,omitempty"`
NavSelf string `json:"@iot.selfLink,omitempty"`
}
// ParseEntity defined to implement Entity
func (b *BaseEntity) ParseEntity(data []byte) error {
return nil
}
// ContainsMandatoryParams defined to implement Entity
func (b *BaseEntity) ContainsMandatoryParams() (bool, []error) {
return false, nil
}
// SetLinks defined to implement Entity
func (b *BaseEntity) SetLinks(externalURL string) error {
return nil
}
// GetID return the ID of the entity
func (b *BaseEntity) GetID() interface{} {
return b.ID
}
// GetEntityType defined to implement Entity
func (b *BaseEntity) GetEntityType() EntityType {
return EntityTypeUnknown
}
// GetPropertyNames returns the available properties of an entity
func (b *BaseEntity) GetPropertyNames() []string {
return nil
}
// GetSelfLink returns the self link of the entity
func (b *BaseEntity) GetSelfLink() string {
return b.NavSelf
}
// GetSupportedEncoding defined to implement Entity
func (b *BaseEntity) GetSupportedEncoding() map[int]EncodingType {
return nil
}
// SetID sets a newID on the entity
func (b *BaseEntity) SetID(newID interface{}) {
b.ID = newID
}
func (b BaseEntity) ClearNav() {
b.NavSelf = ""
}
// ToString return the string representation of the EntityLink.
func (e EntityLink) ToString() string {
return fmt.Sprintf("%s", e)
}
// Entity is the base interface for all SensorThings entities.
type Entity interface {
ClearNav()
ParseEntity(data []byte) error
ContainsMandatoryParams() (bool, []error)
GetID() interface{}
SetID(newID interface{})
SetAllLinks(externalURL string)
SetSelfLink(externalURL string)
SetLinks(externalURL string)
GetSelfLink() string
GetEntityType() EntityType
GetPropertyNames() []string
GetSupportedEncoding() map[int]EncodingType
}
// CheckMandatoryParam checks if the given parameter is nil, if true then an ApiError will be added to the
// given list of errors.
func CheckMandatoryParam(errorList *[]error, param interface{}, entityType EntityType, paramName string) {
isNil := false
if param != nil {
switch t := param.(type) {
case string:
if len(t) == 0 {
isNil = true
}
case *string:
if t != nil {
t1 := *t
if len(t1) == 0 {
isNil = true
}
break
} else {
isNil = true
}
case map[string]string:
if len(t) == 0 {
isNil = true
}
case *Thing:
var contains bool
if t != nil {
contains, _ = t.ContainsMandatoryParams()
}
if t == nil || (t.ID == nil && !contains) {
isNil = true
}
case *Sensor:
var contains bool
if t != nil {
contains, _ = t.ContainsMandatoryParams()
}
if t == nil || (t.ID == nil && !contains) {
isNil = true
}
case *ObservedProperty:
var contains bool
if t != nil {
contains, _ = t.ContainsMandatoryParams()
}
if t == nil || (t.ID == nil && !contains) {
isNil = true
}
case *Datastream:
var contains bool
if t != nil {
contains, _ = t.ContainsMandatoryParams()
}
if t == nil || (t.ID == nil && !contains) {
isNil = true
}
}
} else {
isNil = true
}
err := *errorList
if isNil {
*errorList = append(err, fmt.Errorf("Missing mandatory parameter: %s.%s", entityType, paramName))
}
}
// CheckEncodingSupported returns true of the Location entity supports the given encoding type
func CheckEncodingSupported(encodingType string) (bool, error) {
_, err := CreateEncodingType(encodingType)
if err != nil {
return false, err
}
return true, nil
}
// CreateEntitySelfLink formats the given parameters into an external navigationlink to the entity
// for example: http://example.org/OGCSensorThings/v1.0/Things(27815)
func CreateEntitySelfLink(externalURI string, entityLink string, id interface{}) string {
if id != nil {
entityLink = fmt.Sprintf("%s(%v)", entityLink, id)
}
return fmt.Sprintf("%s/v1.0/%s", externalURI, entityLink)
}
// CreateEntityLink formats the given parameters into a relative navigationlink path
// for example: http://example.org/OGCSensorThings/v1.0/Things(27815)/Datastreams
func CreateEntityLink(isNil bool, externalURI string, entityType1 string, entityType2 string, id interface{}) string {
if !isNil {
return ""
}
if id != nil {
entityType1 = fmt.Sprintf("%s(%v)", entityType1, id)
}
return fmt.Sprintf("%s/v1.0/%s/%s", externalURI, entityType1, entityType2)
}