Moogsoft Docs

Events

Description

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.

The Events module is available to load into any standard MooBot.

To use , at the top of a MooBot js file , define a new global object events to load the Events module:

var events = MooBot.loadModule('Events');

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.

Reference Guide

events.onEvent

events.onEvent()

Takes the name of a valid JavaScript function in a MooBot and also an 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 eventType Event code that specifies what type of Event the MooBot is listening for. It is typically from the Constants module

Return Parameter

Name Type Description
CEventAdaptor
Object An Event adaptor object. Made active with the listen () function in-line to listen for the Event type

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:

function newEvent

function newEvent()

Request Argument

Name Type Description
event
CEvent object An object that encapsulates all the data for the Event from the bus, and allows you to forward the Event to the bus, using the CEvent forward method detailed below

CEvent forward methods

You can emulate MoogDb behaviour 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

You could instead remove the process_output_of lines from the AlertRulesEngine / Sigaliser / Cookbook / Speedbird / Nexus Moolets and explicitly send Events / Alerts / Situations on within the MooBot code using (as an example)

alert.forward("Cookbook");

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 field)

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

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 MooMS 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

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

R eturn Parameter

Type Description
Boolean true if the Event has a field called name , otherwise false

set

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

R eturn Parameter

Type Description
Boolean
Indicates if the operation was successful: true = success, false = fail

value

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

R eturn Parameter

Type Description
Object
A Javascript object containing what is at the key name

Note

Note

Compatibility with MoogDb and MoogDb.V2
Methods and auxiliary objects listed here are compatible with the MoogDb.V2 module