This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailing.js
185 lines (153 loc) · 6.35 KB
/
mailing.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
var fs = require('fs');
var d3 = require('d3-queue');
var moment = require('moment');
var mark = require('markup-js');
var aws = require('aws-sdk');
var db = require('./lib/dbconnect.js');
var satapi = require('./lib/sat-api.js');
var pepsapi = require('./lib/peps-api.js');
var ses = new aws.SES({region: 'us-west-2'});
function sortScenes(a, b) {
return Date.parse(b.date) - Date.parse(a.date);
}
var mailTemplate = fs.readFileSync('./templates/images_update.html', 'utf8');
function construcMailInfo(prop, mail, images) {
var imgTemplate = '<td valign="top" width="280" class="leftColumnContent">' +
'<table border="0" cellpadding="20" cellspacing="0" width="100%">' +
'<tr><td valign="top">' +
'<img src="{{browseURL}}" style="max-width:260px;"/>' +
'<div>' +
'<strong>Satellite:</strong> {{satellite}}<br />' +
'<strong>Date:</strong> {{date}}<br />' +
'<strong>Cloud %:</strong> {{cloud}}<br />' +
'<a href="{{downloadURL}}" target="_blank">link</a>' +
'</div></td>' +
'</tr></table></td>';
var info = {
nbImages : images.length,
name: prop.name,
uuid: prop.uuid,
mail: mail.mail,
imageslist : '<tr>'
};
for(var jj = 0; jj < images.length; jj++) {
var imginfo = {
browseURL: images[jj].browseURL,
satellite: images[jj].sat,
date: images[jj].date,
cloud: (images[jj].sat !== 'sentinel1')? images[jj].cloud : '- ',
downloadURL: images[jj].downURL
}
var imgBlock = mark.up(imgTemplate, imginfo);
info.imageslist += imgBlock;
if (jj % 2 === 1) info.imageslist += '</tr><tr>';
}
info.imageslist += '</tr>';
return info;
}
function sendMail (content, callback) {
ses.sendEmail(content, function (err, data) {
if (err) {
console.log('Internal Error: The email could not be sent.');
} else {
console.log('The email was successfully sent to ' + content.Destination.ToAddresses[0]);
return callback(null, 'The email was successfully sent to ' + content.Destination.ToAddresses[0]);
}
});
}
function getImagery(doc, cb){
var q = d3.queue();
q.defer(pepsapi.getS1Images, doc.feature, {'dateStart': moment(doc.feature.properties.images.sentinel1).utc().add(1, 'days').format('YYYY-MM-DD')});
q.defer(satapi.getL8Images, doc.feature, {'dateStart': moment(doc.feature.properties.images.landsat8).utc().add(1, 'days').format('YYYY-MM-DD')});
q.defer(satapi.getS2Images, doc.feature, {'dateStart': moment(doc.feature.properties.images.sentinel2).utc().add(1, 'days').format('YYYY-MM-DD')});
q.awaitAll(function(error, results) {
var images = [];
if (results.length !== 0) {
for (var j = 0; j <results.length; j++) {
if (results[j]) {
images = images.concat(results[j]);
}
}
}
images.sort(sortScenes);
if (images.length === 0) {
console.log('No new Image for event: ' + doc.feature.properties.name);
return cb(null, 1);
} else {
var s1img = images.filter(function(e){
return (e.sat === 'sentinel1');
});
var s2img = images.filter(function(e){
return (e.sat === 'sentinel2');
});
var l8img = images.filter(function(e){
return (e.sat === 'landsat8');
});
// update database
if (s1img.length !== 0) {
doc.feature.properties.images.sentinel1 = s1img[0].date;
}
if (s2img.length !== 0) {
doc.feature.properties.images.sentinel2 = s2img[0].date;
}
if (l8img.length !== 0) {
doc.feature.properties.images.landsat8 = l8img[0].date;
}
console.log(images.length + ' new Image for event: ' + doc.feature.properties.name);
console.log("S1", s1img.length, "S2", s2img.length, "L8", l8img.length)
var q2 = d3.queue();
for(var ii = 0; ii < doc.mail.length; ii++) {
if (doc.mail[ii].mail === '') {continue}
var img2send = images.filter(function(e){
return (doc.mail[ii].satellite.indexOf(e.sat) > -1);
});
if (img2send.length === 0) {continue}
var info = construcMailInfo(doc.feature.properties, doc.mail[ii], img2send),
message = mark.up(mailTemplate, info),
params = {
Destination: {
ToAddresses: [doc.mail[ii].mail]
},
Message: {
Body: {
Html: {Data: message, Charset: 'UTF-8'}
},
Subject: {
Data: 'New Images Available for Event: ' + info.name,
Charset: 'UTF-8'
}
},
Source: "[email protected]"
};
// Send the email
q2.defer(sendMail, params);
}
q2.awaitAll(function(error, results) {
db.imgDateUpdate(doc.uuid, doc.feature.properties.images, function(err, res) {
return cb(null, 1);
});
});
}
});
}
module.exports.checkImagery = function (event, context) {
db.getDB(function (err, docs) {
if (err) {
context.fail("Cannot connect to db");
}
var count = 0;
for(var i = 0; i < docs.length; i++) {
var doc = docs[i];
getImagery(doc, function(err, res){
// console.log(res)
count += 1
if (count === docs.length){
context.succeed("Done");
}
})
}
if (docs.length === 0) {
context.succeed("Done");
}
});
}