Skip to content

Commit 468e04a

Browse files
jasondu168hugocortes
authored andcommitted
feat: add delorean es api (#6)
* Feat: use es version delores for new apis * Fix: handle both new and old delorean * Fix: change device check in parameters * Fix: enable log and set timeout and deadline * Fix: add LRU cache to device type meta key * Fix: this.cache * Fix: lint error * Fix: send method returns Promise * Fix: return promise * Fix: delete dispose function, use shorter age to test on staging * Fix: change logger to false * Fix: remove LRU cache * Fix: separate Delorean and Delorean-ES
1 parent b8f8bcd commit 468e04a

File tree

5 files changed

+372
-257
lines changed

5 files changed

+372
-257
lines changed

lib/Auth/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ class Auth {
107107
{
108108
headers: { 'content-type': 'application/x-www-form-urlencoded' },
109109
payload,
110-
isPublic: true
110+
isPublic: true,
111+
timeout: 2000,
112+
deadline: 4000
111113
}
112114
).catch(error => {
113115
if (error.status === 400 && grantType === 'refresh_token') {
@@ -158,7 +160,16 @@ class Auth {
158160
throw this.invalidCredentials;
159161
}
160162

161-
opts = Object.assign({ headers: {}, isPublic: false, cache: false }, opts);
163+
opts = Object.assign(
164+
{
165+
headers: {},
166+
isPublic: false,
167+
cache: false,
168+
timeout: 60000,
169+
deadline: 90000
170+
},
171+
opts
172+
);
162173

163174
if (!retry) {
164175
service = `[${service}]`;
@@ -194,6 +205,10 @@ class Auth {
194205
begin = new Date().getTime();
195206
({ status, body } = await superagent(method, url)
196207
.set(opts.headers)
208+
.timeout({
209+
response: opts.timeout,
210+
deadline: opts.deadline
211+
})
197212
.query(opts.query)
198213
.send(opts.payload));
199214

lib/Services/delorean-es.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
class DeloreanES {
2+
/**
3+
* @param {String} url
4+
* @param {typeof Auth} Auth
5+
*/
6+
constructor(url, Auth) {
7+
this.service = DeloreanES.name;
8+
this.url = url;
9+
this.auth = Auth;
10+
}
11+
12+
/**
13+
* get latest checkin timestamp of deviceId
14+
* sensors - get sensors or not
15+
*
16+
* @param {String} userId
17+
* @param {String} deviceId
18+
* @param {boolean} getSensors
19+
* @param {Array} channels
20+
*/
21+
getDeviceCheckIn(userId, deviceId, needSensors, channels) {
22+
const url = `${this.url}/devices/${deviceId}/checkin`;
23+
const query = { user_id: userId, sensors: needSensors, channels: channels };
24+
return this.auth.send(this.service, 'GET', url, { query });
25+
}
26+
27+
/**
28+
* @param {String} userId
29+
* @param {String} deviceId
30+
* @param {Object} query
31+
* @param {String} query.type
32+
* @param {String} query.units
33+
*/
34+
getDeviceState(userId, deviceId, query) {
35+
const url = `${this.url}/devices/${deviceId}/state`;
36+
query = { ...query, user_id: userId };
37+
return this.auth.send(this.service, 'GET', url, { query });
38+
}
39+
40+
/**
41+
* @param {String} userId
42+
* @param {String} deviceIds
43+
* @param {Object} query
44+
*/
45+
getDevicesState(userId, deviceIds, query) {
46+
const url = `${this.url}/devices/state`;
47+
query = { ...query, user_id: userId, devices: deviceIds };
48+
return this.auth.send(this.service, 'GET', url, { query });
49+
}
50+
51+
/**
52+
* @param {String} userId
53+
* @param {String} deviceId
54+
* @param {Object} query
55+
*/
56+
getDeviceTotals(userId, deviceId, query) {
57+
const url = `${this.url}/devices/${deviceId}/totals`;
58+
query = { ...query, user_id: userId };
59+
return this.auth.send(this.service, 'GET', url, { query });
60+
}
61+
62+
/**
63+
* @param {String} userId
64+
* @param {String} deviceId
65+
* @param {Object} query
66+
*/
67+
getDeviceAggregations(userId, deviceId, query) {
68+
const url = `${this.url}/devices/${deviceId}/aggregations`;
69+
query = { ...query, user_id: userId };
70+
return this.auth.send(this.service, 'GET', url, { query });
71+
}
72+
73+
/**
74+
* @param {String} userId
75+
* @param {String} deviceId
76+
* @param {Object} query
77+
*/
78+
getDeviceReadings(userId, deviceId, query) {
79+
const url = `${this.url}/devices/${deviceId}/readings`;
80+
query = { ...query, user_id: userId };
81+
return this.auth.send(this.service, 'GET', url, { query });
82+
}
83+
84+
/**
85+
* @param {String} userId
86+
* @param {String} deviceId
87+
* @param {Object} query
88+
*/
89+
getDeviceReportReadings(userId, deviceId, query) {
90+
const url = `${this.url}/devices/${deviceId}/readings/report`;
91+
query = { ...query, user_id: userId };
92+
return this.auth.send(this.service, 'GET', url, { query });
93+
}
94+
}
95+
96+
module.exports = DeloreanES;

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const Auth = require('./Auth');
22
const Cayenne = require('./Services/cayenne');
33
const Delorean = require('./Services/delorean');
4+
const DeloreanES = require('./Services/delorean-es');
45
const Executor = require('./Services/executor');
56
const Hermes = require('./Services/hermes');
67
const Prometheus = require('./Services/prometheus');
@@ -20,6 +21,7 @@ class Core {
2021
* @param {Object} options Additional options
2122
* @param {String} options.cayenneBaseUrl Things URL
2223
* @param {String} options.deloreanBaseUrl History URL
24+
* @param {String} options.deloreanESBaseUrl History URL
2325
* @param {String} options.executorBaseUrl Jobs URL
2426
* @param {String} options.hermesBaseUrl Notifications URL
2527
* @param {String} options.prometheusBaseUrl Lora URL
@@ -32,9 +34,9 @@ class Core {
3234
options = Object.assign({ logger: false, isOffline: true }, options);
3335

3436
this.auth = new Auth(clientId, clientSecret, authorizationUrl, options);
35-
3637
this.cayenne = new Cayenne(options.cayenneBaseUrl, this.auth);
3738
this.delorean = new Delorean(options.deloreanBaseUrl, this.auth);
39+
this.deloreanES = new DeloreanES(options.deloreanESBaseUrl, this.auth);
3840
this.executor = new Executor(options.executorBaseUrl, this.auth);
3941
this.hermes = new Hermes(options.hermesBaseUrl, this.auth);
4042
this.prometheus = new Prometheus(options.prometheusBaseUrl, this.auth);

0 commit comments

Comments
 (0)