获取节点下所有设备的简单信息, id, name, classification_name

main
周平 11 months ago
parent 6a4100d612
commit 1bb705c5c4

@ -191,7 +191,43 @@ class EnterpriseDeviceRepository(object):
return {"devices": device_dicts, "total_count": total_count}
def list_simple_devices(self, node_id: int) -> list:
"""获取节点下所有设备的简单信息。"""
with get_session() as session:
try:
devices = (
session.query(
EnterpriseDevice,
DeviceClassification.name.label("classification_name"),
)
.join(
DeviceClassification,
EnterpriseDevice.classification == DeviceClassification.suid,
)
.filter(
EnterpriseDevice.node_id == node_id,
EnterpriseDevice.delete != 1,
)
.all()
)
except Exception as e:
logging.error("Failed to list devices")
raise e
device_dicts = []
for device in devices:
device_dict = {
"id": device.id,
"name": device.name,
"classification_name": device.classification_name,
}
device_dicts.append(device_dict)
return {"devices": device_dicts}
def get_device(self, device_id: int) -> dict:
"""
根据设备ID获取设备信息
"""
with get_session() as session:
try:
device = (
@ -219,6 +255,9 @@ class EnterpriseDeviceRepository(object):
return device_dict
def get_devices(self, device_ids: list) -> list:
"""
根据设备ID列表获取设备信息
"""
with get_session() as session:
try:
devices = (

@ -7,6 +7,7 @@ from website.handler import APIHandler, authenticated
from website.util import shortuuid
from website.db.enterprise_device import enterprise_device as DB_Device
class DeviceClassificationAddHandler(APIHandler):
"""
- 描述添加设备分类
@ -24,19 +25,22 @@ class DeviceClassificationAddHandler(APIHandler):
@authenticated
def post(self):
name = self.get_escaped_argument('name', '')
name = self.get_escaped_argument("name", "")
with self.app_mysql.connect() as conn:
cur = conn.execute(
text("SELECT id FROM device_classification WHERE name=:name"), {"name": name}
text("SELECT id FROM device_classification WHERE name=:name"),
{"name": name},
)
row = cur.fetchone()
if row:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '设备分类已存在')
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "设备分类已存在")
conn.execute(
text("INSERT INTO device_classification (name, suid) VALUES (:name, :suid)"),
{"name": name, "suid": shortuuid.ShortUUID().random(10)}
text(
"INSERT INTO device_classification (name, suid) VALUES (:name, :suid)"
),
{"name": name, "suid": shortuuid.ShortUUID().random(10)},
)
conn.commit()
self.finish()
@ -67,7 +71,9 @@ class DeviceClassificationHandler(APIHandler):
def post(self):
with self.app_mysql.connect() as conn:
cur = conn.execute(
text("SELECT id, suid, name FROM device_classification where del=0 ORDER BY id DESC")
text(
"SELECT id, suid, name FROM device_classification where del=0 ORDER BY id DESC"
)
)
res = db_mysql.to_json_list(cur)
res = res and res or []
@ -86,7 +92,7 @@ class DeviceClassificationDeleteHandler(APIHandler):
@authenticated
def post(self):
did = self.get_int_argument('id')
did = self.get_int_argument("id")
with self.app_mysql.connect() as conn:
conn.execute(
text("update device_classification set del=1 WHERE id=:id"), {"id": did}
@ -116,19 +122,19 @@ class DeviceAddHandler(APIHandler):
@authenticated
def post(self):
entity_id = self.get_int_argument('entity_id')
node_id = self.get_int_argument('node_id')
name = self.get_escaped_argument('name', '')
addr = self.get_escaped_argument('addr', '')
classification = self.get_escaped_argument('classification', '')
device_model = self.get_escaped_argument('device_model', '')
param = self.get_escaped_argument('param', '')
comment = self.get_escaped_argument('comment', '')
entity_id = self.get_int_argument("entity_id")
node_id = self.get_int_argument("node_id")
name = self.get_escaped_argument("name", "")
addr = self.get_escaped_argument("addr", "")
classification = self.get_escaped_argument("classification", "")
device_model = self.get_escaped_argument("device_model", "")
param = self.get_escaped_argument("param", "")
comment = self.get_escaped_argument("comment", "")
if not name:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '设备名称不能为空')
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "设备名称不能为空")
if not entity_id or not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点不能为空')
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "企业节点不能为空")
device_data = {
"entity_id": entity_id,
@ -138,7 +144,7 @@ class DeviceAddHandler(APIHandler):
"classification": classification,
"device_model": device_model,
"param": param,
"comment": comment
"comment": comment,
}
db_device = DB_Device.EnterpriseDeviceRepository()
@ -164,17 +170,17 @@ class DeviceEditHandler(APIHandler):
@authenticated
def post(self):
device_id = self.get_int_argument('device_id')
name = self.get_escaped_argument('name', '')
addr = self.get_escaped_argument('addr', '')
classification = self.get_escaped_argument('classification', '')
device_model = self.get_escaped_argument('device_model', '')
param = self.get_escaped_argument('param', '')
comment = self.get_escaped_argument('comment', '')
device_id = self.get_int_argument("device_id")
name = self.get_escaped_argument("name", "")
addr = self.get_escaped_argument("addr", "")
classification = self.get_escaped_argument("classification", "")
device_model = self.get_escaped_argument("device_model", "")
param = self.get_escaped_argument("param", "")
comment = self.get_escaped_argument("comment", "")
if not device_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '设备id不能为空')
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "设备id不能为空")
if not name:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '设备名称不能为空')
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "设备名称不能为空")
device_data = {
"id": device_id,
@ -183,7 +189,7 @@ class DeviceEditHandler(APIHandler):
"classification": classification,
"device_model": device_model,
"param": param,
"comment": comment
"comment": comment,
}
db_device = DB_Device.EnterpriseDeviceRepository()
@ -206,9 +212,11 @@ class DeviceDeleteHandler(APIHandler):
@authenticated
def post(self):
# node_id = self.get_int_argument('node_id')
device_id = self.get_int_argument('device_id')
device_id = self.get_int_argument("device_id")
if not device_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点或设备不能为空')
raise errors.HTTPAPIError(
errors.ERROR_BAD_REQUEST, "企业节点或设备不能为空"
)
db_device = DB_Device.EnterpriseDeviceRepository()
db_device.delete_device(device_id)
self.finish()
@ -241,19 +249,35 @@ class DeviceListHandler(APIHandler):
@authenticated
def post(self):
node_id = self.get_int_argument('node_id')
pageNo = self.get_int_argument('pageNo', 1)
pageSize = self.get_int_argument('pageSize', 20)
node_id = self.get_int_argument("node_id")
pageNo = self.get_int_argument("pageNo", 1)
pageSize = self.get_int_argument("pageSize", 20)
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点不能为空')
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "企业节点不能为空")
db_device = DB_Device.EnterpriseDeviceRepository()
devices = db_device.list_devices(node_id=node_id, pageNo=pageNo, pageSize=pageSize)
devices = db_device.list_devices(
node_id=node_id, pageNo=pageNo, pageSize=pageSize
)
logging.info(devices)
self.finish({"count": devices["total_count"], "data": devices["devices"]})
class DeviceListSimpleHandler(APIHandler):
"""获取节点下所有设备的简单信息"""
@authenticated
def post(self):
node_id = self.get_int_argument("node_id")
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "企业节点不能为空")
db_device = DB_Device.EnterpriseDeviceRepository()
devices = db_device.list_simple_devices(node_id=node_id)
logging.info(devices)
self.finish({"data": devices})
class DeviceInfoHandler(APIHandler):
"""
- 描述企业节点设备信息
@ -272,16 +296,14 @@ class DeviceInfoHandler(APIHandler):
}
```
"""
def post(self):
device_id = self.get_int_argument('device_id')
device_id = self.get_int_argument("device_id")
if not device_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点或设备不能为空')
raise errors.HTTPAPIError(
errors.ERROR_BAD_REQUEST, "企业节点或设备不能为空"
)
db_device = DB_Device.EnterpriseDeviceRepository()
device = db_device.get_device(device_id=device_id)
logging.info(device)
self.finish(device)

Loading…
Cancel
Save