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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ProjectLeaf
StorageLeaf
Commits
1ad5d76b
Commit
1ad5d76b
authored
4 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
added api methods for sensor
parent
9cd4a3da
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
docs/api.yml
+66
-3
66 additions, 3 deletions
docs/api.yml
src/blueprints/Routes.py
+15
-1
15 additions, 1 deletion
src/blueprints/Routes.py
src/logic/Database.py
+7
-7
7 additions, 7 deletions
src/logic/Database.py
with
88 additions
and
11 deletions
docs/api.yml
+
66
−
3
View file @
1ad5d76b
...
@@ -50,6 +50,44 @@ paths:
...
@@ -50,6 +50,44 @@ paths:
application/json
:
application/json
:
schema
:
schema
:
$ref
:
'
#/components/schemas/Device'
$ref
:
'
#/components/schemas/Device'
/device/{deviceName}/sensors/{sensorName}
:
get
:
summary
:
Gets a specific device
operationId
:
device
parameters
:
-
in
:
path
name
:
deviceName
description
:
The device name
required
:
true
schema
:
type
:
string
-
in
:
path
name
:
sensorName
description
:
The sensor name
required
:
true
schema
:
type
:
string
responses
:
'
200'
:
description
:
The sensor
content
:
application/json
:
schema
:
$ref
:
'
#/components/schemas/Sensor'
/sensors
:
get
:
summary
:
Gets all sensors
operationId
:
sensors
responses
:
'
200'
:
description
:
All available sensors
content
:
application/json
:
schema
:
type
:
array
items
:
$ref
:
'
#/components/schemas/Sensor'
components
:
components
:
schemas
:
schemas
:
...
@@ -74,16 +112,41 @@ components:
...
@@ -74,16 +112,41 @@ components:
Device
:
Device
:
type
:
object
type
:
object
required
:
required
:
-
ID
-
id
-
name
-
name
properties
:
properties
:
ID
:
id
:
type
:
integer
type
:
integer
example
:
1
example
:
1
Project
name
:
name
:
type
:
string
type
:
string
example
:
"
My
Device"
example
:
"
My
Device"
Sensor
:
type
:
object
required
:
-
id
-
device_id
-
name
-
sensor_type
-
value
properties
:
id
:
type
:
integer
example
:
1
device_id
:
type
:
integer
example
:
1
name
:
type
:
string
example
:
"
My
Device"
sensor_type
:
type
:
string
example
:
"
temperature"
value
:
type
:
string
example
:
"
20.15"
SuccessResponse
:
SuccessResponse
:
required
:
required
:
-
success
-
success
...
...
This diff is collapsed.
Click to expand it.
src/blueprints/Routes.py
+
15
−
1
View file @
1ad5d76b
...
@@ -55,6 +55,20 @@ def construct_blueprint(settings, version):
...
@@ -55,6 +55,20 @@ def construct_blueprint(settings, version):
database
=
Database
(
settings
[
'
database
'
][
'
databasePath
'
])
database
=
Database
(
settings
[
'
database
'
][
'
databasePath
'
])
return
jsonify
(
database
.
get_device
(
deviceName
))
return
jsonify
(
database
.
get_device
(
deviceName
))
@routes.route
(
'
/sensors
'
,
methods
=
[
'
GET
'
])
def
get_all_sensors
():
database
=
Database
(
settings
[
'
database
'
][
'
databasePath
'
])
return
jsonify
(
database
.
get_all_sensors
())
@routes.route
(
'
/device/<deviceName>/sensors/<sensorName>
'
,
methods
=
[
'
GET
'
])
def
get_sensor
(
deviceName
,
sensorName
):
database
=
Database
(
settings
[
'
database
'
][
'
databasePath
'
])
device
=
database
.
get_device
(
deviceName
)
if
not
device
:
return
jsonify
({
'
success
'
:
False
,
'
msg
'
:
f
'
No device with name
"
{
deviceName
}
"
existing
'
})
return
jsonify
(
database
.
get_sensor
(
device
[
'
id
'
],
sensorName
))
@routes.route
(
'
/device/<deviceName>
'
,
methods
=
[
'
POST
'
])
@routes.route
(
'
/device/<deviceName>
'
,
methods
=
[
'
POST
'
])
def
postSensorData
(
deviceName
):
def
postSensorData
(
deviceName
):
try
:
try
:
...
@@ -80,7 +94,7 @@ def construct_blueprint(settings, version):
...
@@ -80,7 +94,7 @@ def construct_blueprint(settings, version):
sensorName
=
sensorParams
[
SensorParameters
.
NAME
.
value
]
sensorName
=
sensorParams
[
SensorParameters
.
NAME
.
value
]
sensorType
=
sensorParams
[
SensorParameters
.
TYPE
.
value
]
sensorType
=
sensorParams
[
SensorParameters
.
TYPE
.
value
]
sensorValue
=
sensorParams
[
SensorParameters
.
VALUE
.
value
]
sensorValue
=
sensorParams
[
SensorParameters
.
VALUE
.
value
]
sensor
=
database
.
get_sensor
(
device
[
0
],
sensorName
)
sensor
=
database
.
get_sensor
(
device
[
'
id
'
],
sensorName
)
if
sensor
:
if
sensor
:
database
.
update_sensor
(
device
,
sensorName
,
sensorType
,
sensorValue
)
database
.
update_sensor
(
device
,
sensorName
,
sensorType
,
sensorValue
)
else
:
else
:
...
...
This diff is collapsed.
Click to expand it.
src/logic/Database.py
+
7
−
7
View file @
1ad5d76b
import
sqlite3
import
sqlite3
from
datetime
import
datetime
from
datetime
import
datetime
from
enum
import
Enum
from
enum
import
Enum
from
typing
import
Tuple
from
typing
import
Tuple
,
Dict
from
TheCodeLabs_BaseUtils
import
DefaultLogger
from
TheCodeLabs_BaseUtils
import
DefaultLogger
...
@@ -87,18 +87,18 @@ class Database:
...
@@ -87,18 +87,18 @@ class Database:
deviceID
,
name
,
deviceID
,
name
,
fetch_type
=
FetchType
.
ONE
)
fetch_type
=
FetchType
.
ONE
)
def
add_sensor
(
self
,
device
:
Tuple
[
int
,
str
],
name
:
str
,
sensorType
:
str
,
value
:
str
):
def
add_sensor
(
self
,
device
:
Dict
[
str
,
str
],
name
:
str
,
sensorType
:
str
,
value
:
str
):
LOGGER
.
debug
(
f
'
Inserting new sensor
"
{
name
}
"
for device
"
{
device
[
1
]
}
"
'
LOGGER
.
debug
(
f
'
Inserting new sensor
"
{
name
}
"
for device
"
{
device
[
"
name
"
]
}
"
'
f
'
(type:
"
{
sensorType
}
"
, value:
"
{
value
}
"
)
'
)
f
'
(type:
"
{
sensorType
}
"
, value:
"
{
value
}
"
)
'
)
self
.
__query
(
f
'
INSERT INTO
{
self
.
TABLE_SENSOR
}
(name, device_id, type, value, timestamp )
'
self
.
__query
(
f
'
INSERT INTO
{
self
.
TABLE_SENSOR
}
(name, device_id, type, value, timestamp )
'
f
'
VALUES(?, ?, ?, ?, ?)
'
,
f
'
VALUES(?, ?, ?, ?, ?)
'
,
name
,
device
[
0
],
sensorType
,
value
,
self
.
__get_current_datetime
(),
name
,
device
[
'
id
'
],
sensorType
,
value
,
self
.
__get_current_datetime
(),
fetch_type
=
FetchType
.
NONE
)
fetch_type
=
FetchType
.
NONE
)
def
update_sensor
(
self
,
device
:
Tuple
[
int
,
str
],
name
:
str
,
sensorType
:
str
,
value
:
str
):
def
update_sensor
(
self
,
device
:
Dict
[
str
,
str
],
name
:
str
,
sensorType
:
str
,
value
:
str
):
LOGGER
.
debug
(
f
'
Updating sensor
"
{
name
}
"
for device
"
{
device
[
1
]
}
"
'
LOGGER
.
debug
(
f
'
Updating sensor
"
{
name
}
"
for device
"
{
device
[
"
name
"
]
}
"
'
f
'
(type:
"
{
sensorType
}
"
, value:
"
{
value
}
"
)
'
)
f
'
(type:
"
{
sensorType
}
"
, value:
"
{
value
}
"
)
'
)
self
.
__query
(
f
'
UPDATE
{
self
.
TABLE_SENSOR
}
SET value = ?, timestamp = ?
'
self
.
__query
(
f
'
UPDATE
{
self
.
TABLE_SENSOR
}
SET value = ?, timestamp = ?
'
f
'
WHERE device_id = ? AND name = ?
'
,
f
'
WHERE device_id = ? AND name = ?
'
,
value
,
self
.
__get_current_datetime
(),
device
[
0
],
name
,
value
,
self
.
__get_current_datetime
(),
device
[
'
id
'
],
name
,
fetch_type
=
FetchType
.
NONE
)
fetch_type
=
FetchType
.
NONE
)
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