Skip to content

Commit 0addc89

Browse files
committed
2 parents 61a4032 + b9c3853 commit 0addc89

File tree

5 files changed

+300
-35
lines changed

5 files changed

+300
-35
lines changed

app/src/main/java/com/eveningoutpost/dexdrip/models/Calibration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.activeandroid.Model;
1212
import com.activeandroid.annotation.Column;
1313
import com.activeandroid.annotation.Table;
14+
import com.activeandroid.query.Delete;
1415
import com.activeandroid.query.Select;
1516
import com.eveningoutpost.dexdrip.GcmActivity;
1617
import com.eveningoutpost.dexdrip.Home;
@@ -1361,6 +1362,12 @@ public static synchronized void invalidateAllForSensor() {
13611362
JoH.static_toast_long(msg);
13621363
}
13631364

1365+
public static void deleteAll() {
1366+
new Delete()
1367+
.from(Calibration.class)
1368+
.execute();
1369+
}
1370+
13641371
}
13651372

13661373
abstract class SlopeParameters {

app/src/main/java/com/eveningoutpost/dexdrip/models/Sensor.java

Lines changed: 87 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.eveningoutpost.dexdrip.models;
22

3+
import static com.eveningoutpost.dexdrip.models.JoH.tsl;
4+
35
import android.provider.BaseColumns;
46

57
import com.activeandroid.Model;
68
import com.activeandroid.annotation.Column;
79
import com.activeandroid.annotation.Table;
10+
import com.activeandroid.query.Delete;
811
import com.activeandroid.query.Select;
912
import com.eveningoutpost.dexdrip.GcmActivity;
1013
import com.eveningoutpost.dexdrip.Home;
@@ -21,8 +24,12 @@
2124

2225
import java.util.Date;
2326
import java.util.List;
27+
import java.util.Objects;
2428
import java.util.UUID;
2529

30+
import lombok.Setter;
31+
import lombok.val;
32+
2633
/**
2734
* Created by Emma Black on 10/29/14.
2835
*/
@@ -52,22 +59,25 @@ public class Sensor extends Model {
5259
@Column(name = "sensor_location")
5360
public String sensor_location;
5461

55-
public synchronized static Sensor create(long started_at) {
56-
Sensor sensor = new Sensor();
57-
sensor.started_at = started_at;
58-
sensor.uuid = UUID.randomUUID().toString();
5962

60-
sensor.save();
61-
SensorSendQueue.addToQueue(sensor);
62-
Log.d("SENSOR MODEL:", sensor.toString());
63-
return sensor;
63+
@Setter
64+
private volatile static long unitTestStopTime;
65+
66+
public synchronized static Sensor create(long starting_at) {
67+
stopSensor(true); // stop any existing sensor session clamping to the last reading
68+
return create(starting_at, UUID.randomUUID().toString());
6469
}
6570

66-
public synchronized static Sensor create(long started_at, String uuid) {//KS
71+
public synchronized static Sensor create(long starting_at, String uuid) {//KS
6772
Sensor sensor = new Sensor();
68-
sensor.started_at = started_at;
73+
val lastSensor = lastStopped(); // get the last sensor we stopped
74+
// find the time it was stopped or 0 if there is no previous sensor
75+
long lastStoppedTime = lastSensor != null ? lastSensor.stopped_at : 0;
76+
sensor.started_at = Math.max(lastStoppedTime + 1, starting_at);
77+
if (sensor.started_at > tsl()) {
78+
UserError.Log.wtf(TAG, "Sensor create() called with future timestamp, this cannot be right. " + starting_at + " " + lastStoppedTime + " " + sensor.started_at);
79+
}
6980
sensor.uuid = uuid;
70-
7181
sensor.save();
7282
SensorSendQueue.addToQueue(sensor);
7383
Log.d("SENSOR MODEL:", sensor.toString());
@@ -77,19 +87,36 @@ public synchronized static Sensor create(long started_at, String uuid) {//KS
7787
public static Sensor createDefaultIfMissing() {
7888
final Sensor sensor = currentSensor();
7989
if (sensor == null) {
80-
Sensor.create(JoH.tsl());
90+
Sensor.create(tsl());
8191
UserError.Log.ueh(TAG, "Created new default sensor");
8292
}
8393
return currentSensor();
8494
}
8595

8696
public synchronized static void stopSensor() {
97+
stopSensor(false); // by default don't clamp to last reading
98+
}
99+
100+
101+
public synchronized static void stopSensor(final boolean clampToLastReading) {
87102
final Sensor sensor = currentSensor();
88103
if (sensor == null) {
89104
return;
90105
}
91-
sensor.stopped_at = JoH.tsl();
92-
UserError.Log.ueh("SENSOR", "Sensor stopped at " + JoH.dateTimeText(sensor.stopped_at));
106+
107+
// stop now or use specified unit testing stop time
108+
sensor.stopped_at = (unitTestStopTime == 0) ? tsl() : unitTestStopTime;
109+
110+
if (clampToLastReading) {
111+
val lastReading = BgReading.last(true);
112+
if (lastReading != null) {
113+
// if we have a last reading then set the stop time to that last reading so long as it
114+
// was actually from this sensor
115+
sensor.stopped_at = Math.max(sensor.started_at, lastReading.timestamp + 1);
116+
}
117+
}
118+
119+
UserError.Log.ueh("SENSOR", "Sensor stopped at value: " + JoH.dateTimeText(sensor.stopped_at));
93120
sensor.save();
94121
if (currentSensor() != null) {
95122
UserError.Log.wtf(TAG, "Failed to update sensor stop in database");
@@ -122,36 +149,31 @@ public static Sensor lastStopped() {
122149

123150
public static boolean stoppedRecently() {
124151
final Sensor last = lastStopped();
125-
return last != null && last.stopped_at < JoH.tsl() && (JoH.msSince(last.stopped_at) < (Constants.HOUR_IN_MS * 2));
152+
return last != null && last.stopped_at < tsl() && (JoH.msSince(last.stopped_at) < (Constants.HOUR_IN_MS * 2));
126153
}
127154

128155
public static Sensor currentSensor() {
129156
Sensor sensor = new Select()
130157
.from(Sensor.class)
131158
.where("started_at != 0")
132-
.where("stopped_at = 0")
133159
.orderBy("_ID desc")
134160
.limit(1)
135161
.executeSingle();
162+
163+
if (sensor != null) {
164+
if (sensor.stopped_at != 0) {
165+
// last sensor is stopped
166+
return null;
167+
}
168+
}
136169
return sensor;
137170
}
138171

139172
public static boolean isActive() {
140-
Sensor sensor = new Select()
141-
.from(Sensor.class)
142-
.where("started_at != 0")
143-
.where("stopped_at = 0")
144-
.orderBy("_ID desc")
145-
.limit(1)
146-
.executeSingle();
147-
if(sensor == null) {
148-
return false;
149-
} else {
150-
return true;
151-
}
173+
return currentSensor() != null;
152174
}
153175

154-
public static Sensor getByTimestamp(double started_at) {
176+
public static Sensor getByTimestamp(long started_at) {
155177
return new Select()
156178
.from(Sensor.class)
157179
.where("started_at = ?", started_at)
@@ -259,6 +281,14 @@ public static void upsertFromMaster(Sensor jsonSensor) {
259281
}
260282
}
261283

284+
public static void deleteAll() {
285+
// will fail if bg readings not deleted first
286+
SensorSendQueue.deleteAll();
287+
new Delete()
288+
.from(Sensor.class)
289+
.execute();
290+
}
291+
262292
public String toJSON() {
263293
JSONObject jsonObject = new JSONObject();
264294
try {
@@ -273,6 +303,34 @@ public String toJSON() {
273303
return "";
274304
}
275305
}
306+
307+
@Override
308+
public String toString() {
309+
return "SENSOR: " + toJSON() + " started: " + JoH.dateTimeText(started_at) + ((stopped_at != 0) ? " stopped: " + JoH.dateTimeText(stopped_at) : "");
310+
}
311+
312+
@Override
313+
public boolean equals(Object obj) {
314+
if (this == obj) { // Check for reference equality
315+
return true;
316+
}
317+
if (obj == null || getClass() != obj.getClass()) { // Check for null and class type
318+
return false;
319+
}
320+
Sensor sensor = (Sensor) obj; // Typecast
321+
322+
return started_at == sensor.started_at
323+
// && stopped_at == sensor.stopped_at // Note: not checking stopped_at as this maybe transient
324+
&& latest_battery_level == sensor.latest_battery_level
325+
&& Objects.equals(uuid, sensor.uuid)
326+
&& Objects.equals(sensor_location, sensor.sensor_location);
327+
328+
}
329+
330+
@Override
331+
public int hashCode() {
332+
return Objects.hash(started_at, stopped_at, latest_battery_level, uuid, sensor_location);
333+
}
276334

277335
public static Sensor fromJSON(String json) {
278336
if (json.length()==0) {

app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/SensorSendQueue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.activeandroid.Model;
66
import com.activeandroid.annotation.Column;
77
import com.activeandroid.annotation.Table;
8+
import com.activeandroid.query.Delete;
89
import com.activeandroid.query.Select;
910
import com.eveningoutpost.dexdrip.GcmActivity;
1011
import com.eveningoutpost.dexdrip.Home;
@@ -57,4 +58,10 @@ public static void SendToFollower(Sensor sensor) {
5758
GcmActivity.syncSensor(sensor, true);
5859
}
5960
}
61+
62+
public static void deleteAll() {
63+
new Delete()
64+
.from(SensorSendQueue.class)
65+
.execute();
66+
}
6067
}

app/src/test/java/com/eveningoutpost/dexdrip/evaluators/PersistentHighTest.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.eveningoutpost.dexdrip.evaluators;
22

33
import com.eveningoutpost.dexdrip.models.BgReading;
4+
import com.eveningoutpost.dexdrip.models.Calibration;
45
import com.eveningoutpost.dexdrip.models.JoH;
56
import com.eveningoutpost.dexdrip.models.Sensor;
67
import com.eveningoutpost.dexdrip.RobolectricTestWithConfig;
78
import com.eveningoutpost.dexdrip.utilitymodels.Constants;
89

10+
import org.junit.After;
11+
import org.junit.Before;
912
import org.junit.Test;
1013
import org.robolectric.shadows.ShadowLog;
1114

@@ -22,6 +25,22 @@ public class PersistentHighTest extends RobolectricTestWithConfig {
2225
private static long START_TIME = JoH.tsl() - Constants.HOUR_IN_MS * 4;
2326
private static final double HIGH_MARK = 170;
2427

28+
@Before
29+
public void setUp() {
30+
cleanEverything();
31+
}
32+
33+
@After
34+
public void tearDown() {
35+
cleanEverything();
36+
}
37+
38+
private void cleanEverything() {
39+
BgReading.deleteALL();
40+
Calibration.deleteAll();
41+
Sensor.deleteAll();
42+
}
43+
2544
@Test
2645
public void dataQualityCheckTest() {
2746

@@ -32,8 +51,9 @@ public void dataQualityCheckTest() {
3251
assertWithMessage("Predating sensor should fail").that(PersistentHigh.dataQualityCheck(1000, HIGH_MARK)).isFalse();
3352
assertWithMessage("Post sensor start no data should fail").that(PersistentHigh.dataQualityCheck(JoH.tsl(), HIGH_MARK)).isFalse();
3453

35-
BgReading.deleteALL();
36-
Sensor.create(START_TIME);
54+
cleanEverything();
55+
Sensor created = Sensor.create(START_TIME);
56+
assertWithMessage("Sensor started when we wanted").that(created.started_at).isEqualTo(START_TIME);
3757
// various all high time slices
3858
for (int i = 0; i < (12 * 4); i++) {
3959
final long timestamp = START_TIME + Constants.MINUTE_IN_MS * 5 * i;
@@ -44,8 +64,9 @@ public void dataQualityCheckTest() {
4464
}
4565

4666
START_TIME++;
47-
BgReading.deleteALL();
48-
Sensor.create(START_TIME);
67+
cleanEverything();
68+
created = Sensor.create(START_TIME);
69+
assertWithMessage("Sensor started when we wanted b").that(created.started_at).isEqualTo(START_TIME);
4970
// single dipped point in sequence after a point we might have succeeded
5071
for (int i = 0; i < (12 * 4); i++) {
5172
final long timestamp = START_TIME + Constants.MINUTE_IN_MS * 5 * i;
@@ -56,8 +77,9 @@ public void dataQualityCheckTest() {
5677
}
5778

5879
START_TIME++;
59-
BgReading.deleteALL();
60-
Sensor.create(START_TIME);
80+
cleanEverything();
81+
created = Sensor.create(START_TIME);
82+
assertWithMessage("Sensor started when we wanted c").that(created.started_at).isEqualTo(START_TIME);
6183
// single dipped point in sequence before we would succeed
6284
for (int i = 0; i < (12 * 4); i++) {
6385
final long timestamp = START_TIME + Constants.MINUTE_IN_MS * 5 * i;

0 commit comments

Comments
 (0)