Spanish Total.js
Slovenčina
Dokumentácia Pre AI agentov
Obsah

Referencia inštancie

The handlers, functions and properties of a device in exports.make.

.md Web

Táto stránka ešte nie je preložená. Čítaš originál.

Na tejto stránke

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.
ParameterTypPopis
$ Object Správa.

$.data is the data, $.input the id of the input. End every message with $.destroy().

Send a value to an output
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.
ParameterTypPopis
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.

Pass posted data on
instance.notify = function(req) {
	if (req.method === 'POST')
		instance.message(instance.newmessage(req.body));
};
method

instance.newmessage(data)

Creates a message.
ParameterTypPopis
data Object The data of the message.

Vracia Object Správa.

Send it to an output with .send(output), or hand it to the device with instance.message(msg).

Send a value
instance.newmessage(21.4).send('temperature');
method

instance.lastvalue(output, value)

Sets the last value of a sensor without adding it to the statistics.
ParameterTypPopis
output String The id of the output.
value Object The value.
Set the battery level
instance.lastvalue('battery', 85);
method

await instance.readvalue(output)

Reads the last stored value of a sensor of this device.
ParameterTypPopis
output String The id of the output.

Vracia Promise An object { value, prev, ts }, or undefined when the sensor has no value yet.

Numbers, booleans and dates come back in their type.

Compare with the last value
var last = await instance.readvalue('counter');
var difference = last ? $.data.counter - last.value : 0;
method

instance.throw(message)

Creates an alert for the device.
ParameterTypPopis
message String The text of the alert.

The alert appears on the Alerts screen. See Alerts and debugging.

A low battery
instance.throw('Battery low: ' + data.battery + ' %');
method

instance.debug(a, [b], [c], [d])

Writes a line to logs/debugger.log of the stream.
ParameterTypPopis
a Object The first value.
b? Object More values.

The stream writes the collected lines once a minute.

Log a command
instance.debug('Interval changed to', $.data.minutes, 'minutes');
property

instance.storage.insert(table, data)

Inserts a row into a storage table of the driver.

See Storage.

Keep a reading
instance.storage.insert('readings', { sn: data.sn, temperature: 21.4 });
property

instance.id

The id of the device.

The 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.
property

instance.meta

The device: sn, name, locality, lat, lng and reference.

instance.meta.sn is the Device identifier.

property

instance.config

The configuration of the device, the same object as config in exports.make.
property

instance.module

The driver. instance.module.options holds its options.
Vyrobené v Európe