-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdatastream.go
executable file
·183 lines (150 loc) · 6.17 KB
/
datastream.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
package api
import (
"errors"
entities "github.com/gost/core"
gostErrors "github.com/gost/server/errors"
"github.com/gost/server/sensorthings/odata"
)
// GetDatastream retrieves a sensor by id and given query
func (a *APIv1) GetDatastream(id interface{}, qo *odata.QueryOptions, path string) (*entities.Datastream, error) {
ds, err := a.db.GetDatastream(id, qo)
if err != nil {
return nil, err
}
a.SetLinks(ds, qo)
return ds, nil
}
// GetDatastreams retrieves an array of sensors based on the given query
func (a *APIv1) GetDatastreams(qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error) {
datastreams, count, hasNext, err := a.db.GetDatastreams(qo)
return processDatastreams(a, datastreams, qo, path, count, hasNext, err)
}
// GetDatastreamsByThing returns all datastreams linked to the given thing
func (a *APIv1) GetDatastreamsByThing(thingID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error) {
datastreams, count, hasNext, err := a.db.GetDatastreamsByThing(thingID, qo)
return processDatastreams(a, datastreams, qo, path, count, hasNext, err)
}
// GetDatastreamByObservation returns a datastream linked to the given observation
func (a *APIv1) GetDatastreamByObservation(observationID interface{}, qo *odata.QueryOptions, path string) (*entities.Datastream, error) {
ds, err := a.db.GetDatastreamByObservation(observationID, qo)
if err != nil {
return nil, err
}
a.SetLinks(ds, qo)
return ds, nil
}
// GetDatastreamsBySensor returns all datastreams linked to the given sensor
func (a *APIv1) GetDatastreamsBySensor(sensorID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error) {
datastreams, count, hasNext, err := a.db.GetDatastreamsBySensor(sensorID, qo)
return processDatastreams(a, datastreams, qo, path, count, hasNext, err)
}
// GetDatastreamsByObservedProperty returns all datastreams linked to the given ObservedProperty
func (a *APIv1) GetDatastreamsByObservedProperty(oID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error) {
datastreams, count, hasNext, err := a.db.GetDatastreamsByObservedProperty(oID, qo)
return processDatastreams(a, datastreams, qo, path, count, hasNext, err)
}
func processDatastreams(a *APIv1, datastreams []*entities.Datastream, qo *odata.QueryOptions, path string, count int, hasNext bool, err error) (*entities.ArrayResponse, error) {
if err != nil {
return nil, err
}
for idx, item := range datastreams {
i := *item
a.SetLinks(&i, qo)
datastreams[idx] = &i
}
var data interface{} = datastreams
return a.createArrayResponse(count, hasNext, path, qo, data), nil
}
// PostDatastream adds a new datastream to the database
func (a *APIv1) PostDatastream(datastream *entities.Datastream) (*entities.Datastream, []error) {
var errors []error
var err error
var postedObservedProperty *entities.ObservedProperty
var postedSensor *entities.Sensor
var postedObservations []*entities.Observation
_, errors = containsMandatoryParams(datastream)
if len(errors) > 0 {
a.revertPostDatastream(postedObservedProperty, postedSensor, postedObservations)
return nil, errors
}
// Check if ObservedProperty is deep inserted
if datastream.ObservedProperty != nil && datastream.ObservedProperty.ID == nil {
var op *entities.ObservedProperty
if op, err = a.db.PostObservedProperty(datastream.ObservedProperty); err != nil {
a.revertPostDatastream(postedObservedProperty, postedSensor, postedObservations)
return nil, []error{err}
}
datastream.ObservedProperty = op
postedObservedProperty = op
}
// Check if Sensor is deep inserted
if datastream.Sensor != nil && datastream.Sensor.ID == nil {
var s *entities.Sensor
if s, err = a.db.PostSensor(datastream.Sensor); err != nil {
a.revertPostDatastream(postedObservedProperty, postedSensor, postedObservations)
return nil, []error{err}
}
datastream.Sensor = s
postedSensor = s
}
ns, err := a.db.PostDatastream(datastream)
if err != nil {
a.revertPostDatastream(postedObservedProperty, postedSensor, postedObservations)
return nil, []error{err}
}
// Check if Observations are deep inserted
if datastream.Observations != nil {
for _, observation := range datastream.Observations {
ds := &entities.Datastream{}
ds.ID = datastream.ID
observation.Datastream = ds
var o *entities.Observation
if o, errors = a.PostObservation(observation); len(errors) > 0 {
a.revertPostDatastream(postedObservedProperty, postedSensor, postedObservations)
return nil, errors
}
postedObservations = append(postedObservations, o)
}
}
ns.SetAllLinks(a.config.GetExternalServerURI())
return ns, nil
}
func (a *APIv1) revertPostDatastream(op *entities.ObservedProperty, sensor *entities.Sensor, observations []*entities.Observation) {
if op != nil {
a.DeleteObservedProperty(op.ID)
}
if sensor != nil {
a.DeleteSensor(sensor.ID)
}
for _, observation := range observations {
a.DeleteObservation(observation.ID)
}
}
// PostDatastreamByThing adds a new datastream by given thing ID
func (a *APIv1) PostDatastreamByThing(thingID interface{}, datastream *entities.Datastream) (*entities.Datastream, []error) {
t := &entities.Thing{}
t.ID = thingID
datastream.Thing = t
return a.PostDatastream(datastream)
}
// PatchDatastream updates the given datastream in the database
func (a *APIv1) PatchDatastream(id interface{}, datastream *entities.Datastream) (*entities.Datastream, error) {
if datastream.Observations != nil || datastream.Sensor != nil || datastream.ObservedProperty != nil || datastream.Thing != nil {
return nil, gostErrors.NewBadRequestError(errors.New("Deep patch datastream not supported."))
}
return a.db.PatchDatastream(id, datastream)
}
// PutDatastream updates the given thing in the database
func (a *APIv1) PutDatastream(id interface{}, datastream *entities.Datastream) (*entities.Datastream, []error) {
var err2 error
putdatastream, err2 := a.db.PutDatastream(id, datastream)
if err2 != nil {
return nil, []error{err2}
}
// putdatastream.SetAllLinks(a.config.GetExternalServerURI())
return putdatastream, nil
}
// DeleteDatastream deletes a datastream from the database
func (a *APIv1) DeleteDatastream(id interface{}) error {
return a.db.DeleteDatastream(id)
}