You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.3 KiB
Python

12 months ago
# -*- coding: utf-8 -*-
import logging
12 months ago
from sqlalchemy import Column, Integer, String, DateTime, func
12 months ago
from sqlalchemy.ext.declarative import declarative_base
12 months ago
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
12 months ago
Base = declarative_base()
class EnterpriseDevice(Base):
__tablename__ = 'enterprise_device'
id = Column(Integer, primary_key=True)
12 months ago
suid = Column(String(length=10), default="")
12 months ago
entity_id = Column(Integer)
entity_suid = Column(String)
node_id = Column(Integer)
node_suid = Column(String)
classification = Column(String)
name = Column(String)
addr = Column(String)
device_model = Column(String)
param = Column(String)
comment = Column(String)
delete = Column("del", Integer, default=0)
12 months ago
create_time = Column(DateTime, default=func.now())
update_time = Column(DateTime, default=func.now())
12 months ago
class EnterpriseDeviceDB(object):
def add_device(self, device):
entity_id = device["entity_id"]
node_id = device["node_id"]
entity_suid = EnterpriseEntityDB().get_entity_suid(entity_id)
node_suid = EnterpriseNodeDB().get_node_uid(node_id)
device["entity_suid"] = entity_suid
device["node_suid"] = node_suid
12 months ago
device["suid"] = shortuuid.ShortUUID().random(10)
12 months ago
new_device = EnterpriseDevice(**device)
with get_session() as session:
try:
# session.execute(
# text(
# """INSERT INTO enterprise_device
# (suid, entity_id, entity_suid, node_id, node_suid, classification, name, addr, device_model, param, comment)
# values (:suid, :entity_id, :entity_suid, :node_id, :node_suid, :classification, :name, :addr, :device_model, :param, :comment)"""
# ),
# device)
session.add(new_device)
except Exception as e:
logging.error("Failed to add device")
raise e
return
def edit_device(self, device):
device_id = device["id"]
with get_session() as session:
try:
session.query(EnterpriseDevice).filter(EnterpriseDevice.id == device_id).update(device)
except Exception as e:
logging.error("Failed to edit device")
raise e
return
def delete_device(self, device_id):
with get_session() as session:
try:
session.query(EnterpriseDevice).filter(EnterpriseDevice.id == device_id).update({"delete": 1})
except Exception as e:
logging.error("Failed to delete device")
raise e
return
def list_devices(self, node_id, pageNo, pageSize):
with get_session() as session:
try:
devices = 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()
except Exception as e:
logging.error("Failed to list devices")
raise e
return devices
12 months ago