[Feature-12077]Project Script to Get Project Names

I’m looking to replicate this script from 7.9.10 to 8.0. The script first gets the project names for all the projects on the ignition server and then writes the dataset tag to a memory tag. I am trying to do the same with 8.0 but am having trouble writing the dataset information to the memory tag as my script is not writing to my memory tag. I appreciate the help.

The script for 7.9 is:

def getProjectNames():
	from com.inductiveautomation.ignition.gateway import SRContext
	from com.inductiveautomation.ignition.common.project import ProjectVersion
	projects = SRContext.get().getProjectManager().getProjectsLite(ProjectVersion.Published)
	return system.dataset.toDataSet(['Projects'], [[project.getName()] for project in projects])

def getProjectInfo():
	from com.inductiveautomation.ignition.gateway import SRContext
	from com.inductiveautomation.ignition.common.project import ProjectVersion
	
	projects = SRContext.get().getProjectManager().getProjectsLite(ProjectVersion.Staging)

	headers = [
		'Name', 
	]

	info = [
		[
		project.getName(), 
		] 
		for project in projects
	]
	return system.dataset.toDataSet(headers, info)

Then on another tag I am running the script:

projects = shared.Projects.getProjectInfo()
system.tag.write("[.]Projects",projects)

For 8.0 I am running the following script:

def getProjectNames():
	from com.inductiveautomation.ignition.gateway import IgnitionGateway
	return system.dataset.toDataSet(['Projects'], [[name] for name in IgnitionGateway.get().getProjectManager().getProjectNames()])

def getProjectInfo():
	from com.inductiveautomation.ignition.gateway import IgnitionGateway
	
	headers = [
		'Name'	
	]
	
	info = [
		[
		IgnitionGateway.get().getProjectManager().getProjectNames(),
		]
		for project in projects
	]
	
	return system.dataset.toDataSet(headers, info)

Then on a seperate tag I am running:

projects = shared.Projects.getProjectInfo()
system.tag.write("[ . ]Projects", projects)

Please edit your post to use a code block (so we can read it). Place three backquotes (these ```) on a line above your code, and again on a line below your code.

I have updated my post above.

If you can wait a few weeks, there’s going to be a system.project.getProjectNames() function within the next release or two.

2 Likes

system.project.getProjectNames() just got merged in and will be in 8.0.11.

2 Likes

@PGriffith I see this function does work from the gateway:

One thing I am wondering is how can we get project names for all EAM agents on the EAM controller?

What we want to do is take the IP address, resolve it to a DNS, and then build the perspective client string, and for this we need the project names, especially if they are not the same on each gateway (for us this is unfortunately the case).

What we would then do is offer our users a page they can go to to see all available sites, click a link and then be taken to their desired site.

Thanks,

Nick

You could setup a message handler at your EAM controller to receive project names (and collect/store them as you need) from your various EAM agent gateways. Then a scheduled script in each of those to periodically send the list of project names.

2 Likes

@kcollins1 would this be the appropriate function? I’m guessing looking at the parameters that it can reach gateways on different URLs?

https://docs.inductiveautomation.com/display/DOC81/system.util.sendMessage

Thanks,

Nick

sendMessage (can) broadcast over the gateway area network, so you’d use the gateway name(s) of the servers you want to target.

@PGriffith @kcollins1 Thanks for the advice guys, this worked without much fuss. Posting the code here in case it can help answer the same question for others:

Code on the EAM Agent (run in the gateway context to get the hostname)

host = system.net.getHostName()
projects = system.project.getProjectNames()
payload = {
    "host": host,
    "projects": projects
}
system.util.sendMessage(
    project="centralMonitoring",
    messageHandler="projectNames",
    payload=payload,
    scope="G",
    remoteServers=["Ignition EAM"]
)

Code on the EAM Controller

def handleMessage(payload):
    """
    This message handler will be called each time a message of this
    type is received.
    Arguments:
        payload: A dictionary that holds the objects passed to this
                 message handler. Retrieve them with a subscript, e.g.
                 myObject = payload['argumentName']
    """
    logger = system.util.getLogger("Agent Project Names")
    host = payload["host"]
    projects = payload["projects"]
    logger.info("Host: {}, Projects: {}".format(host, projects))