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