BgReading optimization

This commit is contained in:
Milos Kozak 2017-06-06 17:14:17 +02:00
parent a180e182eb
commit 5931abc860
4 changed files with 75 additions and 64 deletions

View file

@ -166,16 +166,7 @@ public class DataService extends IntentService {
bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP);
bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW);
if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) {
if (Config.logIncommingBG)
log.debug("Ignoring old XDRIPREC BG " + bgReading.toString());
return;
}
if (Config.logIncommingBG)
log.debug("XDRIPREC BG " + bgReading.toString());
MainApp.getDbHelper().createIfNotExists(bgReading);
MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP");
}
private void handleNewDataFromGlimp(Intent intent) {
@ -189,11 +180,7 @@ public class DataService extends IntentService {
bgReading.date = bundle.getLong("myTimestamp");
bgReading.raw = 0;
if (Config.logIncommingBG)
log.debug(bundle.toString());
log.debug("GLIMP BG " + bgReading.toString());
MainApp.getDbHelper().createIfNotExists(bgReading);
MainApp.getDbHelper().createIfNotExists(bgReading, "GLIMP");
}
private void handleNewDataFromMM640g(Intent intent) {
@ -221,16 +208,7 @@ public class DataService extends IntentService {
bgReading.date = json_object.getLong("date");
bgReading.raw = json_object.getDouble("sgv");
if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) {
if (Config.logIncommingBG)
log.debug("Ignoring old MM640g BG " + bgReading.toString());
return;
}
if (Config.logIncommingBG)
log.debug("MM640g BG " + bgReading.toString());
MainApp.getDbHelper().createIfNotExists(bgReading);
MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g");
break;
default:
log.debug("Unknown entries type: " + type);
@ -402,14 +380,7 @@ public class DataService extends IntentService {
JSONObject sgvJson = new JSONObject(sgvstring);
NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) {
if (Config.logIncommingData)
log.debug("Ignoring old BG: " + bgReading.toString());
return;
}
MainApp.getDbHelper().createIfNotExists(bgReading);
if (Config.logIncommingData)
log.debug("ADD: Stored new BG: " + bgReading.toString());
MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
}
if (bundles.containsKey("sgvs")) {
@ -419,14 +390,7 @@ public class DataService extends IntentService {
JSONObject sgvJson = jsonArray.getJSONObject(i);
NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) {
if (Config.logIncommingData)
log.debug("Ignoring old BG: " + bgReading.toString());
} else {
MainApp.getDbHelper().createIfNotExists(bgReading);
if (Config.logIncommingData)
log.debug("ADD: Stored new BG: " + bgReading.toString());
}
MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
}
}
} catch (Exception e) {
@ -441,11 +405,6 @@ public class DataService extends IntentService {
JSONObject mbgJson = new JSONObject(mbgstring);
NSMbg nsMbg = new NSMbg(mbgJson);
CareportalEvent careportalEvent = new CareportalEvent(nsMbg);
if (careportalEvent.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) {
if (Config.logIncommingData)
log.debug("Ignoring old MBG: " + careportalEvent.log());
return;
}
MainApp.getDbHelper().createOrUpdate(careportalEvent);
if (Config.logIncommingData)
log.debug("Adding/Updating new MBG: " + careportalEvent.log());
@ -458,11 +417,6 @@ public class DataService extends IntentService {
JSONObject mbgJson = jsonArray.getJSONObject(i);
NSMbg nsMbg = new NSMbg(mbgJson);
CareportalEvent careportalEvent = new CareportalEvent(nsMbg);
if (careportalEvent.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) {
if (Config.logIncommingData)
log.debug("Ignoring old MBG: " + careportalEvent.log());
return;
}
MainApp.getDbHelper().createOrUpdate(careportalEvent);
if (Config.logIncommingData)
log.debug("Adding/Updating new MBG: " + careportalEvent.log());

View file

@ -6,6 +6,8 @@ import com.j256.ormlite.table.DatabaseTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
@ -40,12 +42,10 @@ public class BgReading implements DataPointWithLabelInterface {
@DatabaseField
public String _id = null; // NS _id
public static String units = Constants.MGDL;
public boolean isPrediction = false; // true when drawing predictions as bg points
public BgReading() {}
public BgReading() {
}
public BgReading(NSSgv sgv) {
date = sgv.getMills();
@ -66,7 +66,7 @@ public class BgReading implements DataPointWithLabelInterface {
else return DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL);
}
public String directionToSymbol() {
public String directionToSymbol() {
String symbol = "";
if (direction.compareTo("DoubleDown") == 0) {
symbol = "\u21ca";
@ -105,13 +105,56 @@ public class BgReading implements DataPointWithLabelInterface {
public String toString() {
return "BgReading{" +
"date=" + date +
", date=" + DateUtil.dateAndTimeString(date) +
", date=" + new Date(date).toLocaleString() +
", value=" + value +
", direction=" + direction +
", raw=" + raw +
'}';
}
public boolean isDataChanging(BgReading other) {
if (date != other.date) {
log.error("Comparing different");
return false;
}
if (value != other.value)
return true;
return false;
}
public boolean isEqual(BgReading other) {
if (date != other.date) {
log.error("Comparing different");
return false;
}
if (value != other.value)
return false;
if (raw != other.raw)
return false;
if (!direction.equals(other.direction))
return false;
if (_id == null && other._id != null)
return false;
else if (_id != null && other._id == null)
return false;
else if (_id == null && other._id == null)
;
else if (!_id.equals(other._id))
return false;
return true;
}
public void copyFrom(BgReading other) {
if (date != other.date) {
log.error("Copying different");
return;
}
value = other.value;
raw = other.raw;
direction = other.direction;
_id = other._id;
}
// ------------------ DataPointWithLabelInterface ------------------
@Override
public double getX() {
@ -120,6 +163,7 @@ public class BgReading implements DataPointWithLabelInterface {
@Override
public double getY() {
String units = MainApp.getConfigBuilder().getProfile().getUnits();
return valueToUnits(units);
}
@ -151,6 +195,7 @@ public class BgReading implements DataPointWithLabelInterface {
@Override
public int getColor() {
String units = MainApp.getConfigBuilder().getProfile().getUnits();
Double lowLine = SP.getDouble("low_mark", 0d);
Double highLine = SP.getDouble("high_mark", 0d);
if (lowLine < 1) {
@ -159,7 +204,6 @@ public class BgReading implements DataPointWithLabelInterface {
if (highLine < 1) {
highLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units);
}
String units = MainApp.getConfigBuilder().getProfile().getUnits();
int color = MainApp.sResources.getColor(R.color.inrange);
if (isPrediction)
color = MainApp.sResources.getColor(R.color.prediction);

View file

@ -320,16 +320,31 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return getDao(ProfileSwitch.class);
}
public long roundDateToSec(long date) {
return date - date % 1000;
}
// ------------------- BgReading handling -----------------------
public void createIfNotExists(BgReading bgReading) {
bgReading.date = bgReading.date - bgReading.date % 1000;
public void createIfNotExists(BgReading bgReading, String from) {
try {
getDaoBgReadings().createIfNotExists(bgReading);
bgReading.date = roundDateToSec(bgReading.date);
BgReading old = getDaoBgReadings().queryForId(bgReading.date);
if (old == null) {
getDaoBgReadings().create(bgReading);
log.debug("BG: New record from: " + from + " " + bgReading.toString());
scheduleBgChange();
return;
}
if (!old.isEqual(bgReading)) {
old.copyFrom(bgReading);
getDaoBgReadings().update(old);
log.debug("BG: Updating record from: " + from + " " + old.toString());
scheduleBgChange();
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
scheduleBgChange();
}
private static void scheduleBgChange() {

View file

@ -1097,8 +1097,6 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
deltaView.setText("Δ " + MainApp.sResources.getString(R.string.notavailable));
avgdeltaView.setText("");
}
BgReading.units = profile.getUnits();
} else {
if (updating != null)
updating.setVisibility(View.GONE);