Skip to content

Commit

Permalink
Added middleware and moved notification realtime from account to noti…
Browse files Browse the repository at this point in the history
…fication
  • Loading branch information
vitorfdl committed Aug 11, 2017
1 parent 3bc06f6 commit 78a7d33
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 50 deletions.
7 changes: 4 additions & 3 deletions !examples/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
// myacc.devices.create({'name':'Teste OpenSource'})
// .then(result => console.log(result))
// .catch(result => console.log(result));
process.env.TAGO_API = 'http://api.staging.tago.io';

'use strict';
const Account = require('../account');
const myacc = new Account('aedc4410-7059-11e7-ba44-5bde5c28a365');
const myacc = new Account('5fbee790-7075-11e6-9a4a-a9239fa9b3db');

myacc.notificationListening(console.log).then(console.log);
myacc.notifications.list().then(console.log);
// myacc.notifications.markAsRead(['598dbf22ba30030010c6bf22']).then(console.log);
38 changes: 10 additions & 28 deletions account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const config = require('../config.js');
const default_headers = require('../comum/default_headers.js');
const request = require('../comum/tago_request.js');

const Actions = require('./actions.js');
const Analysis = require('./analysis.js');
const Buckets = require('./buckets.js');
const Dashboards = require('./dashboards.js');
const Devices = require('./devices.js');
const Notifications = require('./notifications.js');
const Actions = require('./actions.js');
const Analysis = require('./analysis.js');
const Buckets = require('./buckets.js');
const Dashboards = require('./dashboards.js');
const Devices = require('./devices.js');
const Notifications = require('./notifications.js');
const Middlewares = require('./middlewares.js');

const Realtime = require('./../utils/').realtime;

Expand Down Expand Up @@ -234,28 +235,6 @@ class Account {
return request(options);

}

/** Start listening the notifaticons
* @param {function} function function to run when realtime is triggered
* @param {class} realtime an realtime with personalized function. Be sure to call listening only inside a connect function (optional)
*/
notificationListening(func, realtime) {
if (!this.realtime && !realtime) this.realtime = new Realtime(this.token);

realtime = realtime || this.realtime;
realtime.get_socket.on('notification', func);

return Promise.resolve('Listening to Notifications.');
}

/** Stop to listen the analysis by its ID
* @param {String} analyze_id id of the analysis
*/
notificationStoplisten(realtime) {
if (!this.realtime && !realtime) return;

realtime = realtime || this.realtime;
}

// ----------- Sub-methods -----------
get actions() {
Expand All @@ -276,6 +255,9 @@ class Account {
get notifications() {
return new Notifications(this.token);
}
get middlewares() {
return new Middlewares(this.token);
}
}

module.exports = Account;
43 changes: 43 additions & 0 deletions account/middlewares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';
const request = require('../comum/tago_request.js');
const config = require('../config.js');
const default_headers = require('../comum/default_headers.js');

class Middlewares {
constructor(acc_token) {
this.token = acc_token;
this.default_options = {
'json': true,
'headers': default_headers(this)
};
}

/**
* Return list of all middlewares available for tokens
* @param {Boolean} owner if true, get a list of middlewares and tokens owned by the account
* @return {Promise}
*/
list(owner) {
let url = `${config.api_url}/middleware`;
if (owner) url = `${url}?owner=true`;
const method = 'GET';

const options = Object.assign({}, this.default_options, {url, method});
return request(options);
}

/**
* Remove older token and returns a new token for the middleware.
* @param {String} middleware_name name of the middleware
* @return {Promise}
*/
genToken(middleware_name) {
const url = `${config.api_url}/middleware/gen_token/${middleware_name}`;
const method = 'PUT';

const options = Object.assign({}, this.default_options, {url, method});
return request(options);
}
}

module.exports = Middlewares;
22 changes: 22 additions & 0 deletions account/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ class Notifications {

const options = Object.assign({}, this.default_options, {url, method});
return request(options);
}

/** Start listening the notifaticons
* @param {function} function function to run when realtime is triggered
* @param {class} realtime an realtime with personalized function. Be sure to call listening only inside a connect function (optional)
*/
listen(func, realtime) {
if (!this.realtime && !realtime) this.realtime = new Realtime(this.token);

realtime = realtime || this.realtime;
realtime.get_socket.on('notification', func);

return Promise.resolve('Listening to Notifications.');
}

/** Stop to listen the analysis by its ID
* @param {String} analyze_id id of the analysis
*/
stoplisten(realtime) {
if (!this.realtime && !realtime) return;

realtime = realtime || this.realtime;
}
}

Expand Down
45 changes: 31 additions & 14 deletions extra/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
'use strict';
const services = {
weather: require('./weather'),
currency: require('./currency'),
geocoding: require('./geocoding'),
distance: require('./distance')
};
const request = require('../comum/tago_request.js');
const config = require('../config.js');
const default_headers = require('../comum/default_headers.js');

class Extra {
constructor(service, api_key) {
if (!service) { throw 'Missing parameter service'; }
else if (!api_key) { throw 'Missing parameter api_key'; }
/** Class for the device and data */
class Middleware {
/** Device
* @param {String} Middleware Token
* @return {Object} Middleware Object
*/
constructor(token, details) {
this.token = token;
this.default_options = {
'json': true,
'headers': default_headers(this)
};

if (!services[service]) {
throw `Can\'t find service ${service}`;
if (details) {
this.default_options.qs = {'details': true};
}
}

/**
* Get token of the device by its serial number.
* Return nothing if the serie number is not associeted with middleware
* @param {STRING} serial_number serial number of the device
* @return {Promise}
*/
getBySerieNum(serial_number) {
let url = `${config.api_url}/middleware/get_by_serienum/${serial_number}`;
let method = 'GET';

return new services[service](api_key);
let options = Object.assign({}, this.default_options, {url, method});
return request(options);
}
}

module.exports = Extra;
module.exports = Middleware;
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
exports.Account = require('./account/');
exports.Analysis = require('./analysis/');
exports.Device = require('./device/');
exports.Services = require('./services/');
exports.Utils = require('./utils/');
exports.Account = require('./account/');
exports.Analysis = require('./analysis/');
exports.Device = require('./device/');
exports.Middleware = require('./middleware/');
exports.Services = require('./services/');
exports.Utils = require('./utils/');
Empty file added middleware/index.js
Empty file.

0 comments on commit 78a7d33

Please sign in to comment.