-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
96 lines (87 loc) · 4.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const fetch = require('node-fetch'), ws = require('ws'), EventEmitter = require('events');
const apiURL = 'https://api.sense.com/apiservice/api/v1/'
var emmitter = new EventEmitter();
var senseWS = null;
var verbose = true;
const setupWS = (onData) => {
const sendData = typeof onData == 'function'
let WSURL = `wss://clientrt.sense.com/monitors/${authData.monitors[0].id}/realtimefeed?access_token=${authData.access_token}`
senseWS = new ws(WSURL)
senseWS.on('open', () => {
if(sendData) {
onData({
status: "Connected"
})
}
});
senseWS.on('message', (data) => {
emmitter.emit('data', JSON.parse(data));
if(sendData) {
onData({
status: "Received",
data: JSON.parse(data)
})
} else {
if (verbose){console.log(data);}
}
})
}
var authData = {};
module.exports = async (config, onData) => {
return new Promise( async (resolve, reject) => {
if(!config.email || !config.password) {
throw new Error('Config missing required parameters, needs email and password (optional base64)')
}
if(Buffer.from(config.password).toString('base64') === config.password) {
config.password = Buffer.from(config.password.toString('base64'))
}
if (config.verbose != undefined){verbose = config.verbose};
authData = await (await fetch(`${apiURL}authenticate`, { method: 'POST', body: `email=${config.email}&password=${config.password}`, headers: {"Content-Type":"application/x-www-form-urlencoded"} })).json()
setInterval(async () => {
authData = await (await fetch(`${apiURL}authenticate`, { method: 'POST', body: `email=${config.email}&password=${config.password}`, headers: {"Content-Type":"application/x-www-form-urlencoded"} })).json()
setupWS(onData)
}, 900000)
//console.log(authData);
if(authData.authorized) {
if(typeof onData == 'function') {
onData({
status: 'Authenticated',
data: authData.monitors
})
}
setupWS(onData);
resolve({
getAuth: async () => {
authData = await (await fetch(`${apiURL}authenticate`, { method: 'POST', body: `email=${config.email}&password=${config.password}`, headers: {"Content-Type":"application/x-www-form-urlencoded"} })).json()
setupWS(onData)
return authData
},
events: emmitter,
getDevices: async () => {
return new Promise( async (resolve, reject) => {
const devices = (await (await fetch(`${apiURL}app/monitors/${authData.monitors[0].id}/devices`, { method: 'GET', headers: {"Authorization": `bearer ${authData.access_token}`} })).json())
resolve(devices)
})
},
getMonitorInfo: async () => {
return new Promise( async (resolve, reject) => {
const monitor = (await (await fetch(`${apiURL}app/monitors/${authData.monitors[0].id}/status`, { method: 'GET', headers: {"Authorization": `bearer ${authData.access_token}`} })).json())
resolve(monitor)
})
},
getTimeline: async () => {
return new Promise( async (resolve, reject) => {
const user = (await (await fetch(`${apiURL}users/${authData.user_id}/timeline`, { method: 'GET', headers: {"Authorization": `bearer ${authData.access_token}`} })).json())
resolve(user)
})
}
})
} else if(authData.status == 'error') {
emmitter.emit('error', authData.error_reason);
reject(new Error(authData.error_reason));
} else {
emmitter.emit('error', 'Unable to make auth request');
reject(new Error('Unable to make auth request'));
}
});
}