Added processing of Prime records from Pump

- it only processes if record is less than 30 minutes old and if it was not processed already (we store last PrimeRecord)
This commit is contained in:
Andy Rozman 2019-10-17 13:23:29 +01:00
parent 5f3d503458
commit 0fcad5646e
3 changed files with 94 additions and 4 deletions

View file

@ -250,4 +250,29 @@ public class DateTimeUtil {
return minutes.getMinutes();
}
public static long getMillisFromATDWithAddedMinutes(long atd, int minutesDiff) {
GregorianCalendar oldestEntryTime = DateTimeUtil.toGregorianCalendar(atd);
oldestEntryTime.add(Calendar.MINUTE, minutesDiff);
return oldestEntryTime.getTimeInMillis();
}
public static long getATDWithAddedMinutes(long atd, int minutesDiff) {
GregorianCalendar oldestEntryTime = DateTimeUtil.toGregorianCalendar(atd);
oldestEntryTime.add(Calendar.MINUTE, minutesDiff);
return oldestEntryTime.getTimeInMillis();
}
public static long getATDWithAddedMinutes(GregorianCalendar oldestEntryTime, int minutesDiff) {
oldestEntryTime.add(Calendar.MINUTE, minutesDiff);
return toATechDate(oldestEntryTime);
}
}

View file

@ -6,6 +6,8 @@ import com.google.gson.GsonBuilder;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDateTime;
import org.joda.time.Minutes;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -20,6 +22,7 @@ import java.util.Map;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.db.DbObjectBase;
import info.nightscout.androidaps.db.ExtendedBolus;
@ -27,6 +30,7 @@ import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TDD;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil;
import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil;
@ -46,6 +50,7 @@ import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicConst;
import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil;
import info.nightscout.androidaps.plugins.treatments.Treatment;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SP;
@ -59,6 +64,7 @@ import info.nightscout.androidaps.utils.SP;
// all times that time changed (TZ, DST, etc.). Data needs to be returned in batches (time_changed batches, so that we can
// handle it. It would help to assign sort_ids to items (from oldest (1) to newest (x)
// All things marked with "TODO: Fix db code" needs to be updated in new 2.5 database code
public class MedtronicHistoryData {
@ -382,6 +388,22 @@ public class MedtronicHistoryData {
*/
public void processNewHistoryData() {
// TODO: Fix db code
// Prime (for reseting autosense)
List<PumpHistoryEntry> primeRecords = getFilteredItems(PumpHistoryEntryType.Prime);
if (isLogEnabled())
LOG.debug("ProcessHistoryData: Prime [count={}, items={}]", primeRecords.size(), gson.toJson(primeRecords));
if (isCollectionNotEmpty(primeRecords)) {
try {
processPrime(primeRecords);
} catch (Exception ex) {
LOG.error("ProcessHistoryData: Error processing Prime entries: " + ex.getMessage(), ex);
throw ex;
}
}
// TDD
List<PumpHistoryEntry> tdds = getFilteredItems(PumpHistoryEntryType.EndResultTotals, getTDDType());
@ -454,6 +476,49 @@ public class MedtronicHistoryData {
}
private void processPrime(List<PumpHistoryEntry> primeRecords) {
long maxAllowedTimeInPast = DateTimeUtil.getATDWithAddedMinutes(new GregorianCalendar(), -30);
long lastPrimeRecord = 0L;
for (PumpHistoryEntry primeRecord : primeRecords) {
if (primeRecord.atechDateTime > maxAllowedTimeInPast) {
if (lastPrimeRecord < primeRecord.atechDateTime) {
lastPrimeRecord = primeRecord.atechDateTime;
}
}
}
if (lastPrimeRecord != 0L) {
long lastPrimeFromAAPS = SP.getLong(MedtronicConst.Statistics.LastPrime, 0L);
if (lastPrimeRecord != lastPrimeFromAAPS) {
uploadCareportalEvent(DateTimeUtil.toMillisFromATD(lastPrimeRecord), CareportalEvent.SITECHANGE);
SP.putLong(MedtronicConst.Statistics.LastPrime, lastPrimeRecord);
}
}
}
private void uploadCareportalEvent(long date, String event) {
if (MainApp.getDbHelper().getCareportalEventFromTimestamp(date) != null)
return;
try {
JSONObject data = new JSONObject();
String enteredBy = SP.getString("careportal_enteredby", "");
if (!enteredBy.equals("")) data.put("enteredBy", enteredBy);
data.put("created_at", DateUtil.toISOString(date));
data.put("eventType", event);
NSUpload.uploadCareportalEntryToNS(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void processTDDs(List<PumpHistoryEntry> tddsIn) {
List<PumpHistoryEntry> tdds = filterTDDs(tddsIn);
@ -681,12 +746,11 @@ public class MedtronicHistoryData {
/**
* findDbEntry - finds Db entries in database, while theoretically this should have same dateTime they
* don't. Entry on pump is few seconds before treatment in AAPS, and on manual boluses on pump there
* is no treatment at all. For now we look fro tratment that was from 0s - 1m59s within pump entry.
* don't. Entry on pump is few seconds before treatment in AAPS, and on manual boluses on pump there
* is no treatment at all. For now we look fro tratment that was from 0s - 1m59s within pump entry.
*
* @param treatment Pump Entry
* @param treatment Pump Entry
* @param entriesFromHistory entries from history
*
* @return DbObject from AAPS (if found)
*/
private DbObjectBase findDbEntry(PumpHistoryEntry treatment, List<? extends DbObjectBase> entriesFromHistory) {

View file

@ -42,6 +42,7 @@ public class MedtronicConst {
public static final String StandardBoluses = StatsPrefix + "std_boluses_delivered";
public static final String SMBBoluses = StatsPrefix + "smb_boluses_delivered";
public static final String LastPumpHistoryEntry = StatsPrefix + "pump_history_entry";
public static final String LastPrime = StatsPrefix + "last_sent_prime";
}
}