updated smartplug

This commit is contained in:
Tommaso Rodolfo Masera 2020-03-16 17:01:21 +01:00
parent 9b9dac533d
commit 2b65198df7

View file

@ -1,5 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -8,11 +11,42 @@ import javax.validation.constraints.NotNull;
@Entity @Entity
public class SmartPlug extends Switchable { public class SmartPlug extends Switchable {
/** The total amount of power that the smart plug has consumed represented in kW/h */
@Column @NotNull private static double totalConsumption;
/** Whether the smart plug is on */ /** Whether the smart plug is on */
@Column(name = "smart_plug_on", nullable = false) @Column(name = "smart_plug_on", nullable = false)
@NotNull @NotNull
private boolean on; private boolean on;
public double getTotalConsumption() {
return totalConsumption;
}
public void resetTotalConsumption() {
totalConsumption = 0;
}
/**
* Updates the consumption of a smart plug storing it in kW/h assuming that an average smart
* plug consumes 3W and by computing the conversion from watts to kW/h
*/
private static void updateTotalConsumption() {
double averageConsumption = 3;
totalConsumption += (averageConsumption / 1000) * (1 / 3600);
}
/** Calls updateTotalConsumption every second */
public void updateConsumptionOverTime() {
final ScheduledExecutorService executorService =
Executors.newSingleThreadScheduledExecutor();
while (isOn()) {
executorService.scheduleAtFixedRate(
SmartPlug::updateTotalConsumption, 0, 1, TimeUnit.SECONDS);
}
}
@Override @Override
public boolean isOn() { public boolean isOn() {
return on; return on;