Calling the system.util.sendMessage function from a Module?

Is it possible to call the system.util.sendMessage function from an Ignition Module?

Not directly, but you could access the same API from your module that the system.util.sendMessage() functions use to send a message.

Are you trying to send from gateway or client scope?

Thanks Kevin for the valuable information.
I’ll try and find the API functions that I need.

Actually I’m trying to use the sendMessage function to send messages to another gateway Module.
Not sure if this is possible? So I would also need a listener in my Module.

To answer your question I’m trying to send from gateway scope.

What you will have to do is dispatch the message using MessageDispatchManager, and then you need to set up a message handler in a project that can call a script function in your module (example shown below).

Send code (Java):

MessageDispatchManager mdm = gatewayContext.getMessageDispatchManager();

PyDictionary payload = new PyDictionary(new PyObject[] {
        new PyString("Source"), new PyString("Gateway"),
        new PyString("Message"), new PyString("testMessage")
});

// Limit to Gateway scope
Properties filterParams = new Properties();
filterParams.setProperty(MessageDispatchManager.KEY_SCOPE, MessageDispatchManager.SCOPE_GATEWAY_ONLY);

// You must specify a project, as message handlers are configured in projects. 
mdm.dispatch("MyProject",
	"MyMessageHandler",
    payload,
    filterParams);

Jython Message handler in a project, which must be named “MyMessageHandler”:

def handleMessage(payload):
	mymodule.myFunction(payload)

Brilliant!
That worked.
Thank you very much.

I have a message setup this way, and it has been working for several days. Today I was looking at the logs and discovered that it is throwing an error. I’m not quite sure what this error is telling me since the popup message is actually working.

INFO | jvm 1 | 2017/05/19 09:52:49 | E [c.i.i.c.s.m.MessageDispatchManager] [16:52:49]: Gateway script MessageHandlerException, project ‘zzPowertrain_Battery’ , message handler ‘GenericMessage’
INFO | jvm 1 | 2017/05/19 09:52:49 | com.inductiveautomation.ignition.common.script.message.MessageHandlerException: The message handler “GenericMessage” could not be found! Check your event script message handlers.
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.common.script.message.MessageHandlerManager.processMessage(MessageHandlerManager.java:168)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.common.script.ScriptConfig.invokeGWMessageHandler(ScriptConfig.java:191)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.gateway.project.ProjectLifecycle.dispatchScriptMessage(ProjectLifecycle.java:431)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.gateway.project.ProjectManagerImpl.invokeGWMessageHandler(ProjectManagerImpl.java:2066)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.gateway.clientcomm.scriptmsg.MessageDispatchManagerImpl.doGatewayNotification(MessageDispatchManagerImpl.java:304)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.gateway.clientcomm.scriptmsg.MessageDispatchManagerImpl.dispatchLocal(MessageDispatchManagerImpl.java:158)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.gateway.clientcomm.scriptmsg.MessageDispatchManagerImpl.dispatch(MessageDispatchManagerImpl.java:98)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.gateway.script.GatewaySystemUtilities.sendMessageInternal(GatewaySystemUtilities.java:147)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.common.script.builtin.SystemUtilities.sendMessage(SystemUtilities.java:675)
INFO | jvm 1 | 2017/05/19 09:52:49 | at sun.reflect.GeneratedMethodAccessor106.invoke(Unknown Source)
INFO | jvm 1 | 2017/05/19 09:52:49 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
INFO | jvm 1 | 2017/05/19 09:52:49 | at java.lang.reflect.Method.invoke(Unknown Source)
INFO | jvm 1 | 2017/05/19 09:52:49 | at org.python.core.PyReflectedFunction.call(PyReflectedFunction.java:186)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.call(ScriptManager.java:438)
INFO | jvm 1 | 2017/05/19 09:52:49 | at org.python.core.PyObject.call(PyObject.java:320)
INFO | jvm 1 | 2017/05/19 09:52:49 | at org.python.pycode.pyx9690.valueChanged$1(tagevent:valueChanged:22)
INFO | jvm 1 | 2017/05/19 09:52:49 | at org.python.pycode.pyx9690.call_function(tagevent:valueChanged)
INFO | jvm 1 | 2017/05/19 09:52:49 | at org.python.core.PyTableCode.call(PyTableCode.java:165)
INFO | jvm 1 | 2017/05/19 09:52:49 | at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
INFO | jvm 1 | 2017/05/19 09:52:49 | at org.python.core.PyFunction.function___call
(PyFunction.java:376)
INFO | jvm 1 | 2017/05/19 09:52:49 | at org.python.core.PyFunction.call(PyFunction.java:371)
INFO | jvm 1 | 2017/05/19 09:52:49 | at org.python.core.PyFunction.call(PyFunction.java:361)
INFO | jvm 1 | 2017/05/19 09:52:49 | at org.python.core.PyFunction.call(PyFunction.java:356)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:663)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$FunctionInvokerImpl.run(TagScriptManager.java:414)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.common.sqltags.scripts.AbstractTagScript.invoke(AbstractTagScript.java:34)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$Task.invoke(TagScriptManager.java:353)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$TagScriptDispatcher.run(TagScriptManager.java:318)
INFO | jvm 1 | 2017/05/19 09:52:49 | at com.inductiveautomation.ignition.common.execution.impl.BasicExecutionEngine$ThrowableCatchingRunnable.run(BasicExecutionEngine.java:546)
INFO | jvm 1 | 2017/05/19 09:52:49 | at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
INFO | jvm 1 | 2017/05/19 09:52:49 | at java.util.concurrent.FutureTask.run(Unknown Source)
INFO | jvm 1 | 2017/05/19 09:52:49 | at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
INFO | jvm 1 | 2017/05/19 09:52:49 | at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
INFO | jvm 1 | 2017/05/19 09:52:49 | at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
INFO | jvm 1 | 2017/05/19 09:52:49 | at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
INFO | jvm 1 | 2017/05/19 09:52:49 | at java.lang.Thread.run(Unknown Source)

Any ideas from anyone? Still have a working popup message and still an error showing in the log file. Strange.

Update:
Actually I just noticed that this thread is about calling this function from a third party module, but in my case it is erroring within Ignition itself (no third part module).

Check your calls to sendMessage(). You need to limit to client scope if you haven’t defined a matching message handler in the gateway event scripts, too.

1 Like

Thank you very much. I guess I did not read the documentation closely enough. The default type is “CG”. I just needed to add the scope=“C” to my sendMessage() command and the error in the logger is no longer appearing.