Added an example of querying an energy summary for a day using the API.

This commit is contained in:
Mark Milligan 2022-06-05 22:36:50 -05:00
parent b8acfd4ea4
commit b0ffd93f8d
2 changed files with 40 additions and 6 deletions

View File

@ -0,0 +1,37 @@
package com.lanternsoftware.currentmonitor;
import com.lanternsoftware.datamodel.currentmonitor.BreakerConfig;
import com.lanternsoftware.datamodel.currentmonitor.EnergySummary;
import com.lanternsoftware.util.DateUtils;
import com.lanternsoftware.util.dao.DaoSerializer;
import com.lanternsoftware.util.http.HttpPool;
import com.lanternsoftware.util.servlet.BasicAuth;
import org.apache.http.client.methods.HttpGet;
import java.util.Date;
import java.util.TimeZone;
public class GetEnergySummary {
public static void main(String[] args) {
HttpPool pool = new HttpPool(10, 10, 10000, 10000, 10000);
HttpGet authRequest = new HttpGet("https://lanternpowermonitor.com/currentmonitor/auth");
authRequest.addHeader("Authorization", BasicAuth.toHeader("<username>", "<password>"));
String authRep = pool.executeToString(authRequest);
String authCode = DaoSerializer.getString(DaoSerializer.parse(authRep), "auth_code");
HttpGet configRequest = new HttpGet("https://lanternpowermonitor.com/currentmonitor/config");
configRequest.addHeader("auth_code", authCode);
String configRep = pool.executeToString(configRequest);
BreakerConfig config = DaoSerializer.parse(configRep, BreakerConfig.class);
Date day = DateUtils.date(6, 5, 2022, TimeZone.getTimeZone("America/Chicago"));
HttpGet summaryRequest = new HttpGet("https://lanternpowermonitor.com/currentmonitor/energy/" + config.getRootGroup().getId() + "/DAY/" + day.getTime());
summaryRequest.addHeader("auth_code", authCode);
EnergySummary summary = DaoSerializer.fromZipBson(pool.executeToByteArray(summaryRequest), EnergySummary.class);
System.out.println(DaoSerializer.toJson(summary));
pool.shutdown();
}
}

View File

@ -1,13 +1,10 @@
package com.lanternsoftware.util.servlet; package com.lanternsoftware.util.servlet;
import java.nio.charset.Charset; import com.lanternsoftware.util.NullUtils;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import com.lanternsoftware.util.NullUtils; import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
public class BasicAuth { public class BasicAuth {
private final String username; private final String username;