mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Added an example of querying an energy summary for a day using the API.
This commit is contained in:
parent
b8acfd4ea4
commit
b0ffd93f8d
|
@ -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();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user