Default Listeners

Listeners are interfaces that define behaviors. They are used for HTTP, REST, MQTT, I2C, etc... To develop a behavior in GreenLighting, you will always be using listener.

GreenLightning provides default listener interfaces to develop highly-specific applications. Below is an overview of the most important ones.

Behaviors tend to be placed in their own package named behaviors, but this is not necessary and only helps with the project structure.

Listeners

RestListener

Provides an interface to listen to HTTP requests. See the Simple REST Server for a more detailed example on how to read and respond to requests. See Posting and Responding with JSON on how to publish a JSON response.

public class RestListenBehavior implements RestListener {
    @Override
    public boolean restRequest(HTTPRequestReader request) {
      // Process the HTTP request here.
      // Ideally, you would publish a HTTP response here, e.g.:
      // return responseService.publishHTTPResponse(request, 200, 
      //				 HTTPContentTypeDefaults.PLAIN, w-> {
	  //			 w.append("Hello World!");
	  //				 });
      
      // If return false, 404 code is returned by default.
      return false;
    }
}

StartupListener and ShutdownListener

Provides an interface for knowing when a behavior gets started and when a shutdown is requested.

You can register a StartupListener or ShutdownListener inside declareBehavior:

PubSubListener

See Publish & Subscribe for more on this.

Provides an interface for publishing and subscribing to messages. Usually, a PubSubFixedTopicService is used to publish to a specific topic.

It is recommended to use a PubSubFixedTopicService instead of directly calling a topic string for publishing, unless you require multiple PubSubs inside one single behavior.

You can also publish a message:

A PubSubListener is registered like this inside declareBehavior:

PubSubMethodListener

See Publish & Subscribe for this.

TimeListener

Provides an interface to provide timing logic inside your behavior.

Timer pulse rate can be configured in the GreenApp declareConfiguration:

And registered like this:

Last updated