175 lines
5.9 KiB
Python
175 lines
5.9 KiB
Python
#!/opt/homebrew/bin/python3
|
|
# -*- coding:utf-8 -*-
|
|
import json
|
|
import uuid
|
|
from PyQt6 import *
|
|
from PyQt6.QtCore import *
|
|
from logs import log
|
|
from typing import Union
|
|
from models import Device, Session
|
|
|
|
class DeviceManager(QObject):
|
|
|
|
def __init__(self, parent=None):
|
|
super().__init__()
|
|
|
|
@pyqtSlot(str,str,QVariant,list, result=str)
|
|
def create(self, group_id,name, devModelId, attr, interface):
|
|
if not isinstance(attr, dict):
|
|
attr = attr.toVariant()
|
|
data = {
|
|
"id": str(uuid.uuid4()),
|
|
"group_id": group_id,
|
|
"name": name,
|
|
"dev_model_id": devModelId,
|
|
"attr": json.dumps(attr),
|
|
"interface": json.dumps(interface)
|
|
}
|
|
ok, res = Session.addByClass(Device, data)
|
|
if ok:
|
|
return str(res["id"])
|
|
return ""
|
|
@pyqtSlot(QVariant, result=str)
|
|
def create_from_json(self, param):
|
|
if not isinstance(param, dict):
|
|
param = param.toVariant()
|
|
data = {
|
|
"id": param["id"],
|
|
"name": param["name"],
|
|
"group_id": param['group_id'],
|
|
"dev_model_id": param['dev_model_id'],
|
|
"attr": json.dumps(param["attr"]),
|
|
"interface": json.dumps(param["interface"])
|
|
}
|
|
ok, res = Session.addByClass(Device, data)
|
|
if ok:
|
|
return str(res["id"])
|
|
return ""
|
|
|
|
@pyqtSlot(QVariant, result=str)
|
|
def create_from_git_json(self, param):
|
|
if not isinstance(param, dict):
|
|
param = param.toVariant()
|
|
data = {
|
|
"id": param["id"],
|
|
"name": param["name"],
|
|
"group_id": param['group_id'],
|
|
"dev_model_id": param['dev_model_id'],
|
|
"attr": json.dumps(param["attr"]),
|
|
"interface": json.dumps(param["interface"])
|
|
}
|
|
ok, res = Session.addByClassFromGit(Device, data)
|
|
if ok:
|
|
return str(res["id"])
|
|
return ""
|
|
|
|
@pyqtSlot(str, result=bool)
|
|
def createCopy(self, id, group_id = None):
|
|
ok,res = Session.queryById(Device, id)
|
|
if ok:
|
|
res["attr"] = json.loads(res["attr"])
|
|
res["interface"] = json.loads(res["interface"])
|
|
if group_id == None:
|
|
res["name"] = res["name"] + "_副本"
|
|
group_id = res["group_id"]
|
|
|
|
copy_id = self.create( group_id, res["name"], res["dev_model_id"], res["attr"], res["interface"])
|
|
if copy_id != "":
|
|
return True
|
|
return False
|
|
|
|
@pyqtSlot(str, QVariant, result=bool)
|
|
def update(self, id, param=None):
|
|
if not isinstance(param, dict):
|
|
param = param.toVariant()
|
|
data = {}
|
|
if "name" in param:
|
|
data["name"] = param["name"]
|
|
if "group_id" in param:
|
|
data["group_id"] = param["group_id"]
|
|
if "attr" in param:
|
|
data["attr"] = json.dumps(param["attr"])
|
|
if "interface" in param:
|
|
data["interface"] = json.dumps( param["interface"] )
|
|
ok,res = Session.updateById(Device, id, data)
|
|
return ok
|
|
|
|
@pyqtSlot(str, result=bool)
|
|
@pyqtSlot(list, result=bool)
|
|
def delete(self, id):
|
|
if isinstance(id, str):
|
|
ok,res = Session.deleteById(Device, id)
|
|
return ok
|
|
else:
|
|
ok,res = Session.deleteByIds(Device, id)
|
|
return ok
|
|
|
|
@pyqtSlot(str, result=QVariant)
|
|
@pyqtSlot(str, result=list)
|
|
def getInfo(self, id = 'all'):
|
|
if id == 'all':
|
|
ok,res = Session.queryByAll(Device)
|
|
if ok:
|
|
for data in res:
|
|
if "attr" in data:
|
|
data["attr_str"] = data["attr"] or ""
|
|
data["attr"] = data["attr"] and json.loads(data["attr"]) or {}
|
|
if "interface" in data:
|
|
data["interface"] = data["interface"] and json.loads(data["interface"]) or []
|
|
return res
|
|
else:
|
|
return []
|
|
else:
|
|
ok,res = Session.queryById(Device, id)
|
|
if ok:
|
|
if "attr" in res:
|
|
res["attr_str"] = res["attr"] or ""
|
|
res["attr"] = res["attr"] and json.loads(res["attr"]) or {}
|
|
if "interface" in res:
|
|
res["interface"] = res["interface"] and json.loads(res["interface"]) or []
|
|
return res
|
|
else:
|
|
return None
|
|
|
|
|
|
# 获取所属组设备列表
|
|
@pyqtSlot(str, result=QVariant)
|
|
def getGroupDevices(self, group_id):
|
|
ok, res = Session.queryByGroupId(Device, group_id)
|
|
if ok:
|
|
for data in res:
|
|
if "attr" in data:
|
|
data["attr_str"] = data["attr"] or ""
|
|
data["attr"] = data["attr"] and json.loads(data["attr"]) or {}
|
|
return res
|
|
else:
|
|
return []
|
|
|
|
@pyqtSlot(str, result=QVariant)
|
|
@pyqtSlot(str, result=list)
|
|
def getOriginalInfo(self, id = 'all'):
|
|
if id == 'all':
|
|
ok,res = Session.queryByAll(Device)
|
|
if ok:
|
|
for data in res:
|
|
if "attr" in data:
|
|
data["attr"] = data["attr"] and json.loads(data["attr"]) or {}
|
|
if "interface" in data:
|
|
data["interface"] = data["interface"] and json.loads(data["interface"]) or []
|
|
return res
|
|
else:
|
|
return []
|
|
else:
|
|
ok,res = Session.queryById(Device, id)
|
|
if ok:
|
|
if "attr" in res:
|
|
res["attr"] = res["attr"] and json.loads(res["attr"]) or {}
|
|
if "interface" in res:
|
|
res["interface"] = res["interface"] and json.loads(res["interface"]) or []
|
|
return res
|
|
else:
|
|
return None
|
|
deviceManager = DeviceManager()
|
|
|
|
|