Skip to content

Commit

Permalink
Attempt to fix summary not storing when transform produces 0 values
Browse files Browse the repository at this point in the history
  • Loading branch information
dlmcpaul committed Aug 23, 2020
1 parent bc934a8 commit 6b347b3
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 51 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/hz/models/database/ElectricityRate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hz.models.database;

import com.hz.models.interfaces.RateInterface;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -12,7 +13,7 @@
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ElectricityRate {
public class ElectricityRate implements RateInterface {
@Id
private LocalDate effectiveDate;
private Double paymentPerKiloWatt = 0.0;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/hz/models/envoy/json/EimType.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ public class EimType extends TypeBase {
private BigDecimal vahToday;
private BigDecimal varhLeadToday;
private BigDecimal varhLagToday;

public EimType() {
this.setWattsNow(BigDecimal.ZERO);
}

public EimType(BigDecimal wattsNow) {
this.setWattsNow(wattsNow);
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/hz/models/envoy/json/InvertersType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -18,7 +19,7 @@ public class InvertersType extends TypeBase {
private static final int MICRO_INVERTER = 1;
private static final int BATTERY = 11;

private List<Inverter> inverterList; // populated from api/v1/production/inverters
private List<Inverter> inverterList = new ArrayList<>(); // populated from api/v1/production/inverters

public List<Inverter> getMicroInvertors() {
return inverterList.stream().filter(inverter -> inverter.getDeviceType() == MICRO_INVERTER).collect(Collectors.toList());
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/hz/models/envoy/json/PowerMeter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public class PowerMeter {
private BigDecimal current;
private BigDecimal freq;

public PowerMeter() {
// Set some defaults
this.activePower = BigDecimal.ZERO;
this.voltage = BigDecimal.ZERO;
}

public PowerMeter(BigDecimal activePower, BigDecimal voltage) {
this.activePower = activePower;
this.voltage = voltage;
}

@JsonProperty(value="channels")
private List<Channel> channelList;
}
61 changes: 22 additions & 39 deletions src/main/java/com/hz/models/envoy/json/Production.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import lombok.extern.log4j.Log4j2;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -33,30 +32,17 @@ public class Production {

@JsonIgnore
public List<Inverter> getMicroInvertorsList() {
Optional<InvertersType> inverter = this.getInverter();
if (inverter.isPresent()) {
return inverter.get().getMicroInvertors();
}

return new ArrayList<>();
return this.getInverter().orElseGet(InvertersType::new).getMicroInvertors();
}

@JsonIgnore
public List<Inverter> getBatteryList() {
Optional<InvertersType> inverter = this.getInverter();
if (inverter.isPresent()) {
return inverter.get().getBatteries();
}

return new ArrayList<>();
return this.getInverter().orElseGet(InvertersType::new).getBatteries();
}

@JsonIgnore
public void setInverterList(List<Inverter> inverterList) {
Optional<InvertersType> inverter = this.getInverter();
if (inverter.isPresent()) {
inverter.get().setInverterList(inverterList);
}
this.getInverter().ifPresent(invertersType -> invertersType.setInverterList(inverterList));
}

@JsonIgnore
Expand Down Expand Up @@ -104,40 +90,37 @@ private Optional<DeviceMeter> getDevice(String measurementType) {

@JsonIgnore
public BigDecimal getProductionVoltage() {

if (getProductionMeter().isPresent()) {
return getProductionMeter().get().getVoltage();
}

if (getProductionEim().isPresent()) {
return getProductionEim().get().getRmsVoltage();
}

return BigDecimal.ZERO;
return getProductionMeter().orElse(new PowerMeter(BigDecimal.ZERO, getProductionEimVoltage())).getVoltage();
}

@JsonIgnore
public BigDecimal getProductionEimVoltage() {
return getProductionEim().orElseGet(EimType::new).getRmsVoltage();
}

@JsonIgnore
public BigDecimal getProductionWatts() {
return getProductionMeter().orElse(new PowerMeter(getProductionEimWatts(), BigDecimal.ZERO)).getActivePower();
}

if (getProductionMeter().isPresent()) {
return getProductionMeter().get().getActivePower();
}

if (getProductionEim().isPresent()) {
return getProductionEim().get().getWattsNow();
}
@JsonIgnore
public BigDecimal getProductionEimWatts() {
return getProductionEim().orElse(new EimType(getInverterWatts())).getWattsNow();
}

return getInverter().get().getWattsNow();
@JsonIgnore
public BigDecimal getInverterWatts() {
return getInverter().orElseGet(InvertersType::new).getWattsNow();
}

@JsonIgnore
public BigDecimal getConsumptionWatts() {
if (getTotalConsumptionMeter().isPresent()) {
return getTotalConsumptionMeter().get().getActivePower();
}
return getTotalConsumptionMeter().orElse(new PowerMeter(getConsumptionEimWatts(), BigDecimal.ZERO)).getActivePower();
}

return getTotalConsumptionEim().get().getWattsNow();
@JsonIgnore
public BigDecimal getConsumptionEimWatts() {
return getTotalConsumptionEim().orElseGet(EimType::new).getWattsNow();
}

private Optional<EimType> findBymeasurementType(List<TypeBase> list, String measurementType) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hz/models/envoy/json/TypeBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class TypeBase {
private int activeCount;
private long readingTime;
@JsonProperty(value="wNow")
private BigDecimal wattsNow;
private BigDecimal wattsNow = BigDecimal.ZERO;
@JsonProperty(value="whLifetime")
private BigDecimal wattsLifetime;

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/hz/models/interfaces/RateInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hz.models.interfaces;

public interface RateInterface {
Double getPaymentPerKiloWatt();
Double getChargePerKiloWatt();
Double getDailySupplyCharge();
}
18 changes: 9 additions & 9 deletions src/main/java/com/hz/services/EnphaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,23 @@ private void calculateSavings(ArrayList<Metric> metricList, BigDecimal productio
public List<Metric> getMetrics(System system) {
ArrayList<Metric> metricList = new ArrayList<>();

Optional<EimType> productionEim = system.getProduction().getProductionEim();
BigDecimal production = system.getProduction().getProductionWatts();
BigDecimal consumption = system.getProduction().getConsumptionWatts();

metricList.add(new Metric("solar.production.current", production, 5));
metricList.add(new Metric("solar.consumption.current", consumption));
metricList.add(new Metric("solar.production.voltage", system.getProduction().getProductionVoltage().floatValue()));

Optional<EimType> productionEim = system.getProduction().getProductionEim();
Optional<InvertersType> inverter = system.getProduction().getInverter();
BigDecimal production = BigDecimal.ZERO;
BigDecimal consumption = BigDecimal.ZERO;
if (productionEim.isPresent() && inverter.isPresent()) {
production = system.getProduction().getProductionWatts();
if (productionEim.isPresent() && inverter.isPresent()) {
log.debug("production: eim time {} eim {} inverter time {} inverter {} calculated {}", Convertors.convertToLocalDateTime(productionEim.get().getReadingTime()), productionEim.get().getWattsNow(), Convertors.convertToLocalDateTime(inverter.get().getReadingTime()), inverter.get().getWattsNow(), production);
metricList.add(new Metric("solar.production.current", production, 5));
metricList.add(new Metric("solar.production.total", inverter.get().getWattsLifetime()));
metricList.add(new Metric("solar.production.voltage", system.getProduction().getProductionVoltage().floatValue()));
}

Optional<EimType> consumptionEim = system.getProduction().getTotalConsumptionEim();
if (consumptionEim.isPresent()) {
consumption = system.getProduction().getConsumptionWatts();
log.debug("consumption: eim time {} eim {} calculated {}", Convertors.convertToLocalDateTime(consumptionEim.get().getReadingTime()), consumptionEim.get().getWattsNow(), consumption);
metricList.add(new Metric("solar.consumption.current", consumption));
metricList.add(new Metric("solar.consumption.total", consumptionEim.get().getWattsLifetime()));
}

Expand Down

0 comments on commit 6b347b3

Please sign in to comment.