public class DefaultEventHandler extends Object
The user will register listeners that will be called when events occur.
A listener is an instance of a listener class, this listener class extends
from the EvtListenerBase object and defines the callEvent function, this function
will be called when the runEventManager()
will detect the specific type of event.
The DefaultEventHandler has a runEventManager()
function that must be called and never
exits. This function will wait for events and dispatch them to the correct listener.
This function should typically be called in its own thread. This can be done also by using
the EventHandlerThread
helper.
Modifier and Type | Method and Description |
---|---|
static void |
addIOServerListener(IOServer ioServer) |
static void |
addTagAlarmListener(EvtTagAlarmListener tagAlarmListener)
Please see
addTagValueListener for
explanation. |
static void |
addTagValueListener(EvtTagValueListener tagValueListener)
Install a listener for the given tag
|
static void |
addWebAstListener(String AstName,
EvtWebAstListener webAstListener)
This function will install an AST handler for the specific keyword.
|
static void |
addWebFormListener(String formName,
EvtWebFormListener webFormListener)
Install a listener for the given formName: http://ewon_ip/rcgi.bin/jvmForm?formName=NNNN
|
static void |
closeEvent(int eventHandler)
Used internally by Listeners when the event has been handled.
|
static void |
delIOServerListener(IOServer ioServer) |
static void |
delTagAlarmListener(EvtTagAlarmListener tagAlarmListener)
Remove a listener previousely added with
addTagAlarmListener . |
static void |
delTagValueListener(EvtTagValueListener tagValueListener)
Remove a listener previousely added with
addTagValueListener . |
static void |
runEventManager()
This is the main DefaultEventHandler function.
|
static void |
setAstFormListenerThreadMode(boolean useThread)
Defines if web AST handler are called in their own thread or not.
|
static void |
setDefaultTagAlarmListener(EvtTagAlarmListener tagAlarmListener)
Please see
setDefaultTagValueListener for
explanation. |
static void |
setDefaultTagValueListener(EvtTagValueListener tagValueListener)
This function will install a default listener for handling any value
change on any tag that has no specific listener (a specific tag listener is added with
addTagValueListener(com.ewon.ewonitf.EvtTagValueListener) ). |
static void |
setDefaultWebAstListener(EvtWebAstListener webAstListener)
This function will install the AST handler for all AST not defined by
addWebAstListener(java.lang.String, com.ewon.ewonitf.EvtWebAstListener) |
static void |
setDefaultWebFormtListener(EvtWebFormListener webFormListener)
Install a default listener for java forms on : http://ewon_ip/rcgi.bin/jvmForm...
|
static void |
setOnButtonListener(EvtListenerBase onButtonListener,
boolean useThread)
This function is used to install a handler to monitor any change of the
eWON button state.
|
static void |
setOnPppListener(EvtListenerBase onPppListener,
boolean useThread)
This function is used to install a handler to monitor connections and disconnections
of the PPP interface.
|
static void |
setOnSmsListener(EvtListenerBase onSmsListener,
boolean useThread)
This function is used to install a handler to monitor new incoming SMS.
|
static void |
setOnSwitchPortListener(EvtListenerBase onSwitchPortListener,
boolean useThread)
This function is used to install a handler to monitor any change of the
eWON 4 port switch (monitor "Link good" status).
|
static void |
setOnVpnListener(EvtListenerBase onVpnListener,
boolean useThread)
This function is used to install a handler to monitor connections and disconnections
of the VPN interface.
|
static void |
setOnWanListener(EvtListenerBase onWanListener,
boolean useThread)
This function is used to install a handler to monitor connections and disconnections
of the WAN interface.
|
static void |
setWebFormListenerThreadMode(boolean useThread)
Defines if web form handler are called in their own thread or not.
|
public static void addTagValueListener(EvtTagValueListener tagValueListener)
This function works exactly as setDefaultTagValueListener
except it is only called for the tag defined in the EvtTagValueListener
paramter.
This is how to install the handler:
MyTagValueListener evtTagValueListener = new MyTagValueListener(); evtTagValueListener.setTagName("MyTag"); DefaultEventHandler.addTagValueListener(evtTagValueListener);
Instead of instanciating the event listener and calling setTagName, you can also add a constructor to you own EventTagValueListener class, and pass the tag name during creation. A set of various constructors can be reused like that.
public class MyTagValueListener extends EvtTagValueListener { MyTagValueListener(String TagName); { super(); } public void callTagChanged() { try { System.out.println("Tag changed: "+ getTagName() + " Change Val:" +Double.toString(getChangedValueAsDouble()) + "CurrentValue: " + Double.toString(getTagValueAsDouble())); } catch (Exception ex) { } } }
Installing the handler now can be simplified with:
DefaultEventHandler.addTagValueListener(new MyTagValueListener("MyTag"));
tagValueListener
- Tag for which the event must be called.setDefaultTagValueListener(com.ewon.ewonitf.EvtTagValueListener)
public static void delTagValueListener(EvtTagValueListener tagValueListener)
addTagValueListener
.tagValueListener
- instance of your listener class.public static void setDefaultTagValueListener(EvtTagValueListener tagValueListener)
addTagValueListener(com.ewon.ewonitf.EvtTagValueListener)
).This example shows how to create a tag value event handler class.
As you can see from the example, when the callTagChanged function is called, we can call any method frompublic class MyTagValueListener extends EvtTagValueListener { public void callTagChanged() { try { System.out.println("Tag changed: "+ getTagName() + " Change Val:" +Double.toString(getChangedValueAsDouble()) + "CurrentValue: " + Double.toString(getTagValueAsDouble())); } catch (Exception ex) { } } }
EvtTagValueListener
to retrieve values related to the tag changed event itself, but also any method related to the Tag that has changed.And this is how to install the handler:
DefaultEventHandler.setDefaultTagValueListener(new MyTagValueListener());
tagValueListener
- You must define a class extending the abstract EvtTagValueListener
class. An abstract callTagChanged function has to be defined and will be called each time the
tag value is changing.TagControl
class, when the callTagChanged method
is triggered, any method from TagControl can also be called.public static void addTagAlarmListener(EvtTagAlarmListener tagAlarmListener)
addTagValueListener
for
explanation. This listener will be called in case of alarm change and not in case of tag change
but otherwise the principle is identical.tagAlarmListener
- instance of your listener class.public static void delTagAlarmListener(EvtTagAlarmListener tagAlarmListener)
addTagAlarmListener
.tagAlarmListener
- instance of your listener class.public static void setDefaultTagAlarmListener(EvtTagAlarmListener tagAlarmListener)
setDefaultTagValueListener
for
explanation. This listener will be called in case of alarm change and not in case of tag change
but otherwise the principle is identical.tagAlarmListener
- public static void addIOServerListener(IOServer ioServer)
public static void delIOServerListener(IOServer ioServer)
public static void addWebFormListener(String formName, EvtWebFormListener webFormListener)
This function works exactly as setDefaultWebFormtListener
except it is only called for the form defined in the formName
paramter.
formName
- value of the jvmForm?formName paramter for which the callForm is called.webFormListener
- See setDefaultWebFormtListener
setDefaultWebFormtListener(EvtWebFormListener)
public static void setDefaultWebFormtListener(EvtWebFormListener webFormListener)
The complete syntax is: http://ewon_ip/rcgi.bin/jvmForm?formName=NNNNN&timeout=TTTTT
Where NNNNN: Name of the form passed to the listener callForm
function.
Where TTTTT: Timeout in msec. This is the maximum amount of time the web server will wait for the java to produce the output (defaults to 60000 msec).
This default handler will be called if no addWebAstListener
has
been called for the given form name.
It is the user's responsibility to produce the output according to the FormName passed to the callForm
function.
This example shows how to create a form event handler class.
public class MyFormWebFormListener extends EvtWebFormListener { public void callForm(String formName) { System.out.println("Form: "+formName+" is called."); //log the formName obtained from web var in the RT log. System.out.println(getWebVar("formName","???")); //set the TestVar web var. setWebVar("TestVar","abcd"); //configure output as text/plain mime type (default is html) setWebHeader("text/plain"); //create a stream to output the form content. PrintStream ps = new PrintStream(this); ps.print("Hello web browser, here I am: "+getWebVar("formName","???")+getWebVar("TestVar","???")); } }
And this is how to install the handler:
DefaultEventHandler.setDefaultWebFormtListener(new MyFormWebFormListener());
webFormListener
- The listener is an instance of an EvtWebFormListener
class extension.
This extension must define the "public void callForm(String formName)
" method.
When the callForm
method is called, the event manager will pass the formName
(NNNNN) as paramter.
The callForm
will be called in the same or in a new thread depending on the configuration
done with setWebFormListenerThreadMode
.
public static void setWebFormListenerThreadMode(boolean useThread)
The default behavior is to use the Event Manager Handler thread.
This function can change the behavior.
useThread
- if true, a new thread will be created to execute the form handler.public static void addWebAstListener(String AstName, EvtWebAstListener webAstListener)
This function works exactly as setDefaultWebAstListener
, but for a
specific AST keyword instead of working for all keywords.
AstName
- Name of the AST tag this handler must respond to.webAstListener
- See setDefaultWebAstListener
public static void setDefaultWebAstListener(EvtWebAstListener webAstListener)
addWebAstListener(java.lang.String, com.ewon.ewonitf.EvtWebAstListener)
A Web AST handler while produce output in place of <%#jvmAst,NNNNN%> tag when
produceAstPage
is called.
This example shows how to create an AST event handler class.
public class WebAstListener extends EvtWebAstListener { public void callAst(String astName) { try { String Output = " Ast " + astName + " called " + getWebVar("formName","???"); write(Output.getBytes()); } catch (IOException ex) {} } }
And this is how to install the handler:
DefaultEventHandler.setDefaultWebAstListener(webAstListener);
webAstListener
- The listener is an instance of a EvtWebAstListener
class extension.
This class must define the public void callAst(String astName)
function and according to the astName
paramter must output the
AST content using write
or using any stream oriented function.
The callAst
will be called in the same or in a new thread depending on the configuration
done with setAstFormListenerThreadMode
.
public static void setAstFormListenerThreadMode(boolean useThread)
The default behavior is to use the Event Manager Handler thread.
This function can change the behavior.
useThread
- if true, a new thread will be created to execute the AST handler.public static void setOnWanListener(EvtListenerBase onWanListener, boolean useThread)
See setOnVpnListener(EvtListenerBase,boolean)
for and example of listener class.
and how to install it.
onWanListener
- Instance of a EvtListenerBase class extension.
This extension class must define the public void callEvent(String Status)
method. In this method, the Status
parameter is a string = "1"
when WAN connection occurs, and "0", when disconnection occurs.
useThread
- When the callEvent
function is called this paramters defined
if a new thread is created to execute the call in background, or if the callEvent
function is called in the context of the event handler thread, thus preventing any other
event that could happen from beeing processed before the callEvent function returns.public static void setOnPppListener(EvtListenerBase onPppListener, boolean useThread)
See setOnVpnListener(EvtListenerBase,boolean)
for and example of listener class.
and how to install it.
onPppListener
- Instance of a EvtListenerBase class extension.
This extension class must define the public void callEvent(String Status)
method. In this method, the Status
parameter is a string = "1"
when PPP connection occurs, and "0", when disconnection occurs.
useThread
- When the callEvent
function is called this paramters defined
if a new thread is created to execute the call in background, or if the callEvent
function is called in the context of the event handler thread, thus preventing any other
event that could happen from beeing processed before the callEvent function returns.public static void setOnVpnListener(EvtListenerBase onVpnListener, boolean useThread)
This example shows how to create a button event handler class.
public class OnNetListener extends EvtListenerBase { private String ListenerName; public void callEvent(String Status) { System.out.println(ListenerName+" Event: "+Status); } OnNetListener(String DListenerName) { super(); ListenerName = DListenerName; } }
And this is how to install the handler:
// The handler function will be called in its own thread. DefaultEventHandler.setOnVpnListener(new OnNetListener("VPN"), true);
onVpnListener
- Instance of a EvtListenerBase class extension.
This extension class must define the public void callEvent(String Status)
method. In this method, the Status
parameter is a string = "1"
when VPN connection occurs, and "0", when disconnection occurs.
useThread
- When the callEvent
function is called this paramters defined
if a new thread is created to execute the call in background, or if the callEvent
function is called in the context of the event handler thread, thus preventing any other
event that could happen from beeing processed before the callEvent function returns.public static void setOnButtonListener(EvtListenerBase onButtonListener, boolean useThread)
This example shows how to create a button event handler class.
public class OnButtonListener extends EvtListenerBase { //status== "1" if button is pressed "0" if release. public void callEvent(String status) { System.out.println("Button state changes: "+ status); } }
And this is how to install the handler:
// The handler function will be called in its own thread. DefaultEventHandler.setOnButtonListener(new OnButtonListener(), true);
onButtonListener
- object must extend the EvtListenerBase
class and overide
the callEvent of this class.useThread
- When the callEvent
function is called this paramters defined
if a new thread is created to execute the call in background, or if the callEvent
function is called in the context of the event handler thread, thus preventing any other
event that could happen from beeing processed before the callEvent function returns.public static void setOnSmsListener(EvtListenerBase onSmsListener, boolean useThread)
This example shows how to create a button event handler class.
public class OnSmsListener extends EvtListenerBase { public void callEvent(String NbSmsTxt) { SmsMessage smsMessage; System.out.println("NbSms: "+NbSmsTxt); try { smsMessage = ModemManager.readSms(); System.out.println("SmsNdx: "+smsMessage.getSmsNdx()); System.out.println("SmsDateTime"+ new Date(smsMessage.getSmsDataTime()).toString()); System.out.println("SmsDateTime"+Long.toString(smsMessage.getSmsDataTime())); System.out.println("SmsFrom: "+smsMessage.getSmsFrom()); System.out.println("SmsMessage: "+smsMessage.getSmsMessage()); } catch (Exception e) { System.out.println("Error: "+e.toString()); } } }
And this is how to install the handler:
// The handler function will be called in its own thread. DefaultEventHandler.setOnSmsListener(new OnSmsListener(), true);
onSmsListener
- object must extend the EvtListenerBase
class and overide
the callEvent of this class.
When the callEvent is called, the ModemManager.readSms()
should be called to
extract the SMS received object from the queue and return all its properties.
See also the SmsMessage
class for a description of this object.
useThread
- When the callEvent
function is called this paramters defined
if a new thread is created to execute the call in background, or if the callEvent
function is called in the context of the event handler thread, thus preventing any other
event that could happen from beeing processed before the callEvent function returns.public static void setOnSwitchPortListener(EvtListenerBase onSwitchPortListener, boolean useThread)
This example shows how to create a switch port event handler class.
public class OnSwitchPort extends EvtListenerBase { //status== "X" with X is an integer where bit 0 is switch port 1, bit 1 is switch port 2 etc... public void callEvent(String status) { System.out.println("Switch change summary: "+ status); } }
And this is how to install the handler:
// The handler function will be called in its own thread. DefaultEventHandler.setOnSwitchPortRegistry(new OnSwitchPortListener(), true);
onSwitchPortListener
- object must extend the EvtListenerBase
class and overide
the callEvent of this class.useThread
- When the callEvent
function is called this paramters defined
if a new thread is created to execute the call in background, or if the callEvent
function is called in the context of the event handler thread, thus preventing any other
event that could happen from beeing processed before the callEvent function returns.public static void runEventManager() throws Exception
This function will run forever until the JVM is stopped. Eventually, this function should be called in its own thread, because the JVM would be blocked otherwise.
Exception
public static void closeEvent(int eventHandler)
Do not use this function.