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
9cd4a3da
Commit
9cd4a3da
authored
4 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
api should return correct dicts
parent
b6d76c4c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/logic/Database.py
+55
-33
55 additions, 33 deletions
src/logic/Database.py
with
55 additions
and
33 deletions
src/logic/Database.py
+
55
−
33
View file @
9cd4a3da
import
sqlite3
import
sqlite3
from
datetime
import
datetime
from
datetime
import
datetime
from
enum
import
Enum
from
typing
import
Tuple
from
typing
import
Tuple
from
TheCodeLabs_BaseUtils
import
DefaultLogger
from
TheCodeLabs_BaseUtils
import
DefaultLogger
...
@@ -9,74 +10,95 @@ from logic import Constants
...
@@ -9,74 +10,95 @@ from logic import Constants
LOGGER
=
DefaultLogger
().
create_logger_if_not_exists
(
Constants
.
APP_NAME
)
LOGGER
=
DefaultLogger
().
create_logger_if_not_exists
(
Constants
.
APP_NAME
)
class
FetchType
(
Enum
):
NONE
=
1
ONE
=
2
ALL
=
3
class
Database
:
class
Database
:
TABLE_DEVICE
=
'
device
'
TABLE_DEVICE
=
'
device
'
TABLE_SENSOR
=
'
sensor
'
TABLE_SENSOR
=
'
sensor
'
DATE_FORMAT
=
'
%Y-%m-%d %H:%M:%S
'
DATE_FORMAT
=
'
%Y-%m-%d %H:%M:%S
'
@staticmethod
def
namedtuple_factory
(
cursor
,
row
):
"""
Returns sqlite rows as dicts.
"""
d
=
{}
for
idx
,
col
in
enumerate
(
cursor
.
description
):
d
[
col
[
0
]]
=
row
[
idx
]
return
d
def
__init__
(
self
,
databasePath
):
def
__init__
(
self
,
databasePath
):
self
.
_databasePath
=
databasePath
self
.
_databasePath
=
databasePath
self
.
__create_database
()
self
.
__create_database
()
def
__create_database
(
self
):
def
__create_database
(
self
):
LOGGER
.
debug
(
'
Creating database tables...
'
)
LOGGER
.
debug
(
'
Creating database tables...
'
)
with
self
.
__get_connection
()
as
connection
:
self
.
__query
(
f
'''
CREATE TABLE IF NOT EXISTS
{
self
.
TABLE_DEVICE
}
(
connection
.
execute
(
f
'''
CREATE TABLE IF NOT EXISTS
{
self
.
TABLE_DEVICE
}
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL)
'''
)
name TEXT NOT NULL)
'''
,
fetch_type
=
FetchType
.
NONE
)
connection
.
execute
(
f
'''
CREATE TABLE IF NOT EXISTS sensor (
self
.
__query
(
f
'''
CREATE TABLE IF NOT EXISTS sensor (
id INTEGER PRIMARY KEY AUTOINCREMENT,
id INTEGER PRIMARY KEY AUTOINCREMENT,
device_id INTEGER,
device_id INTEGER,
name TEXT NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL,
type TEXT NOT NULL,
value TEXT NOT NULL,
value TEXT NOT NULL,
timestamp TEXT NOT NULL)
'''
)
timestamp TEXT NOT NULL)
'''
,
fetch_type
=
FetchType
.
NONE
)
def
__get_connection
(
self
):
def
__get_cursor
(
self
):
return
sqlite3
.
connect
(
self
.
_databasePath
)
connection
=
sqlite3
.
connect
(
self
.
_databasePath
)
connection
.
row_factory
=
Database
.
namedtuple_factory
return
connection
.
cursor
()
def
__query
(
self
,
query
,
*
args
,
fetch_type
=
FetchType
.
ALL
):
cursor
=
self
.
__get_cursor
()
try
:
cursor
.
execute
(
query
,
args
)
if
fetch_type
==
FetchType
.
ONE
:
return
cursor
.
fetchone
()
if
fetch_type
==
FetchType
.
ALL
:
return
cursor
.
fetchall
()
finally
:
cursor
.
close
()
def
__get_current_datetime
(
self
):
def
__get_current_datetime
(
self
):
return
datetime
.
strftime
(
datetime
.
now
(),
self
.
DATE_FORMAT
)
return
datetime
.
strftime
(
datetime
.
now
(),
self
.
DATE_FORMAT
)
def
get_all_devices
(
self
):
def
get_all_devices
(
self
):
with
self
.
__get_connection
()
as
connection
:
return
self
.
__query
(
f
'
SELECT * FROM
{
self
.
TABLE_DEVICE
}
ORDER BY name
'
,
fetch_type
=
FetchType
.
ALL
)
cursor
=
connection
.
execute
(
f
'
SELECT * FROM
{
self
.
TABLE_DEVICE
}
ORDER BY name
'
)
return
cursor
.
fetchall
()
def
get_device
(
self
,
deviceName
:
str
):
def
get_device
(
self
,
deviceName
:
str
):
with
self
.
__get_connection
()
as
connection
:
return
self
.
__query
(
f
'
SELECT * FROM
{
self
.
TABLE_DEVICE
}
WHERE name =
"
{
deviceName
}
"'
,
fetch_type
=
FetchType
.
ONE
)
cursor
=
connection
.
execute
(
f
'
SELECT * FROM
{
self
.
TABLE_DEVICE
}
WHERE name =
"
{
deviceName
}
"'
)
return
cursor
.
fetchone
()
def
add_device
(
self
,
deviceName
:
str
):
def
add_device
(
self
,
deviceName
:
str
):
with
self
.
__get_connection
()
as
connection
:
LOGGER
.
debug
(
f
'
Inserting new device
"
{
deviceName
}
"'
)
LOGGER
.
debug
(
f
'
Inserting new device
"
{
deviceName
}
"'
)
connection
.
execute
(
f
'
INSERT INTO
{
self
.
TABLE_DEVICE
}
(name) VALUES(?)
'
,
(
deviceName
,
)
)
self
.
__query
(
f
'
INSERT INTO
{
self
.
TABLE_DEVICE
}
(name) VALUES(?)
'
,
deviceName
,
fetch_type
=
FetchType
.
NONE
)
def
get_all_sensors
(
self
):
def
get_all_sensors
(
self
):
with
self
.
__get_connection
()
as
connection
:
return
self
.
__query
(
f
'
SELECT * FROM
{
self
.
TABLE_SENSOR
}
ORDER BY device_id, name
'
,
fetch_type
=
FetchType
.
ALL
)
cursor
=
connection
.
execute
(
f
'
SELECT * FROM
{
self
.
TABLE_SENSOR
}
ORDER BY device_id, name
'
)
return
cursor
.
fetchall
()
def
get_sensor
(
self
,
deviceID
:
int
,
name
:
str
):
def
get_sensor
(
self
,
deviceID
:
int
,
name
:
str
):
with
self
.
__get_connection
()
as
connection
:
return
self
.
__query
(
f
'
SELECT * FROM
{
self
.
TABLE_SENSOR
}
WHERE device_id = ? AND name = ?
'
,
cursor
=
connection
.
execute
(
f
'
SELECT * FROM
{
self
.
TABLE_SENSOR
}
WHERE device_id = ? AND name = ?
'
,
deviceID
,
name
,
(
deviceID
,
name
))
fetch_type
=
FetchType
.
ONE
)
return
cursor
.
fetchone
()
def
add_sensor
(
self
,
device
:
Tuple
[
int
,
str
],
name
:
str
,
sensorType
:
str
,
value
:
str
):
def
add_sensor
(
self
,
device
:
Tuple
[
int
,
str
],
name
:
str
,
sensorType
:
str
,
value
:
str
):
with
self
.
__get_connection
()
as
connection
:
LOGGER
.
debug
(
f
'
Inserting new sensor
"
{
name
}
"
for device
"
{
device
[
1
]
}
"
'
LOGGER
.
debug
(
f
'
Inserting new sensor
"
{
name
}
"
for device
"
{
device
[
1
]
}
"
'
f
'
(type:
"
{
sensorType
}
"
, value:
"
{
value
}
"
)
'
)
f
'
(type:
"
{
sensorType
}
"
, value:
"
{
value
}
"
)
'
)
connection
.
execute
(
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
[
0
],
sensorType
,
value
,
self
.
__get_current_datetime
(),
fetch_type
=
FetchType
.
NONE
)
def
update_sensor
(
self
,
device
:
Tuple
[
int
,
str
],
name
:
str
,
sensorType
:
str
,
value
:
str
):
def
update_sensor
(
self
,
device
:
Tuple
[
int
,
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
[
1
]
}
"
'
f
'
(type:
"
{
sensorType
}
"
, value:
"
{
value
}
"
)
'
)
f
'
(type:
"
{
sensorType
}
"
, value:
"
{
value
}
"
)
'
)
with
self
.
__get_connection
()
as
connection
:
self
.
__query
(
f
'
UPDATE
{
self
.
TABLE_SENSOR
}
SET value = ?, timestamp = ?
'
connection
.
execute
(
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
[
0
],
name
,
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