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

Although there are multiple methods to register a listener inside declareBehavior, never register a single behavior implementing multiple listeners more than once. This will cause errors on startup.

One call to registerListener will automatically register everything required.

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.

public class StartupBehavior implements StartupListener {
    @Override
    public void startup() {
        // Do something on startup.
    }
}

public class ShutdownBehavior implements ShutdownListener {
    @Override
	public boolean acceptShutdown() {
    	// Do logic here before shutdown and return true if ready for shutdown.
		// If you're not ready for shutting down, return false to delay and try again in a little bit.
		// Finish whatever you were doing to shutdown, then return true.
    	return true; 
	}
}

You can register a StartupListener or ShutdownListener inside declareBehavior:

public void declareBehavior(GreenRuntime runtime) {
    runtime.addStartupListener(new StartupBehavior());
    runtime.addShutdownListener(new ShutdownBehavior());
}

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.

public class ExampleBehavior implements PubSubMethodListener {
    private final PubSubFixedTopicService channel;
    
    public ExampleBehavior(GreenRuntime runtime) {
        this.channel = runtime.newCommandChannel().newPubSubService("TOPIC_TO_PUBLISH_TO");    
    }
    
    public boolean message(CharSequence topic, ChannelReader payload) {
        ...
        // As a response to something, publish to the topic defined above.
        return channel.publishTopic();
    }
}

You can also publish a message:

return channel.publishTopic(w -> w.append("Message!"));

A PubSubListener is registered like this inside declareBehavior:

public void declareBehavior(GreenRuntime runtime) {
    runtime.addPubSubListener(new ExampleBehavior(runtime)
                     .addSubscription("TOPIC_LISTENING_TO");
}

PubSubMethodListener

See Publish & Subscribe for this.

TimeListener

Provides an interface to provide timing logic inside your behavior.

public class TimeBehavior implements TimeListener {
    @Override
    public void timeEvent(long time, int iteration) {
        // Do something here with time and iteration.
    }
}

Timer pulse rate can be configured in the GreenApp declareConfiguration:

@Override
public void declareConfiguration(Builder config) {
   config.setTimerPulseRate(rate); //the rate at which time is checked in milliseconds 
}

And registered like this:

@Override
public void declareBehavior(GreenRuntime runtime) {
   runtime.addTimePulseListener(new TimeBehavior(runtime));
}

Last updated