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
1080cf8b
Commit
1080cf8b
authored
4 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
save sensor update time in database
parent
9d9fd9bd
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
+14
-5
14 additions, 5 deletions
src/logic/Database.py
with
14 additions
and
5 deletions
src/logic/Database.py
+
14
−
5
View file @
1080cf8b
import
sqlite3
import
sqlite3
from
datetime
import
datetime
from
typing
import
Tuple
from
typing
import
Tuple
from
TheCodeLabs_BaseUtils
import
DefaultLogger
from
TheCodeLabs_BaseUtils
import
DefaultLogger
...
@@ -12,6 +13,8 @@ class Database:
...
@@ -12,6 +13,8 @@ class Database:
TABLE_DEVICE
=
'
device
'
TABLE_DEVICE
=
'
device
'
TABLE_SENSOR
=
'
sensor
'
TABLE_SENSOR
=
'
sensor
'
DATE_FORMAT
=
'
%Y-%m-%d %H:%M:%S
'
def
__init__
(
self
,
databasePath
):
def
__init__
(
self
,
databasePath
):
self
.
_databasePath
=
databasePath
self
.
_databasePath
=
databasePath
self
.
__create_database
()
self
.
__create_database
()
...
@@ -27,11 +30,15 @@ class Database:
...
@@ -27,11 +30,15 @@ class Database:
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)
'''
)
def
__get_connection
(
self
):
def
__get_connection
(
self
):
return
sqlite3
.
connect
(
self
.
_databasePath
)
return
sqlite3
.
connect
(
self
.
_databasePath
)
def
__get_current_datetime
(
self
):
return
datetime
.
strftime
(
datetime
.
now
(),
self
.
DATE_FORMAT
)
def
get_all_devices
(
self
):
def
get_all_devices
(
self
):
with
self
.
__get_connection
()
as
connection
:
with
self
.
__get_connection
()
as
connection
:
cursor
=
connection
.
execute
(
f
'
SELECT * FROM
{
self
.
TABLE_DEVICE
}
ORDER BY name
'
)
cursor
=
connection
.
execute
(
f
'
SELECT * FROM
{
self
.
TABLE_DEVICE
}
ORDER BY name
'
)
...
@@ -62,12 +69,14 @@ class Database:
...
@@ -62,12 +69,14 @@ class Database:
with
self
.
__get_connection
()
as
connection
:
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) VALUES(?, ?, ?, ?)
'
,
connection
.
execute
(
f
'
INSERT INTO
{
self
.
TABLE_SENSOR
}
(name, device_id, type, value, timestamp )
'
(
name
,
device
[
0
],
sensorType
,
value
))
f
'
VALUES(?, ?, ?, ?, ?)
'
,
(
name
,
device
[
0
],
sensorType
,
value
,
self
.
__get_current_datetime
()))
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
:
with
self
.
__get_connection
()
as
connection
:
connection
.
execute
(
f
'
UPDATE
{
self
.
TABLE_SENSOR
}
SET value = ? WHERE device_id = ? AND name = ?
'
,
connection
.
execute
(
f
'
UPDATE
{
self
.
TABLE_SENSOR
}
SET value = ?, timestamp = ?
'
(
value
,
device
[
0
],
name
))
f
'
WHERE device_id = ? AND name = ?
'
,
(
value
,
self
.
__get_current_datetime
(),
device
[
0
],
name
,
))
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