From 2b65198df7c110d8632865da53ca226d8eb9db8b Mon Sep 17 00:00:00 2001 From: tommi27 Date: Mon, 16 Mar 2020 17:01:21 +0100 Subject: [PATCH] updated smartplug --- .../smarthut/models/SmartPlug.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java index fe936b3..533f394 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java @@ -1,5 +1,8 @@ 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.Entity; import javax.validation.constraints.NotNull; @@ -8,11 +11,42 @@ import javax.validation.constraints.NotNull; @Entity 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 */ @Column(name = "smart_plug_on", nullable = false) @NotNull 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 public boolean isOn() { return on;