Allow for forcing send or skip of database, read() returns value
This commit is contained in:
@@ -31,9 +31,9 @@ def readTag(addr, tag):
|
||||
c.close()
|
||||
|
||||
class Tag():
|
||||
global readTag, con, PLC_IP_ADDRESS
|
||||
global readTag, con
|
||||
|
||||
def __init__(self, name, tag, db_id, data_type, change_threshold, guarantee_sec, mapFn=None, plc_type='CLX', plc_ip_address='192.168.1.10'):
|
||||
def __init__(self, name, tag, db_id, data_type, change_threshold, guarantee_sec, mapFn=None, device_type='CLX', ip_address='192.168.1.10'):
|
||||
self.name = str(name)
|
||||
self.tag = str(tag)
|
||||
self.data_type = str(data_type)
|
||||
@@ -43,37 +43,40 @@ class Tag():
|
||||
self.chg_threshold = change_threshold
|
||||
self.last_send_time = 0
|
||||
self.mapFn = mapFn
|
||||
self.plc_type = plc_type
|
||||
self.device_type = device_type
|
||||
self.readFn = readTag
|
||||
self.db_id = db_id
|
||||
if self.plc_type == "u800":
|
||||
if self.device_type == "u800":
|
||||
self.readFn = u800.readTag
|
||||
self.plc_ip_address = plc_ip_address
|
||||
self.ip_address = ip_address
|
||||
|
||||
def read(self, forceSend):
|
||||
writeToDB = False
|
||||
if self.tag:
|
||||
v = self.readFn(self.plc_ip_address, self.tag)
|
||||
v = self.readFn(self.ip_address, self.tag)
|
||||
if v:
|
||||
if self.data_type == 'BOOL' or self.data_type == 'STRING':
|
||||
val = v[0]
|
||||
if self.mapFn:
|
||||
val = self.mapFn[val]
|
||||
if (self.last_send_time == 0) or (self.value is None) or not (self.value == val) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend):
|
||||
if (self.last_send_time == 0) or (self.value is None) or not (self.value == val) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend == True):
|
||||
self.last_value = self.value
|
||||
self.value = val
|
||||
writeToDB = True
|
||||
else:
|
||||
writeToDB = False
|
||||
else:
|
||||
if (self.last_send_time == 0) or (self.value is None) or (abs(self.value - v[0]) > self.chg_threshold) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend):
|
||||
if (self.last_send_time == 0) or (self.value is None) or (abs(self.value - val) > self.chg_threshold) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend == True):
|
||||
self.last_value = self.value
|
||||
self.value = v[0]
|
||||
self.value = val
|
||||
writeToDB = True
|
||||
else:
|
||||
writeToDB = False
|
||||
if forceSend == False:
|
||||
writeToDB = False
|
||||
if writeToDB:
|
||||
self.sendToDB()
|
||||
return self.value
|
||||
|
||||
def sendToDB(self):
|
||||
# TODO: Datetime
|
||||
|
||||
@@ -29,7 +29,7 @@ def readTag(addr, tag):
|
||||
class Tag():
|
||||
global readTag, con
|
||||
|
||||
def __init__(self, name, tag, db_id, data_type, change_threshold, guarantee_sec, mapFn=None, plc_type='CLK', plc_ip_address='192.168.1.10'):
|
||||
def __init__(self, name, tag, db_id, data_type, change_threshold, guarantee_sec, mapFn=None, device_type='CLX', ip_address='192.168.1.10'):
|
||||
self.name = name
|
||||
self.tag = tag
|
||||
self.data_type = data_type
|
||||
@@ -39,37 +39,40 @@ class Tag():
|
||||
self.chg_threshold = change_threshold
|
||||
self.last_send_time = 0
|
||||
self.mapFn = mapFn
|
||||
self.plc_type = plc_type
|
||||
self.device_type = device_type
|
||||
self.readFn = readTag
|
||||
self.db_id = db_id
|
||||
if self.plc_type == "u800":
|
||||
if self.device_type == "u800":
|
||||
self.readFn = u800.readTag
|
||||
self.plc_ip_address = plc_ip_address
|
||||
self.ip_address = ip_address
|
||||
|
||||
def read(self, forceSend):
|
||||
writeToDB = False
|
||||
if self.tag:
|
||||
v = self.readFn(str(self.plc_ip_address), str(self.tag))
|
||||
v = self.readFn(str(self.ip_address), str(self.tag))
|
||||
if v:
|
||||
if self.data_type == 'BOOL' or self.data_type == 'STRING':
|
||||
val = v[0]
|
||||
if self.mapFn:
|
||||
val = self.mapFn[val]
|
||||
if (self.last_send_time == 0) or (self.value is None) or not (self.value == val) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend):
|
||||
if (self.last_send_time == 0) or (self.value is None) or not (self.value == val) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend == True):
|
||||
self.last_value = self.value
|
||||
self.value = val
|
||||
writeToDB = True
|
||||
else:
|
||||
writeToDB = False
|
||||
else:
|
||||
if (self.last_send_time == 0) or (self.value is None) or (abs(self.value - v[0]) > self.chg_threshold) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend):
|
||||
if (self.last_send_time == 0) or (self.value is None) or (abs(self.value - v[0]) > self.chg_threshold) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend == True):
|
||||
self.last_value = self.value
|
||||
self.value = v[0]
|
||||
writeToDB = True
|
||||
else:
|
||||
writeToDB = False
|
||||
if forceSend == False:
|
||||
writeToDB = False
|
||||
if writeToDB:
|
||||
self.sendToDB()
|
||||
return self.value
|
||||
|
||||
def sendToDB(self):
|
||||
query = "INSERT INTO tag_vals (dtime, tagID, val) VALUES ({}, '{}', {})".format(time.time(), self.db_id, self.value)
|
||||
|
||||
@@ -65,10 +65,12 @@ def main():
|
||||
|
||||
try:
|
||||
sa_test = str(configObj['save_all'])
|
||||
if sa_test == "true":
|
||||
if sa_test.lower() == "true":
|
||||
configProperties['save_all'] = True
|
||||
else:
|
||||
elif sa_test.lower() == "false":
|
||||
configProperties['save_all'] = False
|
||||
else:
|
||||
configProperties['save_all'] = "test"
|
||||
print("FYI, value for save_all is {0}".format(configProperties['save_all']))
|
||||
except KeyError:
|
||||
print("FYI, there is no save_all value stored in the database, using False")
|
||||
|
||||
@@ -60,11 +60,13 @@ def main():
|
||||
|
||||
try:
|
||||
sa_test = str(configObj['save_all'])
|
||||
if sa_test == "true":
|
||||
if sa_test.lower() == "true":
|
||||
configProperties['save_all'] = True
|
||||
else:
|
||||
elif sa_test.lower() == "false":
|
||||
configProperties['save_all'] = False
|
||||
print("FYI, value for save_all is {0}".format(configProperties['save_all']))
|
||||
else:
|
||||
configProperties['save_all'] = "test"
|
||||
print("FYI, value for save_all is {0}".format(configProperties['save_all']))'save_all']))
|
||||
except KeyError:
|
||||
print("FYI, there is no save_all value stored in the database, using False")
|
||||
configProperties['save_all'] = False
|
||||
|
||||
Reference in New Issue
Block a user