-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
191 lines (140 loc) · 6.61 KB
/
main.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
var date = new Date();
var path = require('path');
var fs = require('fs');
var client = require('ftp');
var c = new client();
console.log('Starte Monitor ' + date)
var email = require("emailjs");
var server = email.server.connect({
host: "192.168.1.1",
ssl: false
});
// FTP
var connectionProperties = {
host: "192.168.1.2",
user: "usr",
password: "pwd"
};
var searchPath = 'path/destDir';
var fileIsOlderThanSeconds = 120;
var timeZoneCorrection = +2; //in hours
var mailEnvelope = {
text: "AHHHH",
from: "support <[email protected]>",
to: "ITGUY <[email protected]>",
subject: "ERROR .. ROAHR!"
};
// Calculations
var timeZoneCorrectionSeconds = timeZoneCorrection * 3600;
var nowYear = parseFloat( date.getFullYear() );
var nowYearSeconds = nowYear * 31557600;
var nowMonth = parseFloat( date.getMonth() ) + 1;
var nowMonthSeconds = ( nowMonth ) * 2592000;
var nowDay = parseFloat( date.getDate() );
var nowDaySeconds = nowDay * 86400;
var nowHour = parseFloat( date.getHours() );
var nowHourSeconds = nowHour * 3600;
var nowMinute = parseFloat( date.getMinutes() );
var nowMinuteSeconds = nowMinute * 60;
var nowSeconds = parseFloat( date.getSeconds() );
var nowFullSeconds = nowYearSeconds + nowMonthSeconds + nowDaySeconds + nowHourSeconds + nowMinuteSeconds + nowSeconds + timeZoneCorrectionSeconds;
console.log(nowYear, nowMonth, nowDay, nowHour, nowMinute, nowSeconds, timeZoneCorrection, nowYearSeconds, nowMonthSeconds, nowDaySeconds, nowHourSeconds, nowMinuteSeconds, nowSeconds, timeZoneCorrectionSeconds, nowFullSeconds);
var oldFileFound = false;
c.on('ready', function () {
c.list(searchPath, function (err, list) {
if (err) throw err;
list.forEach(function (element, index, array) {
if (element.type !== 'd') { // ignoring the D's
if (path.extname(element.name) == '.RCV') { // watch only for .RCV files
console.log( element.name, element.date );
var fullDate = ''; //string
fullDate = element.date.toString();
var arrDate = []; //array
arrDate = fullDate.split(' ');
if ( arrDate.length > 0 ) {
var year = 0;
year = parseFloat( arrDate[3] );
month = 0;
switch(arrDate[1]) {
case 'Jan':
month = 1;
break;
case 'Feb':
month = 2;
break;
case 'Mar':
month = 3;
break;
case 'Apr':
month = 4;
break;
case 'May':
month = 5;
break;
case 'Jun':
month = 6;
break;
case 'Jul':
month = 7;
break;
case 'Aug':
month = 8;
break;
case 'Sep':
month = 9;
break;
case 'Oct':
month = 10;
break;
case 'Nov':
month = 11;
break;
case 'Dec':
month = 12;
break;
default:
month = 0;
}
var day = 0;
day = parseFloat( arrDate[2] );
var time = []; //array
time = arrDate[4].split(':');
var hour = 0;
hour = parseFloat( time[0] );
var minute = 0;
minute = parseFloat( time[1] );
var second = 0;
second = parseFloat( time[2] );
var yearSeconds = year * 31557600;
var monthSeconds = month * 2592000;
var daySeconds = day * 86400;
var hourSeconds = hour * 3600;
var minuteSeconds = minute * 60;
var elementSeconds = yearSeconds + monthSeconds + daySeconds + hourSeconds + minuteSeconds + second;
var difference = 0;
difference = nowFullSeconds - elementSeconds;
console.log( year, month, day, hour, minute, second, yearSeconds, monthSeconds, daySeconds, hourSeconds, minuteSeconds, second, elementSeconds, difference );
if( difference > fileIsOlderThanSeconds ) {
if( difference < 86400 ) {
// Only Files that are not older than one day.
// There is a bug sometimes, when a file get's touched by the os.. , the date changes to year 1970 something for a blink..
// we wanna prevent that.
oldFileFound = true;
};
} else {
console.log('File ignored, its not old enough.');
};
}
}
}
}); //forEach
if( oldFileFound ) {
// send the message and get a callback with an error or details of the message that was sent
server.send(mailEnvelope, function(err, message) { console.log(err || message); });
} else {
console.log('no hanging file found.');
};
c.end();
});
});
c.connect(connectionProperties);