-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
97 lines (87 loc) · 3.24 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
const fetch = require('node-fetch');
const yaleAuthToken = 'VnVWWDZYVjlXSUNzVHJhcUVpdVNCUHBwZ3ZPakxUeXNsRU1LUHBjdTpkd3RPbE15WEtENUJ5ZW1GWHV0am55eGhrc0U3V0ZFY2p0dFcyOXRaSWNuWHlSWHFsWVBEZ1BSZE1xczF4R3VwVTlxa1o4UE5ubGlQanY5Z2hBZFFtMHpsM0h4V3dlS0ZBcGZzakpMcW1GMm1HR1lXRlpad01MRkw3MGR0bmNndQ==';
var refresh_token = '';
var access_token = '';
const urls = {
auth: 'https://mob.yalehomesystem.co.uk/yapi/o/token/',
getStatus: 'https://mob.yalehomesystem.co.uk/yapi/api/panel/mode/',
setStatus: 'https://mob.yalehomesystem.co.uk/yapi/api/panel/mode/',
services: 'https://mob.yalehomesystem.co.uk/yapi/services/'
};
function getAccessToken(username, password) {
payload = `grant_type=password&username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`;
return fetch(urls.auth, {
method: 'POST',
body: payload,
headers: {
"Authorization": "Basic " + yaleAuthToken,
'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
})
.then(res => res.json())
.then(json => {
if (json.error === 'invalid_grant') {
return Promise.reject(json.error_description);
}
else {
fetch(urls.services, {
method: 'GET',
headers: {
"Authorization": "Bearer " + access_token,
'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
});
return json.access_token;
};
});
}
function setStatus (access_token, alarmstate) {
return new Promise((resolve, reject) => {
if (!access_token || access_token.length === 0) {
reject('Please call getAccessToken to get your access token first');
}
if (alarmstate !== 'arm' && alarmstate !== 'home' && alarmstate !== 'disarm') {
reject('Invalid mode passed to setStatus');
}
return fetch(urls.setStatus, {
method: 'POST',
body: `area=1&mode=${alarmstate}`,
headers: {
"Authorization": "Bearer " + access_token,
'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).then(res => res.json())
.then(json => {
setStatus = json.data.cmd_ack;
resolve(setStatus);
})
});
}
function getStatus (access_token) {
return new Promise((resolve, reject) => {
if (!access_token || access_token.length === 0) {
reject('Please call getAccessToken to get your access token first');
};
return fetch(urls.getStatus, {
method: 'GET',
headers: {
"Authorization": "Bearer " + access_token,
'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
})
.then(res => res.json())
.then(json => {
alarmState = json.data[0].mode;
resolve(alarmState);
})
})
}
module.exports = {
getAccessToken,
getStatus,
setStatus,
}