How to use the thingpedia.BaseDevice 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 / thingpedia-common-devices / com.twitter / device.js View on Github external
return engine.devices.loadOneDevice({ kind: 'com.twitter',
                                              accessToken: twitter.accessToken,
                                              accessTokenSecret: twitter.accessTokenSecret,
                                              userId: result['id_str'],
                                              screenName: result['screen_name'] }, true);
    });
}

// a fixed version of postCustomApiCall that does not append
// the parameters to the url (which would break OAuth)
function postCustomApiCall(url, params, error, success) {
    url = this.baseUrl + url;
    this.doPost(url, params, error, success);
}

module.exports = class TwitterAccountDevice extends Tp.BaseDevice {
    static runOAuth2(engine, req) {
        return Promise.resolve().then(() => {
            if (req === null)
                return runOAuthStep1(engine);
            else
                return runOAuthStep2(engine, req);
        }).catch((e) => {
            console.log(e);
            console.log(e.stack);
            throw e;
        });
    }

    constructor(engine, state) {
        super(engine, state);
github stanford-oval / thingpedia-common-devices / com.bing / device.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// Copyright 2016 Giovanni Campagna 
//
// See LICENSE for details
"use strict";

const Tp = require('thingpedia');

module.exports = class BingClass extends Tp.BaseDevice {
    constructor(engine, state) {
        super(engine, state);
        this.uniqueId = 'com.bing';
        this.name = "Bing Search";
        this.description = "Search the web, using Bing";
    }

    get_web_search({query}) {
        const locale = this.engine.platform.locale;
        const language = locale.split('-')[0];

        const url = `https://api.cognitive.microsoft.com/bing/v5.0/search?count=5&mkt=${locale}&setLang=${language}&q=${encodeURIComponent(query)}&responseFilter=Webpages`;
        return Tp.Helpers.Http.get(url, {
            extraHeaders: { 'Ocp-Apim-Subscription-Key': this.constructor.metadata.auth.subscription_key }
        }).then((response) => {
            let parsedResponse = JSON.parse(response);
github stanford-oval / thingengine-core / lib / device_selector.js View on Github external
getTrigger: function(id, params) {
        return this.engine.channels.getNamedPipe('thingengine-system-' + id, 'r');
    },

    getAction: function(id) {
        return this.engine.channels.getNamedPipe('thingengine-system-' + id, 'w');
    },
});
PipeSystemDevice.metadata = { types: [] };

// A app, wrapped as a device to appease DeviceView
// very evil, look away
const AppDevice = new lang.Class({
    Name: 'AppDevice',
    Extends: Tp.BaseDevice,

    _init: function(engine, app) {
        this.parent(engine, { kind: 'thingengine-app' });
        this.app = app;

        // there is only one device in the context where this is put
        // so there is no need for this to be unique
        // it is not shared between rules or anything, sharing happens
        // at the app level
        this.uniqueId = 'thingengine-app';
    },

    // we really only need to implement getTrigger/getAction,
    // for anything else BaseDevice is fine

    getTrigger: function(id, params) {
github stanford-oval / thingpedia-common-devices / com.gmail / device.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// Copyright 2016 Silei Xu 
//           2018 Giovanni Campagna 
//
// See LICENSE for details
"use strict";

const Tp = require('thingpedia');
const mailcomposer = require('mailcomposer');
const Url = require('url');
const { simpleParser } = require('mailparser');

module.exports = class GMailAccount extends Tp.BaseDevice {
    static get runOAuth2() {
        return Tp.Helpers.OAuth2({
            scope: ['openid', 'profile', 'email',
                'https://www.googleapis.com/auth/gmail.readonly',
                'https://www.googleapis.com/auth/gmail.send'
            ],
            authorize: 'https://accounts.google.com/o/oauth2/auth',
            get_access_token: 'https://www.googleapis.com/oauth2/v3/token',
            set_access_type: true,
            callback(engine, accessToken, refreshToken) {
                const auth = 'Bearer ' + accessToken;
                return Tp.Helpers.Http.get('https://www.googleapis.com/oauth2/v2/userinfo', {auth: auth, accept: 'application/json'}).then((response) => {
                    const parsed = JSON.parse(response);
                    return engine.devices.loadOneDevice({
                        kind: 'com.gmail',
                        accessToken: accessToken,
github stanford-oval / thingpedia-common-devices / com.google.contacts / index.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// Copyright 2016 Giovanni Campagna 
//
// See LICENSE for details
"use strict";

const Tp = require('thingpedia');

const URL = "https://people.googleapis.com/v1/";

module.exports = class GoogleContactsDevice extends Tp.BaseDevice {
    static get runOAuth2() {
        return Tp.Helpers.OAuth2({
            kind: 'com.google.contacts',
            scope: ['openid','profile', 'email',
                'https://www.googleapis.com/auth/contacts.readonly'],
            authorize: 'https://accounts.google.com/o/oauth2/auth',
            get_access_token: 'https://www.googleapis.com/oauth2/v3/token',
            set_access_type: true,
            callback(engine, accessToken, refreshToken) {
                var auth = 'Bearer ' + accessToken;
                return Tp.Helpers.Http.get('https://www.googleapis.com/oauth2/v2/userinfo', { auth: auth, accept: 'application/json' })
                .then((response) => {
                    var parsed = JSON.parse(response);
                    return engine.devices.loadOneDevice({ kind: 'com.google.contacts',
                                                          accessToken: accessToken,
                                                          refreshToken: refreshToken,
github stanford-oval / thingpedia-common-devices / com.google.drive / device.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 
// Copyright 2016 Linyu He 
//                Lingbin Li 
//
// See COPYING for details
"use strict";

const Tp = require('thingpedia');

const PAGE_SIZE = 10;

module.exports = class GoogleDriveDevice extends Tp.BaseDevice {
    static get runOAuth2() {
        return Tp.Helpers.OAuth2({
            kind: 'com.google.drive',
            scope: ['openid','profile','email',
                    'https://www.googleapis.com/auth/drive',
                    'https://www.googleapis.com/auth/drive.appdata',
                    'https://www.googleapis.com/auth/drive.file'],
            authorize: 'https://accounts.google.com/o/oauth2/auth',
            get_access_token: 'https://www.googleapis.com/oauth2/v3/token',
            set_access_type: true,
            callback(engine, accessToken, refreshToken) {
                var auth = 'Bearer ' + accessToken;
                return Tp.Helpers.Http.get('https://www.googleapis.com/oauth2/v2/userinfo', { auth: auth, accept: 'application/json' })
                .then((response) => {
                    var parsed = JSON.parse(response);
                    return engine.devices.loadOneDevice({ kind: 'com.google.drive',
github stanford-oval / thingpedia-common-devices / com.uber / device.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// Copyright 2016 Juan Vimberg 
//                Tucker L. Ward 
//                Giovanni Campagna 
//                Silei Xu 
//
// See LICENSE for details
"use strict";

const Tp = require('thingpedia');

module.exports = class UberDeviceClass extends Tp.BaseDevice {
    static get runOAuth2() {
        return Tp.Helpers.OAuth2({
            kind: 'com.uber',
            scope: ['profile', 'request', 'history', 'all_trips'],
            authorize: 'https://login.uber.com/oauth/v2/authorize',
            get_access_token: 'https://login.uber.com/oauth/v2/token',
            redirect_uri: 'https://thingengine.stanford.edu/devices/oauth2/callback/com.uber',

            callback(engine, accessToken, refreshToken) {
                var auth = 'Bearer ' + accessToken;
                return Tp.Helpers.Http.get('https://api.uber.com/v1.2/me', {
                    auth: auth,
                    accept: 'application/json'
                }).then((response) => {
                    var parsed = JSON.parse(response);
                    return engine.devices.loadOneDevice({ kind: 'com.uber',
github stanford-oval / thingpedia-common-devices / org.thingpedia.holidays / device.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// Copyright 2016 Giovanni Campagna 
//                Daniel Melendez 
//
// See COPYING for details
"use strict";

const Tp = require('thingpedia');
const ICAL = require('ical.js');

module.exports = class ICalendarHolidaysDevice extends Tp.BaseDevice {
    constructor(engine, state) {
        super(engine, state);

        this.name = "Holidays";
        this.description = "Checks for holiday events of a region.";
        this.uniqueId = 'org.thingpedia.holidays';
    }

    get_get_holidays({ country }) {
        country = String(country);
        let url;
        if (country === 'us')
            url = "https://calendar.google.com/calendar/ical/en.usa%23holiday@group.v.calendar.google.com/public/basic.ics";
        else if (country === 'uk')
            url = "https://calendar.google.com/calendar/ical/en_gb.uk%23holiday@group.v.calendar.google.com/public/basic.ics";
        else if (country === 'it')
github stanford-oval / thingpedia-common-devices / com.nest / device.js View on Github external
}

    get kind() {
        return 'com.nest';
    }

    checkAvailable() {
        return this.state.is_online ? Tp.Availability.AVAILABLE :
            Tp.Availability.UNAVAILABLE;
    }
};
SmokeAlarmDevice.metadata = {
    types: ['smoke-alarm']
};

class NestDevice extends Tp.BaseDevice {
    static get runOAuth2() {
        return Tp.Helpers.OAuth2({
            kind: 'com.nest',
            scope: null,
            set_state: true,
            authorize: 'https://home.nest.com/login/oauth2',
            get_access_token: 'https://api.home.nest.com/oauth2/access_token',
            redirect_uri: 'https://thingengine.stanford.edu/devices/oauth2/callback/com.nest',

            callback(engine, accessToken, refreshToken) {
                return engine.devices.loadOneDevice({ kind: 'com.nest',
                                                      accessToken: accessToken,
                                                      refreshToken: refreshToken }, true);
            }
        });
    }
github stanford-oval / thingpedia-common-devices / com.youtube / device.js View on Github external
},
  causes: {
    id: "GCQ2F1c2VzICYgTm9uLXByb2ZpdHM",
    title: "Causes & Non-profits"
  },
  news_and_politics: {
    id: "GCTmV3cyAmIFBvbGl0aWNz",
    title: "News & Politics"
  },
  lifestyle: {
    id: "GCTGlmZXN0eWxl",
    title: "Lifestyle"
  }
};

module.exports = class YouTubeDevice extends Tp.BaseDevice {
    static get runOAuth2() {
        return Tp.Helpers.OAuth2({
            kind: 'com.youtube',
            scope: ['openid','profile','email',
                    'https://www.googleapis.com/auth/youtube.force-ssl',
                    'https://www.googleapis.com/auth/youtube',
                    'https://www.googleapis.com/auth/youtube.readonly',
                    'https://www.googleapis.com/auth/youtube.upload'],
            authorize: 'https://accounts.google.com/o/oauth2/auth',
            get_access_token: 'https://www.googleapis.com/oauth2/v3/token',
            set_access_type: true,
            callback: function(engine, accessToken, refreshToken) {
                var auth = 'Bearer ' + accessToken;
                return Tp.Helpers.Http.get('https://www.googleapis.com/oauth2/v2/userinfo', { auth: auth, accept: 'application/json' })
                .then((response) => {
                    const parsed = JSON.parse(response);