-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwastecollectionProvider_33.js
More file actions
92 lines (76 loc) · 2.83 KB
/
wastecollectionProvider_33.js
File metadata and controls
92 lines (76 loc) · 2.83 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
//<provider>33</provider><version>2.0.2</version><parms>"Zipcode","HouseNr"</parms>
//provider homeassistant API (thanks to heyajohnny) testdata: https://trashapi.azurewebsites.net/trash?ZipCode=2992DL&HouseNumber=80&ShowWholeYear=true
function readCalendar(wasteZipcode, wasteHouseNr, extraDates, enableCreateICS, wasteICSId, wasteStreet, wasteStreetName, wasteCity, wasteFullICSUrl) {
var i = 0;
var j = 0;
var wasteDatesString = "";
var wasteType = "";
var wasteDatesArray = [];
var inputArray = {};
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
inputArray = JSON.parse(xmlhttp.responseText);
for (i=0;i < inputArray.length; i++) {
wasteType = wasteTypeCode(inputArray[i]["name"]);
wasteDatesArray.push(inputArray[i]["date"].substring(0, 10) + "," + wasteType);
}
var tmp = sortArray2(wasteDatesArray, extraDates);
for (i = 0; i < tmp.length; i++) {
wasteDatesString = wasteDatesString + tmp[i] + "\n";
}
writeWasteDates(wasteDatesString, enableCreateICS);
}
}
}
xmlhttp.open("GET", "https://trashapi.azurewebsites.net/trash?ZipCode=" + wasteZipcode + "&HouseNumber=" + wasteHouseNr + "&ShowWholeYear=true", true);
xmlhttp.send();
}
function wasteTypeCode(shortName) {
switch (shortName) {
case "Restafval": return 0;
case "Gft": return 3;
case "Takken": return 4;
case "Papier": return 2;
case "Pbd": return 1;
case "Kca": return 7;
case "Textiel": return 5;
case "Grofvuil": return 8;
default: break;
}
return "?";
}
function sortArray2(inputarray, extraDates) {
var newArray = inputarray.concat(extraDates);
newArray.sort();
return newArray;
}
function writeWasteDates(wasteDatesString, enableCreateICS) {
var doc2 = new XMLHttpRequest();
doc2.open("PUT", "file:///var/volatile/tmp/wasteDates.txt");
doc2.onreadystatechange=function() {
if (doc2.readyState === 4){
if (doc2.status === 0) {
updateWasteIcon("no");
}
}
}
doc2.send(wasteDatesString);
// create ICS file for use in the calendar app when requested
if (enableCreateICS) {
var outputICS = "";
var tmpICS = wasteDatesString.split("\n");
for (var i = 0; i < tmpICS.length; i++) {
if (tmpICS[i].length > 10) {
outputICS = outputICS + "BEGIN:VEVENT\r\n";
outputICS = outputICS + "DTSTART;VALUE=DATE:" + tmpICS[i].substring(0,4) + tmpICS[i].substring(5,7) + tmpICS[i].substring(8,10) + "\r\n";
outputICS = outputICS + "SUMMARY:" + wasteTypeFriendlyName(tmpICS[i].substring(11,12)) + "\r\n";
outputICS = outputICS + "BEGIN:VEVENT\r\n";
}
}
var doc3 = new XMLHttpRequest();
doc3.open("PUT", "file:///var/volatile/tmp/wasteDates.ics");
doc3.send(outputICS);
}
}