eWON JTK

com.ewon.ewonitf
Class DefaultEventHandler

java.lang.Object
  extended by com.ewon.ewonitf.DefaultEventHandler

public class DefaultEventHandler
extends Object

This static class will manage all the events that occure in the system and that may require some attention from the JAVA application.

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.


Method Summary
static void addIOServerListener(IOServer ioServer)
           
static void addMqttListener(MqttClient mqttClientListener)
          Install a listener for the given tag
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 delMqttListener(MqttClient mqttClientListener)
          Remove a listener previousely added with addTagValueListener.
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.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addTagValueListener

public static void addTagValueListener(EvtTagValueListener tagValueListener)
Install a listener for the given tag

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"));
 

Parameters:
tagValueListener - Tag for which the event must be called.
Since:
javaetk 1.3
See Also:
setDefaultTagValueListener(com.ewon.ewonitf.EvtTagValueListener)

delTagValueListener

public static void delTagValueListener(EvtTagValueListener tagValueListener)
Remove a listener previousely added with addTagValueListener.

Parameters:
tagValueListener - instance of your listener class.
Since:
javaetk 1.3

setDefaultTagValueListener

public 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)).

This example shows how to create a tag value event handler class.

 
 
    public 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)
            {
            }
        }
    }
 
As you can see from the example, when the callTagChanged function is called, we can call any method from EvtTagValueListener to retrieve values related to the tag changed event itself, but also any method related to the Tag that has changed.
The TagControl object is made pointing to the changed tag before to callTagChanged method is called.

And this is how to install the handler:

   DefaultEventHandler.setDefaultTagValueListener(new MyTagValueListener());
 

Parameters:
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.
The listener is also extending the TagControl class, when the callTagChanged method is triggered, any method from TagControl can also be called.
Since:
javaetk 1.3

addTagAlarmListener

public static void addTagAlarmListener(EvtTagAlarmListener tagAlarmListener)
Please see 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.

Parameters:
tagAlarmListener - instance of your listener class.
Since:
javaetk 1.3

delTagAlarmListener

public static void delTagAlarmListener(EvtTagAlarmListener tagAlarmListener)
Remove a listener previousely added with addTagAlarmListener.

Parameters:
tagAlarmListener - instance of your listener class.
Since:
javaetk 1.3

setDefaultTagAlarmListener

public static void setDefaultTagAlarmListener(EvtTagAlarmListener tagAlarmListener)
Please see 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.

Parameters:
tagAlarmListener -
Since:
javaetk 1.3

addMqttListener

public static void addMqttListener(MqttClient mqttClientListener)
Install a listener for the given tag

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"));
 

Parameters:
mqttClientListener - Tag for which the event must be called.
Since:
javaetk 1.3
See Also:
setDefaultTagValueListener(com.ewon.ewonitf.EvtTagValueListener)

delMqttListener

public static void delMqttListener(MqttClient mqttClientListener)
Remove a listener previousely added with addTagValueListener.

Parameters:
mqttClientListener - instance of your listener class.
Since:
javaetk 1.3

addIOServerListener

public static void addIOServerListener(IOServer ioServer)

delIOServerListener

public static void delIOServerListener(IOServer ioServer)

addWebFormListener

public static void addWebFormListener(String formName,
                                      EvtWebFormListener webFormListener)
Install a listener for the given formName: http://ewon_ip/rcgi.bin/jvmForm?formName=NNNN

This function works exactly as setDefaultWebFormtListener except it is only called for the form defined in the formName paramter.

Parameters:
formName - value of the jvmForm?formName paramter for which the callForm is called.
webFormListener - See setDefaultWebFormtListener
See Also:
setDefaultWebFormtListener(EvtWebFormListener)

setDefaultWebFormtListener

public static void setDefaultWebFormtListener(EvtWebFormListener webFormListener)
Install a default listener for java forms on : http://ewon_ip/rcgi.bin/jvmForm...

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());
 

Parameters:
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.


setWebFormListenerThreadMode

public static void setWebFormListenerThreadMode(boolean useThread)
Defines if web form handler are called in their own thread or not.

The default behavior is to use the Event Manager Handler thread.

This function can change the behavior.

Parameters:
useThread - if true, a new thread will be created to execute the form handler.

addWebAstListener

public static void addWebAstListener(String AstName,
                                     EvtWebAstListener webAstListener)
This function will install an AST handler for the specific keyword.

This function works exactly as setDefaultWebAstListener, but for a specific AST keyword instead of working for all keywords.

Parameters:
AstName - Name of the AST tag this handler must respond to.
webAstListener - See setDefaultWebAstListener

setDefaultWebAstListener

public 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)

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);
 

Parameters:
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.


setAstFormListenerThreadMode

public static void setAstFormListenerThreadMode(boolean useThread)
Defines if web AST handler are called in their own thread or not.

The default behavior is to use the Event Manager Handler thread.

This function can change the behavior.

Parameters:
useThread - if true, a new thread will be created to execute the AST handler.

setOnWanListener

public static void setOnWanListener(EvtListenerBase onWanListener,
                                    boolean useThread)
This function is used to install a handler to monitor connections and disconnections of the WAN interface.

See setOnVpnListener(EvtListenerBase,boolean) for and example of listener class. and how to install it.

Parameters:
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.

setOnPppListener

public static void setOnPppListener(EvtListenerBase onPppListener,
                                    boolean useThread)
This function is used to install a handler to monitor connections and disconnections of the PPP interface.

See setOnVpnListener(EvtListenerBase,boolean) for and example of listener class. and how to install it.

Parameters:
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.

setOnVpnListener

public static void setOnVpnListener(EvtListenerBase onVpnListener,
                                    boolean useThread)
This function is used to install a handler to monitor connections and disconnections of the VPN interface.

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);
 

Parameters:
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.

setOnButtonListener

public static void setOnButtonListener(EvtListenerBase onButtonListener,
                                       boolean useThread)
This function is used to install a handler to monitor any change of the eWON button state.

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);
 

Parameters:
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.

setOnSmsListener

public static void setOnSmsListener(EvtListenerBase onSmsListener,
                                    boolean useThread)
This function is used to install a handler to monitor new incoming SMS.

This example shows how to create a SMS 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);
 

Parameters:
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.

setOnSwitchPortListener

public 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).

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);
 

Parameters:
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.

runEventManager

public static void runEventManager()
                            throws Exception
This is the main DefaultEventHandler function. It is the events handler loop.

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.

Throws:
Exception

closeEvent

public static void closeEvent(int eventHandler)
Used internally by Listeners when the event has been handled.

Do not use this function.


eWON JTK

www.eWON.biz: Your Eyes Watching Over Net