-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
239 lines (214 loc) · 5.93 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
(function() {
var resource = '..';
var setStatus = (function() {
var statusBar;
function countDown(str, now) {
statusBar.textContent = str.replace(/\${time}/g, now);
if(now - 1 > 0) {
setTimeout(function() {
countDown(str, now - 1);
}, 1000);
}
}
return function(s, t) {
if(!statusBar) {
statusBar = document.querySelector('#status p');
}
if(t) {
countDown(s, t);
} else {
statusBar.textContent = s;
}
};
})();
var inputPrompt = (function() {
var notice = document.createElement('div');
notice.setAttribute('id', 'notice');
var h1 = document.createElement('h1');
notice.appendChild(h1);
var input = document.createElement('input');
notice.appendChild(input);
return function(msg, type) {
input.setAttribute('type', type || 'text');
h1.textContent = msg;
document.body.appendChild(notice);
input.focus();
return new Promise(function(resolve, reject) {
input.onkeypress = function(event) {
if(event.keyCode === 13) {
var value = input.value;
input.value = '';
input.onkeypress = null;
document.body.removeChild(notice);
resolve(value);
}
};
});
};
})();
function getCredentials() {
return inputPrompt('Enter your authorized username').then(function(username) {
return inputPrompt('Enter your password', 'password').then(function(password) {
return [username, password];
});
});
}
function getNodeList(credentials) {
setStatus('Fetching node list');
var username = credentials[0];
var password = credentials[1];
return new Promise(function(resolve, reject) {
var formData = new FormData();
formData.append('nl', '*');
var xhr = new XMLHttpRequest();
xhr.open('POST', resource + '/json', true, username, password);
xhr.onload = function(e) {
if(this.status === 200) {
try {
resolve(JSON.parse(this.responseText));
} catch(e) {
reject(e);
}
} else {
reject(new Error(this.status + ': ' + this.statusText));
}
};
xhr.onerror = function(e) {
reject('Request error');
};
xhr.send('nl=*');
});
}
function parseNodes(nodeList) {
setStatus('Parsing node list');
var devices = [];
for(var node in nodeList.nodes) {
for(var item in nodeList.nodes[node]) {
if(/^DEV\d+$/.test(item)) {
devices.push({
id: node,
device: item,
deviceId: parseInt(/^DEV(\d+)$/.exec(item)[1], 10),
name: nodeList.nodes[node][item],
active: nodeList.nodes[node]['ZigBee'] === 0 || nodeList.nodes[node]['LOCAL'] === 0 || false,
type: 'VAV box'
});
break;
}
}
}
devices.sort(function(a, b) {
var idA = a.deviceId;
var idB = b.deviceId;
if(idA < idB) {
return -1;
} else if(idA > idB) {
return 1;
} else {
return 0;
}
});
return {
local: nodeList.local,
devices: devices
};
};
var registerDevices = (function() {
var controllerList;
var localNode;
function addSelector(device) {
var li = document.createElement('li');
var input = document.createElement('input');
input.setAttribute('id', 's_' + device.id);
input.setAttribute('type', 'radio');
input.setAttribute('name', 'device');
var label = document.createElement('label');
label.setAttribute('for', 's_' + device.id);
if(device.type) {
label.setAttribute('data-type', device.type);
}
label.textContent = device.name;
if(!device.active) {
li.setAttribute('data-inactive', '');
} else {
if(device.id === localNode) {
li.setAttribute('data-local', '');
}
}
li.appendChild(input);
li.appendChild(label);
return li;
}
return function(parsedNodeList) {
setStatus('Registering devices');
localNode = parsedNodeList.local;
var controllerMap = new Map();
if(!controllerList) {
controllerList = document.querySelector('#controllers ol');
}
for(var i = 0; i < parsedNodeList.devices.length; i++) {
var e = addSelector(parsedNodeList.devices[i]);
controllerList.appendChild(e);
controllerMap.set(parsedNodeList.devices[i].id, {
html: e,
node: parsedNodeList.devices[i]
});
}
return controllerMap;
};
})();
var waitTime = (function() {
var lastAttemptTime = Date.now();
var maxTime = 60 * 60 * 1;
var lastTime = 0;
return function() {
if(Date.now() - lastAttemptTime > 1000 * maxTime) {
lastTime = 5;
} else {
lastTime = Math.max(5, Math.min(maxTime, lastTime * 2.5))|0;
}
lastAttemptTime = Date.now();
return lastTime;
};
})();
function retry(fn, reason) {
function timedRetry(reason) {
var time = waitTime();
setStatus(reason + '. Trying again in ${time} seconds', time);
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(fn());
}, time * 1000);
});
}
if(!reason) {
return function(reason) {
return timedRetry(reason);
};
} else {
return timedRetry(reason);
}
}
function init() {
setStatus('Running startup');
getCredentials()
.then(getNodeList)
.then(parseNodes)
.then(registerDevices)
.then(function(value) {
setStatus('Monitoring');
}, function(reason) {
if(reason.message === '401: Unauthorized') {
init();
} else {
retry(init, reason);
}
});
}
function main() {
init();
}
window.onload = function() {
main();
};
})();