Call a python method when powering off UR

I have a python daemon running in the background of my URsim and the python file also has a serial connection to a motor.

I have a method in the python script like so:
image

Although there isn’t a huge problem without closing the connection. I would like to know if there is a way to call this method directly before the robot powers down.

I know that the function

public void stop(BundleContext context)

in the Activator runs when powering the robot down, but I am unable to reference the XmlRpc interface (declared in the InstallationContributionNode) to call a method in the python daemon.

Thanks for reading!

2 Likes

You can do something like this:

public class Activator implements BundleActivator {
  private static yourDaemonService daemon_service_;
  private static yourInstallationNodeService installation_service_;

  @Override
  public void start(final BundleContext context) throws Exception {
    daemon_service_ = new yourDaemonService();
    installation_service_ = new yourInstallationNodeService(daemon_service_);
    context.registerService(DaemonService.class, daemon_service_, null);
    context.registerService(SwingInstallationNodeService.class, installation_service_, null);

    // ...
  }

  @Override
  public void stop(BundleContext context) throws Exception {
    installation_service_.getContribution().disconnect();
    // where the disconnect() method calls your close_serial() RPC
  }
}
4 Likes

Thanks! It seems to be working.

To add on, add a try catch around the daemon python method call.

Usually in the contribution when you call a python method via XmlRpcInterface you throw exceptions in the java method, and it requires you to handle the exception. However, it seem that when you call the XmlRpc java function (in this case)

installation_service_.getContribution().disconnect();

you will not get a error message in java telling you to handle the exception, but you still need to. (I tested it with an error, and a the daemon service doesn’t close if you don’t handle the exception if you get one)

1 Like