control the well from a text-based cli
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
package com.henrypump.poc;
|
||||
|
||||
import org.bson.Document;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static java.lang.Math.abs;
|
||||
|
||||
/**
|
||||
@@ -37,6 +41,26 @@ public class Measurement {
|
||||
this.sendDelta = sendDelta;
|
||||
this.lastSentValue = 0.0;
|
||||
this.sendTimeDelta = sendTimeDelta;
|
||||
|
||||
Document lastStored = this.db.getLastStoredMeasurement(this.tagName);
|
||||
try {
|
||||
ZonedDateTime timestamp = ZonedDateTime.parse((CharSequence) lastStored.get("timestamp"));
|
||||
if (isToday(timestamp)){
|
||||
this.average = lastStored.getDouble("dailyAverage");
|
||||
this.total = lastStored.getDouble("dailyTotal");
|
||||
this.lastSentValue = lastStored.getDouble("currentValue");
|
||||
this.dailyMax = lastStored.getDouble("maxDailyValue");
|
||||
this.dailyMin = lastStored.getDouble("minDailyValue");
|
||||
this.lastSentTimestamp = timestamp.toEpochSecond();
|
||||
this.numMeasurements = lastStored.getLong("numMeasurements");
|
||||
System.out.println("Using stored value from " + timestamp.toString() + " for " + this.tagName);
|
||||
} else {
|
||||
System.out.println("Cannot use stored value from " + timestamp.toString() + " for " + this.tagName);
|
||||
}
|
||||
} catch (NullPointerException e){
|
||||
System.out.println("There was no previous measurement in the database for " + this.tagName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Measurement(String tagName, boolean storeInDatabase){
|
||||
@@ -83,9 +107,17 @@ public class Measurement {
|
||||
return dailyMin;
|
||||
}
|
||||
|
||||
public static boolean isToday(ZonedDateTime inpZDT){
|
||||
ZonedDateTime now = ZonedDateTime.now();
|
||||
if (now.toLocalDate().equals(inpZDT.toLocalDate())){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
public void update(double value)
|
||||
{
|
||||
long currentTimestamp = System.currentTimeMillis();
|
||||
long currentTimestamp = ZonedDateTime.now().toEpochSecond();
|
||||
lastValue = currentValue;
|
||||
currentValue = value;
|
||||
|
||||
@@ -101,6 +133,7 @@ public class Measurement {
|
||||
currentTimestamp - lastSentTimestamp > (sendTimeDelta * 1000)){
|
||||
long l = db.newMeasurement(this);
|
||||
lastSentValue = currentValue;
|
||||
lastSentTimestamp = currentTimestamp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user