Events
The Events Moobot module allows you to make a Moobot driven by the occurrence of events by defining the type of event that interests the Moobot. It is available to load into any standard Moobot.
To use, at the top of a Moobot js file, define a new global variable events
to load the Events module:
var events = MooBot.loadModule('Events');
Note
Compatibility with MoogDb and MoogDb V2 Methods and auxiliary objects listed here are compatible with the MoogDb V2 module.
Method
events.onEvent
The Events module has only one method, onEvent
. This method points the Moobot to a supplied JavaScript function, which is called when a specified event type occurs.
The parameters to the called function depend on the type of event that you are listening for.
In a Moobot, this method is typically the last line in a script.
The type of event adaptor chosen is specific to the type of Moobot you are building.
onEvent method
Takes the name of a valid JavaScript function in a Moobot and also event code (from the Constants module eventType
), and returns an event adaptor object
Request Arguments
Name | Type | Description |
---|---|---|
functionName | String | The name of a valid JavaScript function in the Moobot that is called when the event arrives. |
type | CEvent |
|
Return Parameter
Name | Type | Description |
---|---|---|
CEventAdaptor | Object | An event adaptor object. Made active with the |
Example
For the AlertBuilder MooBot:
events.onEvent("newEvent",constants.eventType("Event")).listen();
When the Moolet starts and loads this events Moobot, its JavaScript file executes, initializing the Moobot to respond in an event-driven way to events arriving.
newEvent JavaScript function
The format of the function newEvent (which is called when you get an event), is as follows:
newEvent()
Request Argument
Name | Type | Description |
---|---|---|
event | CEvent object | An object that encapsulates all the data for the event from the Message Bus, and allows you to forward the event to the bus, using the CEvent forward method detailed below. |
Event forward methods
The advantage of this approach is that alerts / Situations can be forwarded to different AlertRulesEngines / Sigalisers dynamically in the Moobots (for example based on the value of the source file).
alert.forward("Cookbook");
You could instead remove the process_output_of
lines from the AlertRulesEngine / Sigaliser / Cookbook / Speedbird Moolets and explicitly send events / alerts / Situations on within the Moobot code using (as an example):
You can emulate MoogDb behavior by running the MoogDb.V2 Moobots.For example, the alert.forward(this) line
will send an alert onto the Moolets specified in the appropriate process_output_of
block within moog_farmd.conf
.
CEventAdaptor auxiliary object
This object is a utility class used by the Events module to allow for the programmatic activation of event listening. It has one method:
listen()
Starts the event adaptor listening, which then calls the specified function when an event occurs.
Request Argument
None.
Return Parameter
Void - no value returned.
CEvent auxiliary object
This object encapsulates a generic Message Bus event object, and the contents of it are specific to the event type it represents. You can however access the key-value pairs contained in the object, and also set the values. Its methods include:
contains
Returns true if the Event contains a value stored at the key name
.
Request Argument
Name | Type | Description |
---|---|---|
name | String | The name of the key being queried. |
Return Parameter
Type | Description |
---|---|
Boolean | True if the event has a field called |
set
Associates the specified value
with the specified name
in the event.
Previous key
mapping has the old value
replaced.
Request Argument
Name | Type | Description |
---|---|---|
name | String | The key with which the specified value is to be associated. |
value | Object | The value associated with the key. |
Return Parameter
Type | Description |
---|---|
Boolean | Indicates if the operation was successful: true = success, false = fail |
value
Returns the object stored at the key name
.
Request Argument
Name | Type | Description |
---|---|---|
name | String | The name of the key to return the object from. |
Return Parameter
Type | Description |
---|---|
Object | A Javascript object containing what is at the key |