|
@@ -1,7 +1,16 @@
|
|
|
-package cz.senslog.watchdog.messagebroker;
|
|
|
|
|
|
|
+package cz.senslog.watchdog.messagebroker.email;
|
|
|
|
|
|
|
|
import cz.senslog.watchdog.config.EmailMessageBrokerConfig;
|
|
import cz.senslog.watchdog.config.EmailMessageBrokerConfig;
|
|
|
import cz.senslog.watchdog.config.MessageBrokerType;
|
|
import cz.senslog.watchdog.config.MessageBrokerType;
|
|
|
|
|
+import cz.senslog.watchdog.domain.ObservationInfo;
|
|
|
|
|
+import cz.senslog.watchdog.domain.SimpleReport;
|
|
|
|
|
+import cz.senslog.watchdog.domain.StatusReport;
|
|
|
|
|
+import cz.senslog.watchdog.messagebroker.MessageBroker;
|
|
|
|
|
+import cz.senslog.watchdog.messagebroker.MessageBrokerHandler;
|
|
|
|
|
+import cz.senslog.watchdog.messagebroker.MessageStatus;
|
|
|
|
|
+import cz.senslog.watchdog.domain.Report;
|
|
|
|
|
+import cz.senslog.watchdog.messagebroker.writer.HtmlTableWriter;
|
|
|
|
|
+import cz.senslog.watchdog.messagebroker.writer.TableWriter;
|
|
|
import cz.senslog.watchdog.provider.Record;
|
|
import cz.senslog.watchdog.provider.Record;
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
import org.apache.logging.log4j.Logger;
|
|
import org.apache.logging.log4j.Logger;
|
|
@@ -9,8 +18,11 @@ import org.apache.logging.log4j.Logger;
|
|
|
import javax.mail.*;
|
|
import javax.mail.*;
|
|
|
import javax.mail.internet.*;
|
|
import javax.mail.internet.*;
|
|
|
import java.time.Instant;
|
|
import java.time.Instant;
|
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
+import static cz.senslog.watchdog.util.TimeConverter.dayToSec;
|
|
|
|
|
+import static java.time.format.DateTimeFormatter.ofPattern;
|
|
|
import static javax.mail.Message.RecipientType.TO;
|
|
import static javax.mail.Message.RecipientType.TO;
|
|
|
|
|
|
|
|
public class EmailMessageBroker implements MessageBroker {
|
|
public class EmailMessageBroker implements MessageBroker {
|
|
@@ -22,13 +34,10 @@ public class EmailMessageBroker implements MessageBroker {
|
|
|
private final EmailMessageBrokerConfig config;
|
|
private final EmailMessageBrokerConfig config;
|
|
|
private final Session session;
|
|
private final Session session;
|
|
|
|
|
|
|
|
- private final Set<Record> sendRecords;
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
public EmailMessageBroker(EmailMessageBrokerConfig config) {
|
|
public EmailMessageBroker(EmailMessageBrokerConfig config) {
|
|
|
this.config = config;
|
|
this.config = config;
|
|
|
this.session = openSession(config);
|
|
this.session = openSession(config);
|
|
|
- this.sendRecords = new HashSet<>();
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private Session openSession(EmailMessageBrokerConfig config) {
|
|
private Session openSession(EmailMessageBrokerConfig config) {
|
|
@@ -47,7 +56,7 @@ public class EmailMessageBroker implements MessageBroker {
|
|
|
return Session.getInstance(props, auth);
|
|
return Session.getInstance(props, auth);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private Message createMessage(Instant created, List<Record> records) throws MessagingException {
|
|
|
|
|
|
|
+ private Message createMessage(Report report) throws MessagingException {
|
|
|
Message message = new MimeMessage(session);
|
|
Message message = new MimeMessage(session);
|
|
|
message.setFrom(new InternetAddress(config.getSender()));
|
|
message.setFrom(new InternetAddress(config.getSender()));
|
|
|
message.setRecipients(TO, InternetAddress.parse(config.getRecipient()));
|
|
message.setRecipients(TO, InternetAddress.parse(config.getRecipient()));
|
|
@@ -55,18 +64,26 @@ public class EmailMessageBroker implements MessageBroker {
|
|
|
|
|
|
|
|
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
|
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
|
|
StringBuilder content = new StringBuilder();
|
|
StringBuilder content = new StringBuilder();
|
|
|
- content.append(String.format("Data reported at the time <b>%s</b>", created))
|
|
|
|
|
- .append(BREAK_LINE).append(BREAK_LINE);
|
|
|
|
|
-
|
|
|
|
|
- content.append("<b>") // TODO refactor
|
|
|
|
|
- .append("unitId").append(";")
|
|
|
|
|
- .append("sensorId").append(";")
|
|
|
|
|
- .append("lastObservation")
|
|
|
|
|
- .append("</b>").append(BREAK_LINE);
|
|
|
|
|
|
|
|
|
|
- for (Record record : records) {
|
|
|
|
|
- content.append(record.getMessage()).append(BREAK_LINE);
|
|
|
|
|
|
|
+ TableWriter tableWriter = HtmlTableWriter.createWithHeader("width: 100%;", "background-color: #dddddd")
|
|
|
|
|
+ .cell("unitId").cell("sensorId").cell("timestamp").cell("reported").cell("status").end();
|
|
|
|
|
+
|
|
|
|
|
+ String reportedTime = report.getCreated().format(ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
+ final String rowStyle = "border: 1px solid #dddddd; text-align: left; padding: 8px;";
|
|
|
|
|
+ for (SimpleReport simpleReport : report.getReports()) {
|
|
|
|
|
+ if (simpleReport.getRecord() instanceof ObservationInfo) {
|
|
|
|
|
+ ObservationInfo observation = (ObservationInfo)simpleReport.getRecord();
|
|
|
|
|
+ tableWriter.row(rowStyle + (simpleReport.getStatus().equals(StatusReport.OK) ? "background-color: #CCFFCC" : "background-color: #FFCCCC"))
|
|
|
|
|
+ .cell(String.valueOf(observation.getSource().getUnitId()), rowStyle)
|
|
|
|
|
+ .cell(String.valueOf(observation.getSource().getSensorId()), rowStyle)
|
|
|
|
|
+ .cell(observation.getTimestamp().toString(), rowStyle)
|
|
|
|
|
+ .cell(reportedTime, rowStyle)
|
|
|
|
|
+ .cell(simpleReport.getStatus().name(), rowStyle)
|
|
|
|
|
+ .end();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ content.append(tableWriter.table()).append(BREAK_LINE);
|
|
|
mimeBodyPart.setContent(content.toString(), "text/html");
|
|
mimeBodyPart.setContent(content.toString(), "text/html");
|
|
|
|
|
|
|
|
Multipart multipart = new MimeMultipart();
|
|
Multipart multipart = new MimeMultipart();
|
|
@@ -76,16 +93,6 @@ public class EmailMessageBroker implements MessageBroker {
|
|
|
return message;
|
|
return message;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private List<Record> prepareRecords(Report report) {
|
|
|
|
|
- List<Record> readyToSend = new ArrayList<>();
|
|
|
|
|
- for (Record record : report.getRecords()) {
|
|
|
|
|
- if (!sendRecords.contains(record)) {
|
|
|
|
|
- readyToSend.add(record);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return readyToSend;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void send(Report report, MessageBrokerHandler status) {
|
|
public void send(Report report, MessageBrokerHandler status) {
|
|
|
if (report == null) {
|
|
if (report == null) {
|
|
@@ -93,25 +100,18 @@ public class EmailMessageBroker implements MessageBroker {
|
|
|
status.handle(new MessageStatus(null, "No report to send.")); return;
|
|
status.handle(new MessageStatus(null, "No report to send.")); return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- List<Record> recordsToSend = prepareRecords(report);
|
|
|
|
|
- if (recordsToSend.isEmpty()) {
|
|
|
|
|
- logger.info("The same report was already send.");
|
|
|
|
|
- status.handle(new MessageStatus(report)); return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
try {
|
|
try {
|
|
|
- Message message = createMessage(report.getCreated(), recordsToSend);
|
|
|
|
|
|
|
+ Message message = createMessage(report);
|
|
|
logger.info("Sending a message via email.");
|
|
logger.info("Sending a message via email.");
|
|
|
Transport.send(message);
|
|
Transport.send(message);
|
|
|
logger.info("The message was send successfully.");
|
|
logger.info("The message was send successfully.");
|
|
|
- sendRecords.addAll(recordsToSend);
|
|
|
|
|
status.handle(new MessageStatus(report));
|
|
status.handle(new MessageStatus(report));
|
|
|
} catch (MessagingException e) {
|
|
} catch (MessagingException e) {
|
|
|
logger.catching(e);
|
|
logger.catching(e);
|
|
|
status.handle(new MessageStatus(report, e.getMessage()));
|
|
status.handle(new MessageStatus(report, e.getMessage()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- vanish();
|
|
|
|
|
|
|
+ // vanish();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -120,9 +120,9 @@ public class EmailMessageBroker implements MessageBroker {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Record[] vanish() {
|
|
public Record[] vanish() {
|
|
|
- Instant deadline = Instant.now().minusSeconds(60 * 60 * 24 * config.getVanishPeriodDays());
|
|
|
|
|
|
|
+ Instant deadline = Instant.now().minusSeconds(dayToSec(config.getVanishPeriodDays()));
|
|
|
List<Record> deletedRecords = new ArrayList<>();
|
|
List<Record> deletedRecords = new ArrayList<>();
|
|
|
- Iterator<Record> iterator = sendRecords.iterator();
|
|
|
|
|
|
|
+ Iterator<Record> iterator = Collections.emptyListIterator();
|
|
|
while(iterator.hasNext()) {
|
|
while(iterator.hasNext()) {
|
|
|
Record record = iterator.next();
|
|
Record record = iterator.next();
|
|
|
if (record.getTimestamp().isBefore(deadline)) {
|
|
if (record.getTimestamp().isBefore(deadline)) {
|