Riferimento dell'istanza
The handlers, functions and properties of a device in exports.make.
Questa pagina non è ancora tradotta. Stai leggendo l’originale.
In questa pagina
The instance is one device of the driver. exports.make receives it, and you define its handlers there:
exports.make = function(instance, config) {
instance.message = function($) {
$.destroy();
};
};Entries of type event are functions that you define and the stream calls. Entries of type method are functions that the stream provides.
API
event
instance.message = function($) {}
Receives the data and the commands of the device.
instance.message = function($) {}| Parametro | Tipo | Descrizione |
|---|---|---|
$ |
Object |
Il messaggio. |
$.data is the data, $.input the id of the input. End every message with $.destroy().
instance.message = function($) {
if ($.data.temperature != null)
instance.newmessage($.data.temperature).send('temperature');
$.destroy();
};
event
instance.notify = function(req) {}
Receives the requests sent to the notify address of the device.
instance.notify = function(req) {}| Parametro | Tipo | Descrizione |
|---|---|---|
req |
Object |
The request with method, body, query, headers, ip and url. |
The address is /notify/stream-<device id>/ on the stream. See Receiving data.
instance.notify = function(req) {
if (req.method === 'POST')
instance.message(instance.newmessage(req.body));
};
method
instance.newmessage(data)
Creates a message.
instance.newmessage(data)| Parametro | Tipo | Descrizione |
|---|---|---|
data |
Object |
The data of the message. |
Restituisce Object Il messaggio.
Send it to an output with .send(output), or hand it to the device with instance.message(msg).
instance.newmessage(21.4).send('temperature');
method
instance.lastvalue(output, value)
Sets the last value of a sensor without adding it to the statistics.
instance.lastvalue(output, value)| Parametro | Tipo | Descrizione |
|---|---|---|
output |
String |
The id of the output. |
value |
Object |
The value. |
instance.lastvalue('battery', 85);
method
await instance.readvalue(output)
Reads the last stored value of a sensor of this device.
await instance.readvalue(output)| Parametro | Tipo | Descrizione |
|---|---|---|
output |
String |
The id of the output. |
Restituisce Promise An object { value, prev, ts }, or undefined when the sensor has no value yet.
Numbers, booleans and dates come back in their type.
var last = await instance.readvalue('counter');
var difference = last ? $.data.counter - last.value : 0;
method
instance.throw(message)
Creates an alert for the device.
instance.throw(message)| Parametro | Tipo | Descrizione |
|---|---|---|
message |
String |
The text of the alert. |
The alert appears on the Alerts screen. See Alerts and debugging.
instance.throw('Battery low: ' + data.battery + ' %');
method
instance.debug(a, [b], [c], [d])
Writes a line to logs/debugger.log of the stream.
instance.debug(a, [b], [c], [d])| Parametro | Tipo | Descrizione |
|---|---|---|
a |
Object |
The first value. |
b? |
Object |
More values. |
The stream writes the collected lines once a minute.
instance.debug('Interval changed to', $.data.minutes, 'minutes');
property
instance.storage.insert(table, data)
Inserts a row into a storage table of the driver.
instance.storage.insert(table, data)See Storage.
instance.storage.insert('readings', { sn: data.sn, temperature: 21.4 });
property
instance.id
The id of the device.
instance.idThe id shown in the device detail and on the status page of the stream.
property
instance.component
The id of the driver of the device.
instance.component
property
instance.meta
The device: sn, name, locality, lat, lng and reference.
instance.metainstance.meta.sn is the Device identifier.
property
instance.config
The configuration of the device, the same object as config in exports.make.
instance.config
property
instance.module
The driver. instance.module.options holds its options.
instance.module