-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·116 lines (86 loc) · 3.08 KB
/
app.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
#!/usr/bin/env node
var nodemailer = require('nodemailer');
var Client = require('node-rest-client').Client;
var client = new Client();
var mail_message = require('./lib/mail-message');
var cfgfile = process.env.CONFIG || './config';
console.log('Loading config from ' + cfgfile);
var config = require(cfgfile);
var cron = require('node-cron');
var muellstorage = {};
var muelltypes = config.muellshack.types;
for (var i = 0; i < muelltypes.length; i++) {
muellstorage[muelltypes[i]] = Object.assign({},{date:null, main_action_done: null, mail_sended: null});
}
console.log(muellstorage);
function getMUELL () {
var muelltypes = config.muellshack.types;
for (var i = 0; i < muelltypes.length; i++) {
console.log('GET Request: ' + muelltypes[i]);
var req = client.get(config.muellshack.url + muelltypes[i], function (data, response) {
Object.assign(muellstorage[data.muelltype], data);
checkSendEmail(data.muelltype);
});
}
}
function checkSendEmail(muelltype) {
// if(muelltype !== 'gelber_sack') return;
const limit = 1;
var dat_limit = new Date(muellstorage[muelltype].date);
dat_limit = new Date(dat_limit.setDate(dat_limit.getDate() - limit));
var min_limit = new Date(dat_limit);
min_limit.setHours(19,00);
// min_limit.setHours(18,44);
var max_limit = new Date(dat_limit);
max_limit.setHours(20,00);
// max_limit.setHours(18,46);
console.log('Handling: ' + muelltype);
console.log(dat_limit + ' (collection)');
console.log(min_limit + ' (earliest)');
console.log(max_limit +' (latest)');
console.log((new Date()) + ' (current)');
if (min_limit < new Date() && max_limit > new Date())
{
console.log('Mail senden')
if(muellstorage[muelltype].mail_sended === true) {
console.log('Mail ist bereits gesendet');
return;
}
if(muellstorage[muelltype].main_action_done === true) {
console.log('main_action_done is true, no action needed');
return;
}
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(config.email);
var mailOptions = mail_message.get_mail_options(muelltype, muellstorage[muelltype].date,config.muellshack.send_to);
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
muellstorage[muelltype].mail_sended = true;
var args = {
data: {mail_sended: true},
headers: { "Content-Type": "application/json" }
};
client.post(config.muellshack.url + muelltype, args, function (data, response) {
// parsed response body as js object
console.log('POST {mail_sended: true}');
});
});
} else {
console.log('Mail nicht senden')
}
}
getMUELL();
cron.schedule('0 */5 * * * *', function(){
getMUELL();
});
cron.schedule('0 31 17 * * *', function(){
console.log('daily trigger started', new Date());
getMUELL();
});
cron.schedule('0 */5 * * * *', function(){
console.log(muellstorage);
});