-
Notifications
You must be signed in to change notification settings - Fork 8
/
node_helper.js
241 lines (205 loc) · 6.58 KB
/
node_helper.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
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
const NodeHelper = require("node_helper");
const WebUntis = require("webuntis");
module.exports = NodeHelper.create({
start: function() {
},
socketNotificationReceived: function(notification, payload) {
if (notification === "FETCH_DATA") {
//Copy and save config
this.config = payload;
// iterate through students, fetch and send lessons
for (let i in this.config.students) {
var student = this.config.students[i];
if (student.username) {
this.fetchLessonsLogin(student, this.config.days);
}
else if (student.class) {
this.fetchLessonsAnonymous(student, this.config.days);
}
else {
console.log("Error: Student '" + student.title + "' has an configuration error!");
}
}
}
},
fetchLessonsLogin: function(studentData, days) {
const untis = new WebUntis(
studentData.school,
studentData.username,
studentData.password,
studentData.server
);
if (days<1 || days>10 || isNaN(days)) {days = 1;}
// create lessons array to be sent to module
var lessons = [];
// array to get lesson number by start time
var startTimes = [];
untis
.login()
.then(response => {
var rangeStart = new Date();
var rangeEnd = new Date();
rangeEnd.setDate(rangeStart.getDate()+days);
untis.getTimegrid()
.then(grid => {
// use grid of first day and assume all days are the same
grid[0].timeUnits.forEach(element => {
startTimes[element.startTime] = element.name;
})
})
.catch(error => {
console.log("Error in getTimegrid: " + error);
})
if (studentData.useClassTimetable) {
return untis.getOwnClassTimetableForRange(rangeStart, rangeEnd);
} else {
return untis.getOwnTimetableForRange(rangeStart, rangeEnd);
}
})
.then(timetable => {
lessons = this.timetableToLessons(startTimes, timetable);
this.sendSocketNotification("GOT_DATA", {title: studentData.title, lessons: lessons});
})
.catch(error => {
console.log("ERROR for " + studentData.title + ": " + error.toString());
/*
let today = new Date();
let errorObject = [ {
year: today.getFullYear(),
month: today.getMonth()+1,
day: today.getDate(),
hour: today.getHours(),
minutes: today.getMinutes(),
subject: "ERROR",
teacher: error.toString(),
code: "error"
} ];
this.sendSocketNotification("GOT_DATA", {title: studentData.title, lessons: errorObject});
*/
});
untis.logout();
},
fetchLessonsAnonymous: function(studentData, days) {
const untis = new WebUntis.WebUntisAnonymousAuth(
studentData.school,
studentData.server
);
if (days<1 || days>10 || isNaN(days)) {days = 1;}
// create lessons array to be sent to module
var lessons = [];
// array to get lesson number by start time
var startTimes = [];
var classid = -1;
untis
.login()
.then(() => {
return untis.getClasses();
})
.then((classes) => {
// Get timetable for the first class
for (let i in classes) {
if (classes[i].name === studentData.class){
classid = classes[i].id;
break;
}
}
if (classid === -1) {
console.error("ERROR for" + studentData.title + ": Class not found!");
}
})
.then(response => {
var rangeStart = new Date();
var rangeEnd = new Date();
rangeEnd.setDate(rangeStart.getDate()+days);
untis.getTimegrid()
.then(grid => {
// use grid of first day and assume all days are the same
grid[0].timeUnits.forEach(element => {
startTimes[element.startTime] = element.name;
})
})
.catch(error => {
console.log("Error in getTimegrid: " + error);
})
//return untis.getTimetableForToday(classid, WebUntis.TYPES.CLASS);
return untis.getTimetableForRange(rangeStart, rangeEnd, classid, WebUntis.TYPES.CLASS);
})
.then(timetable => {
lessons = this.timetableToLessons(startTimes, timetable);
this.sendSocketNotification("GOT_DATA", {title: studentData.title, lessons: lessons});
})
.catch(error => {
console.log("ERROR for " + studentData.title + ": " + error.toString());
/*
let today = new Date();
let errorObject = [ {
year: today.getFullYear(),
month: today.getMonth()+1,
day: today.getDate(),
hour: today.getHours(),
minutes: today.getMinutes(),
subject: "ERROR",
teacher: error.toString(),
code: "error"
} ];
this.sendSocketNotification("GOT_DATA", {title: studentData.title, lessons: errorObject});
*/
});
untis.logout();
},
timetableToLessons: function(startTimes, timetable) {
var lessons = [];
timetable.forEach(element => {
let lesson = {};
//Parse date and time information
lesson.year = element.date.toString().substring(0,4);
lesson.month = element.date.toString().substring(4,6);
lesson.day = element.date.toString().substring(6);
lesson.hour = element.startTime.toString();
lesson.hour = (lesson.hour.length == 3 ? ("0" + lesson.hour.substring(0,1)) : lesson.hour.substring(0,2));
lesson.minutes = element.startTime.toString();
lesson.minutes = lesson.minutes.substring(lesson.minutes.length-2);
//Parse lesson number by start time
lesson.lessonNumber = startTimes[element.startTime];
//Parse data about teacher
if (element.te) {
lesson.teacher = element.te[0].longname;
lesson.teacherInitial = element.te[0].name;
}
else {
lesson.teacher = "";
lesson.teacherInitial = "";
}
//Parse data about subject
if (element.su[0]) {
lesson.subject = element.su[0].longname;
lesson.subjectShort = element.su[0].name;
}
else {
lesson.subject = "";
lesson.subjectShort = "";
}
//Parse other information
lesson.code = element.code ? element.code : "";
lesson.text = element.lstext ? element.lstext : "";
lesson.substText = element.substText ? element.substText : "";
//Set code to "info" if there is an "substText" from WebUntis to display it if configuration "showRegularLessons" is set to false
if (lesson.substText != "" && lesson.code == "") {
lesson.code = "info";
}
//Create sort string
lesson.sortString = lesson.year + lesson.month + lesson.day + lesson.hour + lesson.minutes;
switch (lesson.code) {
case "cancelled": lesson.sortString += "1"; break;
case "irregular": lesson.sortString += "2"; break;
case "info": lesson.sortString += "3"; break;
default: lesson.sortString += "9";
}
lessons.push(lesson);
});
if (this.config.debug) {
console.log("MMM-Webuntis: Timetable and Lessons: ", JSON.stringify({timetable: timetable, lessons: lessons}));
}
return lessons;
},
})