-
Notifications
You must be signed in to change notification settings - Fork 10
/
node_helper.js
95 lines (69 loc) · 2.97 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
var NodeHelper = require("node_helper");
var fs = require('fs');
var parse = require("csv-parse");
var moment = require("moment");
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node_helper for module: " + this.name);
this.schedule = null;
//new schedule file can be downloaded at
//https://www.toronto.ca/city-government/data-research-maps/open-data/open-data-catalogue/garbage-and-recycling/#8e932504-cabb-71b1-b23a-6cf504f7c474
this.scheduleCSVFile = this.path + "/schedule.csv";
this.scheduleCustomCSVFile = this.path + "/schedule_custom.csv";
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if (this.schedule == null) {
//not yet setup. Load and parse the data file; set up variables.
var scheduleFile = this.scheduleCSVFile;
if (payload.collectionCalendar == "Custom") {
scheduleFile = this.scheduleCustomCSVFile;
}
fs.readFile(scheduleFile, "utf8", function(err, rawData) {
if (err) throw err;
parse(rawData, {delimiter: ",", columns: true, ltrim: false}, function(err, parsedData) {
if (err) throw err;
self.schedule = parsedData;
self.postProcessSchedule();
self.getNextPickups(payload);
});
});
} else {
this.getNextPickups(payload);
}
},
postProcessSchedule: function() {
this.schedule.forEach( function(obj) {
//convert date strings to moment.js Date objects
//UPDATE Nov 2021. Date format has changed in Toronto.ca data files.
//So we do a check for the format first and convert to a Moment object
//accordingly, while maintaining compatibility for people using the
//custom calendar
if (obj.WeekStarting.indexOf("/") != -1 ) {
obj.pickupDate = moment(obj.WeekStarting, "MM/DD/YY");
} else if (obj.WeekStarting.indexOf("-") != -1 ) {
obj.pickupDate = moment(obj.WeekStarting, "YYYY-MM-DD");
}
// to do:
// check if pickup date lands on a holiday.
// If so, move to next day
//reassign strings to booleans for particular waste type
obj.GreenBin = (obj.GreenBin == "0" ? false : true);
obj.Garbage = (obj.Garbage == "0" ? false : true);
obj.Recycling = (obj.Recycling == "0" ? false : true);
obj.YardWaste = (obj.YardWaste == "0" ? false : true);
obj.ChristmasTree = (obj.ChristmasTree == "0" ? false : true);
});
},
getNextPickups: function(payload) {
var start = moment().startOf("day"); //today, 12:00 AM
var end = moment().startOf("day").add(payload.weeksToDisplay * 7, "days");
//find info for next pickup dates
var nextPickups = this.schedule.filter(function (obj) {
return obj.Calendar == payload.collectionCalendar &&
obj.pickupDate.isSameOrAfter(start) &&
obj.pickupDate.isBefore(end);
});
this.sendSocketNotification('MMM-MYWASTEPICKUP-RESPONSE' + payload.instanceId, nextPickups);
}
});