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
1067f8e8
Commit
1067f8e8
authored
Apr 17, 2020
by
Vladislav Rykov
Browse files
device dao changed
parent
6b335653
Changes
3
Hide whitespace changes
Inline
Side-by-side
dao/device/device.py
View file @
1067f8e8
import
psycopg2
from
psycopg2
import
sql
import
bcrypt
from
misc
import
with_psql
@
with_psql
def
create_datatable
(
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
,)
class
DeviceDao
:
def
__init__
(
self
):
pass
# decorator implementation
def
with_psql
(
f
):
def
_with_psql
(
*
args
,
**
kwargs
):
conn
=
psycopg2
.
connect
(
'dbname=gateway'
)
cur
=
conn
.
cursor
()
@
with_psql
def
delete_datatable
(
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
create_table
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
cur
.
execute
(
sql
.
SQL
(
"""CREATE TABLE {} (
name VARCHAR(30) NOT NULL,
dev_id NUMERIC(3) PRIMARY KEY,
description VARCHAR(200)
)"""
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
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
@
staticmethod
@
with_psql
def
create_datatable
(
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
):
tn
=
'devices_'
+
str
(
appkey
)
cur
.
execute
(
psycopg2
.
sql
.
SQL
(
"DROP TABLE {}"
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
with_psql
def
create
(
cur
,
name
,
dev_id
,
appkey
,
desc
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
INSERT INTO
{}
VALUES
(%s, %s, %s)
"""
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)),
[
name
,
dev_id
,
desc
])
return
(
True
,)
@
with_psql
def
delete
(
cur
,
appkey
,
dev_id
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
DELETE FROM
{}
WHERE
dev_id = %s
"""
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)),
[
dev_id
])
return
(
True
,)
@
with_psql
def
get
(
cur
,
appkey
,
dev_id
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
SELECT * FROM
{}
WHERE
dev_id = %s
"""
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)),
[
dev_id
])
dev
=
cur
.
fetchone
()
@
staticmethod
@
with_psql
def
delete_datatable
(
cur
,
appkey
,
dev_id
):
tn
=
'dev_'
+
str
(
appkey
)
+
'_'
+
str
(
dev_id
)
cur
.
execute
(
psycopg2
.
sql
.
SQL
(
"DROP TABLE {}"
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
staticmethod
@
with_psql
def
create_table
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
cur
.
execute
(
sql
.
SQL
(
"""CREATE TABLE {} (
name VARCHAR(30) NOT NULL,
dev_id NUMERIC(3) PRIMARY KEY,
description VARCHAR(200)
)"""
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
staticmethod
@
with_psql
def
delete_table
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
cur
.
execute
(
psycopg2
.
sql
.
SQL
(
"DROP TABLE {}"
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,)
@
staticmethod
@
with_psql
def
create
(
cur
,
name
,
dev_id
,
appkey
,
desc
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
INSERT INTO
{}
VALUES
(%s, %s, %s)
"""
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)),
[
name
,
dev_id
,
desc
])
return
(
True
,)
@
staticmethod
@
with_psql
def
delete
(
cur
,
appkey
,
dev_id
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
DELETE FROM
{}
WHERE
dev_id = %s
"""
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)),
[
dev_id
])
return
(
True
,)
@
staticmethod
@
with_psql
def
get
(
cur
,
appkey
,
dev_id
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
SELECT * FROM
{}
WHERE
dev_id = %s
"""
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)),
[
dev_id
])
dev
=
cur
.
fetchone
()
if
(
dev
is
None
):
return
(
False
,
'There is no device with dev_id = {}'
.
format
(
dev_id
))
else
:
return
(
True
,
dev
)
@
staticmethod
@
with_psql
def
get_list
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
SELECT * FROM
{}
"""
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,
cur
.
fetchall
())
if
(
dev
is
None
):
return
(
False
,
'There is no device with dev_id = {}'
.
format
(
dev_id
))
else
:
return
(
True
,
dev
)
@
with_psql
def
get_list
(
cur
,
appkey
):
tn
=
'devices_'
+
str
(
appkey
)
query
=
"""
SELECT * FROM
{}
"""
cur
.
execute
(
sql
.
SQL
(
query
).
format
(
sql
.
Identifier
(
tn
)))
return
(
True
,
cur
.
fetchall
())
dao/device/device.pyc
View file @
1067f8e8
No preview for this file type
server.py
View file @
1067f8e8
...
...
@@ -98,12 +98,11 @@ def new_application():
def
app
():
if
'name'
in
session
:
if
request
.
method
==
'GET'
:
dh
=
dd
.
DeviceDao
()
session
[
'appkey'
]
=
request
.
args
.
get
(
'appkey'
)
app
=
ad
.
get
(
session
[
'appkey'
])
devs
=
d
h
.
get_list
(
app
[
1
][
1
])
devs
=
d
d
.
get_list
(
app
[
1
][
1
])
try
:
filelist
=
[
f
for
f
in
os
.
listdir
(
DATA_DOWNLOAD_DIR
)
if
f
.
startswith
(
session
[
'appkey'
])]
...
...
@@ -125,8 +124,7 @@ def app():
if
not
res
[
0
]:
return
render_template
(
'new-app.html'
,
feedback
=
res
[
1
])
dh
=
dd
.
DeviceDao
()
res
=
dh
.
create_table
(
appkey
)
res
=
dd
.
create_table
(
appkey
)
if
not
res
[
0
]:
ad
.
delete
(
appkey
)
...
...
@@ -139,13 +137,12 @@ def app():
@
server
.
route
(
'/delete-app'
)
def
delete_app
():
if
'name'
in
session
:
dh
=
dd
.
DeviceDao
()
devs
=
dh
.
get_list
(
session
[
'appkey'
])
devs
=
dd
.
get_list
(
session
[
'appkey'
])
for
dev
in
devs
[
1
]:
data
.
delete_table
(
session
[
'appkey'
],
dev
[
1
])
d
h
.
delete_table
(
session
[
'appkey'
])
d
d
.
delete_table
(
session
[
'appkey'
])
res
=
ad
.
delete
(
session
[
'appkey'
])
...
...
@@ -160,8 +157,7 @@ def delete_app():
@
server
.
route
(
'/add-dev'
)
def
new_dev
():
if
'name'
in
session
:
dh
=
dd
.
DeviceDao
()
dev_list
=
dh
.
get_list
(
session
[
'appkey'
])
dev_list
=
dd
.
get_list
(
session
[
'appkey'
])
#print('dev list : ', dev_list)
...
...
@@ -177,9 +173,8 @@ def new_dev():
@
server
.
route
(
'/dev'
,
methods
=
[
'GET'
,
'POST'
])
def
dev
():
if
'name'
in
session
:
dh
=
dd
.
DeviceDao
()
if
request
.
method
==
'GET'
:
dev
=
d
h
.
get
(
session
[
'appkey'
],
request
.
args
.
get
(
'id'
))
dev
=
d
d
.
get
(
session
[
'appkey'
],
request
.
args
.
get
(
'id'
))
session
[
'devid'
]
=
dev
[
1
][
1
]
session
[
'devname'
]
=
dev
[
1
][
0
]
...
...
@@ -193,7 +188,7 @@ def dev():
return
render_template
(
'dev.html'
,
dev
=
dev
[
1
],
appkey
=
session
[
'appkey'
],
ltup
=
ltup
)
else
:
res
=
d
h
.
create
(
request
.
form
[
'devname'
],
request
.
form
[
'devid'
],
session
[
'appkey'
],
request
.
form
[
'devdesc'
])
res
=
d
d
.
create
(
request
.
form
[
'devname'
],
request
.
form
[
'devid'
],
session
[
'appkey'
],
request
.
form
[
'devdesc'
])
if
not
res
[
0
]:
return
render_template
(
'add-dev.html'
,
feedback
=
res
[
1
])
...
...
@@ -201,7 +196,7 @@ def dev():
res
=
data
.
create_table
(
session
[
'appkey'
],
request
.
form
[
'devid'
])
if
not
res
[
0
]:
d
h
.
delete
(
session
[
'appkey'
],
request
.
form
[
'devid'
])
d
d
.
delete
(
session
[
'appkey'
],
request
.
form
[
'devid'
])
return
render_template
(
'add-dev.html'
,
feedback
=
res
[
1
])
else
:
return
redirect
(
url_for
(
'app'
,
appkey
=
session
[
'appkey'
]))
...
...
@@ -244,9 +239,8 @@ def dev_conf():
@
server
.
route
(
'/delete-dev'
)
def
delete_dev
():
if
'name'
in
session
and
'devid'
in
session
:
dh
=
dd
.
DeviceDao
()
data
.
delete_table
(
session
[
'appkey'
],
session
[
'devid'
])
res
=
d
h
.
delete
(
session
[
'appkey'
],
session
[
'devid'
])
res
=
d
d
.
delete
(
session
[
'appkey'
],
session
[
'devid'
])
return
redirect
(
url_for
(
'app'
,
appkey
=
session
[
'appkey'
]))
else
:
...
...
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