-
Notifications
You must be signed in to change notification settings - Fork 19
/
models.go
executable file
·271 lines (232 loc) · 15.5 KB
/
models.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
package models
import (
"net/http"
entities "github.com/gost/core"
"github.com/gost/server/configuration"
"github.com/gost/server/sensorthings/odata"
)
const (
// APIPrefix for V1.0 endpoint
APIPrefix string = "v1.0"
)
// API describes all request and responses to fulfill the SensorThings API standard
type API interface {
Start()
GetConfig() *configuration.Config
GetAcceptedPaths() []string
GetVersionInfo() *VersionInfo
GetBasePathInfo() *entities.ArrayResponse
GetEndpoints() *map[entities.EntityType]Endpoint
GetTopics(prefix string) *[]Topic
GetThing(id interface{}, qo *odata.QueryOptions, path string) (*entities.Thing, error)
GetThingByDatastream(id interface{}, qo *odata.QueryOptions, path string) (*entities.Thing, error)
GetThingsByLocation(id interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetThingByHistoricalLocation(id interface{}, qo *odata.QueryOptions, path string) (*entities.Thing, error)
GetThings(qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
PostThing(thing *entities.Thing) (*entities.Thing, []error)
PatchThing(id interface{}, thing *entities.Thing) (*entities.Thing, error)
PutThing(id interface{}, thing *entities.Thing) (*entities.Thing, []error)
DeleteThing(id interface{}) error
GetLocation(id interface{}, qo *odata.QueryOptions, path string) (*entities.Location, error)
GetLocations(qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetLocationsByHistoricalLocation(hlID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetLocationsByThing(thingID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
PostLocation(location *entities.Location) (*entities.Location, []error)
PostLocationByThing(thingID interface{}, location *entities.Location) (*entities.Location, []error)
PatchLocation(id interface{}, location *entities.Location) (*entities.Location, error)
PutLocation(id interface{}, location *entities.Location) (*entities.Location, []error)
DeleteLocation(id interface{}) error
GetHistoricalLocation(id interface{}, qo *odata.QueryOptions, path string) (*entities.HistoricalLocation, error)
GetHistoricalLocations(qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetHistoricalLocationsByLocation(locationID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetHistoricalLocationsByThing(thingID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
PostHistoricalLocation(hl *entities.HistoricalLocation) (*entities.HistoricalLocation, []error)
PutHistoricalLocation(id interface{}, hl *entities.HistoricalLocation) (*entities.HistoricalLocation, []error)
PatchHistoricalLocation(id interface{}, hl *entities.HistoricalLocation) (*entities.HistoricalLocation, error)
DeleteHistoricalLocation(id interface{}) error
GetDatastream(id interface{}, qo *odata.QueryOptions, path string) (*entities.Datastream, error)
GetDatastreams(qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetDatastreamByObservation(id interface{}, qo *odata.QueryOptions, path string) (*entities.Datastream, error)
GetDatastreamsByThing(thingID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetDatastreamsBySensor(sensorID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetDatastreamsByObservedProperty(sensorID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
PostDatastream(datastream *entities.Datastream) (*entities.Datastream, []error)
PostDatastreamByThing(thingID interface{}, datastream *entities.Datastream) (*entities.Datastream, []error)
PatchDatastream(id interface{}, datastream *entities.Datastream) (*entities.Datastream, error)
PutDatastream(id interface{}, datastream *entities.Datastream) (*entities.Datastream, []error)
DeleteDatastream(id interface{}) error
GetFeatureOfInterest(id interface{}, qo *odata.QueryOptions, path string) (*entities.FeatureOfInterest, error)
GetFeatureOfInterestByObservation(id interface{}, qo *odata.QueryOptions, path string) (*entities.FeatureOfInterest, error)
GetFeatureOfInterests(qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
PostFeatureOfInterest(foi *entities.FeatureOfInterest) (*entities.FeatureOfInterest, []error)
PatchFeatureOfInterest(id interface{}, foi *entities.FeatureOfInterest) (*entities.FeatureOfInterest, error)
PutFeatureOfInterest(id interface{}, foi *entities.FeatureOfInterest) (*entities.FeatureOfInterest, []error)
DeleteFeatureOfInterest(id interface{}) error
GetObservation(id interface{}, qo *odata.QueryOptions, path string) (*entities.Observation, error)
GetObservations(qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetObservationsByDatastream(datastreamID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetObservationsByFeatureOfInterest(foiID interface{}, qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
PostObservation(observation *entities.Observation) (*entities.Observation, []error)
PostObservationByDatastream(datastreamID interface{}, observation *entities.Observation) (*entities.Observation, []error)
PatchObservation(id interface{}, observation *entities.Observation) (*entities.Observation, error)
PutObservation(id interface{}, observation *entities.Observation) (*entities.Observation, []error)
DeleteObservation(id interface{}) error
GetObservedProperty(id interface{}, qo *odata.QueryOptions, path string) (*entities.ObservedProperty, error)
GetObservedProperties(qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
GetObservedPropertyByDatastream(datastreamID interface{}, qo *odata.QueryOptions, path string) (*entities.ObservedProperty, error)
PostObservedProperty(op *entities.ObservedProperty) (*entities.ObservedProperty, []error)
PatchObservedProperty(id interface{}, op *entities.ObservedProperty) (*entities.ObservedProperty, error)
PutObservedProperty(id interface{}, op *entities.ObservedProperty) (*entities.ObservedProperty, []error)
DeleteObservedProperty(id interface{}) error
GetSensor(id interface{}, qo *odata.QueryOptions, path string) (*entities.Sensor, error)
GetSensorByDatastream(id interface{}, qo *odata.QueryOptions, path string) (*entities.Sensor, error)
GetSensors(qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error)
PostSensor(sensor *entities.Sensor) (*entities.Sensor, []error)
PatchSensor(id interface{}, sensor *entities.Sensor) (*entities.Sensor, error)
DeleteSensor(id interface{}) error
PutSensor(id interface{}, sensor *entities.Sensor) (*entities.Sensor, []error)
PostCreateObservations(co *entities.CreateObservations) ([]string, []error)
LinkLocation(thingID interface{}, locationID interface{}) error
SetLinks(entity entities.Entity, qo *odata.QueryOptions)
CreateNextLink(incomingURL string, qo *odata.QueryOptions) string
}
// Database specifies the operations that the database provider needs to support
type Database interface {
Start()
CreateSchema(location string) error
GetThing(id interface{}, qo *odata.QueryOptions) (*entities.Thing, error)
GetThingByDatastream(id interface{}, qo *odata.QueryOptions) (t *entities.Thing, e error)
GetThingsByLocation(id interface{}, qo *odata.QueryOptions) (t []*entities.Thing, count int, hasNext bool, e error)
GetThingByHistoricalLocation(id interface{}, qo *odata.QueryOptions) (t *entities.Thing, e error)
GetThings(qo *odata.QueryOptions) (t []*entities.Thing, count int, hasNext bool, e error)
PostThing(*entities.Thing) (*entities.Thing, error)
PatchThing(interface{}, *entities.Thing) (*entities.Thing, error)
PutThing(interface{}, *entities.Thing) (*entities.Thing, error)
DeleteThing(id interface{}) error
GetLocation(id interface{}, qo *odata.QueryOptions) (*entities.Location, error)
GetLocations(qo *odata.QueryOptions) (l []*entities.Location, count int, hasNext bool, e error)
GetLocationsByHistoricalLocation(id interface{}, qo *odata.QueryOptions) (l []*entities.Location, count int, hasNext bool, e error)
GetLocationsByThing(id interface{}, qo *odata.QueryOptions) (l []*entities.Location, count int, hasNext bool, e error)
GetLocationByDatastreamID(id interface{}, qo *odata.QueryOptions) (*entities.Location, error)
PostLocation(*entities.Location) (*entities.Location, error)
LinkLocation(id interface{}, locationID interface{}) error
PatchLocation(interface{}, *entities.Location) (*entities.Location, error)
DeleteLocation(id interface{}) error
PutLocation(interface{}, *entities.Location) (*entities.Location, error)
GetObservedProperty(id interface{}, qo *odata.QueryOptions) (*entities.ObservedProperty, error)
GetObservedPropertyByDatastream(id interface{}, qo *odata.QueryOptions) (*entities.ObservedProperty, error)
GetObservedProperties(qo *odata.QueryOptions) (o []*entities.ObservedProperty, count int, hasNext bool, e error)
PostObservedProperty(*entities.ObservedProperty) (*entities.ObservedProperty, error)
PatchObservedProperty(interface{}, *entities.ObservedProperty) (*entities.ObservedProperty, error)
PutObservedProperty(interface{}, *entities.ObservedProperty) (*entities.ObservedProperty, error)
DeleteObservedProperty(id interface{}) error
GetSensor(id interface{}, qo *odata.QueryOptions) (*entities.Sensor, error)
GetSensorByDatastream(id interface{}, qo *odata.QueryOptions) (*entities.Sensor, error)
GetSensors(qo *odata.QueryOptions) (s []*entities.Sensor, count int, hasNext bool, e error)
PostSensor(*entities.Sensor) (*entities.Sensor, error)
PatchSensor(interface{}, *entities.Sensor) (*entities.Sensor, error)
PutSensor(interface{}, *entities.Sensor) (*entities.Sensor, error)
DeleteSensor(id interface{}) error
GetDatastream(id interface{}, qo *odata.QueryOptions) (*entities.Datastream, error)
GetDatastreams(qo *odata.QueryOptions) (d []*entities.Datastream, count int, hasNext bool, e error)
GetDatastreamByObservation(id interface{}, qo *odata.QueryOptions) (*entities.Datastream, error)
GetDatastreamsByThing(id interface{}, qo *odata.QueryOptions) (d []*entities.Datastream, count int, hasNext bool, e error)
GetDatastreamsBySensor(id interface{}, qo *odata.QueryOptions) (d []*entities.Datastream, count int, hasNext bool, e error)
GetDatastreamsByObservedProperty(id interface{}, qo *odata.QueryOptions) (d []*entities.Datastream, count int, hasNext bool, e error)
PostDatastream(*entities.Datastream) (*entities.Datastream, error)
PatchDatastream(interface{}, *entities.Datastream) (*entities.Datastream, error)
DeleteDatastream(id interface{}) error
DatastreamExists(int) bool
PutDatastream(interface{}, *entities.Datastream) (*entities.Datastream, error)
GetFeatureOfInterest(id interface{}, qo *odata.QueryOptions) (*entities.FeatureOfInterest, error)
GetFeatureOfInterestIDByLocationID(id interface{}) (interface{}, error)
GetFeatureOfInterestByObservation(id interface{}, qo *odata.QueryOptions) (*entities.FeatureOfInterest, error)
GetFeatureOfInterests(qo *odata.QueryOptions) (f []*entities.FeatureOfInterest, count int, hasNext bool, e error)
PostFeatureOfInterest(*entities.FeatureOfInterest) (*entities.FeatureOfInterest, error)
PutFeatureOfInterest(interface{}, *entities.FeatureOfInterest) (*entities.FeatureOfInterest, error)
PatchFeatureOfInterest(interface{}, *entities.FeatureOfInterest) (*entities.FeatureOfInterest, error)
DeleteFeatureOfInterest(id interface{}) error
GetObservation(id interface{}, qo *odata.QueryOptions) (*entities.Observation, error)
GetObservations(qo *odata.QueryOptions) (o []*entities.Observation, count int, hasNext bool, e error)
GetObservationsByDatastream(id interface{}, qo *odata.QueryOptions) (o []*entities.Observation, count int, hasNext bool, e error)
GetObservationsByFeatureOfInterest(id interface{}, qo *odata.QueryOptions) (o []*entities.Observation, count int, hasNext bool, e error)
PostObservation(*entities.Observation) (*entities.Observation, error)
PatchObservation(interface{}, *entities.Observation) (*entities.Observation, error)
PutObservation(interface{}, *entities.Observation) (*entities.Observation, error)
DeleteObservation(id interface{}) error
GetHistoricalLocation(id interface{}, qo *odata.QueryOptions) (*entities.HistoricalLocation, error)
GetHistoricalLocations(qo *odata.QueryOptions) (h []*entities.HistoricalLocation, count int, hasNext bool, e error)
GetHistoricalLocationsByLocation(id interface{}, qo *odata.QueryOptions) (h []*entities.HistoricalLocation, count int, hasNext bool, e error)
GetHistoricalLocationsByThing(id interface{}, qo *odata.QueryOptions) (h []*entities.HistoricalLocation, count int, hasNext bool, e error)
PostHistoricalLocation(*entities.HistoricalLocation) (*entities.HistoricalLocation, error)
PutHistoricalLocation(interface{}, *entities.HistoricalLocation) (*entities.HistoricalLocation, error)
PatchHistoricalLocation(interface{}, *entities.HistoricalLocation) (*entities.HistoricalLocation, error)
DeleteHistoricalLocation(id interface{}) error
ThingExists(thingID interface{}) bool
LocationExists(thingID interface{}) bool
}
// MQTTClient interface defines the needed MQTT client operations
type MQTTClient interface {
Start(*API)
Stop()
Publish(string, string, byte) //topic, message, qos
}
// Endpoint defines the rest endpoint options
type Endpoint interface {
GetName() string
GetURL() string
GetOperations() []EndpointOperation
GetSupportedExpandParams() []string
GetSupportedSelectParams() []string
ShowOutputInfo() bool
}
// HTTPHandler func defines the format of the handler to process the incoming request
type HTTPHandler func(w http.ResponseWriter, r *http.Request, e *Endpoint, a *API)
// EndpointOperation contains the needed information to create an endpoint in the HTTP.Router
type EndpointOperation struct {
OperationType HTTPOperation
Path string //relative path to the endpoint for example: /v1.0/myendpoint/
Handler HTTPHandler
}
// HTTPOperation describes the HTTP operation such as GET POST DELETE.
type HTTPOperation string
// HTTPOperation is a "enumeration" of the HTTP operations needed for all endpoints.
const (
HTTPOperationGet HTTPOperation = "GET"
HTTPOperationPost HTTPOperation = "POST"
HTTPOperationPatch HTTPOperation = "PATCH"
HTTPOperationPut HTTPOperation = "PUT"
HTTPOperationDelete HTTPOperation = "DELETE"
)
// Topic defines the MQTT PUBLISH topics
type Topic struct {
Path string
Handler MQTTHandler
}
// MQTTHandler func defines the format of the handler to process the incoming MQTT publish message
type MQTTHandler func(a *API, prefix, topic string, message []byte)
// MQTTInternalHandler func defines the format of the handler to process the incoming MQTT publish message
type MQTTInternalHandler func(a *API, message []byte, id string)
// VersionInfo describes the version info for the GOST server version and supported SensorThings API version
type VersionInfo struct {
GostServerVersion GostServerVersion `json:"gostServerVersion"`
APIVersion APIVersion `json:"sensorThingsApiVersion"`
}
// GostServerVersion contains version information on the GOST server
type GostServerVersion struct {
Version string `json:"version"`
}
// APIVersion contains version information on the supported SensorThings API version
type APIVersion struct {
Version string `json:"version"`
}
// ErrorResponse is the default response format for sending errors back
type ErrorResponse struct {
Error ErrorContent `json:"error"`
}
// ErrorContent holds information on the error that occurred
type ErrorContent struct {
StatusText string `json:"status"`
StatusCode int `json:"code"`
Messages []string `json:"message"`
}