Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
StorageLeaf
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ProjectLeaf
StorageLeaf
Commits
b2426ec4
Commit
b2426ec4
authored
4 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
#8 - post route for sensor
parent
5b786e3a
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
docs/api.yml
+48
-3
48 additions, 3 deletions
docs/api.yml
src/blueprints/Measurements.py
+3
-1
3 additions, 1 deletion
src/blueprints/Measurements.py
src/blueprints/Sensors.py
+31
-1
31 additions, 1 deletion
src/blueprints/Sensors.py
src/logic/Parameters.py
+1
-0
1 addition, 0 deletions
src/logic/Parameters.py
with
83 additions
and
5 deletions
docs/api.yml
+
48
−
3
View file @
b2426ec4
...
...
@@ -238,6 +238,34 @@ paths:
type
:
array
items
:
$ref
:
'
#/components/schemas/Measurement'
/sensor
:
post
:
tags
:
-
sensor
summary
:
Adds a new sensor. If a sensor with the provided name already exists for the given device id an error response is returned.
operationId
:
addSensor
security
:
-
bearerAuth
:
[]
requestBody
:
description
:
Sensor to add
required
:
true
content
:
application/json
:
schema
:
$ref
:
'
#/components/schemas/NewSensor'
responses
:
'
200'
:
description
:
success response
content
:
application/json
:
schema
:
$ref
:
'
#/components/schemas/SuccessResponse'
default
:
description
:
error response
content
:
application/json
:
schema
:
$ref
:
'
#/components/schemas/ErrorResponse'
/measurements
:
get
:
...
...
@@ -374,7 +402,7 @@ components:
-
id
-
device_id
-
name
-
sensor_
type
-
type
properties
:
id
:
type
:
integer
...
...
@@ -384,8 +412,8 @@ components:
example
:
1
name
:
type
:
string
example
:
"
My
Device
"
sensor_
type
:
example
:
"
My
Sensor
"
type
:
type
:
string
example
:
"
temperature"
...
...
@@ -453,6 +481,23 @@ components:
type
:
string
example
:
"
myDevice"
NewSensor
:
type
:
object
required
:
-
device_id
-
name
-
type
properties
:
device_id
:
type
:
integer
example
:
1
name
:
type
:
string
example
:
"
My
Sensor"
type
:
type
:
string
example
:
"
temperature"
SuccessResponse
:
required
:
-
success
...
...
This diff is collapsed.
Click to expand it.
src/blueprints/Measurements.py
+
3
−
1
View file @
b2426ec4
...
...
@@ -36,7 +36,9 @@ def construct_blueprint(settings):
sensors
=
parameters
[
DeviceParameters
.
SENSORS
.
value
]
for
sensor
in
sensors
:
sensorParams
=
RequestValidator
.
validate_parameters
(
sensor
,
SensorParameters
.
get_values
(),
[
SensorParameters
.
NAME
.
value
,
SensorParameters
.
TYPE
.
value
,
SensorParameters
.
VALUE
.
value
],
f
'
sensor
"
{
sensor
}
"'
)
sensor
=
__add_sensor_if_not_exists
(
database
,
int
(
device
[
'
id
'
]),
sensorParams
)
database
.
measurementAccess
.
add_measurement
(
int
(
sensor
[
'
id
'
]),
...
...
This diff is collapsed.
Click to expand it.
src/blueprints/Sensors.py
+
31
−
1
View file @
b2426ec4
from
flask
import
Blueprint
,
jsonify
from
flask
import
Blueprint
,
jsonify
,
request
from
logic.AuthenticationWrapper
import
require_api_key
from
logic.Parameters
import
SensorParameters
from
logic.RequestValidator
import
RequestValidator
,
ValidationError
from
logic.database.Database
import
Database
...
...
@@ -46,4 +48,32 @@ def construct_blueprint(settings):
database
.
sensorAccess
.
delete_sensor
(
sensorID
)
return
jsonify
({
'
success
'
:
True
})
@sensors.route
(
'
/sensor
'
,
methods
=
[
'
POST
'
])
@require_api_key
(
password
=
settings
[
'
api
'
][
'
key
'
])
def
add_sensor
():
try
:
parameters
=
RequestValidator
.
validate
(
request
,
[
SensorParameters
.
NAME
.
value
,
SensorParameters
.
TYPE
.
value
,
SensorParameters
.
DEVICE_ID
.
value
])
database
=
Database
(
settings
[
'
database
'
][
'
databasePath
'
])
deviceID
=
parameters
[
SensorParameters
.
DEVICE_ID
.
value
]
sensorName
=
parameters
[
SensorParameters
.
NAME
.
value
]
sensorType
=
parameters
[
SensorParameters
.
TYPE
.
value
]
device
=
database
.
deviceAccess
.
get_device
(
deviceID
)
if
not
device
:
return
jsonify
({
'
success
'
:
False
,
'
msg
'
:
f
'
No device with id
"
{
deviceID
}
"
existing
'
})
existingSensor
=
database
.
sensorAccess
.
get_sensor_by_name_and_device_id
(
deviceID
,
sensorName
)
if
existingSensor
:
return
jsonify
({
'
success
'
:
False
,
'
msg
'
:
f
'
A sensor called
"
{
sensorName
}
"
already exists (ID:
{
existingSensor
[
"
id
"
]
}
) for device
{
deviceID
}
'
})
database
.
sensorAccess
.
add_sensor
(
deviceID
,
sensorName
,
sensorType
)
except
ValidationError
as
e
:
return
e
.
response
,
400
return
jsonify
({
'
success
'
:
True
})
return
sensors
This diff is collapsed.
Click to expand it.
src/logic/Parameters.py
+
1
−
0
View file @
b2426ec4
...
...
@@ -14,6 +14,7 @@ class SensorParameters(Enum):
NAME
=
'
name
'
TYPE
=
'
type
'
VALUE
=
'
value
'
DEVICE_ID
=
'
device_id
'
@staticmethod
def
get_values
():
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment