mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Migrating from from wiringpi to pigpio. This increases the sample rate by a factor of 3 and will allow creation of a board that can monitor over 30 breakers with a single raspberry pi.
This is based on pi4j 2.0 which is in a beta status. I have fixed a few bugs in a local version of pi4j 2.0 to get it to work but I haven't submitted those changes to pi4j yet. This change requires a migration to Java 11 and will *NOT* be backwards compatible. Upgrading to this hub software will require that java 11 and pigpio be installed on the hub. This can be done from an ssh session with the following commands: apt-get update apt-get install openjdk-11-jre-headless apt-get install pigpio Alternatively, you can download a new sd image, reflash your sd card, and re-adopt your hub.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package com.lanternsoftware.currentmonitor.email;
|
||||
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EmailCredentials;
|
||||
|
||||
public interface IEmailProvider {
|
||||
int sendTextEmail(EmailCredentials _credentials, String _to, String _subject, String _message);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.lanternsoftware.currentmonitor.email;
|
||||
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EmailCredentials;
|
||||
import com.mailjet.client.ClientOptions;
|
||||
import com.mailjet.client.MailjetClient;
|
||||
import com.mailjet.client.MailjetRequest;
|
||||
import com.mailjet.client.MailjetResponse;
|
||||
import com.mailjet.client.resource.Emailv31;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MailJetProvider implements IEmailProvider {
|
||||
protected static final Logger LOG = LoggerFactory.getLogger(MailJetProvider.class);
|
||||
|
||||
@Override
|
||||
public int sendTextEmail(EmailCredentials _credentials, String _to, String _subject, String _message) {
|
||||
MailjetClient client;
|
||||
MailjetRequest request;
|
||||
MailjetResponse response;
|
||||
client = new MailjetClient(_credentials.getApiKey(), _credentials.getApiSecret(), new ClientOptions("v3.1"));
|
||||
request = new MailjetRequest(Emailv31.resource).property(Emailv31.MESSAGES, new JSONArray().put(new JSONObject()
|
||||
.put(Emailv31.Message.FROM, new JSONObject().put("Email", _credentials.getEmailFrom()).put("Name", "Lantern Power Monitor"))
|
||||
.put(Emailv31.Message.TO, new JSONArray().put(new JSONObject().put("Email", _to)))
|
||||
.put(Emailv31.Message.SUBJECT, _subject)
|
||||
.put(Emailv31.Message.TEXTPART, _message)));
|
||||
try {
|
||||
response = client.post(request);
|
||||
return response.getStatus();
|
||||
} catch (Exception _e) {
|
||||
LOG.error("Failed to send email", _e);
|
||||
return 500;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.lanternsoftware.currentmonitor.email;
|
||||
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EmailCredentials;
|
||||
|
||||
public class SendGridProvider implements IEmailProvider {
|
||||
@Override
|
||||
public int sendTextEmail(EmailCredentials _credentials, String _to, String _subject, String _message) {
|
||||
/* Email to = new Email(email);
|
||||
Content content = new Content("text/plain", "Reset your password using this link:\nhttps://lanternpowermonitor.com/currentmonitor/resetPassword/" + key);
|
||||
Mail mail = new Mail(from, subject, to, content);
|
||||
SendGrid sg = new SendGrid(api_key);
|
||||
Request request = new Request();
|
||||
try {
|
||||
request.setMethod(Method.POST);
|
||||
request.setEndpoint("mail/send");
|
||||
request.setBody(mail.build());
|
||||
Response response = sg.api(request);
|
||||
LOG.info("Password reset email status: {}\nfrom: {}\nto: {}\nkey: {}\nhost: {}", response.getStatusCode(), from.getEmail(), to.getEmail(), api_key, sg.getHost());
|
||||
zipBsonResponse(_resp, new DaoEntity("success", response.getStatusCode() == 200));
|
||||
} catch (IOException ex) {
|
||||
LOG.error("Failed to send password reset email", ex);
|
||||
_resp.setStatus(500);
|
||||
}*/
|
||||
return 500;
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,19 @@
|
||||
package com.lanternsoftware.currentmonitor.servlet;
|
||||
|
||||
import com.lanternsoftware.currentmonitor.context.Globals;
|
||||
import com.lanternsoftware.currentmonitor.email.IEmailProvider;
|
||||
import com.lanternsoftware.currentmonitor.email.MailJetProvider;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.Account;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EmailCredentials;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.external.LanternFiles;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.ResourceLoader;
|
||||
import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.email.EmailValidator;
|
||||
import com.lanternsoftware.util.external.LanternFiles;
|
||||
import com.lanternsoftware.util.servlet.FreemarkerConfigUtil;
|
||||
import com.lanternsoftware.util.servlet.FreemarkerServlet;
|
||||
import com.sendgrid.Method;
|
||||
import com.sendgrid.Request;
|
||||
import com.sendgrid.Response;
|
||||
import com.sendgrid.SendGrid;
|
||||
import com.sendgrid.helpers.mail.Mail;
|
||||
import com.sendgrid.helpers.mail.objects.Content;
|
||||
import com.sendgrid.helpers.mail.objects.Email;
|
||||
import freemarker.template.Configuration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -25,13 +22,13 @@ import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet("/resetPassword/*")
|
||||
public class ResetPasswordServlet extends FreemarkerServlet {
|
||||
protected static final Logger LOG = LoggerFactory.getLogger(ResetPasswordServlet.class);
|
||||
protected static final Configuration CONFIG = FreemarkerConfigUtil.createConfig(ResetPasswordServlet.class, "/templates", 100);
|
||||
protected static final String api_key = ResourceLoader.loadFileAsString(LanternFiles.CONFIG_PATH + "sendgrid.txt");
|
||||
protected static final EmailCredentials credentials = DaoSerializer.parse(ResourceLoader.loadFileAsString(LanternFiles.CONFIG_PATH + "email.json"), EmailCredentials.class);
|
||||
protected static final IEmailProvider provider = new MailJetProvider();
|
||||
|
||||
@Override
|
||||
protected Configuration getFreemarkerConfig() {
|
||||
@@ -63,25 +60,11 @@ public class ResetPasswordServlet extends FreemarkerServlet {
|
||||
} else {
|
||||
DaoEntity payload = getRequestZipBson(_req);
|
||||
String email = DaoSerializer.getString(payload, "email");
|
||||
if (EmailValidator.getInstance().isValid(email)) {
|
||||
Account account = Globals.dao.getAccountByUsername(email);
|
||||
if ((account != null) && EmailValidator.getInstance().isValid(email)) {
|
||||
String key = Globals.dao.addPasswordResetKey(email);
|
||||
Email from = new Email("mark.milligan@lanternsoftware.com");
|
||||
String subject = "Password Reset - Lantern Power Monitor";
|
||||
Email to = new Email(email);
|
||||
Content content = new Content("text/plain", "Reset your password using this link:\nhttps://lanternpowermonitor.com/currentmonitor/resetPassword/" + key);
|
||||
Mail mail = new Mail(from, subject, to, content);
|
||||
SendGrid sg = new SendGrid(api_key);
|
||||
Request request = new Request();
|
||||
try {
|
||||
request.setMethod(Method.POST);
|
||||
request.setEndpoint("mail/send");
|
||||
request.setBody(mail.build());
|
||||
Response response = sg.api(request);
|
||||
zipBsonResponse(_resp, new DaoEntity("success", response.getStatusCode() == 200));
|
||||
} catch (IOException ex) {
|
||||
LOG.error("Failed to send password reset email", ex);
|
||||
_resp.setStatus(500);
|
||||
}
|
||||
int status = provider.sendTextEmail(credentials, email, "Password Reset - Lantern Power Monitor", "Reset your password using this link:\n" + credentials.getServerUrlBase() + "resetPassword/" + key);
|
||||
zipBsonResponse(_resp, new DaoEntity("success", status == 200));
|
||||
}
|
||||
else
|
||||
_resp.setStatus(400);
|
||||
|
||||
@@ -6,10 +6,10 @@ import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
|
||||
import com.google.api.client.http.javanet.NetHttpTransport;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.lanternsoftware.currentmonitor.context.Globals;
|
||||
import com.lanternsoftware.util.external.LanternFiles;
|
||||
import com.lanternsoftware.util.ResourceLoader;
|
||||
import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.external.LanternFiles;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user