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.date = bundle.getLong(Intents.EXTRA_TIMESTAMP);
bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW);
if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) { MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP");
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);
} }
private void handleNewDataFromGlimp(Intent intent) { private void handleNewDataFromGlimp(Intent intent) {
@ -189,11 +180,7 @@ public class DataService extends IntentService {
bgReading.date = bundle.getLong("myTimestamp"); bgReading.date = bundle.getLong("myTimestamp");
bgReading.raw = 0; bgReading.raw = 0;
if (Config.logIncommingBG) MainApp.getDbHelper().createIfNotExists(bgReading, "GLIMP");
log.debug(bundle.toString());
log.debug("GLIMP BG " + bgReading.toString());
MainApp.getDbHelper().createIfNotExists(bgReading);
} }
private void handleNewDataFromMM640g(Intent intent) { private void handleNewDataFromMM640g(Intent intent) {
@ -221,16 +208,7 @@ public class DataService extends IntentService {
bgReading.date = json_object.getLong("date"); bgReading.date = json_object.getLong("date");
bgReading.raw = json_object.getDouble("sgv"); bgReading.raw = json_object.getDouble("sgv");
if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) { MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g");
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);
break; break;
default: default:
log.debug("Unknown entries type: " + type); log.debug("Unknown entries type: " + type);
@ -402,14 +380,7 @@ public class DataService extends IntentService {
JSONObject sgvJson = new JSONObject(sgvstring); JSONObject sgvJson = new JSONObject(sgvstring);
NSSgv nsSgv = new NSSgv(sgvJson); NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv); BgReading bgReading = new BgReading(nsSgv);
if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
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());
} }
if (bundles.containsKey("sgvs")) { if (bundles.containsKey("sgvs")) {
@ -419,14 +390,7 @@ public class DataService extends IntentService {
JSONObject sgvJson = jsonArray.getJSONObject(i); JSONObject sgvJson = jsonArray.getJSONObject(i);
NSSgv nsSgv = new NSSgv(sgvJson); NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv); BgReading bgReading = new BgReading(nsSgv);
if (bgReading.date < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
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());
}
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -441,11 +405,6 @@ public class DataService extends IntentService {
JSONObject mbgJson = new JSONObject(mbgstring); JSONObject mbgJson = new JSONObject(mbgstring);
NSMbg nsMbg = new NSMbg(mbgJson); NSMbg nsMbg = new NSMbg(mbgJson);
CareportalEvent careportalEvent = new CareportalEvent(nsMbg); 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); MainApp.getDbHelper().createOrUpdate(careportalEvent);
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("Adding/Updating new MBG: " + careportalEvent.log()); log.debug("Adding/Updating new MBG: " + careportalEvent.log());
@ -458,11 +417,6 @@ public class DataService extends IntentService {
JSONObject mbgJson = jsonArray.getJSONObject(i); JSONObject mbgJson = jsonArray.getJSONObject(i);
NSMbg nsMbg = new NSMbg(mbgJson); NSMbg nsMbg = new NSMbg(mbgJson);
CareportalEvent careportalEvent = new CareportalEvent(nsMbg); 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); MainApp.getDbHelper().createOrUpdate(careportalEvent);
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("Adding/Updating new MBG: " + careportalEvent.log()); 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
@ -40,12 +42,10 @@ public class BgReading implements DataPointWithLabelInterface {
@DatabaseField @DatabaseField
public String _id = null; // NS _id public String _id = null; // NS _id
public static String units = Constants.MGDL;
public boolean isPrediction = false; // true when drawing predictions as bg points public boolean isPrediction = false; // true when drawing predictions as bg points
public BgReading() {} public BgReading() {
}
public BgReading(NSSgv sgv) { public BgReading(NSSgv sgv) {
date = sgv.getMills(); date = sgv.getMills();
@ -66,7 +66,7 @@ public class BgReading implements DataPointWithLabelInterface {
else return DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL); else return DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL);
} }
public String directionToSymbol() { public String directionToSymbol() {
String symbol = ""; String symbol = "";
if (direction.compareTo("DoubleDown") == 0) { if (direction.compareTo("DoubleDown") == 0) {
symbol = "\u21ca"; symbol = "\u21ca";
@ -105,13 +105,56 @@ public class BgReading implements DataPointWithLabelInterface {
public String toString() { public String toString() {
return "BgReading{" + return "BgReading{" +
"date=" + date + "date=" + date +
", date=" + DateUtil.dateAndTimeString(date) + ", date=" + new Date(date).toLocaleString() +
", value=" + value + ", value=" + value +
", direction=" + direction + ", direction=" + direction +
", raw=" + raw + ", 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 ------------------ // ------------------ DataPointWithLabelInterface ------------------
@Override @Override
public double getX() { public double getX() {
@ -120,6 +163,7 @@ public class BgReading implements DataPointWithLabelInterface {
@Override @Override
public double getY() { public double getY() {
String units = MainApp.getConfigBuilder().getProfile().getUnits();
return valueToUnits(units); return valueToUnits(units);
} }
@ -151,6 +195,7 @@ public class BgReading implements DataPointWithLabelInterface {
@Override @Override
public int getColor() { public int getColor() {
String units = MainApp.getConfigBuilder().getProfile().getUnits();
Double lowLine = SP.getDouble("low_mark", 0d); Double lowLine = SP.getDouble("low_mark", 0d);
Double highLine = SP.getDouble("high_mark", 0d); Double highLine = SP.getDouble("high_mark", 0d);
if (lowLine < 1) { if (lowLine < 1) {
@ -159,7 +204,6 @@ public class BgReading implements DataPointWithLabelInterface {
if (highLine < 1) { if (highLine < 1) {
highLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units); highLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units);
} }
String units = MainApp.getConfigBuilder().getProfile().getUnits();
int color = MainApp.sResources.getColor(R.color.inrange); int color = MainApp.sResources.getColor(R.color.inrange);
if (isPrediction) if (isPrediction)
color = MainApp.sResources.getColor(R.color.prediction); color = MainApp.sResources.getColor(R.color.prediction);

View file

@ -320,16 +320,31 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return getDao(ProfileSwitch.class); return getDao(ProfileSwitch.class);
} }
public long roundDateToSec(long date) {
return date - date % 1000;
}
// ------------------- BgReading handling ----------------------- // ------------------- BgReading handling -----------------------
public void createIfNotExists(BgReading bgReading) { public void createIfNotExists(BgReading bgReading, String from) {
bgReading.date = bgReading.date - bgReading.date % 1000;
try { 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) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
scheduleBgChange();
} }
private static void 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)); deltaView.setText("Δ " + MainApp.sResources.getString(R.string.notavailable));
avgdeltaView.setText(""); avgdeltaView.setText("");
} }
BgReading.units = profile.getUnits();
} else { } else {
if (updating != null) if (updating != null)
updating.setVisibility(View.GONE); updating.setVisibility(View.GONE);