How to use the thingpedia.BaseChannel function in thingpedia

To help you get started, we’ve selected a few thingpedia examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stanford-oval / thingengine-core / lib / devices / thingpedia / generic.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2015 Giovanni Campagna 
//
// See COPYING for details
"use strict";

const Q = require('q');
const WebSocket = require('ws');
const Tp = require('thingpedia');

class GenericWebhookChannel extends Tp.BaseChannel {
    static get requiredCapabilities() {
        return ['webhook-api'];
    }

    _lateInit() {
        var id = this.name;
        var block;
        var ast = this.device.constructor.metadata;
        if (id in ast.triggers) {
            block = ast.triggers[id];
        } else {
            throw new Error('Invalid channel ' + id + ' in ' + this.device.kind);
        }

        var props = {};
        for (var name in block) {
github stanford-oval / thingengine-core / lib / devices / thingpedia / builtins / thingengine.builtin / say.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2015 Giovanni Campagna 
//
// See COPYING for details
"use strict";

const Tp = require('thingpedia');

module.exports = class NotifyChannel extends Tp.BaseChannel {
    static get requiredCapabilities() {
        return ['assistant'];
    }

    sendEvent(event, env) {
        return env.say(event[0]);
    }
}
github stanford-oval / thingengine-core / lib / devices / thingpedia / builtins / thingengine.phone / receive_sms.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2016 Giovanni Campagna 
//
// See COPYING for details
"use strict";

const Q = require('q');
const Tp = require('thingpedia');

module.exports = class ReceiveSmsChannel extends Tp.BaseChannel {
    static get requiredCapabilities() {
        return ['sms'];
    }

    constructor(engine, device, params) {
        super(engine, device);
        this._sms = engine.platform.getCapability('sms');
    }

    formatEvent(event) {
        return this.engine._("New SMS from %s: %s").format(event[0], event[1]);
    }

    _doOpen() {
        this._sms.onsmsreceived = this._onSmsReceived.bind(this);
        return this._sms.start();
github stanford-oval / thingengine-core / lib / devices / thingpedia / builtins / thingengine.builtin / get_random_between.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2016 Giovanni Campagna 
//
// See COPYING for details
"use strict";

const Tp = require('thingpedia');

module.exports = class GetRandomBetweenChannel extends Tp.BaseChannel {
    formatEvent(event) {
        var low = event[0];
        var high = event[1];
        var number = event[2];
        return String(number);
    }

    invokeQuery(filters) {
        var low = filters[0];
        var high = filters[1];
        if (low === undefined || high === undefined ||
            low === null || high === null)
            throw new TypeError('Missing required parameters');

        return [[low, high, Math.round(low + (Math.random() * (high - low)))]];
    }
github stanford-oval / thingengine-core / lib / devices / builtins / omlet / index.js View on Github external
var msg = event[2];

        return feed.open().then(() => {
            if (msgType === 'text')
                return feed.sendText(msg);
            else if (msgType === 'picture')
                return feed.sendPicture(msg);
            else
                throw new TypeError('Invalid message type, expected text or picture');
        }).finally(() => {
            return feed.close();
        });
    }
}

class SendTextAction extends Tp.BaseChannel {
    sendEvent(event) {
        var to = event[0];
        var msg = event[1];
        var identityHash = null;

        if (to.indexOf(':') >= 0) {
            // already encoded
            identityHash = to;
        } else if (to.indexOf('@') >= 0) {
            identityHash = 'email:' + to;
        } else if (/^[+0-9]+$/.exec(to) !== null) {
            identityHash = 'phone:' + to;
        } else {
            identityHash = 'omlet:' + to;
        }
github stanford-oval / thingengine-core / lib / devices / thingpedia / builtins / thingengine.phone / get_gps.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2016 Giovanni Campagna 
//
// See COPYING for details
"use strict";

const Q = require('q');
const Tp = require('thingpedia');

module.exports = class GpsChannel extends Tp.BaseChannel {
    static get requiredCapabilities() {
        return ['gps'];
    }

    constructor(engine, device, params) {
        super(engine, device);

        this._gps = engine.platform.getCapability('gps');
    }

    formatEvent([location, altitude, bearing, speed], filters, hint, formatter) {
        return this.engine._("Current Location: %s").format(formatter.locationToString(location));
    }

    invokeQuery() {
        return this._gps.getCurrentLocation().then((location) => {
github stanford-oval / thingengine-core / lib / devices / thingpedia / generic.js View on Github external
}

    _doClose() {
        if (this._isWebsocket) {
            if (this._connection)
                this._connection.close();
            this._connection = null;
        } else if (this._timeout) {
            clearInterval(this._timeout);
            this._timeout = null;
        }
        return Q();
    }
};

class GenericAction extends Tp.BaseChannel {
    _lateInit() {
        var id = this.name;
        var ast = this.device.constructor.metadata;
        var block;
        if (id in ast.actions) {
            block = ast.actions[id];
        } else {
            throw new Error('Invalid channel ' + id + ' in ' + this.device.kind);
        }

        var props = {};
        for (var name in block) {
            if (typeof block[name] === 'string')
                props[name] = String.prototype.format.apply(block[name],
                                                            this.device.params);
            else
github stanford-oval / thingengine-core / lib / app_executor.js View on Github external
_doOpen: function() {
        return this.engine.channels.getNamedPipe('thingengine-app-notify', 'w')
            .then(function(ch) {
                this._inner = ch;
            }.bind(this));
    },

    _doClose: function() {
        return this._inner.close();
    },
});

const AppInputChannel = new lang.Class({
    Name: 'AppInputChannel',
    Extends: Tp.BaseChannel,

    _init: function(engine, app) {
        this.parent();
        this.engine = engine;
        this._app = null;

        this._inner = null;
        this._listener = this._onEvent.bind(this);
    },

    _onEvent: function(data) {
        var app = data[0];
        var event = data[1];
        if (app === this._app.uniqueId)
            this.emitEvent(event);
    },
github stanford-oval / thingengine-core / lib / device-classes / omlet / inmessage.js View on Github external
}, this);
        }.bind(this));
    },

    close: function() {
        this._messaging.removeListener('feed-added', this._feedAddedListener);
        this._messaging.removeListener('feed-removed', this._feedRemovedListener);
        for (var feedId in this._feeds)
            this._onFeedRemoved(feedId);
        this._feeds = {};
    }
});

module.exports = new lang.Class({
    Name: 'InMessageChannel',
    Extends: Tp.BaseChannel,

    _init: function(engine, device, params) {
        this.parent();
        this.engine = engine;
        this.device = device;

        if (params.length >= 1) {
            if (!params[0].isFeed)
                throw new Error('Invalid @omlet.[new,incoming]message() parameters');

            this._feed = params[0].value;
            this.filterString = 'feed-' + this._feed.feedId.replace(/[^a-zA-Z0-9]+/g, '-');
            this._channel = new FeedMessageChannel(this._feed, this.signal);
        } else {
            this._channel = new AllFeedsChannel(this.engine, this.signal);
        }
github stanford-oval / thingengine-core / lib / devices / builtins / omlet / inmessage.js View on Github external
feeds.forEach(function(feedId) {
                this._onFeedAdded(feedId);
            }, this);
        });
    }

    close() {
        this._messaging.removeListener('feed-added', this._feedAddedListener);
        this._messaging.removeListener('feed-removed', this._feedRemovedListener);
        for (var feedId in this._feeds)
            this._onFeedRemoved(feedId);
        this._feeds = {};
    }
}

module.exports = class InMessageChannel extends Tp.BaseChannel {
    constructor(engine, device, params) {
        super(engine, device);
        this.engine = engine;
        this.device = device;

        this._feedId = params[0];
        if (this._feedId !== undefined) {
            let feedId = String(this._feedId);
            this._feed = this._messaging.getFeed(feedId);
            this.filterString = 'feed-' + feedId.replace(/[^a-zA-Z0-9]+/g, '-');
            this._channel = new FeedMessageChannel(this._feed, this.device, this.signal);
        } else {
            this._channel = new AllFeedsChannel(this.engine, this.device, this.signal);
        }

        this._channel.on('event', this._onEvent.bind(this));