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.
publicclassRestListenBehaviorimplementsRestListener{@OverridepublicbooleanrestRequest(HTTPRequestReaderrequest){ // 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.returnfalse;}}
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:
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: