|
|
@ -1,36 +1,32 @@
|
|
|
|
import collections
|
|
|
|
import collections
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
from utils.DBModels.DeviceModels import DeviceDB
|
|
|
|
from utils.DBModels.DeviceModels import DeviceDB
|
|
|
|
|
|
|
|
from model.ProjectModel.AreaManage import Area
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Device():
|
|
|
|
class Device():
|
|
|
|
areas = []
|
|
|
|
areas = []
|
|
|
|
startAddress = None
|
|
|
|
startAddress = None
|
|
|
|
endAddress = None
|
|
|
|
endAddress = 0
|
|
|
|
protocolType = None
|
|
|
|
protocolType = None
|
|
|
|
masterOrSlave = None
|
|
|
|
masterOrSlave = None
|
|
|
|
|
|
|
|
deviceName = None
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def addAreas(self, type, nums, bytes, deviceName):
|
|
|
|
def addArea(self, type, nums, bytes):
|
|
|
|
if DeviceDB.getByName(deviceName=deviceName).areaJson is None:
|
|
|
|
area = Area()
|
|
|
|
jsonCon = ([{
|
|
|
|
area.startAddress = self.endAddress
|
|
|
|
"id": 1,
|
|
|
|
length = int(nums) * int(bytes)
|
|
|
|
"type": type,
|
|
|
|
if length % 2 != 0:
|
|
|
|
"nums": nums,
|
|
|
|
length = (length + 1) // 2
|
|
|
|
"bytes": bytes,
|
|
|
|
|
|
|
|
}])
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
jsonCon = json.loads(DeviceDB.getByName(deviceName=deviceName).areaJson)
|
|
|
|
length = length / 2
|
|
|
|
id = jsonCon[-1]["id"] + 1
|
|
|
|
area.endAddress = startAddress + length
|
|
|
|
jsonCon.append({
|
|
|
|
self.endAddress = area.endAddress
|
|
|
|
"id": id,
|
|
|
|
for index, address in range(area.startAddress, area.endAddress):
|
|
|
|
"type": type,
|
|
|
|
if index % bytes == 0 and self.protocolType == 'DP':
|
|
|
|
"nums": nums,
|
|
|
|
area.
|
|
|
|
"bytes": bytes,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
areaJson = json.dumps(jsonCon)
|
|
|
|
|
|
|
|
DeviceDB.update(areaJson=areaJson).where(DeviceDB.deviceName == deviceName).execute()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getValueLength(self):
|
|
|
|
def getValueLength(self):
|
|
|
@ -40,6 +36,7 @@ class Device():
|
|
|
|
def writeAreas(self):
|
|
|
|
def writeAreas(self):
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def delAreas(self, deviceName, ids):
|
|
|
|
def delAreas(self, deviceName, ids):
|
|
|
|
|
|
|
|
|
|
|
|
jsonCon = json.loads(DeviceDB.getByName(deviceName=deviceName).areaJson)
|
|
|
|
jsonCon = json.loads(DeviceDB.getByName(deviceName=deviceName).areaJson)
|
|
|
@ -57,18 +54,7 @@ class Device():
|
|
|
|
DeviceDB.update(areaJson=areaJson).where(DeviceDB.deviceName == deviceName).execute()
|
|
|
|
DeviceDB.update(areaJson=areaJson).where(DeviceDB.deviceName == deviceName).execute()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getAreaID(self, deviceNames):
|
|
|
|
@classmethod
|
|
|
|
deviceName = deviceNames
|
|
|
|
|
|
|
|
jsonConsStr = DeviceDB.getByName(deviceName=deviceName).areaJson
|
|
|
|
|
|
|
|
if jsonConsStr is None:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
jsonCons = json.loads(jsonConsStr)
|
|
|
|
|
|
|
|
id = []
|
|
|
|
|
|
|
|
for jsonCon in jsonCons:
|
|
|
|
|
|
|
|
id.append(jsonCon["id"])
|
|
|
|
|
|
|
|
return id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getAreaJson(self, deviceNames):
|
|
|
|
def getAreaJson(self, deviceNames):
|
|
|
|
deviceName = deviceNames
|
|
|
|
deviceName = deviceNames
|
|
|
|
jsonConsStr = DeviceDB.getByName(deviceName=deviceName).areaJson
|
|
|
|
jsonConsStr = DeviceDB.getByName(deviceName=deviceName).areaJson
|
|
|
@ -80,61 +66,68 @@ class Device():
|
|
|
|
|
|
|
|
|
|
|
|
class DevicesManange():
|
|
|
|
class DevicesManange():
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
self.masterDevices = collections.OrderedDict()
|
|
|
|
self.dpMasterDevices = collections.OrderedDict()
|
|
|
|
self.slaveDevices = collections.OrderedDict() # 有序字典 (OrderedDict)
|
|
|
|
self.dpSlaveDevices = collections.OrderedDict() # 有序字典 (OrderedDict)
|
|
|
|
self.initDevices()
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def initDevices(self):
|
|
|
|
@classmethod
|
|
|
|
devices = DeviceDB.get_all()
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
if devices is 'error':
|
|
|
|
@classmethod
|
|
|
|
|
|
|
|
def getAreaID(self, deviceNames):
|
|
|
|
|
|
|
|
deviceName = deviceNames
|
|
|
|
|
|
|
|
jsonConsStr = DeviceDB.getByName(deviceName=deviceName).areaJson
|
|
|
|
|
|
|
|
if jsonConsStr is None:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
for x in devices:
|
|
|
|
|
|
|
|
masterSlaveModel = x.masterSlaveModel
|
|
|
|
|
|
|
|
if masterSlaveModel == '主站':
|
|
|
|
|
|
|
|
devices = x.deviceName
|
|
|
|
|
|
|
|
masterByteLong = 0
|
|
|
|
|
|
|
|
if x.areaJson is not None:
|
|
|
|
|
|
|
|
for y in json.loads(x.areaJson):
|
|
|
|
|
|
|
|
numbers = y['nums']
|
|
|
|
|
|
|
|
bytes = int(y['bytes'])
|
|
|
|
|
|
|
|
if bytes % 2 == 0:
|
|
|
|
|
|
|
|
byte = bytes // 2
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
byte = (bytes + 1) // 2
|
|
|
|
|
|
|
|
masterByteLong += int(numbers) * int(byte)
|
|
|
|
|
|
|
|
self.masterDevices[devices] = masterByteLong
|
|
|
|
|
|
|
|
elif masterSlaveModel == '从站':
|
|
|
|
|
|
|
|
devices = x.deviceName
|
|
|
|
|
|
|
|
salveByteLong = 0
|
|
|
|
|
|
|
|
if x.areaJson is not None:
|
|
|
|
|
|
|
|
for y in json.loads(x.areaJson):
|
|
|
|
|
|
|
|
numbers = y['nums']
|
|
|
|
|
|
|
|
bytes = int(y['bytes'])
|
|
|
|
|
|
|
|
if bytes % 2 == 0:
|
|
|
|
|
|
|
|
byte = bytes // 2
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
byte = (bytes + 1) // 2
|
|
|
|
|
|
|
|
salveByteLong += int(numbers) * (byte)
|
|
|
|
|
|
|
|
self.slaveDevices[devices] = salveByteLong
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def addDevice(self, deviceName,channelNums, channelBytes):
|
|
|
|
|
|
|
|
channelBytes = int(channelBytes)
|
|
|
|
|
|
|
|
if channelBytes % 2 == 0:
|
|
|
|
|
|
|
|
channelBytes = channelBytes // 2
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
channelBytes = (channelBytes + 1) // 2
|
|
|
|
jsonCons = json.loads(jsonConsStr)
|
|
|
|
if deviceName in self.masterDevices:
|
|
|
|
id = []
|
|
|
|
self.masterDevices[deviceName] = self.masterDevices[deviceName] + int(channelNums) * int(channelBytes)
|
|
|
|
for jsonCon in jsonCons:
|
|
|
|
elif deviceName in self.slaveDevices:
|
|
|
|
id.append(jsonCon["id"])
|
|
|
|
self.slaveDevices[deviceName] = self.slaveDevices[deviceName] + int(channelNums) * int(channelBytes)
|
|
|
|
return id
|
|
|
|
elif '主站' in deviceName:
|
|
|
|
|
|
|
|
self.masterDevices[deviceName] = int(channelNums) * int(channelBytes)
|
|
|
|
|
|
|
|
elif '从站' in deviceName:
|
|
|
|
|
|
|
|
self.slaveDevices[deviceName] = int(channelNums) * int(channelBytes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return self.masterDevices, self.slaveDevices #分别返回PA和DP每个设备的字节的总长度
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@classmethod
|
|
|
|
def getChannelLength(self, deviceName):
|
|
|
|
def getChannelLength(self, deviceName):
|
|
|
@ -150,21 +143,7 @@ class DevicesManange():
|
|
|
|
def editDevies(self):
|
|
|
|
def editDevies(self):
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def getByteLengthStart(self):
|
|
|
|
@classmethod
|
|
|
|
masterAddress = 0
|
|
|
|
|
|
|
|
slaveAddress = 0
|
|
|
|
|
|
|
|
masterAddressList = {}
|
|
|
|
|
|
|
|
slaveAddressList = {}
|
|
|
|
|
|
|
|
for key in self.masterDevices:
|
|
|
|
|
|
|
|
masterAddressList[key] = int(masterAddress)
|
|
|
|
|
|
|
|
masterAddress = int(self.masterDevices[key]) + masterAddress
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for key in self.slaveDevices:
|
|
|
|
|
|
|
|
slaveAddressList[key] = int(slaveAddress)
|
|
|
|
|
|
|
|
slaveAddress = int(self.slaveDevices[key]) + slaveAddress
|
|
|
|
|
|
|
|
print(masterAddressList, '主站', slaveAddressList, '从站')
|
|
|
|
|
|
|
|
return masterAddressList, slaveAddressList #返回PA和DP每个字节长度的起始值
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getAllDevice(self):
|
|
|
|
def getAllDevice(self):
|
|
|
|
# 查询所有设备
|
|
|
|
# 查询所有设备
|
|
|
|
devices = DeviceDB.get_all()
|
|
|
|
devices = DeviceDB.get_all()
|
|
|
|