-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathevnotify.js
More file actions
291 lines (254 loc) · 10.9 KB
/
evnotify.js
File metadata and controls
291 lines (254 loc) · 10.9 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
(function () {
'use strict';
var RESTURL = 'https://app.evnotify.de/';
/**
* Function which sends a specific request to backend server and returns the information/data (or error information if any)
* @param {String} [type] the HTTP method to use (POST|GET|PUT|DELETE)
* @param {String} [fnc] the function which should be used (e.g. login)
* @param {*} [data] the data to send (mostly an object)
* @param {Function} callback callback function
* @return {void}
*/
var sendRequest = function(type, fnc, data, callback) {
try {
var xmlHttp = new XMLHttpRequest(),
retData;
type = type.toUpperCase();
data = ((typeof data === 'object' && data != null) ? data : '');
// apply listener for the request
xmlHttp.onreadystatechange = function() {
if (this.readyState === 4) {
// try to parse the response as JSON
try {retData = JSON.parse(this.responseText);} catch (e) {retData = this.responseText}
callback(((this.status !== 200)? this.status : null), {
status: this.status,
data: retData
});
}
};
xmlHttp.onerror = function(e) {
callback(e, null);
};
// send the request
xmlHttp.open(type, RESTURL + ((fnc)? fnc : '') + ((type === 'GET' && data) ?
"?" + Object
.keys(data)
.map(function(key){
return key+"="+encodeURIComponent(data[key])
})
.join("&") : ''
), true);
xmlHttp.setRequestHeader('Content-Type', 'application/json');
xmlHttp.send(((data)? JSON.stringify(data) : data));
} catch (e) {
callback(e, null);
}
};
/**
* The EVNotify constructor class
* @constructor EVNotify
*/
function EVNotify() {
// prevent wrong declaration
if(!(this instanceof EVNotify) || this.__previouslyConstructedByEVNotify) throw new Error('EVNotify must be called as constructor. Missing new keyword?');
this.__previouslyConstructedByEVNotify = true;
}
/**
* Function to retrieve a random akey which was available at the time of the request
* @param {Function} [callback] callback function
* @return {Object} returns this
*/
EVNotify.prototype.getKey = function(callback) {
var self = this;
sendRequest('get', 'key', null, function(err, res) {
// send response to callback if applied
if(typeof callback === 'function') callback(err, ((err)? null : ((res && res.data)? res.data.akey : null)));
});
return self;
};
/**
* Function to register account for given akey with specified password to retrieve and set token and the AKey
* @param {String} akey the AKey to register
* @param {String} password the password to use for the AKey
* @param {Function} [callback] callback function
* @return {Object} returns this
*/
EVNotify.prototype.register = function (akey, password, callback) {
var self = this;
sendRequest('post', 'register', {akey: akey, password: password}, function(err, res) {
// attach token
self.token = ((!err && res && res.data)? res.data.token : null);
// attach AKey
self.akey = ((self.token)? akey : null);
// send response to callback if applied
if(typeof callback === 'function') callback(err, ((err)? null : self.token));
});
return self;
};
/**
* Function to login account with given credentials and applies the AKey the returned token
* @param {String} akey the AKey to login
* @param {String} password the password to use for the account
* @param {Function} [callback] callback function
* @return {Object} returns this
*/
EVNotify.prototype.login = function (akey, password, callback) {
var self = this;
sendRequest('post', 'login', {akey: akey, password: password}, function(err, res) {
// attach token
self.token = ((!err && res && res.data)? res.data.token : null);
// attach AKey
self.akey = ((self.token)? akey : null);
// send response to callback if applied
if(typeof callback === 'function') callback(err, ((err)? null : self.token));
});
return self;
};
/**
* Function to change the password of the account for specified AKey with given old password and the new password
* NOTE: Requires previous authentication via login request
* @param {String} oldpassword the old (current) password
* @param {String} newpassword the new password to set
* @param {Function} [callback] callback function
* @return {Object} returns this
*/
EVNotify.prototype.changePW = function(oldpassword, newpassword, callback) {
var self = this;
// check authentication
if(!self.akey || !self.token) {
if(typeof callback === 'function') callback(401, null); // missing previous login request
} else {
sendRequest('post', 'changepw', {akey: self.akey, token: self.token, oldpassword: oldpassword, newpassword: newpassword}, function(err, res) {
if(typeof callback === 'function') callback(err, (!!(!err && res)));
});
}
return self;
};
/**
* Function to renew and change the current token to a new persistent one
* NOTE: Requires previous authentication via login request
* @param {String} password the password of the AKey to change the token for
* @param {Function} [callback] callback function
* @return {Object} returns this
*/
EVNotify.prototype.renewToken = function(password, callback) {
var self = this;
// check authentication
if(!self.akey || !self.token) {
if(typeof callback === 'function') callback(401, null); // missing previous login request
} else {
sendRequest('put', 'renewtoken', {akey: self.akey, password: password}, function(err, res) {
// attach new token
self.token = ((!err && res && res.data && res.data.token)? res.data.token : self.token);
if(typeof callback === 'function') callback(err, ((err)? null : self.token));
});
}
return self;
};
/**
* Function to get the settings and stats of the account for the given AKey
* @param {Function} [callback] callback function
* @return {Object} returns this
*/
EVNotify.prototype.getSettings = function(callback) {
var self = this;
// check authentication
if(!self.akey || !self.token) {
if(typeof callback === 'function') callback(401, null); // missing previous login request
} else {
sendRequest('get', 'settings', {akey: self.akey, token: self.token}, function(err, res) {
if(typeof callback === 'function') callback(err, ((!err && res && res.data && res.data.settings)? res.data.settings : null));
});
}
return self;
};
/**
* Function to set the settings and stats of the account for the given AKey
* @param {Object} settingsObj the object containing all keys to set
* @param {Function} [callback] callback function
* @return {Object} returns this
*/
EVNotify.prototype.setSettings = function(settingsObj, callback) {
var self = this;
// check authentication
if(!self.akey || !self.token) {
if(typeof callback === 'function') callback(401, null); // missing previous login request
} else {
sendRequest('put', 'settings', {
akey: self.akey,
token: self.token,
settings: settingsObj
}, function(err, res) {
if(typeof callback === 'function') callback(err, (!!(!err && res)));
});
}
return self;
};
/**
* Function to submit the current state of charge for the AKey
* @param {Number} display the state of charge (display) to set
* @param {Number} bms the state of charge (bms) to set
* @param {Function} callback callback function
* @return {Object} returns this
*/
EVNotify.prototype.setSOC = function(display, bms, callback) {
var self = this;
// check authentication
if(!self.akey || !self.token) {
if(typeof callback === 'function') callback(401, null); // missing previous login request
} else {
sendRequest('post', 'soc', {
akey: self.akey,
token: self.token,
display: display,
bms: bms
}, function(err, res) {
if(typeof callback === 'function') callback(err, (!!(!err && res)));
});
}
return self;
};
/**
* Function to get the current state of charge for the AKey
* @param {Function} callback callback function
* @return {Object} returns this
*/
EVNotify.prototype.getSOC = function(callback) {
var self = this;
// check authentication
if(!self.akey || !self.token) {
if(typeof callback === 'function') callback(401, null); // missing previous login request
} else {
sendRequest('get', 'soc', {
akey: self.akey,
token: self.token
}, function(err, socObj) {
if(typeof callback === 'function') callback(err, ((!err && socObj && socObj.data) ? socObj.data : null));
});
}
return self;
};
/**
* Function to send out all available / enabled notifications for the AKey
* @param {Function} callback callback function
* @return {Object} returns this
*/
EVNotify.prototype.sendNotification = function(abort, callback) {
var self = this;
// check authentication
if(!self.akey || !self.token) {
if(typeof callback === 'function') callback(401, null); // missing previous login request
} else {
sendRequest('post', 'notification', {
akey: self.akey,
token: self.token,
abort: abort
}, function(err, res) {
if(typeof callback === 'function') callback(err, (!!(!err && res)));
});
}
return self;
};
// apply to window
window.EVNotify = EVNotify;
}());