forked from AshMartian/unofficial-sense
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
162 lines (139 loc) · 5.99 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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) => {
try {
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);}
}
});
senseWS.onclose = (data) => {
emmitter.emit('close', data);
if (verbose){console.log("Connection closed: " + data);}
};
senseWS.onerror = (data) => {
emmitter.emit('error',data);
if (verbose){console.log("Error: " + data);}
};
} catch (error) {
console.error(`Websocket error: ${error.message}`);
}
}
var authData = {};
module.exports =
//Constructor
async (config, onData) => {
return new Promise( async (resolve, reject) => {
try {
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};
if (config.access_token){
authData = {"access_token":config.access_token, user_id: config.user_id, authorized: true, monitors: [{id:0}]}
}
else{
authData = await doAuth();
}
async function doAuth(){
return new Promise( async (resolve, reject) => {
try {
const res = await fetch(`${apiURL}authenticate`, { method: 'POST', body: `email=${encodeURI(config.email)}&password=${encodeURI(config.password)}`, headers: {"Content-Type":"application/x-www-form-urlencoded"} })
if (res.status == 200){
resolve(await res.json());
}
else if (res.status == 401 || res.status == 400){
reject(new Error('Authentication failed! Check username/password and try again.'));
}
else{
reject(new Error(`Authentication failed! ${res.statusText}`));
}
} catch (error) {
reject(new Error(`Authentication error: ${error.message}`));
}
})
}
//Only proceed if auth succeeded
if(authData.authorized) {
if(typeof onData == 'function') {
onData({
status: 'Authenticated',
data: authData.monitors
})
}
//Utility function for all callout methods; handles 401's with a special reject so client can reliably catch it
async function doSenseCallout (URL) {
return new Promise( async (resolve, reject) => {
try {
const res = await fetch(URL, { method: 'GET', headers: {"Authorization": `bearer ${authData.access_token}`} })
if (res.status == 200){
resolve(await res.json());
}
else if (res.status == 401){
reject(new Error('Authentication failed!'));
}
else{
reject(new Error(await res.json()));
}
} catch (error) {
reject(error);
}
})
}
//Final resolve - list of exposed methods
resolve({
authData: authData,
events: emmitter,
openStream: () => {
setupWS(onData);
},
closeStream: () => {
senseWS.close();
},
getAuth: async () => {
return authData = await doAuth();
},
getDevices: async () => {
return doSenseCallout(`${apiURL}app/monitors/${authData.monitors[0].id}/devices`);},
getMonitorInfo: async () => {
return doSenseCallout(`${apiURL}app/monitors/${authData.monitors[0].id}/status`);
},
getTimeline: async () => {
return doSenseCallout(`${apiURL}users/${authData.user_id}/timeline`);
},
getDailyUsage: async (startTime) => {
return doSenseCallout(`${apiURL}app/history/trends?monitor_id=${authData.monitors[0].id}&scale=DAY&start=${encodeURI(startTime)}`)
}
})
} else if(authData.status == 'error') {
reject(new Error(authData.error_reason));
} else {
reject(new Error('Unable to make auth request'));
}
} catch (error) {
reject(error);
}
});
}