Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Vladislav Rykov
THSO.server
Commits
df4cbc69
Commit
df4cbc69
authored
Apr 14, 2020
by
Vladislav Rykov
Browse files
added data dao
parent
eaffc178
Changes
6
Show whitespace changes
Inline
Side-by-side
dao/data/__init.py__
0 → 100644
View file @
df4cbc69
dao/data/data.py
0 → 100644
View file @
df4cbc69
import
psycopg2
from
psycopg2
import
sql
# decorator implementation
def
with_psql
(
f
):
def
_with_psql
(
*
args
,
**
kwargs
):
conn
=
psycopg2
.
connect
(
'dbname=gateway'
)
cur
=
conn
.
cursor
()
try
:
res
=
f
(
cur
,
*
args
,
**
kwargs
)
except
(
Exception
,
psycopg2
.
DatabaseError
)
as
error
:
conn
.
rollback
()
res
=
(
False
,
error
)
else
:
conn
.
commit
()
finally
:
cur
.
close
()
conn
.
close
()
return
res
return
_with_psql
@
with_psql
def
create_table
(
cur
,
appkey
,
dev_id
):
tn
=
'dev_'
+
str
(
appkey
)
+
'_'
+
str
(
dev_id
)
cur
.
execute
(
sql
.
SQL
(
"""CREATE TABLE {} (
utc NUMERIC(10) NOT NULL,
timedate VARCHAR(100) NOT NULL,
data json NOT NULL
)"""
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
with_psql
def
delete_table
(
cur
,
appkey
,
dev_id
):
tn
=
'dev_'
+
str
(
appkey
)
+
'_'
+
str
(
dev_id
)
cur
.
execute
(
psycopg2
.
sql
.
SQL
(
"DROP TABLE {}"
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
with_psql
def
get_last_n
(
cur
,
appkey
,
dev_id
,
n
):
tn
=
'dev_'
+
str
(
appkey
)
+
'_'
+
str
(
dev_id
)
query
=
"""
SELECT * FROM
{}
ORDER BY
utc DESC
LIMIT %s
"""
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)),
[
n
])
data
=
cur
.
fetchall
()
if
(
data
==
[]):
return
(
False
,
'There is no data for the device.'
)
else
:
return
(
True
,
data
)
@
with_psql
def
get_all
(
cur
,
appkey
,
devid
):
tn
=
'dev_'
+
str
(
appkey
)
+
'_'
+
str
(
dev_id
)
query
=
"""
SELECT * FROM
{}
"""
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,
cur
.
fetchall
())
dao/device/device.py
View file @
df4cbc69
...
...
@@ -36,6 +36,7 @@ class DeviceDao:
sql
.
SQL
(
"""CREATE TABLE {} (
utc NUMERIC(10) NOT NULL,
timedate VARCHAR(100) NOT NULL,
data json NOT NULL
)"""
).
format
(
sql
.
Identifier
(
tn
)))
...
...
dao/device/device.pyc
View file @
df4cbc69
No preview for this file type
server.py
View file @
df4cbc69
...
...
@@ -175,7 +175,7 @@ def dev():
dh
.
delete
(
session
[
'appkey'
],
request
.
form
[
'devid'
])
return
render_template
(
'add-dev.html'
,
feedback
=
res
[
1
])
else
:
return
redirect
(
url_for
(
'app'
))
return
redirect
(
url_for
(
'app'
,
appkey
=
session
[
'appkey'
]
))
@
server
.
route
(
'/dev-conf'
,
methods
=
[
'GET'
,
'POST'
])
...
...
@@ -206,6 +206,7 @@ def dev_conf():
return
redirect
(
url_for
(
'dev'
,
id
=
session
[
'devid'
]))
@
server
.
route
(
'/delete-dev'
)
def
delete_dev
():
dh
=
dd
.
DeviceDao
()
...
...
@@ -215,6 +216,10 @@ def delete_dev():
return
redirect
(
url_for
(
'app'
,
appkey
=
session
[
'appkey'
]))
@
server
.
route
(
'/dev-data'
)
def
dev_data
():
pass
if
__name__
==
'__main__'
:
server
.
secret_key
=
'sdjfklsjf^$654sd^#sPH'
server
.
run
(
debug
=
True
,
host
=
'0.0.0.0'
)
...
...
templates/dev-data.html
0 → 100644
View file @
df4cbc69
{% extends 'layout.html' %}
{% block title %} Device Data: {% endblock %}
{% block content %}
<div
class=
"row"
>
<div
class=
"col-md-6 col-md-offset-3"
>
<p>
Last 5 messages:
</p>
<table>
{% for data %}
<tr>
<th>
{{ data[1] }}
</th>
<th>
{{ data[2] }}
</th>
</tr>
{% endfor %}
</table>
<p>
Total {{ total }} messages.
</p>
<a
href=
"/data-csv"
><button
type=
"submit"
class=
"btn btn-primary"
>
Download CSV
</button></a>
</div>
</div>
<script
type=
"text/javascrypt"
>
function
conf
()
{
return
confirm
(
"
Are you sure? It will remove all device data.
"
)
}
</script>
{% endblock %}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment