Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MyEMS
MyEMS能源管理系统
提交
f58582db
M
MyEMS能源管理系统
项目概览
MyEMS
/
MyEMS能源管理系统
通知
46
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
MyEMS能源管理系统
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f58582db
编写于
2月 06, 2024
作者:
nengyuangzhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
udpated sensor export, import and clone functions to myems-api
上级
f2d278d1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
79 addition
and
26 deletion
+79
-26
myems-api/MyEMS.postman_collection.json
myems-api/MyEMS.postman_collection.json
+3
-3
myems-api/core/sensor.py
myems-api/core/sensor.py
+76
-23
未找到文件。
myems-api/MyEMS.postman_collection.json
浏览文件 @
f58582db
{
"info": {
"_postman_id": "f
28f778d-45d4-4e43-901c-1f5f3536cd9b
",
"_postman_id": "f
7b17d56-68f2-4e2a-a174-f96b40d0f8ff
",
"name": "MyEMS",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "10906958"
...
...
@@ -14486,7 +14486,7 @@
},
{
"key": "Token",
"value": "
c5f872b51ed40ccf55f1c1d6dbd8cb86eefba5d1010e23b2386bd82be431f3eafc0e3360dee18a5327d9e9852e3cf7caad3b81e252f9f311790c22f7a62a90e1
",
"value": "
d5cca8934b9dfb63ae6a621519c582f539af9cbb110450086c584b39645ef2d844c4b1a069d19b9274c77b4d9e853dfbefeb8798be41fc348db056b041841bba
",
"type": "text",
"description": "Login first to get a valid token"
},
...
...
@@ -14500,7 +14500,7 @@
],
"body": {
"mode": "raw",
"raw": "{\"name\": \"传感器123\", \"description\": \"sensor description\"}",
"raw": "{\"name\": \"传感器123\", \"description\": \"sensor description\"
, \n\"points\": [{\"id\": 1, \"name\": \"Active Energy Import Tariff 1\"},\n {\"id\": 2, \"name\": \"Working hours counter\"}]
}",
"options": {
"raw": {
"language": "json"
myems-api/core/sensor.py
浏览文件 @
f58582db
import
uuid
from
datetime
import
datetime
,
time
zone
,
time
delta
from
datetime
import
datetime
,
timedelta
import
falcon
import
mysql.connector
import
simplejson
as
json
...
...
@@ -477,6 +477,7 @@ class SensorPointItem:
resp
.
status
=
falcon
.
HTTP_204
class
SensorExport
:
@
staticmethod
def
__init__
():
...
...
@@ -506,8 +507,6 @@ class SensorExport:
" WHERE id = %s "
)
cursor
.
execute
(
query
,
(
id_
,))
row
=
cursor
.
fetchone
()
cursor
.
close
()
cnx
.
close
()
if
row
is
None
:
raise
falcon
.
HTTPError
(
status
=
falcon
.
HTTP_404
,
title
=
'API.NOT_FOUND'
,
...
...
@@ -516,10 +515,25 @@ class SensorExport:
meta_result
=
{
"id"
:
row
[
0
],
"name"
:
row
[
1
],
"uuid"
:
row
[
2
],
"description"
:
row
[
3
]}
"description"
:
row
[
3
],
"points"
:
None
}
query
=
(
" SELECT p.id, p.name "
" FROM tbl_points p, tbl_sensors_points sp "
" WHERE sp.sensor_id = %s AND p.id = sp.point_id "
" ORDER BY p.id "
)
cursor
.
execute
(
query
,
(
id_
,))
rows
=
cursor
.
fetchall
()
result
=
list
()
if
rows
is
not
None
and
len
(
rows
)
>
0
:
for
row
in
rows
:
point_result
=
{
"id"
:
row
[
0
],
"name"
:
row
[
1
]}
result
.
append
(
point_result
)
meta_result
[
'points'
]
=
result
cursor
.
close
()
cnx
.
close
()
resp
.
text
=
json
.
dumps
(
meta_result
)
class
SensorImport
:
@
staticmethod
def
__init__
():
...
...
@@ -577,6 +591,26 @@ class SensorImport:
str
(
uuid
.
uuid4
()),
description
))
new_id
=
cursor
.
lastrowid
if
'points'
in
new_values
.
keys
()
and
\
new_values
[
'points'
]
is
not
None
and
\
len
(
new_values
[
'points'
])
>
0
:
for
point
in
new_values
[
'points'
]:
if
'id'
in
point
and
isinstance
(
point
[
'id'
],
int
):
cursor
.
execute
(
" SELECT name "
" FROM tbl_points "
" WHERE id = %s "
,
(
point
[
'id'
],))
if
cursor
.
fetchone
()
is
None
:
cursor
.
close
()
cnx
.
close
()
raise
falcon
.
HTTPError
(
status
=
falcon
.
HTTP_404
,
title
=
'API.NOT_FOUND'
,
description
=
'API.POINT_NOT_FOUND'
)
add_row
=
(
" INSERT INTO tbl_sensors_points (sensor_id, point_id) "
" VALUES (%s, %s) "
)
cursor
.
execute
(
add_row
,
(
new_id
,
point
[
'id'
],))
else
:
raise
falcon
.
HTTPError
(
status
=
falcon
.
HTTP_400
,
title
=
'API.NOT_FOUND'
,
description
=
'API.INVALID_POINT_ID'
)
cnx
.
commit
()
cursor
.
close
()
cnx
.
close
()
...
...
@@ -584,6 +618,7 @@ class SensorImport:
resp
.
status
=
falcon
.
HTTP_201
resp
.
location
=
'/sensors/'
+
str
(
new_id
)
class
SensorClone
:
@
staticmethod
...
...
@@ -611,18 +646,6 @@ class SensorClone:
cnx
=
mysql
.
connector
.
connect
(
**
config
.
myems_system_db
)
cursor
=
cnx
.
cursor
()
query
=
(
" SELECT id, name, uuid "
" FROM tbl_meters "
)
cursor
.
execute
(
query
)
rows_master_sensors
=
cursor
.
fetchall
()
master_meter_dict
=
dict
()
if
rows_master_sensors
is
not
None
and
len
(
rows_master_sensors
)
>
0
:
for
row
in
rows_master_sensors
:
master_meter_dict
[
row
[
0
]]
=
{
"id"
:
row
[
0
],
"name"
:
row
[
1
],
"uuid"
:
row
[
2
]}
query
=
(
" SELECT id, name, uuid, description "
" FROM tbl_sensors "
" WHERE id = %s "
)
...
...
@@ -634,16 +657,31 @@ class SensorClone:
raise
falcon
.
HTTPError
(
status
=
falcon
.
HTTP_404
,
title
=
'API.NOT_FOUND'
,
description
=
'API.SENSOR_NOT_FOUND'
)
else
:
meta_result
=
{
"id"
:
row
[
0
],
"name"
:
row
[
1
],
"uuid"
:
row
[
2
],
"description"
:
row
[
3
]}
meta_result
=
{
"id"
:
row
[
0
],
"name"
:
row
[
1
],
"uuid"
:
row
[
2
],
"description"
:
row
[
3
],
"points"
:
None
}
query
=
(
" SELECT p.id, p.name "
" FROM tbl_points p, tbl_sensors_points sp "
" WHERE sp.sensor_id = %s AND p.id = sp.point_id "
" ORDER BY p.id "
)
cursor
.
execute
(
query
,
(
id_
,))
rows
=
cursor
.
fetchall
()
result
=
list
()
if
rows
is
not
None
and
len
(
rows
)
>
0
:
for
row
in
rows
:
point_result
=
{
"id"
:
row
[
0
],
"name"
:
row
[
1
]}
result
.
append
(
point_result
)
meta_result
[
'points'
]
=
result
timezone_offset
=
int
(
config
.
utc_offset
[
1
:
3
])
*
60
+
int
(
config
.
utc_offset
[
4
:
6
])
if
config
.
utc_offset
[
0
]
==
'-'
:
timezone_offset
=
-
timezone_offset
new_name
=
str
.
strip
(
meta_result
[
'name'
])
+
\
(
datetime
.
now
()
+
timedelta
(
minutes
=
timezone_offset
)).
isoformat
(
sep
=
'-'
,
timespec
=
'seconds'
)
new_name
=
(
str
.
strip
(
meta_result
[
'name'
])
+
(
datetime
.
now
()
+
timedelta
(
minutes
=
timezone_offset
)).
isoformat
(
sep
=
'-'
,
timespec
=
'seconds'
))
add_values
=
(
" INSERT INTO tbl_sensors "
" (name, uuid, description) "
...
...
@@ -652,6 +690,21 @@ class SensorClone:
str
(
uuid
.
uuid4
()),
meta_result
[
'description'
]))
new_id
=
cursor
.
lastrowid
if
len
(
meta_result
[
'points'
])
>
0
:
for
point
in
meta_result
[
'points'
]:
cursor
.
execute
(
" SELECT name "
" FROM tbl_points "
" WHERE id = %s "
,
(
point
[
'id'
],))
if
cursor
.
fetchone
()
is
None
:
cursor
.
close
()
cnx
.
close
()
raise
falcon
.
HTTPError
(
status
=
falcon
.
HTTP_404
,
title
=
'API.NOT_FOUND'
,
description
=
'API.POINT_NOT_FOUND'
)
add_row
=
(
" INSERT INTO tbl_sensors_points (sensor_id, point_id) "
" VALUES (%s, %s) "
)
cursor
.
execute
(
add_row
,
(
new_id
,
point
[
'id'
],))
cnx
.
commit
()
cursor
.
close
()
cnx
.
close
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录