How to get the project default datasource inside module

I am trying to get the default datasource of the project from inside the module to create SRConnection to the right database dynamically. Please suggest the right approach
thanks.

What scope? If you are in the client or designer, you can call

context.getDefaultDatasourceName().

From the gateway, you would do something similar to the following:

r = context.getProjectManager().getProject("acme_inc",ApplicationScope.parseScope("A"),ProjectVersion.Published).getResourceOfType("","sr.global.props")

gp = XMLDeserializer.deserialize(context.createDeserializer(), r.getData(), None)

return gp.getDefaultDatasourceName()
1 Like

Thanks Kyle, But we are trying to create a module per our requirements and uploading it on Ignition gateway and accessing methods inside that module from the designer.
To make this module generic, we are looking for a way to get the default datasource of the project. In the above approach I will have to hard code the project name. This should also be coming by default so that the module methods can be used by any project for their default database name.
I got stuck into something else hence could not respond you.

The GatewayHook method getRPCHandler() is given a session object and a projectId. You must instantiate your RPC handler with those as constructor parameters and save them for later use in your RPC method implementations.
The simple example in the Module SDK Documentation for Designer-Gateway communications uses a “throwaway” RPC implementation object. In your case, you should cache these objects based on the session, and maintain any client- or designer-specific state within. Use a WeakHashMap for the cache so you don’t leak memory of dead sessions.

Well, you need a way to dictate the desired project. There are a few ways to do that, but I can not help without further details.

You replace "acme_inc" in your example with the projectId from the getRPCHandler() method. ProjectManager has an overload of getProject() for that.

Yeah, I was just referring to the fact that, depending on the scope, there are numerous ways to get the project name or id.

You keep mentioning this, but Datasource and SRConnection are gateway-only classes.

You can use the RPC handler to send the project id down as you stated. You could also create a module where you define what project scope to run events in, from the gateway. Like I said, it all depends on what his module is doing. Also, projects exist in the gateway scope as well, so an RPC would not be needed.

Kumar’s first follow-up answers this – he’s trying to get the right project for a designer session. To then use gateway classes. Which can only mean resolving the project on the gateway side of an RPC. (-:

Ah I missed that part. RPC handler it is!!

1 Like