This repository has been archived by the owner on Feb 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (83 loc) · 2.8 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
var rp = require('request-promise-native');
var WebSocket = require('faye-websocket');
var garageDoor;
var placeId;
var cookieData;
var irisWebSocket;
module.exports = function (iris_user, iris_password, action, callback, deviceName) {
var options = {
method: 'POST',
uri: 'https://bc.irisbylowes.com/login',
form: {
user: iris_user,
password: iris_password,
public: true
},
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
resolveWithFullResponse: true
};
function setPlace() {
irisWebSocket.send(JSON.stringify({ type: 'sess:SetActivePlace',
headers: { destination: 'SERV:sess:', correlationId: '78f7d29a-222e-4976-9d2b-d1f553cf8881', isRequest: true },
payload: { messageType: 'sess:SetActivePlace', attributes: { placeId: placeId } }
}));
listDevices();
}
function listDevices() {
irisWebSocket.send(JSON.stringify({ type: 'place:ListDevices',
headers: { destination: 'SERV:place:' + placeId, correlationId: '6606672e-57f8-47d1-8002-5fe59d34c1d8', isRequest: true },
payload: { messageType: 'place:ListDevices', attributes: {} }
}));
}
function toggleGarageDoor(setGarageState) {
irisWebSocket.send(JSON.stringify({
headers: { destination: garageDoor['base:address'], correlationId: '790525f5-171f-4533-a952-0dcafb9b5310', isRequest: true },
payload: { messageType: 'base:SetAttributes', attributes: { 'motdoor:doorstate': setGarageState } }
}));
irisWebSocket.close();
}
function onMessage(messageData) {
if (messageData.headers.correlationId === '6606672e-57f8-47d1-8002-5fe59d34c1d8') {
messageData.payload.attributes.devices.forEach(function(device) {
if (
device['motdoor:doorstate']
&& (device['dev:name'] === deviceName || !deviceName)
) {
garageDoor = device;
}
});
switch (action) {
case 'open':
toggleGarageDoor('OPEN');
return callback('OPENING');
case 'close':
toggleGarageDoor('CLOSED');
return callback('CLOSING');
default:
case 'status':
irisWebSocket.close();
return callback(garageDoor['motdoor:doorstate']);
}
}
if (!placeId && messageData.payload.attributes.places) {
placeId = messageData.payload.attributes.places[0].placeId;
setPlace();
}
}
rp(options).then(function (response) {
cookieData = response.headers['set-cookie'][0].split(';')[0];
irisWebSocket = new WebSocket.Client(
'wss://bc.irisbylowes.com/websocket',
[],
{ headers: { Cookie: cookieData} }
);
irisWebSocket.on('message', function (evt) {
onMessage(JSON.parse(evt.data));
});
})
.catch(function (err) {
console.log('Could not get Iris By Lowe\'s cookie');
});
}