提交 f58582db 编写于 作者: nengyuangzhang's avatar nengyuangzhang

udpated sensor export, import and clone functions to myems-api

上级 f2d278d1
{
"info": {
"_postman_id": "f28f778d-45d4-4e43-901c-1f5f3536cd9b",
"_postman_id": "f7b17d56-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"
import uuid
from datetime import datetime, timezone, timedelta
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.
先完成此消息的编辑!
想要评论请 注册