#set starting point and DB to connect to
checkcount = 0
updatecount = 0
DB = "MSSQL"
checkpath = "[Design]SQL DB Verification/Input Matrix/CheckCount"
stoptimePath = "[Design]SQL DB Verification/Input Matrix/Stop Time"
starttimePath = "[Design]SQL DB Verification/Input Matrix/Start Time"
inprogressPath = "[Design]SQL DB Verification/Input Matrix/Checking in progress"
updatetagcountPath = "[Design]SQL DB Verification/Input Matrix/Update Tag Count"
starttime = system.date.now()
values = 1, starttime, updatecount
progresspaths = inprogressPath, starttimePath, updatetagcountPath
#system.tag.writeAsync(progresspaths, values)
maxPLCPath = "[Design]System/Max PLC Num"
value = system.tag.readBlocking(maxPLCPath)
maxPLC = value[0].value +1 #range function is not inclusive of end
for PLC in range(2,maxPLC):
basePath = "[PLC"+str(PLC)+"]PLC Data/Info/"
maxInputPath = basePath + "PLC_MAXIMUM_INPUT"
totalActionPath = basePath + "Memory/Input Action Count"
tagPathsRange = maxInputPath, totalActionPath
values=system.tag.readBlocking(tagPathsRange)
maxInput = values[0].value
totalAction = values[1].value
if maxInput is not None:
lastFixed = maxInput * 10 + 9
overflowIndex = []
#iterate through actions
for index in range(30000,(totalAction+1)):
checkcount += 1
#system.tag.writeAsync(checkpath, checkcount)
UniqueActionID = (PLC*100000+index)
#clear Overflow Input number (will be intered during overflow check at end)
query = "UPDATE TGMS_InputDBAction set Input_Number=0 WHERE OverflowUniqueActionID = ?"
args = [UniqueActionID]
system.db.runPrepUpdate(query, args, DB)
#get Tag Paths
basePath = "[PLC"+str(PLC)+"]Input Action/"+str(index)
PLC_NUMBER = basePath+"/PLC_NUMBER"
OUTPUT_NUMBER = basePath+"/OUTPUT_NUMBER"
# Grab tag values
tagPaths = PLC_NUMBER, OUTPUT_NUMBER
values = system.tag.readBlocking(tagPaths)
TagValuePLC = values[0].value
TagValueNum = values[1].value
print values
#continue if tag values valid
if TagValuePLC is not None and TagValueNum is not None:
print "PLC=" + str(TagValuePLC)
print "Num=" + str(TagValueNum)
print "got here"
#if negative add to overflow index to check at end
if TagValuePLC <0 and TagValueNum <0 and TagValueNum < TagValuePLC:
overflowIndex.append[index]
#get input Number based on index value (ignore overflow till later)
if index <= lastFixed:
Input_Number = index/10
else:
Input_Number = 0
updateString = "UPDATE TGMS_InputDBAction SET Output_PLC = ?, Output_Number = ? OUTPUT DELETED.Output_PLC, DELETED.Output_Number WHERE UniqueActionID = ?"
updateArgs = [TagValuePLC, TagValueNum, UniqueActionID]
insertString = "INSERT INTO TGMS_InputDBAction (ActionID, Input_PLC, Input_Number, Output_PLC, Output_Number) Values (?,?,?,?,?)"
insertArgs = [index, PLC, Input_Number, TagValuePLC, TagValueNum]
try:
DBvalues = system.db.runPrepQuery(updateString, updateArgs, DB)
DBValuePLC = DBvalues.getValueAt(0,0)
DBValueNum = DBvalues.getValueAt(0,1)
if DBValuePLC != TagValuePLC or DBValueNum != TagValueNum:
updatecount += 1
updated = 1
#set values to str for audit log
OldValue = "PLC-"+str(DBValuePLC)+" Num-"+ str(DBValueNum)
NewValue = "PLC-"+str(TagValuePLC)+" Num-"+ str(TagValueNum)
ColName = "Output PLC/Number Updated"
except:
print "I did get to the except"
system.db.runPrepUpdate(insertString, insertArgs, DB)
updatecount += 1
updated = 1
#set values to str for audit log
OldValue = "inserted row"
NewValue = "PLC-"+str(TagValuePLC)+" Num-"+ str(TagValueNum)
ColName = "Output PLC/Number Updated"
if updated == 1: #add entry to audit log
auditQuery = "INSERT INTO TGMS_AuditInputAction (ModifiedUniqueActionID, OldValue, NewValue, ColumnChanged, TagPath) VALUES (?,?,?,?,?)"
auditArgs = UniqueActionID, OldValue, NewValue, ColName, basePath
system.db.runPrepUpdate(auditQuery, auditArgs, DB)
updated = 0
# iterate through overflow index values and set input number on overflow
for index in overflowIndex:
UniqueActionID = (PLC*100000+index)
#get Tag Paths
basePath = "[PLC"+str(PLC)+"]Input Action/"+str(index)
PLC_NUMBER = basePath+"/PLC_NUMBER"
OUTPUT_NUMBER = basePath+"/OUTPUT_NUMBER"
# Grab tag values for all tags listed in one call
tagPaths = PLC_NUMBER, OUTPUT_NUMBER
values = system.tag.readBlocking(tagPaths)
#Update DB INPUT NUMBER for for overflow if initially Negative values
#set initial values values to Positive
StartID = values[1].value *-1
EndID = values[0].value *-1 #SQL between statement is inclusive of start and end
#only update if valid entry
if StartID < EndID and StartID >0 and EndID >0:
#unique id for start and end inclusive
UniqueActionStart = PLC *100000+StartID
UniqueActionEND = PLC *100000+EndID
#update Database
query = "UPDATE TGMS_InputDBAction set Input_Number=?, OverflowUniqueActionID=? WHERE UniqueActionID BETWEEN ? AND ?"
args = index, UniqueActionID, UniqueActionStart, UniqueActionEND
system.db.runPrepUpdate(query, args, DB)
stoptime = system.date.now()
values = 0, stoptime, updatecount, checkcount
progresspaths = inprogressPath, stoptimePath, updatetagcountPath, checkpath
#system.tag.writeAsync(progresspaths, values)