-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.js
163 lines (152 loc) · 4.25 KB
/
Connection.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
.pragma library
//var rootUrl = 'http://api.dev';
var rootUrl = 'https://yhteys.kotivo.fi';
var apiUrl = rootUrl + '/api/v1';
var systemUrl = rootUrl + '';
function getRootUrl() {
return rootUrl;
}
function setRootUrl(url) {
if (!url) {
url = 'https://yhteys.kotivo.fi';
}
rootUrl = url;
apiUrl = rootUrl + '/api/v1';
systemUrl = rootUrl + '';
}
function request(method, url, headers, body, raw, cb_ok, cb_error) {
var http = new XMLHttpRequest();
http.open(method, url);
for (var key in headers) {
console.log('header: ' + key + ': ' + headers[key]);
http.setRequestHeader(key, headers[key]);
}
http.setRequestHeader('Content-Type', 'application/json');
http.onreadystatechange = function() { // Call a function when the state changes.
if (http.readyState == 4) {
if (http.status == 200) {
try {
var response = http.responseText;
if (raw !== true) {
response = JSON.parse(response).data;
}
if (cb_ok !== undefined) {
cb_ok(response);
}
} catch (e) {
console.log('error (' + http.status + '): ' + http.responseText.slice(0, 100));
if (cb_error !== undefined) {
cb_error(http);
}
}
} else if (http.status < 200 || http.status == 403 || http.status == 500) {
/* connection to server failed somehow or access denied, show login */
console.log('error (' + http.status + ')');
if (cb_error !== undefined) {
cb_error(http);
}
} else {
/* some other error occured, just show error */
console.log('error (' + http.status + ')');
if (cb_error !== undefined) {
cb_error(http);
}
}
}
}
console.log(method + ' ' + url);
http.send(body);
}
function get(postfix, cb_ok, cb_error) {
postfix += postfix.slice(-1) !== '/' ? '/' : '';
request('GET', apiUrl + postfix, {}, undefined, false, cb_ok, cb_error);
}
function put(postfix, data, cb_ok, cb_error) {
postfix += postfix.slice(-1) !== '/' ? '/' : '';
request('PUT', apiUrl + postfix, {}, JSON.stringify(data), false, cb_ok, cb_error);
}
function login(username, password, admin_username, admin_password, cb_ok, cb_error) {
if (admin_username && admin_password) {
request('GET', systemUrl + '/account/fake/' + username + '/', {
'Authorization': 'Basic ' + Qt.btoa(admin_username + ':' + admin_password)
}, undefined, true, cb_ok, cb_error);
} else {
put('/accounts/login/', {
'username': username,
'password': password
}, cb_ok, cb_error);
}
}
function logout(callback) {
get('/accounts/logout/', callback);
}
function controllersGet(model, cb_error) {
get('/controllers/', function(controllers) {
for (var j = 0; j < model.count; j++) {
var found = false;
for (var i = 0; i < controllers.length; i++) {
if (model.get(j).id === controllers[i].id) {
found = true;
break;
}
}
if (!found) {
model.remove(j);
}
}
for (var i = 0; i < controllers.length; i++) {
var found = false;
for (var j = 0; j < model.count; j++) {
if (model.get(j).id === controllers[i].id) {
found = true;
break;
}
}
if (!found) {
model.append(controllers[i]);
} else {
model.set(j, controllers[i]);
}
}
}, cb_error);
}
function controllerAway(controller, away, cb_ok, cb_error) {
put('/controllers/' + controller.id + '/profiles/away/', {
'away': away
}, cb_ok, cb_error);
}
function modulesGet(controller, model) {
get('/controllers/' + controller.id + '/modules/', function(modules) {
for (var j = 0; j < model.count; j++) {
var found = false;
for (var i = 0; i < modules.length; i++) {
if (model.get(j).id === modules[i].id) {
found = true;
break;
}
}
if (!found) {
model.remove(j);
}
}
for (var i = 0; i < modules.length; i++) {
var found = false;
for (var j = 0; j < model.count; j++) {
if (model.get(j).id === modules[i].id) {
found = true;
break;
}
}
if (!found) {
model.append(modules[i]);
} else {
model.set(j, modules[i]);
}
}
});
}
function moduleAway(controller, module, away, cb_ok, cb_error) {
put('/controllers/' + controller.id + '/modules/' + module.id, {
'away': away
}, cb_ok, cb_error);
}