Importing Java Libraries and accessing Java Classes from script console

How to access the getAvailableConnections() method from com.inductiveautomation.ignition.gateway.gan using the script console ?

When I try this

“from com.inductiveautomation.ignition.gateway import gan” I get the following error

Traceback (most recent call last):
File “”, line 1, in
ImportError: cannot import name gan

Also if I manually import the Java classes with a custom module,
I get “getAvailableConnections(): expected 1 args; got 0” when I run
[print GatewayNetworkManager.getAvailableConnections()] from the script console.

You don’t, that’s something that is only available in gateway scope.

Maybe you can take a few steps back and talk about what you’re ultimately trying to accomplish.

Kevin,

My end goal is to create a script that can auto approve incoming connections from a dockerized instance of Ignition.

I am trying to figure out if I can access these classes and methods using the python script instead of creating a custom module.

Thanks in Advance

If you want to accept all incoming connections automatically it would be easier to just set the Connection Policy setting for the gateway network to “Unrestricted” instead of “ApprovedOnly”.

Kevin,

Need to approve the certificates also automatically because for every pod restart a new certificate will be generated

Kevin,
I have created a module based on the ignition-scripting-function sdk where I am trying to read the Incoming connection Id and output it to the logger but I am not sure how or from where I can call this method? What’s the correct way to read the output of a method that is available only in the gateway scope?

GatewayScriptModule

package com.inductiveautomation.ignition.examples.scripting;

import com.inductiveautomation.ignition.gateway.gan.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class GatewayScriptModule extends AbstractScriptModule {

    private final Logger logger = LoggerFactory.getLogger("GAN");
    @Override
    protected int multiplyImpl(int arg0, int arg1) {
        return arg0 * arg1;
    }

    public long connId(){
        long conn = new IncomingConnection().getId();

        if ((System.currentTimeMillis() / 1000)%10 == 0){
            String s =String.valueOf(conn);
            logger.info(s);}
        return conn;
    }





}

GatewayModuleHook

package com.inductiveautomation.ignition.examples.scripting;

import com.inductiveautomation.ignition.common.licensing.LicenseState;
import com.inductiveautomation.ignition.common.script.ScriptManager;
import com.inductiveautomation.ignition.common.script.hints.PropertiesFileDocProvider;
import com.inductiveautomation.ignition.gateway.clientcomm.ClientReqSession;
import com.inductiveautomation.ignition.gateway.model.AbstractGatewayModuleHook;
import com.inductiveautomation.ignition.gateway.model.GatewayContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class GatewayHook extends AbstractGatewayModuleHook {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final GatewayScriptModule scriptModule = new GatewayScriptModule();


    @Override
    public void setup(GatewayContext gatewayContext) {
        logger.info("setup()");
    }

    @Override
    public void startup(LicenseState licenseState) {
        logger.info("startup()");
    }

    @Override
    public void shutdown() {
        logger.info("shutdown()");
    }

    @Override
    public void initializeScriptManager(ScriptManager manager) {
        super.initializeScriptManager(manager);

        manager.addScriptModule(
                "system.example",
                scriptModule,
                new PropertiesFileDocProvider());
    }

    @Override
    public Object getRPCHandler(ClientReqSession session, String projectName) {
        return scriptModule;
    }
}

See this topic:

Especially note the comment about the lameness of the script module example for this case.

1 Like

@pturmel Thank you for your response. I am still new to Ignition’s SDK development and Java and I can’t seem to figure out why you redirected me to that post. Please correct me if I am wrong but my understanding of the RPC is to serve as a connection between the Designer/Client and the Gateway but since I am running only in the Gateway scope, I am not sure if the RPC is the problem in my instance.

Also when I try to call the connId() method and output it’s contents to the logger, I get the following error

Error running action 'dom.onClick' on yt@D/root/Button: Traceback (most recent call last): File "<function:runAction>", line 2, in runAction at simpleorm.dataset.SFieldScalar.getRawFieldValue(SFieldScalar.java:132) at simpleorm.dataset.SRecordInstance.getObject(SRecordInstance.java:228) at simpleorm.dataset.SRecordInstance.getLong(SRecordInstance.java:456) at com.inductiveautomation.ignition.gateway.gan.IncomingConnection.getId(IncomingConnection.java:35) at com.inductiveautomation.ignition.examples.scripting.GatewayScriptModule.connId(GatewayScriptModule.java:16) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) simpleorm.utils.SException$Error: simpleorm.utils.SException$Error: Cannot get unretrieved/unset field [IncomingConnection ].[F IncomingConnection.wsincomingconnection_ID]

Aside from any other issues, what you’re ultimately trying to do won’t work as-is. You can’t just create a new instance of com.inductiveautomation.ignition.gateway.gan.IncomingConnection and expect things to work.

You need to ask the PersistenceInterface (available from the GatewayContext you get in setup) for existing IncomingConnection records. But even if you get them, I’m not sure just blindly setting the security status (if that’s what you’re ultimately trying to do) will actually do anything.

The incoming connection is the session, which is available in the gateway hook when the RPC subsystem looks up your script module. But the sample code doesn’t make unique RPC handlers per session, so that information is lost. You are creating a new connection object in your code, but that would have no bearing on the caller of your script.

Oh, and the script console is not in gateway scope. It is in designer scope, which is much like vision client scope.

Oooo! Incoming GAN connection. Yeah, what Paul said.

@pturmel and @PGriffith , thank you for your response, My ultimate goal is to have the remote certificates for Incoming GAN connection be approved without manual intervention. What’s your recommendation on trying to implement this? Can you point me in the right direction.

Also, I don’t see the Incoming Remote certificates for GAN in the internal db?

@pturmel is calling the method via button in perspective session considered a gateway scope?

Yes.

@pturmel and @PGriffith , can you please tell me how to use Squery to query existing records from the internal db that shows all the fields?

When I try the following

	from com.inductiveautomation.ignition.gateway import IgnitionGateway
	from com.inductiveautomation.ignition.gateway.gan import WSConnectionSettings
	from com.inductiveautomation.ignition.gateway.gan import IncomingConnection
	from simpleorm.dataset import SQuery,SQueryMode
	
	gateway = IgnitionGateway.get()
	pi = gateway.getPersistenceInterface()
	gan_manager = gateway.getGatewayAreaNetworkManager()
	connections = str(gan_manager.getInboundConnections())
	
	query = SQuery(IncomingConnection.META)
	record = pi.query(query)
	
	logger = system.util.getLogger("myLogger")
	logger.info(str(record))

I get
[[IncomingConnection 5]]

which is correct because in the internal db, I see this

but how can I get all the other columns in the output?