diff --git a/website/db/device_classification.py b/website/db/device_classification.py new file mode 100644 index 0000000..489e0d3 --- /dev/null +++ b/website/db/device_classification.py @@ -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()) \ No newline at end of file diff --git a/website/db/enterprise_device.py b/website/db/enterprise_device.py index 062e091..6334df1 100644 --- a/website/db/enterprise_device.py +++ b/website/db/enterprise_device.py @@ -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) - - new_device = EnterpriseDevice(**device) + name = device["name"] + 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 + \ No newline at end of file diff --git a/website/handlers/enterprise_device/handler.py b/website/handlers/enterprise_device/handler.py index 7119f3a..5ff75d9 100644 --- a/website/handlers/enterprise_device/handler.py +++ b/website/handlers/enterprise_device/handler.py @@ -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) + diff --git a/website/handlers/enterprise_device/url.py b/website/handlers/enterprise_device/url.py index fa9f702..0bad0b5 100644 --- a/website/handlers/enterprise_device/url.py +++ b/website/handlers/enterprise_device/url.py @@ -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 = [