I have a property change script on a radio button of an inheratable project . It seems like the script is been executed multiple times (so in each child project). What can I do to avoid this? I thought of storing the script in another project that is not inheritable, but since it is a component event script, I don't know how to realise this.
Thats what the script does: calculate the history and store it in a dataset
if event.propertyName=="actu" or event.propertyName=="selected" :
if (event.source.selected==True and event.source.actu==True) or event.source.selected==True:
start=event.source.parent.getComponent('CalStart').date
ende=event.source.parent.getComponent('CalEnd').date
folderPath = event.source.parent.dataPath
folderArchiv = folderPath + "/AlarmData/alarmArchiv"
modifiedHistoryDataset = system.dataset.toDataSet(["ID","Description", "Start", "End", "Duration"], [])
tagPaths=system.tag.readBlocking(folderPath +"/AlarmData/alarmList")[0].value
startTimestamp=None
for x in range(tagPaths.rowCount):
identification = tagPaths.getValueAt(x,0)
description =tagPaths.getValueAt(x,1)
tagPath = tagPaths.getValueAt(x,2)
history = system.tag.queryTagHistory([tagPath],start,ende, aggregationMode="LastValue")
for i in range(history.rowCount):
if history.getValueAt(i,1) == 1: # Check if the tag value is 1
startTimestamp = history.getValueAt(i,0)
endTimestamp=history.getValueAt(i + 1, 0) if i<history.rowCount-1 else system.date.now()
if startTimestamp is not None and endTimestamp is not None:
formatted_duration = shared.Func_Dataset.calculate_duration(startTimestamp, endTimestamp)
startFormatted = system.date.format(startTimestamp, "dd.MM.yyyy HH:mm:ss")
endFormatted = system.date.format(endTimestamp, "dd.MM.yyyy HH:mm:ss")
modifiedHistoryDataset = system.dataset.addRow(modifiedHistoryDataset, [identification, description, startFormatted, endFormatted, formatted_duration])
modifiedHistoryDataset=system.dataset.sort(modifiedHistoryDataset, 2, False)
system.tag.writeBlocking(folderArchiv, modifiedHistoryDataset)
event.source.actu=False
I have tested the code in a script console and for the same time range I got with the script console 54 entries in the dataset but with the component event script there are more than 7250 entries and the entries are duplicated, sometimes x2 or x3 but sometimes even x10.
I can't tell you why it would execute multiple times, but frankly I'm not even sure that's what's actually happening. I can't see how multiple executions would result in duplicated data, since you're overwriting the dataset anyway.
What I can tell you for sure is that this script needs some work:
Try not to do multiple call queryTagHistory in a loop, it takes a list of paths to query all of them at once
Don't store formatted dates. Dataset can stored actual date objects.
Can you tell us what you're trying to achieve with this ? What's the endgoal ?
I kinda feel like you're going in the wrong direction.
It works when I save the script in the property change of both calendar components instead.
if event.propertyName=="date":
ende = event.source.date
start = event.source.parent.getComponent('CalStart').date
if event.source.parent.getComponent("radioButton").selected:
I want to display for a given time range the start, end and duration when an alarm was on.
No it doesn't help to call queryTagHistory just once. The tagPaths are store in a dataset, to store it in a list, I first have to iterate through the entire dataset and retrieve them. Then the result I get is a dataset with number columns= number tagPaths +1 and number rows= number of different timestamps. With this structure, in order to calculate the duration each time each tag was on, I have to go through all columns and rows and that takes much more time.