完成设备功能自测

main
周平 12 months ago
parent a2abb09955
commit 48c14a9651

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime, func
Base = declarative_base()
"""
设备分类表
"""
class DeviceClassification(Base):
__tablename__ = 'device_classification'
id = Column(Integer, primary_key=True)
name = Column(String(255), default='', comment='名称')
suid = Column(String(10), default='', comment='short uuid')
delete = Column("del", Integer, default=0)
create_time = Column(DateTime, default=func.now())

@ -5,14 +5,26 @@ import logging
from sqlalchemy import Column, Integer, String, DateTime, func
from sqlalchemy.ext.declarative import declarative_base
from website import errors
from website.db_mysql import get_session
from website.util import shortuuid
from website.db.enterprise_entity import EnterpriseEntityDB
from website.db.enterprise_node import EnterpriseNodeDB
from website.db.device_classification import DeviceClassification
def row2dict(row):
d = {}
for column in row.__table__.columns:
d[column.name] = str(getattr(row, column.name))
return d
Base = declarative_base()
Base = declarative_base()
"""
企业设备表
"""
class EnterpriseDevice(Base):
__tablename__ = 'enterprise_device'
@ -44,9 +56,17 @@ class EnterpriseDeviceDB(object):
device["entity_suid"] = entity_suid
device["node_suid"] = node_suid
device["suid"] = shortuuid.ShortUUID().random(10)
name = device["name"]
new_device = EnterpriseDevice(**device)
with get_session() as session:
existing_device = session.query(EnterpriseDevice) \
.filter_by(node_id=node_id, name=name, delete=0) \
.first()
if existing_device:
logging.error(f"Failed to add device: device with node_id={node_id} and name={name} already exists")
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "设备已存在")
new_device = EnterpriseDevice(**device)
try:
# session.execute(
# text(
@ -83,14 +103,75 @@ class EnterpriseDeviceDB(object):
return
def list_devices(self, node_id, pageNo, pageSize):
def list_devices(self, node_id: int, pageNo: int, pageSize: int) -> dict:
with get_session() as session:
try:
devices = session.query(EnterpriseDevice) \
total_count = session.query(EnterpriseDevice) \
.filter(EnterpriseDevice.node_id == node_id, EnterpriseDevice.delete != 1) \
.order_by(EnterpriseDevice.create_time.desc()).limit(pageSize).offset((pageNo - 1) * pageSize).all()
.count()
devices = session.query(EnterpriseDevice,
DeviceClassification.name.label("classification_name")) \
.join(DeviceClassification, EnterpriseDevice.classification == DeviceClassification.suid) \
.filter(EnterpriseDevice.node_id == node_id, EnterpriseDevice.delete != 1) \
.order_by(EnterpriseDevice.id.desc()) \
.limit(pageSize) \
.offset((pageNo - 1) * pageSize) \
.all()
except Exception as e:
logging.error("Failed to list devices")
raise e
return devices
device_dicts = []
for device, classification_name in devices:
device_dict = {
"id": device.id,
"suid": device.suid,
"entity_id": device.entity_id,
"entity_suid": device.entity_suid,
"node_id": device.node_id,
"node_suid": device.node_suid,
"classification": classification_name,
"name": device.name,
"addr": device.addr,
"device_model": device.device_model,
"param": device.param,
"comment": device.comment,
"delete": device.delete,
"create_time": device.create_time.strftime('%Y-%m-%d %H:%M:%S'),
"update_time": str(device.update_time)
}
device_dicts.append(device_dict)
# for row in devices:
# logging.info(row.name)
logging.info(device_dicts)
return {
"devices": device_dicts,
"total_count": total_count
}
def get_device(self, device_id: int) -> dict:
with get_session() as session:
try:
device = session.query(EnterpriseDevice) \
.filter(EnterpriseDevice.id == device_id, EnterpriseDevice.delete != 1) \
.first()
except Exception as e:
logging.error("Failed to get device")
raise e
device_dict = {}
if device:
device_dict = {
"name": device.name,
"addr": device.addr,
"device_model": device.device_model,
"param": device.param,
"comment": device.comment,
"classification": device.classification
}
return device_dict

@ -56,6 +56,7 @@ class DeviceClassificationHandler(APIHandler):
"data": [
{
"id": 123,
"suid": "xxx",
"name": "xxx"
},
...
@ -68,7 +69,7 @@ class DeviceClassificationHandler(APIHandler):
def post(self):
with self.app_mysql.connect() as conn:
cur = conn.execute(
text("SELECT id, 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 []
@ -206,9 +207,9 @@ class DeviceDeleteHandler(APIHandler):
@authenticated
def post(self):
node_id = self.get_int_argument('node_id')
# node_id = self.get_int_argument('node_id')
device_id = self.get_int_argument('device_id')
if not node_id or not device_id:
if not device_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点或设备不能为空')
db_device = DB_Device.EnterpriseDeviceDB()
db_device.delete_device(device_id)
@ -248,5 +249,37 @@ class DeviceListHandler(APIHandler):
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点不能为空')
db_device = DB_Device.EnterpriseDeviceDB()
data = db_device.list_devices(node_id=node_id, pageNo=pageNo, pageSize=pageSize)
self.finish()
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 DeviceInfoHandler(APIHandler):
"""
- 描述企业节点设备信息
- 请求方式post
- 请求参数
> - device_id, int, 设备id
- 返回值
```
{
"name": "xxx",
"addr": "xxx",
"classification": "xxx",
"device_model": "xxx",
"param": "xxx",
"comment": "xxx",
}
```
"""
def post(self):
device_id = self.get_int_argument('device_id')
if not device_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点或设备不能为空')
db_device = DB_Device.EnterpriseDeviceDB()
device = db_device.get_device(device_id=device_id)
logging.info(device)
self.finish(device)

@ -12,6 +12,7 @@ handlers = [
("/enterprise/entity/nodes/device/edit", handler.DeviceEditHandler),
("/enterprise/entity/nodes/device/delete", handler.DeviceDeleteHandler),
("/enterprise/entity/nodes/device/list", handler.DeviceListHandler),
("/enterprise/entity/nodes/device/info", handler.DeviceInfoHandler),
]
page_handlers = [

Loading…
Cancel
Save