Script to wait for X minutes to grab data

I am using ignition Edge and trying to add script to a button such that when the button is pressed it takes the present time(startTime) and adds X minutes to it (endTime) which will be obtained from the dropdown on the screen. Now it keeps waiting for X minutes and then generates a CSV file of the data selected betwwen these two times, also during this time I want to show a popup which can show remaining time and to cancel the operation.

import csv
import datetime
startTime = system.date.now()
customer_selected_minutes=60
endTime = system.date.addMinutes(startTime,customer_selected_minutes)

> wait here
> show popup
> if cancelled from popup exit

data = system.tag.queryTagHistory(paths = ["[Edge]Pump1_engine_rpm.value"],startDate=startTime, endDate=endTime, returnSize=Return_size, aggregationMode="Average", returnFormat='Wide')
csv = system.dataset.toCSV(data)
custname =event.source.parent.getComponent("cstnm")
CustomerName = custname.text
jbn=event.source.parent.getComponent("Jobnumber")
JobNumber =jbn.text
fnm = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M.csv")
fnm1 = CustomerName+"_"+JobNumber+"_" +fnm 
filename = system.file.saveFile(fnm1)

system.file.writeFile(filename,csv)

1 Like

You are gonna let the users wait 1h to do nothing on your client? That does not seem practical…

1 Like

it is purely for collecting the test data coming from the PLC . I have other script that can pull data in between Now to X minutes backwards - the purpose is to provide them both options they can start a test for X minutes and once the time has elapsed save data to CSV file and the other option is to pull the data backwards

Alright… this is perspective or vision?

1 Like

Vision

I would create 3 client tags:

  • one that stores the selected time period and
  • one that marks that the timer is active and
  • one, an expression that checks if the timer active’s timestamp when it turned on + selected time period is greater than the current time. Add an onChange script to it which does your csv export stuff if it’s not initialChange and current value is True

E.g.
Tag: SelectedTimePeriod [integer, minutes]

Tag: TimerActive [boolean]

Tag: LogTrigger [boolean]
Expression:

if({[.]TimerActive}
    ,addMinutes({[.]TimerActive.timestamp}, {[.]SelectedTimePeriod}) > now(1000)
    ,false
)

onChange script: do your thing

The TimerActive tag is needed to stop the now() from executing all the time, at least I think this will stop it… you can also increase the update rate
Also, completely untested.

1 Like

there is an error, I am new to python scripting
Do I need to set timeractive to true as well when the button is hit
I also tried to pass the minutes to the client tag

import csv
import datetime
startTime = system.date.now()
customer_selected_min=event.source.parent.getComponent('Dropdown_Minutes').selectedValue
endTime = system.date.addMinutes(startTime,customer_selected_min)
Return_size =event.source.parent.getComponent('Dropdown_Datapoints').selectedValue
[client]SelectedTimePeriod = customer_selected_min

if({[client]TimerActive},
    ,addMinutes({[client]TimerActive.timestamp}, {[client]SelectedTimePeriod}) > now(1000)
    ,false)
    data = system.tag.queryTagHistory(paths = ["[Edge]Pump1_engine_rpm.value"],startDate=startTime, endDate=endTime, returnSize=Return_size, aggregationMode="Average", returnFormat='Wide')
    csv = system.dataset.toCSV(data)
    custname =event.source.parent.getComponent("cstnm")
    CustomerName = custname.text
    jbn=event.source.parent.getComponent("Jobnumber")
    JobNumber =jbn.text
    fnm = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M.csv")
    fnm1 = CustomerName+"_"+JobNumber+"_" +fnm 
    filename = system.file.saveFile(fnm1)
    
    system.file.writeFile(filename,csv)

You need to read client tags with system.tag.readBlocking and write using writeBlocking
Also, all of that script should be in the log trigger client tag’s tag change event script. You want the csv to be generated by the tag change script so that it doesn’t lock up your gui


it seems it is not possible to add script to tag in Edge - I do not see any option in Tag editor

Ah, you need the compute module to do any gateway scripting or tag events.
If that’s not an option, then I would use system.util.invokeAsync to run your code in your original button, so that again, it doesn’t lock up your gui.

E.g.

On “Go” button actionPerformed event handler:

def writeCSV()
	import csv
	import datetime
	startTime = system.date.now()
	customer_selected_min = event.source.parent.getComponent('Dropdown_Minutes').selectedValue
	endTime = system.date.addMinutes(startTime, customer_selected_min)
	Return_size = event.source.parent.getComponent('Dropdown_Datapoints').selectedValue

	data = system.tag.queryTagHistory(paths = ["[Edge]Pump1_engine_rpm"],
									  startDate = startTime, endDate = endTime, 
									  returnSize = Return_size, aggregationMode = "Average", returnFormat = 'Wide')
	csv = system.dataset.toCSV(data)
	CustomerName = event.source.parent.getComponent("cstnm").text
	JobNumber = event.source.parent.getComponent("Jobnumber").text
	#fnm = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M.csv")
	# prefer java date time object to Python's
    fnm = ssystem.date.format(system.date.now(), "YYYY-mm-dd-HH-MM") + '.csv'
	fnm1 = "{}_{}_{}".format(CustomerName, JobNumber, fnm)
	filename = system.file.saveFile(fnm1)

	system.file.writeFile(filename,csv)

system.util.invokeAsync(writeCSV)
1 Like