registerConsumer
[ Function ]
public static registerConsumer(
filter: Filter | null,
callbacks: {
list?: (data: Events.ListResult) => void,
subscription?: (data: Events.SubscriptionResult) => void
}): DestroyFunction | null;
Registers a consumer for server events. A consumer can specify two optional functions: list
is called after the consumer has logged in or when the localization changes, subscription
is called when alarms change or new events are created.
Only events matching the specified filter are forwarded to the consumer. If no filter is to be used, null must be passed.
![]() | Using this function can lead to a drastic reduction in client performance if there is a high volume of events. Instead, a subscription to the ListEvents symbol of the server should be used. |
Parameter
Name | Type | Description |
---|---|---|
filter | Defines which events are to be queried | |
callbacks | { list?: (data: Events.ListResult) => void, subscription?: (data: Events.SubscriptionResult) => void } | An object containing the callback functions to which received events are passed. |
Return value
Type | Description |
---|---|
A function with which the consumer can be logged off again. If the consumer login fails, null is returned. |
Sample - JavaScript
// filter for unconfirmed alarms
var filter = [
{
path: 'type',
comparator: '==',
value: TcHmi.Server.Events.Type.Alarm
},
{
logic: 'AND'
},
{
path: 'timeConfirmed',
comparator: '==',
value: new Date(null)
}
];
var myAlarms = [];
// function to handle list of events
function consumeEventList(data) {
if (data.error === TcHmi.Errors.NONE) {
myAlarms = data.events;
}
else {
myAlarms = [];
}
}
// function to handle new and changed incoming events
function consumeEventSubscription(data) {
if (data.error !== TcHmi.Errors.NONE) {
return;
}
if (data.removedByFilter) {
myAlarms = myAlarms.filter(function (alarm) {
return alarm.id !== data.event.id;
});
}
else if (data.changeType === TcHmi.Server.Events.ChangeType.AlarmRaised) {
myAlarms.push(data.event);
}
else {
for (var i = 0; i < myAlarms.length; i++) {
if (myAlarms[i].id === data.event.id) {
myAlarms[i] = data.event;
break;
}
}
}
}
// register a consumer to receive events
var destroyFunction = TcHmi.Server.Events.registerConsumer(filter, {
list: consumeEventList,
subscription: consumeEventSubscription
});
![]() | Available from version 1.10 |