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

Receiving data

Create an address on the stream, find the device a reading belongs to, use a token, and use extensions and the notify address.

.md Web

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

Na tejto stránke

A driver decides how data reaches it. The usual way is an HTTP route that the driver creates on the stream when it is installed.

Create a route

Create the route in exports.install and remove it in exports.uninstall. com is the driver as a component of the stream:

exports.install = function(com) {

    com.schema = '*sn:String,temperature:Number'.toJSONSchema();

    com.route = ROUTE('POST /demoweather/', function($) {

        var data = com.schema.transform($.body);
        if (data.error) {
            $.invalid(data.error);
            return;
        }

        var payload = data.response;
        var flow = com.main.meta.flow;
        var count = 0;

        for (var key in flow) {
            var instance = flow[key];
            if (instance.component === com.id && instance.meta.sn === payload.sn) {
                instance.message(instance.newmessage(payload));
                count++;
            }
        }

        $.json({ count: count });
    });
};

exports.uninstall = function(com) {
    com.route && com.route.remove();
};
  • com.main.meta.flow holds every running instance of the stream. The instances of your devices have component equal to the id of the driver.
  • instance.meta.sn is the Device identifier of the device. instance.meta also has name, locality, lat, lng and reference.
  • instance.newmessage(data) creates a message, and instance.message(msg) hands it to the device.

The path of the route is shared by the whole stream, so give each driver its own path.

Check a token

Add an option to the driver and compare it with a header. The option is set in Drivers > ... > Settings and is available in com.options:

exports.options = [{ id: 'token', type: 'string', name: 'Token', value: '' }];

exports.install = function(com) {
    com.route = ROUTE('POST /demoweather/', function($) {

        if (com.options.token && com.options.token !== $.headers['x-token']) {
            $.invalid(401);
            return;
        }

        // ...
    });
};

When somebody changes the token, the stream applies it at its next reload, within three minutes. Simulations send the token of an endpoint in the x-token header, so this check works with them too.

One address for many drivers: extensions

An extension is a driver with exports.extension = true. It runs once for the whole stream, not once for each device, and it is not offered when people create a device. Use it when a gateway sends the readings of different devices to one address. This extension passes each reading of a batch to the device with the same serial number, whatever its driver:

<script total>

    exports.id = 'demohub';
    exports.name = 'Demo gateway';
    exports.manufacturer = 'Example Ltd.';
    exports.icon = 'ti ti-router';
    exports.color = '#8B5CF6';
    exports.author = 'Jane Doe';
    exports.version = '1';
    exports.extension = true;

    exports.install = function(com) {

        com.route = ROUTE('POST /demohub/', function($) {

            // The gateway sends a batch: [{ sn: 'WS-0001', temperature: 21.4 }, ...]
            var items = $.body instanceof Array ? $.body : [$.body];
            var flow = com.main.meta.flow;
            var count = 0;

            for (var item of items) {
                for (var key in flow) {
                    var instance = flow[key];
                    if (instance.meta && instance.meta.sn === item.sn) {
                        instance.message(instance.newmessage(item));
                        count++;
                    }
                }
            }

            $.json({ count: count });
        });
    };

    exports.uninstall = function(com) {
        com.route && com.route.remove();
    };

    exports.make = function(instance) {
        instance.message = function($) {
            $.destroy();
        };
    };

</script>

<readme>
Receives batches of readings on `POST /demohub/` and passes each reading to the device with the same serial number.
</readme>
curl -X POST https://stream.example.com/demohub/ \
  -H 'Content-Type: application/json' \
  -d '[{"sn":"WS-0001","temperature":20.8},{"sn":"WS-0002","humidity":57}]'

The answer is {"count":2}. Each device processes its reading in its own instance.message. The Extensions list in Drivers shows the installed extensions.

The notify address

Without a route of its own, a driver can receive data at the notify address of the stream, one address for each device:

POST https://stream.example.com/notify/stream-<device id>/

The stream calls instance.notify of that device with the request:

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

The stream answers {"success":true}. The address has no token check of its own: check req.headers in instance.notify when the data must come from a known sender.

Vyrobené v Európe