This page is part of the External (Extended) Documentation
The Events service
As you may know from the page about the Logging, system events can pass parameters with the first non-string argument.
console.log('#money, #input', {amount: 20})
There is a system service Events, which listens to the log stream and notifies the subscribers about events.
A comprehensive example
Suppose that a BillValidator instance sends log messages about the entered amount every 500 milliseconds, and messages about the opening of the cassette - every 5 seconds.
The example below shows how to use this information log by implementing a simple cash monitoring service.
// tell the Domain to start the Events service. This service is always a singleton.
// if it is already up and running, this function returns a reference to it
Domain.SvcDomain.startService('Events', function (svc) {
// this variable counts the entered amount of cash
var count = 0;
// the Events service defines the method 'subscribe', which accepts an object...
svc.subscribe({
// ...with the 'listen' key, in which you can pass a listener (as an object) or an array of listeners
// in this example, there is an array of 2 listeners
listen: [{
// the event is passed as the first argument of the callback function
// it may be skipped if distinguishing events is not required
event: 'billInsert',
// the logging namespace
namespace: 'Devices.BillValidator',
// tags to filter the messages
// if multiple tags are specified, they ALL must be present in the message
// if no tags are specified, all messages from the namespace will be listened
filter: ['bill']
}, {
event: 'cashCollection',
namespace: 'Devices.BillValidator',
filter: ['cassetteopen']
}],
// the callback function invoked on each message that satisfies the filtering conditions
fn: function (
// the event attribute of the fitting subscriber
event,
// data object: the first non-string argument of the console.log() call
// (be careful - this puts restrictions on the data logging in system events)
info,
// The following arguments are auxiliary and are not often used:
// the object that contains all tags specified in the 'filter' argument of the listener (these tags are keys of this object)
matched,
// the listener object after the filtration; can be used to save the state
listener,
// the entire LogObject containing all information connected with the message; avoid using this argument
logObject
) {
// the function is invoked in the context of the listener object
if(event === 'billInsert') {
/* if the event of the listener corresonds to 'billInsert' ->
add the amount extracted from the first non-string argument */
count += info.amount;
}else if(event === 'cashCollection'){
/* if a cash collection has taken place -> empty the amount count */
console.log('#cashMonitoringService #billStackEmpty', count);
count = 0;
}
console.log('#cashMonitoringService #amount', count);
}
});
});