forked from chucknthem/Simple-Usage-Meter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exetel.js
85 lines (77 loc) · 3.07 KB
/
exetel.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
function exetel_getConfig() {
var cfg = new Object();
cfg.url = "https://www.exetel.com.au/members/usagemeter_xml.php?{USERNAME},{PASSWORD}";
cfg.requestType = "GET";
cfg.requestParams = null;
return cfg;
}
/*
* get topup limits for netspace plans
* @param name limit name
* @param limits array of xml LIMIT nodes with BLOCK child nodes
*/
function exetel_getBlockLimits(name, limits) {
var limit = 0;
for(var i = 0; i < limits.length; i++) {
if(limits[i].getAttribute("NAME") == name) {
var blocks = limits[i].getElementsByTagName("BLOCK");
if(blocks) {
for(var b = 0; b < blocks.length; b++) {
limit += parseInt(blocks[b].getAttribute("MEGABYTES"));
}
}
}
}
return limit;
}
/*
* calculate days left until quota is reset
* the xml doesn't define when the end of billing cycle is,
* allegedly, it's the end of the month
*/
function _getDaysLeft(xml) {
var today = new Date();
var endDate = new Date(today.getFullYear(), today.getMonth() + 1, 1);
return Math.floor((endDate.getTime() - today.getTime())/(1000*60*60*24));
}
/*
* nestpace isp
* @param xml root xml document with USAGE as the first node
*/
function exetel_parseXML(text) {
var xml = (new DOMParser()).parseFromString(text, "text/xml");
var results = new Array();
var daysLeft = _getDaysLeft(xml);
if (daysLeft == null) {
return mkError('invalid XML');
}
var planDetails = xml.evaluate('/Response/PlanDetails', xml, null, XPathResult.ANY_TYPE, null);
var qResults = planDetails.iterateNext();
var usageDetails = xml.evaluate('/Response/CurrentMonthUsage', xml, null, XPathResult.ANY_TYPE, null);
var qUsageResults = usageDetails.iterateNext();
var results = new Array();
for(var i = 0; qResults; qResults = planDetails.iterateNext(), qUsageResults = usageDetails.iterateNext(), i += 2){
//exetel has a plan name and then peak + offpeak for each name
var mainName = qResults.getElementsByTagName("PlanName")[0].childNodes[0].nodeValue;
//peak
results[i] = new Object();
results[i]['name'] = mainName + '- peak';
results[i]['limit'] = parseInt(qResults.getElementsByTagName("PeakTimeDownloadInMB")[0].childNodes[0].nodeValue);
results[i]['usagemb'] = parseInt(qUsageResults.getElementsByTagName("PeakDownload")[0].childNodes[0].nodeValue);
results[i]['daysleft'] = daysLeft;
results[i]['custom'] = false;
//offpeak
//some plans don't have off peak plans, so we check if they do or don't based whether the xml defines an offpeak limit
var offpeakNodeLimit = qResults.getElementsByTagName("OffpeakTimeDownloadInMB")[0].childNodes[0];
if(typeof(offpeakNodeLimit) != 'undefined') {
results[i + 1] = new Object();
results[i + 1]['limit'] = parseInt(offpeakNodeLimit.nodeValue);
results[i + 1]['name'] = mainName + '- offpeak';
results[i + 1]['limit'] = parseInt(qResults.getElementsByTagName("OffpeakTimeDownloadInMB")[0].childNodes[0].nodeValue);
results[i + 1]['usagemb'] = parseInt(qUsageResults.getElementsByTagName("OffpeakDownload")[0].childNodes[0].nodeValue);
results[i + 1]['daysleft'] = daysLeft;
results[i + 1]['custom'] = false;
}
}
return results;
}