120 lines
4.0 KiB
Python
120 lines
4.0 KiB
Python
from datetime import datetime
|
|
import time
|
|
import minimalmodbus
|
|
from pycomm.ab_comm.clx import Driver as clx
|
|
from pycomm.cip.cip_base import CommError, DataError
|
|
|
|
class DataPoint(object):
|
|
def __init__(self,changeThreshold=0,guaranteed=3600, name="datapoint",alertThreshold=[],alertCondition=[],alertResponse=[],alertContact=[]):
|
|
self.value = None
|
|
self.lastvalue = None
|
|
self.lastsend = 0
|
|
self.changeThreshold = changeThreshold
|
|
self.guaranteed = guaranteed
|
|
self.name = name
|
|
self.alerted = False
|
|
self.alertThreshold = alertThreshold
|
|
self.alertCondition = alertCondition
|
|
self.alertResponse = alertResponse
|
|
self.alertContact = alertContact
|
|
|
|
|
|
def checkSend(self,value):
|
|
if value != self.lastvalue or (time.time() - self.lastsend > self.guaranteed):
|
|
self.lastsend = time.time()
|
|
self.lastvalue = value
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def checkAlert(self,value):
|
|
conditions = {
|
|
"gt": "value > threshold",
|
|
"lt": "value < threshold",
|
|
"eq": "value == threshold",
|
|
"gte": "value >= threshold",
|
|
"lte": "value <= threshold",
|
|
"not": "value != threshold"
|
|
}
|
|
|
|
for thres,cond in zip(self.alertThreshold,self.alertCondition):
|
|
#check value for alert threshold
|
|
evalVars = {
|
|
"value": value,
|
|
"threshold": thres
|
|
}
|
|
func = conditions.get(cond)
|
|
if func == None:
|
|
print("Not an available function: {}".format(cond))
|
|
else:
|
|
if eval(func, evalVars):
|
|
return {"message":"Read value for {} is {} threshold value {}".format(self.name,value,thres)}
|
|
else:
|
|
self.alerted = False
|
|
return None
|
|
|
|
|
|
class modbusDataPoint(DataPoint):
|
|
def __init__(self,changeThreshold,guaranteed,name,register=1,baud=19200,stopBits=1,parity=None, device='/dev/ttyS0'):
|
|
DataPoint.__init__(self,changeThreshold,guaranteed,name)
|
|
self.register = register
|
|
self.baud = baud
|
|
self.stopBits = stopBits
|
|
self.parity = parity
|
|
self.device = device
|
|
def read(self):
|
|
pass
|
|
|
|
def write(self):
|
|
pass
|
|
|
|
class plcDataPoint(DataPoint):
|
|
def __init__(self,changeThreshold,guaranteed,name,plcIP='192.168.1.10',plcType='Micro800',tag=None,alertThreshold=[],alertCondition=[],alertResponse=[],alertContact=[]):
|
|
DataPoint.__init__(self,changeThreshold,guaranteed,name,alertThreshold,alertCondition,alertResponse,alertContact)
|
|
self.plcIP = plcIP
|
|
self.plcType = plcType
|
|
self.tag = tag
|
|
|
|
def read(self):
|
|
direct_connect = self.plcType == "Micro800"
|
|
c = clx()
|
|
try:
|
|
if c.open(self.plcIP,direct_connect):
|
|
try:
|
|
val = c.read_tag(self.tag)
|
|
c.close()
|
|
alertMessage = self.checkAlert(val[0])
|
|
return val[0], alertMessage
|
|
except DataError as derr:
|
|
print("Error: {}".format(derr))
|
|
c.close()
|
|
except CommError as cerr:
|
|
print("Error: {}".format(cerr))
|
|
|
|
return False
|
|
|
|
def write(self):
|
|
pass
|
|
|
|
class currentDataPoint(DataPoint):
|
|
def __init__(self,changeThreshold,guaranteed,name, euMin=0, euMax=100, rawMin=4, rawMax=20):
|
|
DataPoint.__init__(self,changeThreshold,guaranteed,name)
|
|
self.euMin = euMin
|
|
self.euMax = euMax
|
|
self.rawMin = rawMin
|
|
self.rawMax = rawMax
|
|
|
|
def read(self):
|
|
pass
|
|
|
|
class voltageDataPoint(DataPoint):
|
|
def __init__(self,changeThreshold,guaranteed,name, euMin=0, euMax=100, rawMin=0, rawMax=10):
|
|
DataPoint.__init__(self,changeThreshold,guaranteed,name)
|
|
self.euMin = euMin
|
|
self.euMax = euMax
|
|
self.rawMin = rawMin
|
|
self.rawMax = rawMax
|
|
|
|
def read(self):
|
|
pass
|