I have setup some code that at least works but probably could use some efficiency improvements, Here it is below if you want to look it over. I'm open for some feedback in how to improve. Looks like it takes just under a min to run without changes ( normally shouldn't expect any changes since tag change script should take care of that --- I did move that into gateway tag change instead of in the tag)
I have this set at fixed rate with delay run of 6 hrs currently.
#set starting point and DB to connect to
plc = 1
num = 1
DB = "MSSQL"
for plc in range(1,7):
for num in range(1,1001):
#ID to check
UniqueID = plc*10000+num
#get provider Data for given PLC
providerlistpath = ["[TGMSSERVFRONT1]Provider List/Dataset"]
providertag = system.tag.readBlocking(providerlistpath)
provider = providertag[0].value.getValueAt(plc,1)
#get Tag Values
strProvider = str(provider)
strplc = str(plc)
strnum = str(num)
tagpathpartA = "[TGMSSERVIO"+strProvider+"]PLC"+strplc+"/Inputs/Input"+strnum
# [x] = tagpart.. [x] value needs to match column name in DB
POINT = tagpathpartA+"/Info/POINT"
INPUT_TYPE = tagpathpartA+"/Info/INPUT_TYPE"
LONG_NAME1 = tagpathpartA+"/Info/LONG_NAME1"
RACK = tagpathpartA+"/Info/RACK"
SHORT_NAME = tagpathpartA+"/Info/SHORT_NAME"
SLOT = tagpathpartA+"/Info/SLOT"
BUILDING = tagpathpartA+"/Info/Physical Location/BUILDING"
LEVEL = tagpathpartA+"/Info/Physical Location/LEVEL"
ROOM = tagpathpartA+"/Info/Physical Location/ROOM"
SECTOR = tagpathpartA+"/Info/Physical Location/SECTOR"
EXTENDED_DELAY = tagpathpartA+"/Control/EXTENDED_DELAY"
EGU = tagpathpartA+"/ANALOG/EGU"
FULL_SCALE = tagpathpartA+"/ANALOG/FULL_SCALE"
MAXIMUM_EGU = tagpathpartA+"/ANALOG/MAXIMUM_EGU"
MINIMUM_EGU = tagpathpartA+"/ANALOG/MINIMUM_EGU"
NO_LATCH = tagpathpartA+"/DISCRETE/NO_LATCH"
NO_WARNING = tagpathpartA+"/DISCRETE/NO_WARNING"
NORMAL_STATE = tagpathpartA+"/DISCRETE/NORMAL_STATE"
NAC_ZONE_01 = tagpathpartA+"/NAC_ZONE_01"
NAC_ZONE_02 = tagpathpartA+"/NAC_ZONE_02"
NAC_ZONE_03 = tagpathpartA+"/NAC_ZONE_03"
NAC_ZONE_04 = tagpathpartA+"/NAC_ZONE_04"
# Grab tag values for all tags listed in one call
tagPaths = POINT, INPUT_TYPE, LONG_NAME1, RACK, SHORT_NAME, SLOT, BUILDING, LEVEL, ROOM, SECTOR, EXTENDED_DELAY, EGU, FULL_SCALE, MAXIMUM_EGU, MINIMUM_EGU, NO_LATCH, NO_WARNING, NORMAL_STATE, NAC_ZONE_01, NAC_ZONE_02, NAC_ZONE_03, NAC_ZONE_04
values = system.tag.readBlocking(tagPaths)
# Grab tag values for all tags listed in one call
tagPaths = POINT, INPUT_TYPE, LONG_NAME1, RACK, SHORT_NAME, SLOT, BUILDING, LEVEL, ROOM, SECTOR, EXTENDED_DELAY, EGU, FULL_SCALE, MAXIMUM_EGU, MINIMUM_EGU, NO_LATCH, NO_WARNING, NORMAL_STATE, NAC_ZONE_01, NAC_ZONE_02, NAC_ZONE_03, NAC_ZONE_04
values = system.tag.readBlocking(tagPaths)
# DBcollist needs to be duplicate with "[name]" of the tagPaths = statement above to keep columns aligned with same index
DBcollist = "POINT", "INPUT_TYPE", "LONG_NAME1", "RACK", "SHORT_NAME", "SLOT", "BUILDING", "LEVEL", "ROOM", "SECTOR", "EXTENDED_DELAY", "EGU", "FULL_SCALE", "MAXIMUM_EGU", "MINIMUM_EGU", "NO_LATCH", "NO_WARNING", "NORMAL_STATE", "NAC_ZONE_01", "NAC_ZONE_02", "NAC_ZONE_03", "NAC_ZONE_04"
# get Dataset from SQL DB to compare to tag values
query = "SELECT {} FROM TGMS_INPUT WHERE UniqueID = ?".format(','.join(DBcollist))
args = [UniqueID]
InitialDB = system.db.runPrepQuery(query, args, DB)
# For each Tag Path, iterate through our results...
for index in range(len(tagPaths)):
#set values to compare and Column name checking
ColName = DBcollist[index]
tagPath = tagPaths[index]
DbValue = InitialDB.getValueAt(0,ColName)
TagValue = values[index].value
#check if DB value and Tag Value don't match
if DbValue != TagValue:
#update DB value
query = "UPDATE TGMS_Input SET " + ColName + " = ? OUTPUT INSERTED.UniqueID WHERE UniqueID = ?"
args= TagValue , UniqueID
Update = system.db.runPrepQuery(query,args, DB)
UpdateID = Update.getValueAt(0,0)
OldValue = str(DbValue)
NewValue = str(TagValue)
ColName = str(ColName)
#add entry to audit log
auditQuery = "INSERT INTO InputAuditLog (ModifiedID, OldValue, NewValue, ColumnChanged, TagPath) VALUES (?,?,?,?,?)"
auditArgs = UpdateID, OldValue, NewValue, ColName, tagPath
system.db.runPrepUpdate(auditQuery, auditArgs, DB)