2025-11-11 21:39:12 +08:00
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
from app import db
|
|
|
|
|
|
|
|
|
|
|
|
class Device(db.Model):
|
|
|
|
|
|
"""设备模型"""
|
|
|
|
|
|
__tablename__ = 'device'
|
|
|
|
|
|
|
|
|
|
|
|
device_id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
machine_code = db.Column(db.String(64), unique=True, nullable=False, index=True)
|
|
|
|
|
|
license_id = db.Column(db.Integer, db.ForeignKey('license.license_id'), nullable=True)
|
|
|
|
|
|
product_id = db.Column(db.String(32), db.ForeignKey('product.product_id'), nullable=False)
|
|
|
|
|
|
software_version = db.Column(db.String(16), nullable=True)
|
2025-11-19 22:49:24 +08:00
|
|
|
|
ip_address = db.Column(db.String(45), nullable=True) # 添加IP地址字段
|
2025-11-11 21:39:12 +08:00
|
|
|
|
status = db.Column(db.Integer, nullable=False, default=1) # 0=禁用, 1=正常
|
|
|
|
|
|
activate_time = db.Column(db.DateTime, nullable=True)
|
|
|
|
|
|
last_verify_time = db.Column(db.DateTime, nullable=True)
|
|
|
|
|
|
create_time = db.Column(db.DateTime, default=datetime.utcnow)
|
|
|
|
|
|
update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
|
super(Device, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def is_active(self):
|
|
|
|
|
|
"""设备是否激活"""
|
|
|
|
|
|
return self.status == 1
|
|
|
|
|
|
|
|
|
|
|
|
def is_online(self, days=7):
|
|
|
|
|
|
"""设备是否在线(最近N天内有验证记录)"""
|
|
|
|
|
|
if not self.last_verify_time:
|
|
|
|
|
|
return False
|
|
|
|
|
|
return (datetime.utcnow() - self.last_verify_time).days <= days
|
|
|
|
|
|
|
|
|
|
|
|
def activate(self, license_id, software_version):
|
|
|
|
|
|
"""激活设备"""
|
|
|
|
|
|
self.license_id = license_id
|
|
|
|
|
|
self.software_version = software_version
|
|
|
|
|
|
self.status = 1
|
|
|
|
|
|
self.activate_time = datetime.utcnow()
|
|
|
|
|
|
self.last_verify_time = datetime.utcnow()
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def deactivate(self):
|
|
|
|
|
|
"""禁用设备"""
|
|
|
|
|
|
self.status = 0
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def update_verify_time(self):
|
|
|
|
|
|
"""更新验证时间"""
|
|
|
|
|
|
self.last_verify_time = datetime.utcnow()
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def get_license_info(self):
|
|
|
|
|
|
"""获取关联的卡密信息"""
|
|
|
|
|
|
if self.license:
|
|
|
|
|
|
return {
|
|
|
|
|
|
'license_key': self.license.license_key,
|
|
|
|
|
|
'type': self.license.type,
|
|
|
|
|
|
'type_name': '试用' if self.license.type == 0 else '正式',
|
|
|
|
|
|
'expire_time': self.license.expire_time.strftime('%Y-%m-%d %H:%M:%S') if self.license.expire_time else None
|
|
|
|
|
|
}
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_uptime_days(self):
|
|
|
|
|
|
"""获取使用天数"""
|
|
|
|
|
|
if not self.activate_time:
|
|
|
|
|
|
return 0
|
|
|
|
|
|
return (datetime.utcnow() - self.activate_time).days
|
|
|
|
|
|
|
|
|
|
|
|
def to_dict(self, include_license=False):
|
|
|
|
|
|
"""转换为字典"""
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'device_id': self.device_id,
|
|
|
|
|
|
'machine_code': self.machine_code,
|
|
|
|
|
|
'license_id': self.license_id,
|
|
|
|
|
|
'license_key': self.license.license_key if self.license else None,
|
|
|
|
|
|
'product_id': self.product_id,
|
|
|
|
|
|
'product_name': self.product.product_name if self.product else None,
|
|
|
|
|
|
'software_version': self.software_version,
|
2025-11-19 22:49:24 +08:00
|
|
|
|
'ip_address': self.ip_address, # 添加IP地址字段
|
2025-11-11 21:39:12 +08:00
|
|
|
|
'status': self.status,
|
|
|
|
|
|
'status_name': '正常' if self.status == 1 else '禁用',
|
|
|
|
|
|
'is_online': self.is_online(),
|
|
|
|
|
|
'activate_time': self.activate_time.strftime('%Y-%m-%d %H:%M:%S') if self.activate_time else None,
|
|
|
|
|
|
'last_verify_time': self.last_verify_time.strftime('%Y-%m-%d %H:%M:%S') if self.last_verify_time else None,
|
|
|
|
|
|
'uptime_days': self.get_uptime_days(),
|
|
|
|
|
|
'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
|
|
'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if include_license:
|
|
|
|
|
|
data['license_info'] = self.get_license_info()
|
|
|
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
|
return f'<Device {self.machine_code}>'
|