Help: Posting from Gateway Tag Change/Event to Webhook?

Good morning KB, welcome to the forum.

And great first post. You are on the right track, I think. But some items to change to ensure you get good diagnostics:

  • Move your creation of your httpClient outside the function. The client object is designed to be re-used, and can cause memory leaks if you keep making more of them. Like so:
#  ....
client = system.net.httpClient()

def postTagChangeData(event):
# ....
  • In your event, remove the import from dataPublisher. Never import from project library scripts. They auto-load when referenced, so the event script should be a one-liner:
dataPublisher.postTagChangeData(event)
  • Your except clause calls out jython's Exception, which does not catch java exceptions. Different inheritance hierarchy. I recommend using two clauses, and making the jython exceptions more logger-friendly. You need java's Throwable class in your imports, like so:
from java.lang import Throwable

Then your catch clauses should look like this:

    except Throwable as t:
        # Log any exceptions that occur
        logger.error("Java Error sending data to Losant", t)
    except Exception as e:
        # Log any exceptions that occur
        logger.error("Jython Error sending data to Losant", later.PythonAsJavaException(e))

Notice that java loggers accept Throwable last arguments and will place the full backtrace in your logs. Place my later.py script in your project library to have the wrapper that converts jython exceptions into java logger-compatible exceptions.

With this last change, the real problem will likely be logged.

Side note: If you are feeling brave, you can try the new, more capable, exception wrapper I posted yesterday:

2 Likes