I have two sets of code. The first one works just fine. but the second one does not. There is one difference in that there are multiple parameter's in the second code. My question is why does ignition update the UDT instances on the first code, but not the second?
first code(works fine):
"""Changes the Definintion's valuesource to OPC
• Fill in UDTFolderPath - This will change based on tag provider
"""
system.perspective.print("Process Started")
# import libraries
import csv
from datetime import datetime
# get our dates and time to put into file name
now = datetime.now()
now_str = now.strftime("%Y-%m-%d_%H%M")
# functions:
def createCSVOutPut(headers, dataSet, saveLocation):
""" create a dataSet with list, then make it a CSV file and save it to location provided
• Creates a CSV file based on headers and data provide to teh dataSet.
• headers and dataSet must be a list
"""
DataSet = system.dataset.toDataSet(headers, dataSet)
csv = system.dataset.toCSV(DataSet)
system.file.writeFile(saveLocation, csv)
# variables needed
configsCSVDict = {} # black Dict to keep CSV/Ignition info in - helps with faster processing times
totalCount = 0 # total tags looked at
tagUpdateCount = 0 # tags updated
dataSet = [] # empty list to create our CSV file
# *******************************************************************************************************Change as needed
# for csv file out put
headers = ['TagPAth', 'Update Status', 'Updated Value/Status']
# location where CSV file will write to
saveLocation = r"C:\Users\rkj823_ca\Desktop\Ignition Scripting File\Tag updates_" + now_str + ".csv"
# Path that gets infrom from a SCV file for us to work on
csvPath = r"C:\Users\rkj823_ca\Desktop\Ignition Scripting File\get tags paths paramaeters.csv"
# main folder path in ignition's UDT Definitions to start on
UDTFolderPath = "[Cochina]STX"
# *******************************************************************************************************Change as needed
# Open CSV and put CSV file into the dict - Can be done with ignition tag objects as well
with open(csvPath, 'rU') as csvFile:
csvReader = csv.reader(csvFile, delimiter=',')
next(csvReader)
for row in csvReader:
totalCount += 1
tagPath = row[0]
CSVparameter = row[1]
configsCSVDict[tagPath] = CSVparameter
#here to get full tag path so we can update tags accordingly -True so we get all UDT instnances
browse = system.tag.browse(UDTFolderPath, filter= {'tagType':"UdtInstance", "recursive":True})
for tagBrowse in browse:
# path of UDT definition without tagName for system.tag.configure function updates
IgnitionTagPath = tagBrowse['fullPath']
# get tag configs - False so we do not get all Atomic Tags only the UDT Instance configs
UDTInstanceConfigs = system.tag.getConfiguration(IgnitionTagPath, False)
for UdtInstance in UDTInstanceConfigs:
ignitionTagPath = str(UdtInstance['path'])
udtFolderPath = ignitionTagPath.rsplit("/",1)[0]
if ignitionTagPath in str(configsCSVDict):
parameterValue = None
# some paths have a single tag componet so it has the tag name - This try will group those for us in a CSV file to work on
try:
# use ignition current path to grab the tag decription from the CSV file
parameterValue = configsCSVDict[ignitionTagPath]
except KeyError as e:
dataSet.append([ignitionTagPath, "Key Error in CSV Dict", "Single Tag Component/has a tag name instead of UDT instance"])
break # Exit the inner loop and proceed to the next tagBrowse
# this was added due to the try except stopp the script from writing to the UDT parameter
if parameterValue != None:
UdtInstance['parameters']['L3'] = parameterValue
system.tag.configure(udtFolderPath, UdtInstance, 'm') # update tag properties
dataSet.append([ignitionTagPath, "Updated", parameterValue]) # add to our list
tagUpdateCount += 1
else:
dataSet.append([ignitionTagPath, "No Update made", "UDTPath not in CSV"])# add to our list
# Call the make CSV file function
createCSVOutPut(headers, dataSet, saveLocation)
# finsihed and print out updates
system.perspective.print("Process Finished")
system.perspective.print("%d out of %d tags were updated check file named: Tag updates " % (tagUpdateCount, totalCount))
Second code(Does not work):
system.perspective.print("Process Started")
import csv
from datetime import datetime
ignitionTagpath = self.getSibling("TagPath").props.text
CSVDict = {} # for faster processing
collisionPolicy = "m"
csvFilePath = r"\\CSWAPPFEGPV01\c$\Users\rkj823_ca\Desktop\Update UDT parms 7-17-2023-One tag test.csv"
# put ignition configs into a dict so we can process our CSV file faster
with open(csvFilePath, 'rU') as csvfile:
readerObj = csv.DictReader(csvfile, delimiter=',')
for row in readerObj:
tagPathToUpdate = row["Ignition UDT Instance Path"]
Facility = row['Facility']
site = row['site']
service = row['service']
facilityid = row['facilityid']
parameters = {
'Facility': Facility,
'site': site,
'service': service,
'facilityid': facilityid
}
CSVDict[tagPathToUpdate] = parameters
browse = system.tag.browse(ignitionTagpath, filter={"tagType": "UdtInstance", "recursive": True})
for tagBrowse in browse:
CurrentTagPath = tagBrowse['fullPath']
configs = system.tag.getConfiguration(CurrentTagPath, False)
for config in configs:
configPath = str(config['path'])
TopFolder = configPath.rsplit("/", 1)[0]
if configPath in CSVDict:
config['parameters']['Facility'] = CSVDict[configPath]['Facility']
# config['parameters']['Site'] = site
# config['parameters']['Service'] = service
# config['parameters']['FacilityID'] = facilityid
system.tag.configure(TopFolder, config, "m")
system.perspective.print("Process Finished")