165 lines
5.2 KiB
Python
165 lines
5.2 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 instructionModel.instructionManager import instructionManager
|
||
|
|
from models import DevModel, Session
|
||
|
|
|
||
|
|
class DevModelManager(QObject):
|
||
|
|
|
||
|
|
def __init__(self, parent=None):
|
||
|
|
super().__init__()
|
||
|
|
|
||
|
|
@pyqtSlot(str,str,QVariant, result=str)
|
||
|
|
def create(self, group_id, name, category, attr):
|
||
|
|
if not isinstance(attr, dict):
|
||
|
|
attr = attr.toVariant()
|
||
|
|
data = {
|
||
|
|
"id": str(uuid.uuid4()),
|
||
|
|
"group_id": group_id,
|
||
|
|
"name": name,
|
||
|
|
"category": category,
|
||
|
|
"attr": json.dumps(attr)
|
||
|
|
}
|
||
|
|
ok, res = Session.addByClass(DevModel, 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"],
|
||
|
|
"group_id": param["group_id"],
|
||
|
|
"name": param["name"],
|
||
|
|
"category": param["category"],
|
||
|
|
"attr": json.dumps(param["attr"])
|
||
|
|
}
|
||
|
|
ok, res = Session.addByClass(DevModel, 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"],
|
||
|
|
"group_id": param["group_id"],
|
||
|
|
"name": param["name"],
|
||
|
|
"category": param["category"],
|
||
|
|
"attr": json.dumps(param["attr"])
|
||
|
|
}
|
||
|
|
ok, res = Session.addByClassFromGit(DevModel, data)
|
||
|
|
if ok:
|
||
|
|
return str(res["id"])
|
||
|
|
return ""
|
||
|
|
|
||
|
|
@pyqtSlot(str, result=bool)
|
||
|
|
def createCopy(self, id, group_id = None):
|
||
|
|
ok,res = Session.queryById(DevModel, id)
|
||
|
|
if ok:
|
||
|
|
res["attr"] = json.loads(res["attr"])
|
||
|
|
if group_id == None:
|
||
|
|
res["name"] = res["name"] + "_副本"
|
||
|
|
group_id = res["group_id"]
|
||
|
|
copy_id = self.create( group_id, res["name"], res["category"], res["attr"])
|
||
|
|
if copy_id != "":
|
||
|
|
if instructionManager.copyDevModel(id, 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 "category" in param:
|
||
|
|
data["category"] = param["category"]
|
||
|
|
if "attr" in param:
|
||
|
|
data["attr"] = json.dumps(param["attr"])
|
||
|
|
if "group_id" in param:
|
||
|
|
data["group_id"] = param["group_id"]
|
||
|
|
ok,res = Session.updateById(DevModel, id, data)
|
||
|
|
return ok
|
||
|
|
|
||
|
|
@pyqtSlot(str, result=bool)
|
||
|
|
@pyqtSlot(list, result=bool)
|
||
|
|
def delete(self, id):
|
||
|
|
if isinstance(id, str):
|
||
|
|
ok,res = Session.deleteById(DevModel, id)
|
||
|
|
return ok
|
||
|
|
else:
|
||
|
|
ok,res = Session.deleteByIds(DevModel, id)
|
||
|
|
return ok
|
||
|
|
|
||
|
|
@pyqtSlot(str, result=QVariant)
|
||
|
|
@pyqtSlot(str, result=list)
|
||
|
|
def getInfo(self, id = 'all'):
|
||
|
|
if id == 'all':
|
||
|
|
ok,res = Session.queryByAll(DevModel)
|
||
|
|
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 []
|
||
|
|
else:
|
||
|
|
ok,res = Session.queryById(DevModel, id)
|
||
|
|
if ok:
|
||
|
|
if "attr" in res:
|
||
|
|
res["attr_str"] = res["attr"] or ""
|
||
|
|
res["attr"] = res["attr"] and json.loads(res["attr"]) or {}
|
||
|
|
return res
|
||
|
|
else:
|
||
|
|
return None
|
||
|
|
|
||
|
|
@pyqtSlot(str, result=QVariant)
|
||
|
|
@pyqtSlot(str, result=list)
|
||
|
|
def getOriginalInfo(self, id = 'all'):
|
||
|
|
if id == 'all':
|
||
|
|
ok,res = Session.queryByAll(DevModel)
|
||
|
|
if ok:
|
||
|
|
for data in res:
|
||
|
|
if "attr" in data:
|
||
|
|
data["attr"] = data["attr"] and json.loads(data["attr"]) or {}
|
||
|
|
return res
|
||
|
|
else:
|
||
|
|
return []
|
||
|
|
else:
|
||
|
|
ok,res = Session.queryById(DevModel, id)
|
||
|
|
if ok:
|
||
|
|
if "attr" in res:
|
||
|
|
res["attr"] = res["attr"] and json.loads(res["attr"]) or {}
|
||
|
|
return res
|
||
|
|
else:
|
||
|
|
return None
|
||
|
|
|
||
|
|
# 获取所有属于设备模型的指令
|
||
|
|
@pyqtSlot(str, result=QVariant)
|
||
|
|
def getGroupDm(self, group_id):
|
||
|
|
ok, res = Session.queryByGroupId(DevModel, 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 []
|
||
|
|
|
||
|
|
devModelManager = DevModelManager()
|
||
|
|
|
||
|
|
|