mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Add billing plans so different plans can be compared. Performance enhancements to charge calculations.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.lanternsoftware.currentmonitor.servlet;
|
||||
|
||||
import com.lanternsoftware.currentmonitor.context.Globals;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EnergyViewMode;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.dao.auth.AuthCode;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.util.Date;
|
||||
|
||||
@WebServlet("/charge/*")
|
||||
public class ChargeServlet extends SecureServlet {
|
||||
@Override
|
||||
protected void get(AuthCode _authCode, HttpServletRequest _req, HttpServletResponse _rep) {
|
||||
String[] path = path(_req);
|
||||
if (path.length < 3) {
|
||||
_rep.setStatus(400);
|
||||
return;
|
||||
}
|
||||
int accountId = DaoSerializer.toInteger(CollectionUtils.getFirst(_authCode.getAllAccountIds()));
|
||||
if (accountId == 0) {
|
||||
_rep.setStatus(404);
|
||||
return;
|
||||
}
|
||||
EnergyViewMode viewMode = NullUtils.toEnum(EnergyViewMode.class, path[2], EnergyViewMode.DAY);
|
||||
Date start = new Date(NullUtils.toLong(path[3]));
|
||||
byte[] charges = Globals.dao.getChargeSummaryBinary(accountId, DaoSerializer.toInteger(path[0]), path[1], viewMode, start);
|
||||
if (charges == null)
|
||||
_rep.setStatus(404);
|
||||
else
|
||||
setResponseEntity(_rep, 200, MediaType.APPLICATION_OCTET_STREAM, charges);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.lanternsoftware.currentmonitor.servlet;
|
||||
|
||||
import com.lanternsoftware.currentmonitor.context.Globals;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EnergyViewMode;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.dao.auth.AuthCode;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.util.Date;
|
||||
|
||||
@WebServlet("/energy/*")
|
||||
public class EnergyServlet extends SecureServlet {
|
||||
@Override
|
||||
protected void get(AuthCode _authCode, HttpServletRequest _req, HttpServletResponse _rep) {
|
||||
String[] path = path(_req);
|
||||
if (path.length < 3) {
|
||||
_rep.setStatus(400);
|
||||
return;
|
||||
}
|
||||
int accountId = DaoSerializer.toInteger(CollectionUtils.getFirst(_authCode.getAllAccountIds()));
|
||||
if (accountId == 0) {
|
||||
_rep.setStatus(404);
|
||||
return;
|
||||
}
|
||||
EnergyViewMode viewMode = NullUtils.toEnum(EnergyViewMode.class, path[1], EnergyViewMode.DAY);
|
||||
Date start = new Date(NullUtils.toLong(path[2]));
|
||||
byte[] energy = Globals.dao.getEnergySummaryBinary(accountId, path[0], viewMode, start);
|
||||
if (energy == null)
|
||||
_rep.setStatus(404);
|
||||
else
|
||||
setResponseEntity(_rep, 200, MediaType.APPLICATION_OCTET_STREAM, energy);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
package com.lanternsoftware.currentmonitor.servlet;
|
||||
|
||||
import com.lanternsoftware.currentmonitor.context.Globals;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerGroupEnergy;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EnergyBlockViewMode;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.*;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.DateUtils;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.dao.auth.AuthCode;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@WebServlet("/energy/group/*")
|
||||
public class GroupEnergyServlet extends SecureServlet {
|
||||
@@ -23,34 +23,31 @@ public class GroupEnergyServlet extends SecureServlet {
|
||||
_rep.setStatus(400);
|
||||
return;
|
||||
}
|
||||
EnergyBlockViewMode viewMode = NullUtils.toEnum(EnergyBlockViewMode.class, path[1], EnergyBlockViewMode.DAY);
|
||||
Date start = new Date(NullUtils.toLong(path[2]));
|
||||
if ((CollectionUtils.size(_authCode.getAllAccountIds()) == 1) && NullUtils.isEqual(CollectionUtils.get(path, 3), "bin")) {
|
||||
byte[] energy = Globals.dao.getBreakerGroupEnergyBinary(CollectionUtils.getFirst(_authCode.getAllAccountIds()), path[0], viewMode, start);
|
||||
if (energy == null)
|
||||
_rep.setStatus(404);
|
||||
else
|
||||
setResponseEntity(_rep, 200, MediaType.APPLICATION_OCTET_STREAM, energy);
|
||||
}
|
||||
List<BreakerGroupEnergy> energies = CollectionUtils.transform(_authCode.getAllAccountIds(), _id->Globals.dao.getBreakerGroupEnergy(_id, path[0], viewMode, start), true);
|
||||
if (CollectionUtils.isNotEmpty(energies)) {
|
||||
BreakerGroupEnergy energy;
|
||||
if (energies.size() > 1) {
|
||||
energy = new BreakerGroupEnergy();
|
||||
energy.setAccountId(_authCode.getAccountId());
|
||||
energy.setGroupId("Sites");
|
||||
energy.setGroupName("Sites");
|
||||
energy.setStart(start);
|
||||
energy.setViewMode(viewMode);
|
||||
energy.setSubGroups(CollectionUtils.asArrayList(energies));
|
||||
}
|
||||
else
|
||||
energy = CollectionUtils.getFirst(energies);
|
||||
if (NullUtils.isEqual(CollectionUtils.get(path, 3), "bin"))
|
||||
zipBsonResponse(_rep, energy);
|
||||
else
|
||||
jsonResponse(_rep, energy);
|
||||
} else
|
||||
int accountId = DaoSerializer.toInteger(CollectionUtils.getFirst(_authCode.getAllAccountIds()));
|
||||
if (accountId == 0) {
|
||||
_rep.setStatus(404);
|
||||
return;
|
||||
}
|
||||
EnergyViewMode viewMode = NullUtils.toEnum(EnergyViewMode.class, path[1], EnergyViewMode.DAY);
|
||||
Date start = new Date(NullUtils.toLong(path[2]));
|
||||
EnergySummary summary = Globals.dao.getEnergySummary(accountId, path[0], viewMode, start);
|
||||
if (summary == null)
|
||||
_rep.setStatus(404);
|
||||
else {
|
||||
BreakerConfig config = Globals.dao.getConfig(accountId);
|
||||
Account acct = Globals.dao.getAccount(accountId);
|
||||
TimeZone tz = DateUtils.fromTimeZoneId(acct.getTimezone(), "America/Chicago");
|
||||
List<BillingRate> rates = CollectionUtils.filter(config.getBillingRates(), _r->_r.isApplicableForDay(start, tz));
|
||||
Map<String, Integer> breakerGroupMeters = new HashMap<>();
|
||||
for (BreakerGroup group : config.getAllBreakerGroups()) {
|
||||
Breaker b = CollectionUtils.getFirst(group.getBreakers());
|
||||
if (b != null)
|
||||
breakerGroupMeters.put(group.getId(), b.getMeter());
|
||||
}
|
||||
BreakerGroupEnergy energy = new BreakerGroupEnergy(summary, rates, breakerGroupMeters);
|
||||
energy.setToGrid(-summary.flow(null, true, GridFlow.TO));
|
||||
energy.setFromGrid(summary.flow(null, true, GridFlow.FROM));
|
||||
setResponseEntity(_rep, 200, MediaType.APPLICATION_OCTET_STREAM, DaoSerializer.toZipBson(energy));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user