Listeners
Here is the angular stone of Webserial
, the listeners. With them, you can listen to the events that the device emits.
How to listen to an event
When you create a new instance of the Jofemar
class, you can listen to the events that the device emits.
machine.on('serial:message', (data) => {
console.log(data);
});
Each event has a different data structure, so you need to check the data structure for each event.
All events will return SerialEvent
as shown in the image below.
Get all available listeners
For jofemar class there events that you can listen to, of course only listen to the events that you need.
Object
To get all the available listeners, you can use the availableListeners
property.
console.log(machine.availableListeners);
The result will be an object with the available listeners, and if you are listen each event
[
{
"type": "channel:status",
"listening": false
},
{
"type": "channels",
"listening": false
},
{
"type": "check:beeper",
"listening": false
},
{
"type": "check:elevator-speed",
"listening": false
},
{
"type": "check:engine-voltage",
"listening": false
},
{
"type": "check:expiration-after",
"listening": false
},
{
"type": "check:expiration-by-temperature",
"listening": false
},
{
"type": "check:extractor-after-dispense",
"listening": false
},
{
"type": "check:isolation-tray",
"listening": false
},
{
"type": "check:language",
"listening": false
},
{
"type": "check:machine-id",
"listening": false
},
{
"type": "check:push-over",
"listening": false
},
{
"type": "check:standby-after-collect",
"listening": false
},
{
"type": "check:standby-without-collect",
"listening": false
},
{
"type": "check:temperature-before-expiration",
"listening": false
},
{
"type": "check:temperature-scale",
"listening": false
},
{
"type": "clock:registers",
"listening": false
},
{
"type": "dispensed",
"listening": false
},
{
"type": "dispensing",
"listening": false
},
{
"type": "dispensing:withdrawal",
"listening": false
},
{
"type": "door:event",
"listening": false
},
{
"type": "jofemar:error",
"listening": false
},
{
"type": "jofemar:warning",
"listening": false
},
{
"type": "keyboard:pressed",
"listening": false
},
{
"type": "machine:activity",
"listening": false
},
{
"type": "machine:faults",
"listening": false
},
{
"type": "not-dispensed",
"listening": false
},
{
"type": "program:version",
"listening": false
},
{
"type": "reset:errors",
"listening": false
},
{
"type": "command-executed",
"listening": false
},
{
"type": "serial:connected",
"listening": false
},
{
"type": "serial:connecting",
"listening": false
},
{
"type": "serial:disconnected",
"listening": false
},
{
"type": "serial:error",
"listening": false
},
{
"type": "serial:lost",
"listening": false
},
{
"type": "serial:message",
"listening": true
},
{
"type": "serial:need-permission",
"listening": false
},
{
"type": "serial:reconnect",
"listening": false
},
{
"type": "serial:sent",
"listening": false
},
{
"type": "serial:soft-reload",
"listening": false
},
{
"type": "serial:timeout",
"listening": false
},
{
"type": "serial:unsupported",
"listening": false
},
{
"type": "temperature:current",
"listening": false
},
{
"type": "temperature:working",
"listening": false
},
{
"type": "machine:status",
"listening": false
},
{
"type": "unknown",
"listening": false
},
{
"type": "corrupt:message",
"listening": false
}
]
Array
But sometimes you don't want the object you want only an array with the available listeners, you can do this:
machine.availableListeners.map(el => el.type)
And one more time here is the result
[
"channel:status",
"channels",
"check:beeper",
"check:elevator-speed",
"check:engine-voltage",
"check:expiration-after",
"check:expiration-by-temperature",
"check:extractor-after-dispense",
"check:isolation-tray",
"check:language",
"check:machine-id",
"check:push-over",
"check:standby-after-collect",
"check:standby-without-collect",
"check:temperature-before-expiration",
"check:temperature-scale",
"clock:registers",
"dispensed",
"dispensing",
"dispensing:withdrawal",
"door:event",
"jofemar:error",
"jofemar:warning",
"keyboard:pressed",
"machine:activity",
"machine:faults",
"not-dispensed",
"program:version",
"reset:errors",
"command-executed",
"serial:connected",
"serial:connecting",
"serial:disconnected",
"serial:error",
"serial:lost",
"serial:message",
"serial:need-permission",
"serial:reconnect",
"serial:sent",
"serial:soft-reload",
"serial:timeout",
"serial:unsupported",
"temperature:current",
"temperature:working",
"machine:status",
"unknown",
"corrupt:message"
]
Serial connected
When the serial is connected, this event is dispatched.
machine.on('serial:connected', event => {
console.log(event.detail);
});
Serial connecting
When the serial is trying to connect, this event is dispatched.
machine.on('serial:connecting', event => {
// without data
});
Serial disconnected
When the serial is disconnected, this event is dispatched.
machine.on('serial:disconnected', event => {
console.log(event.detail);
});
Serial error
When the serial has an error, this event is dispatched.
machine.on('serial:error', event => {
console.log(event.detail);
});
Serial lost
When the serial is lost, this event is dispatched.
machine.on('serial:lost', event => {
console.log(event.detail);
});
Serial message
When the serial receives a message, this event is dispatched.
This event is dispatched along others events that send event of machine, so this event is only for debug.
machine.on('serial:message', event => {
console.log(event.detail);
});
Serial need permission
When the serial needs permission, this event is dispatched. This occurs because user interaction is needed to request the serial.
Recommendation: Use a button or a screen to show what is happening.
machine.on('serial:need-permission', event => {
console.log(event.detail);
});
Serial reconnect
When the serial is trying to reconnect, this event is dispatched.
machine.on('serial:reconnect', event => {
// without data
});
Serial sent
When the serial sends a message to machine, this event is dispatched.
This event is more oriented for debug.
machine.on('serial:sent', event => {
console.log(event.detail);
});
{
"event": "string",
"bytes": []
}
Serial soft reload
When the serial is trying to soft reload, this event is dispatched.
machine.on('serial:soft-reload', event => {
// without data
});
Serial timeout
When the serial has a timeout, this event is dispatched.
machine.on('serial:timeout', event => {
console.log(event.detail);
});
Serial unsupported
When the serial is unsupported, this event is dispatched.
machine.on('serial:unsupported', event => {
console.log(event.detail);
});
Channel status
This event is dispatched when you request the status of the channel.
machine.on('channel:status', event => {
console.log(event.detail.active);
});
Channels
When you run assignChannels
method, the device will emit the channels
event when finish the process.
machine.on('channels', event => {
console.log(event.detail.channels);
});
// structure of event.detail.channels
// [{
// selection: 1,
// active: true,
// },
// {
// selection: 2,
// active: false,
// }, ...
// ]
Check events
All the next events will start with check:
because they are events that return when you consult something.
Check beeper
Possible values are true
or false
.
machine.on('check:beeper', event => {
// enable = true, disable = false
console.log(event.detail.beeper);
});
Check elevator speed
Possible values are low
or high
.
machine.on('check:elevator-speed', event => {
console.log(event.detail.speed);
});
Check engine voltage
Voltage will be a number between 5 and 9.5
machine.on('check:engine-voltage', event => {
console.log(event.detail.voltage);
});
Check expiration after X minutes
Will return the minutes before the expiration.
machine.on('check:expiration-after', event => {
console.log(event.detail.minutes);
});
Check expiration by temperature
Possible values are true
or false
.
machine.on('check:expiration-by-temperature', event => {
console.log(event.detail.enabled);
});
Check extractor after dispense
Return seconds that the extractor will be active after dispense.
machine.on('check:extractor-after-dispense', event => {
console.log(event.detail.seconds);
});
Check isolation tray
Return the number of the isolation tray.
machine.on('check:isolation-tray', event => {
console.log(event.detail.enabled);
});
Check language
Return the language of the machine. Possible values are:
- Spanish
- English
- French
- unknown
machine.on('check:language', event => {
console.log(event.detail.language);
});
Check machine ID
Return the machine ID.
machine.on('check:machine-id', event => {
console.log(event.detail);
});
Check push over
Return is the selection consult is enabled or disabled to push over.
machine.on('check:push-over', event => {
console.log(event.detail.push);
});
Check standby after collect
Return the seconds that the machine will be waiting before go to standby after collect.
machine.on('check:standby-after-collect', event => {
console.log(event.detail.seconds);
});
Check standby without collect
Return the minutes that the machine will be waiting before go to standby without collect.
machine.on('check:standby-without-collect', event => {
console.log(event.detail.minutes);
});
Check temperature before expiration
Return the temperature before the expiration.
machine.on('check:temperature-before-expiration', event => {
console.log(event.detail.temperature);
});