This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
183 lines (153 loc) · 5.92 KB
/
background.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
window.addEventListener("load", init);
function init() {
console.log("init called");
timeouthandle = setInterval(function() {
fetchdata();
}, 60*60*1000); // update every hour
fetchdata();
}
function fetchdata() {
console.log('fetching data');
var url = "https://t4t.services.telenet.be/TelemeterService";
var username = localStorage['username'];
var password = localStorage['password'];
if (!username || !password) {
// no account details yet
console.log('called fetchdata without credentials');
return;
}
var data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tel=\"http://www.telenet.be/TelemeterService/\"><soapenv:Header/><soapenv:Body><tel:RetrieveUsageRequest><UserId>" + username + "</UserId><Password>" + password + "</Password></tel:RetrieveUsageRequest></soapenv:Body></soapenv:Envelope>";
// create the request
var xhr = new XMLHttpRequest();
xhr.open('POST', url, false);
// mandatory header for the server to accept the request
xhr.setRequestHeader('Content-Type','text/xml');
try {
xhr.send(data);
} catch (e) {
console.log('error occurred while fetching data from service');
return;
}
if (xhr.status != 200) {
// no 200 OK response
console.log('telemeter service returned status ' + xhr.status);
return;
}
// parse the response
parser = new DOMParser();
xmlDoc = parser.parseFromString(xhr.responseText, "text/xml");
var FUP = xmlDoc.getElementsByTagName("FUP");
if(FUP.length == 0) {
hasDataLimit();
} else if(FUP.length == 1) {
hasFUP();
}
}
function hasDataLimit() {
// get general values from the response
var arr = xmlDoc.getElementsByTagName("TotalUsage");
var totalusage = arr[0].childNodes[0].data;
console.log("totalusage: " + totalusage);
arr = xmlDoc.getElementsByTagName("Limit");
var limit = arr[0].childNodes[0].data;
console.log("limit: " + limit);
arr = xmlDoc.getElementsByTagName("Unit");
var unit = arr[0].childNodes[0].data;
console.log("unit: " + unit);
arr = xmlDoc.getElementsByTagName("Timestamp");
var timestamp = arr[0].childNodes[0].data;
console.log("timestamp: " + timestamp);
var dailyusage = new Array();
// get usage per day from the response
arr = xmlDoc.getElementsByTagName("DailyUsage");
for (i = 0; i < arr.length; i++) {
var obj = new Object();
obj.day = formatdate(arr[i].childNodes[0].childNodes[0].data.substring(0, 10));
obj.usage = arr[i].childNodes[1].childNodes[0].data;
dailyusage.push(obj);
}
// get first and last day of this dataset
var lastday = "unknown";
var firstday = "unknown";
if (dailyusage.length > 0) {
lastday = dailyusage.slice(-1)[0].day;
firstday = dailyusage.slice(0,1)[0].day;
}
// save values to localstorage
localStorage['totalusage'] = totalusage;
localStorage['limit'] = limit;
localStorage['unit'] = unit;
localStorage['timestamp'] = timestamp;
localStorage['firstday'] = firstday;
localStorage['lastday'] = lastday;
// convert dailyusage array to JSON before storing in localstorage
str = JSON.stringify(dailyusage);
localStorage['dailyusage'] = str
localStorage['type'] = 0;
}
function hasFUP() {
//Get the ticket values
var arr = xmlDoc.getElementsByTagName("Timestamp");
var timestamp = arr[0].childNodes[0].data;
console.log("timestamp: " + timestamp);
// get the period values
arr = xmlDoc.getElementsByTagName("From");
var from = arr[0].childNodes[0].data;
from = from.split("+")[0];
from = formatdate(from);
console.log("from: " + from);
arr = xmlDoc.getElementsByTagName("Till");
var till = arr[0].childNodes[0].data;
till = till.split("+")[0];
till = formatdate(till);
console.log("till: " + till);
arr = xmlDoc.getElementsByTagName("CurrentDay");
var currentDay = arr[0].childNodes[0].data;
console.log("currentDay: " + currentDay);
// get the usage values
arr = xmlDoc.getElementsByTagName("TotalUsage");
var totalusage = arr[0].childNodes[0].data;
console.log("totalusage: " + totalusage);
arr = xmlDoc.getElementsByTagName("MinUsageRemaining");
var minUsageRemaining = arr[0].childNodes[0].data;
console.log("MinUsageRemaining: " + minUsageRemaining);
arr = xmlDoc.getElementsByTagName("MaxUsageRemaining");
var maxUsageRemaining = arr[0].childNodes[0].data;
console.log("MaxUsageRemaining: " + maxUsageRemaining);
arr = xmlDoc.getElementsByTagName("Unit");
var unit = arr[0].childNodes[0].data;
console.log("unit: " + unit);
arr = xmlDoc.getElementsByTagName("LastUpdate");
var lastUpdate = arr[0].childNodes[0].data;
var tmpLastUpdate = lastUpdate.split('T');
var time = tmpLastUpdate[1].split('.');
lastUpdate = tmpLastUpdate[0] + ' ' + time[0];
console.log("lastUpdate: " + lastUpdate);
//get the status values
arr = xmlDoc.getElementsByTagName("Status");
var status = arr[0].childNodes[0].data;
console.log("status: " + status);
arr = xmlDoc.getElementsByTagName("NL");
var statusDescription = arr[0].childNodes[0].data;
console.log("statusDescription: " + statusDescription);
// save values to localstorage
localStorage['timestamp'] = timestamp;
localStorage['from'] = from;
localStorage['till'] = till;
localStorage['currentday'] = currentDay;
localStorage['totalusage'] = totalusage;
localStorage['minusageremaining'] = minUsageRemaining;
localStorage['maxusageremaining'] = maxUsageRemaining;
localStorage['unit'] = unit;
localStorage['lastupdate'] = lastUpdate;
localStorage['status'] = status;
localStorage['statusdescription'] = statusDescription;
localStorage['type'] = 1;
}
function formatdate(input) {
arr = input.split('-');
if (arr.length != 3) {
return input;
}
return arr[2] + '/' + arr[1] + '/' + arr[0];
}