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.

155 lines
4.7 KiB
Python

2 years ago
import collections
2 years ago
import json
from utils.DBModels.DeviceModels import DeviceDB
2 years ago
from model.ProjectModel.AreaManage import Area
2 years ago
2 years ago
class Device():
areas = []
startAddress = None
2 years ago
endAddress = 0
2 years ago
protocolType = None
masterOrSlave = None
2 years ago
deviceName = None
2 years ago
def __init__(self):
pass
2 years ago
def addArea(self, type, nums, bytes):
area = Area()
area.startAddress = self.endAddress
length = int(nums) * int(bytes)
if length % 2 != 0:
length = (length + 1) // 2
2 years ago
else:
2 years ago
length = length / 2
area.endAddress = startAddress + length
self.endAddress = area.endAddress
for index, address in range(area.startAddress, area.endAddress):
if index % bytes == 0 and self.protocolType == 'DP':
area.
2 years ago
def getValueLength(self):
pass
def writeAreas(self):
pass
2 years ago
@classmethod
2 years ago
def delAreas(self, deviceName, ids):
2 years ago
2 years ago
jsonCon = json.loads(DeviceDB.getByName(deviceName=deviceName).areaJson)
2 years ago
for id in ids:
2 years ago
jsonCon.pop(id - 1)
2 years ago
if jsonCon == []:
areaJson = None
DeviceDB.update(areaJson=areaJson).where(DeviceDB.deviceName == deviceName).execute()
else:
2 years ago
for index, areajsonId in enumerate(jsonCon):
areajsonId["id"] = index + 1
2 years ago
areaJson = json.dumps(jsonCon)
2 years ago
2 years ago
DeviceDB.update(areaJson=areaJson).where(DeviceDB.deviceName == deviceName).execute()
2 years ago
@classmethod
2 years ago
def getAreaJson(self, deviceNames):
deviceName = deviceNames
jsonConsStr = DeviceDB.getByName(deviceName=deviceName).areaJson
if jsonConsStr is None:
return
else:
jsonCons = json.loads(jsonConsStr)
return jsonCons
2 years ago
class DevicesManange():
def __init__(self):
2 years ago
self.dpMasterDevices = collections.OrderedDict()
self.dpSlaveDevices = collections.OrderedDict() # 有序字典 (OrderedDict)
self.paMasterDevices = collections.OrderedDict()
self.paSlaveDevices = collections.OrderedDict()
def addDevice(self, proType, masterSlaveModel, deviceName):
device = Device()
device.type = proType
device.masterSlaveModel = masterSlaveModel
device.deviceName = deviceName
masterSlaveModel = masterSlaveModel
if proType == "DP" and masterSlaveModel == "主站":
curProDict = self.dpMasterDevices
elif proType == "DP" and masterSlaveModel == "从站":
curProDict = self.dpSlaveDevices
elif proType == "PA" and masterSlaveModel == "主站":
curProDict = self.paMasterDevices
elif proType == "PA" and masterSlaveModel == "从站":
curProDict = self.paSlaveDevices
if len(curProDict) == 0:
device.startAddress = 0
else:
device.startAddress = curProDict.values[-1].endAddress + 1
curProDict[deviceName] = device
2 years ago
2 years ago
@classmethod
def addAreas(self, type, nums, bytes, deviceName):
if DeviceDB.getByName(deviceName=deviceName).areaJson is None:
jsonCon = ([{
"id": 1,
"type": type,
"nums": nums,
"bytes": bytes,
}])
else:
jsonCon = json.loads(DeviceDB.getByName(deviceName=deviceName).areaJson)
id = jsonCon[-1]["id"] + 1
jsonCon.append({
"id": id,
"type": type,
"nums": nums,
"bytes": bytes,
})
areaJson = json.dumps(jsonCon)
DeviceDB.update(areaJson=areaJson).where(DeviceDB.deviceName == deviceName).execute()
2 years ago
2 years ago
@classmethod
def getAreaID(self, deviceNames):
deviceName = deviceNames
jsonConsStr = DeviceDB.getByName(deviceName=deviceName).areaJson
if jsonConsStr is None:
2 years ago
return
else:
2 years ago
jsonCons = json.loads(jsonConsStr)
id = []
for jsonCon in jsonCons:
id.append(jsonCon["id"])
return id
2 years ago
2 years ago
@classmethod
def getChannelLength(self, deviceName):
number = 0
if DeviceDB.getByName(deviceName=deviceName).areaJson is None:
return number
else:
numbers = json.loads(DeviceDB.getByName(deviceName=deviceName).areaJson)
for i in numbers:
number += int(i['nums'])
return number
2 years ago
def editDevies(self):
pass
2 years ago
@classmethod
2 years ago
def getAllDevice(self):
# 查询所有设备
devices = DeviceDB.get_all()
if devices is 'error':
return
l = []
for x in devices:
l.append([x.deviceName, x.proType, x.masterSlaveModel, x.areaJson])
return l