-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
414 lines (319 loc) · 9.02 KB
/
utils.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package gossamer
import (
"encoding/json"
"errors"
"github.com/satori/go.uuid"
"log"
"net/http"
"strings"
)
func ResolveEntityLink(id string, ent EntityType) string {
s := string(ent)
if id != "" {
s += "(" + id + ")"
}
return s
}
func ResolveSelfLinkUrl(id string, ent EntityType) string {
return "http://" + GLOB_ENV_HOST + "/v1.0/" + ResolveEntityLink(id, ent)
}
func GetAssociatedEntityId(entity SensorThing, et EntityType) string {
switch entity.GetType() {
case ENTITY_THINGS:
// e := (entity).(*ThingEntity)
case ENTITY_OBSERVATIONS:
// e := (entity).(*ObservationEntity)
case ENTITY_HISTORICALLOCATIONS:
// e := (entity).(*HistoricalLocationEntity)
case ENTITY_SENSORS:
// e := (entity).(*SensorEntity)
case ENTITY_LOCATIONS:
// e := (entity).(*LocationEntity)
case ENTITY_FEATURESOFINTEREST:
// e := (entity).(*FeatureOfInterestEntity)
case ENTITY_DATASTREAMS:
e := (entity).(*DatastreamEntity)
switch {
case et == ENTITY_SENSOR || et == ENTITY_SENSORS:
return e.IdSensor
case et == ENTITY_THING || et == ENTITY_THINGS:
return e.IdThing
case et == ENTITY_OBSERVEDPROPERTIES || et == ENTITY_OBSERVEDPROPERTY:
return e.IdObservedProperty
}
case ENTITY_OBSERVEDPROPERTIES:
// e := (entity).(*ObservedPropertyEntity)
}
return ""
}
func SetAssociatedEntityId(entity SensorThing, et EntityType, id string) {
switch entity.GetType() {
case ENTITY_THINGS:
e := (entity).(*ThingEntity)
if et == ENTITY_LOCATIONS {
entity := NewLocationEntity()
entity.Id = id
e.Locations = []*LocationEntity{entity}
}
if et == ENTITY_DATASTREAMS {
entity := NewDatastreamEntity()
entity.Id = id
e.Datastreams = []*DatastreamEntity{entity}
}
case ENTITY_OBSERVATIONS:
e := (entity).(*ObservationEntity)
if et == ENTITY_DATASTREAMS {
e.Datastream = NewDatastreamEntity()
e.Datastream.Id = id
}
if et == ENTITY_FEATURESOFINTEREST {
e.FeatureOfInterest = NewFeatureOfInterestEntity()
e.FeatureOfInterest.Id = id
}
case ENTITY_HISTORICALLOCATIONS:
e := (entity).(*HistoricalLocationEntity)
log.Println(e)
case ENTITY_SENSORS:
e := (entity).(*SensorEntity)
log.Println(e)
case ENTITY_LOCATIONS:
e := (entity).(*LocationEntity)
log.Println(e)
case ENTITY_FEATURESOFINTEREST:
e := (entity).(*FeatureOfInterestEntity)
log.Println(e)
case ENTITY_DATASTREAMS:
e := (entity).(*DatastreamEntity)
if et == ENTITY_THINGS {
e.Thing = NewThingEntity()
e.Thing.Id = id
}
if et == ENTITY_OBSERVEDPROPERTIES {
e.ObservedProperty = NewObservedPropertyEntity()
e.ObservedProperty.Id = id
}
if et == ENTITY_SENSORS {
e.Sensor = NewSensorEntity()
e.Sensor.Id = id
}
case ENTITY_OBSERVEDPROPERTIES:
e := (entity).(*ObservedPropertyEntity)
log.Println(e)
}
}
func DecodeJsonToEntityStruct(decoder *json.Decoder, et EntityType) (SensorThing, error) {
var err error
switch et {
case ENTITY_THINGS:
var e ThingEntity
err = decoder.Decode(&e)
return &e, err
case ENTITY_OBSERVATIONS:
var e ObservationEntity
err = decoder.Decode(&e)
return &e, err
case ENTITY_HISTORICALLOCATIONS:
return nil, errors.New("Adding Historical Locations not allowed")
case ENTITY_SENSORS:
var e SensorEntity
err = decoder.Decode(&e)
return &e, err
case ENTITY_LOCATIONS:
var e LocationEntity
err = decoder.Decode(&e)
return &e, err
case ENTITY_FEATURESOFINTEREST:
var e FeatureOfInterestEntity
err = decoder.Decode(&e)
return &e, err
case ENTITY_DATASTREAMS:
var e DatastreamEntity
err = decoder.Decode(&e)
return &e, err
case ENTITY_OBSERVEDPROPERTIES:
var e ObservedPropertyEntity
err = decoder.Decode(&e)
return &e, err
}
return nil, errors.New("Unknown Entity Type")
}
func ThrowHttpOk(msg string, w http.ResponseWriter) {
http.Error(w, msg, http.StatusOK)
}
func ThrowHttpCreated(msg string, w http.ResponseWriter) {
http.Error(w, msg, http.StatusCreated)
}
func ThrowHttpBadRequest(msg string, w http.ResponseWriter) {
http.Error(w, msg, http.StatusBadRequest)
}
func ThrowHttpInternalServerError(msg string, w http.ResponseWriter) {
http.Error(w, msg, http.StatusInternalServerError)
}
func ThrowHttpMethodNotAllowed(msg string, w http.ResponseWriter) {
http.Error(w, msg, http.StatusMethodNotAllowed)
}
func ThrowNotAcceptable(msg string, w http.ResponseWriter) {
http.Error(w, msg, http.StatusNotAcceptable)
}
func GenerateEntityId() string {
return uuid.NewV4().String()
}
func IsEntity(e string) bool {
if strings.HasPrefix(e, "Thing") ||
strings.HasPrefix(e, "Things") ||
strings.HasPrefix(e, "Location") ||
strings.HasPrefix(e, "Locations") ||
strings.HasPrefix(e, "HistoricalLocation") ||
strings.HasPrefix(e, "HistoricalLocations") ||
strings.HasPrefix(e, "Datastream") ||
strings.HasPrefix(e, "Datastreams") ||
strings.HasPrefix(e, "Sensor") ||
strings.HasPrefix(e, "Sensors") ||
strings.HasPrefix(e, "Observation") ||
strings.HasPrefix(e, "Observations") ||
strings.HasPrefix(e, "ObservedProperty") ||
strings.HasPrefix(e, "ObservedProperties") ||
strings.HasPrefix(e, "FeaturesOfInterest") {
return true
}
return false
}
func IsSingularEntity(e string) bool {
if (strings.HasPrefix(e, "Location") && !strings.HasPrefix(e, "Locations")) ||
(strings.HasPrefix(e, "Thing") && !strings.HasPrefix(e, "Things")) ||
(strings.HasPrefix(e, "HistoricalLocation") && !strings.HasPrefix(e, "HistoricalLocations")) ||
(strings.HasPrefix(e, "Datastream") && !strings.HasPrefix(e, "Datastreams")) ||
(strings.HasPrefix(e, "Sensor") && !strings.HasPrefix(e, "Sensors")) ||
(strings.HasPrefix(e, "Observation") && !strings.HasPrefix(e, "Observations")) ||
(strings.HasPrefix(e, "ObservedProperty") && !strings.HasPrefix(e, "ObservedProperties")) {
return true
}
return false
}
func DiscoverEntityType(e string) EntityType {
switch {
case strings.HasPrefix(e, "Things"):
return ENTITY_THINGS
case strings.HasPrefix(e, "Thing"):
return ENTITY_THING
case strings.HasPrefix(e, "Locations"):
return ENTITY_LOCATIONS
case strings.HasPrefix(e, "Location"):
return ENTITY_LOCATION
case strings.HasPrefix(e, "Datastreams"):
return ENTITY_DATASTREAMS
case strings.HasPrefix(e, "Datastream"):
return ENTITY_DATASTREAM
case strings.HasPrefix(e, "Sensors"):
return ENTITY_SENSORS
case strings.HasPrefix(e, "Sensor"):
return ENTITY_SENSOR
case strings.HasPrefix(e, "Observations"):
return ENTITY_OBSERVATIONS
case strings.HasPrefix(e, "Observation"):
return ENTITY_OBSERVATION
case strings.HasPrefix(e, "ObservedProperties"):
return ENTITY_OBSERVEDPROPERTIES
case strings.HasPrefix(e, "ObservedProperty"):
return ENTITY_OBSERVEDPROPERTY
case strings.HasPrefix(e, "FeaturesOfInterest"):
return ENTITY_FEATURESOFINTEREST
case strings.HasPrefix(e, "HistoricalLocations"):
return ENTITY_HISTORICALLOCATIONS
case strings.HasPrefix(e, "HistoricalLocation"):
return ENTITY_HISTORICALLOCATION
default:
return ENTITY_UNKNOWN
}
}
func NewThingEntity() *ThingEntity {
t := &ThingEntity{}
return t
}
func NewThingEntityWithId(id string) *ThingEntity {
t := &ThingEntity{}
t.Id = id
return t
}
func NewLocationEntity() *LocationEntity {
e := &LocationEntity{}
return e
}
func NewLocationEntityWithId(id string) *LocationEntity {
e := &LocationEntity{}
e.Id = id
return e
}
func NewHistoricalLocationEntity() *HistoricalLocationEntity {
e := &HistoricalLocationEntity{}
return e
}
func NewHistoricalLocationEntityWithId(id string) *HistoricalLocationEntity {
e := &HistoricalLocationEntity{}
e.Id = id
return e
}
func NewDatastreamEntity() *DatastreamEntity {
e := &DatastreamEntity{}
return e
}
func NewDatastreamEntityWithId(id string) *DatastreamEntity {
e := &DatastreamEntity{}
e.Id = id
return e
}
func NewSensorEntity() *SensorEntity {
e := &SensorEntity{}
return e
}
func NewSensorEntityWithId(id string) *SensorEntity {
e := &SensorEntity{}
e.Id = id
return e
}
func NewObservedPropertyEntity() *ObservedPropertyEntity {
e := &ObservedPropertyEntity{}
return e
}
func NewObservedPropertyEntityWithId(id string) *ObservedPropertyEntity {
e := &ObservedPropertyEntity{}
e.Id = id
return e
}
func NewObservationEntity() *ObservationEntity {
e := &ObservationEntity{}
return e
}
func NewObservationEntityWithId(id string) *ObservationEntity {
e := &ObservationEntity{}
e.Id = id
return e
}
func CloneObservationEntity(o Observation) *ObservationEntity {
n := NewObservationEntity()
n.Id = o.GetId()
n.Parameters = o.GetParameters()
n.PhenomenonTime = o.GetPhenomenonTime()
n.Result = o.GetResult()
n.ResultQuality = o.GetResultQuality()
n.ResultTime = o.GetResultTime()
n.ValidTime = o.GetValidTime()
return n
}
func CloneSensorEntity(o Sensor) *SensorEntity {
n := NewSensorEntity()
n.Id = o.GetId()
n.Description = o.GetDescription()
n.EncodingType = o.GetEncodingType()
n.Metadata = o.GetMetadata()
return n
}
func NewFeatureOfInterestEntity() *FeatureOfInterestEntity {
e := &FeatureOfInterestEntity{}
return e
}
func NewFeatureOfInterestEntityWithId(id string) *FeatureOfInterestEntity {
e := &FeatureOfInterestEntity{}
e.Id = id
return e
}