Skip to content
This repository was archived by the owner on May 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #195 from pazaan/develop-v0.6.1
Browse files Browse the repository at this point in the history
Develop v0.6.1 to Master
  • Loading branch information
pazaan authored Feb 3, 2018
2 parents 4a727d2 + 687f53b commit 13b787f
Show file tree
Hide file tree
Showing 21 changed files with 445 additions and 239 deletions.
12 changes: 0 additions & 12 deletions app/src/main/java/info/nightscout/android/UploaderApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,6 @@ public void onCreate() {
.modules(new HistoryModule())
.deleteRealmIfMigrationNeeded()
.build();

Realm storeRealm = Realm.getInstance(storeConfiguration);
if (storeRealm.where(DataStore.class).findFirst() == null) {
storeRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.createObject(DataStore.class);
}
});
}
storeRealm.close();

}

public static long getStartupRealtime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ public void onCreate(Bundle savedInstanceState) {
historyRealm = Realm.getInstance(UploaderApplication.getHistoryConfiguration());

dataStore = storeRealm.where(DataStore.class).findFirst();
if (dataStore == null) {
storeRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
dataStore = realm.createObject(DataStore.class);
}
});
}

// limit date for NS backfill sync period, set to this init date to stop overwrite of older NS data (pref option to override)
if (dataStore.getNightscoutLimitDate() == null) {
Expand Down Expand Up @@ -548,6 +556,7 @@ public void execute(Realm realm) {
dataStore.setSysPollErrorRetry(Long.parseLong(sharedPreferences.getString("sysPollErrorRetry", "90000")));
dataStore.setSysPollOldSgvRetry(Long.parseLong(sharedPreferences.getString("sysPollOldSgvRetry", "90000")));
dataStore.setSysEnableWait500ms(sharedPreferences.getBoolean("sysEnableWait500ms", false));
dataStore.setSysEnableUsbPermissionDialog(sharedPreferences.getBoolean("sysEnableUsbPermissionDialog", false));

// debug
dataStore.setDbgEnableExtendedErrors(sharedPreferences.getBoolean("dbgEnableExtendedErrors", false));
Expand All @@ -569,6 +578,7 @@ public void execute(Realm realm) {
dataStore.setNsProfileDefault(Integer.parseInt(sharedPreferences.getString("nsProfileDefault", "1")));
dataStore.setNsActiveInsulinTime(Float.parseFloat(sharedPreferences.getString("nsActiveInsulinTime", "3")));
dataStore.setNsEnablePatternChange(sharedPreferences.getBoolean("nsEnablePatternChange", true));
dataStore.setNsGramsPerExchange(Integer.parseInt(sharedPreferences.getString("nsGramsPerExchange", "15")));
dataStore.setNsEnableInsertBGasCGM(sharedPreferences.getBoolean("nsEnableInsertBGasCGM", false));

// pattern and preset naming
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public PumpHistoryHandler(Context context) {
historyRealm = Realm.getInstance(UploaderApplication.getHistoryConfiguration());

historyDB = new ArrayList<>();
historyDB.add(new DBitem("CGM", false, 400, historyRealm.where(PumpHistoryCGM.class).findAll()));
historyDB.add(new DBitem("BOLUS", true, 30, historyRealm.where(PumpHistoryBolus.class).findAll()));
historyDB.add(new DBitem("BASAL", true,30, historyRealm.where(PumpHistoryBasal.class).findAll()));
historyDB.add(new DBitem("CGM", false, 300, historyRealm.where(PumpHistoryCGM.class).findAll()));
historyDB.add(new DBitem("BOLUS", true, 20, historyRealm.where(PumpHistoryBolus.class).findAll()));
historyDB.add(new DBitem("BASAL", true,20, historyRealm.where(PumpHistoryBasal.class).findAll()));
historyDB.add(new DBitem("PATTERN", true,10, historyRealm.where(PumpHistoryPattern.class).findAll()));
historyDB.add(new DBitem("BG", true,30, historyRealm.where(PumpHistoryBG.class).findAll()));
historyDB.add(new DBitem("BG", true,20, historyRealm.where(PumpHistoryBG.class).findAll()));
historyDB.add(new DBitem("PROFILE", false,10, historyRealm.where(PumpHistoryProfile.class).findAll()));
historyDB.add(new DBitem("MISC", true,10, historyRealm.where(PumpHistoryMisc.class).findAll()));
historyDB.add(new DBitem("LOOP", true,300, historyRealm.where(PumpHistoryLoop.class).findAll()));
historyDB.add(new DBitem("LOOP", true,200, historyRealm.where(PumpHistoryLoop.class).findAll()));
historyDB.add(new DBitem("DEBUG", true,20, historyRealm.where(PumpHistoryDebug.class).findAll()));
}

Expand Down Expand Up @@ -280,34 +280,74 @@ public void execute(Realm realm) {
if (dataStore.isNameBasalPatternChanged() &&
(dataStore.isNsEnableProfileSingle() || dataStore.isNsEnableProfileOffset())) {

userLogMessage("Basal Pattern Names changed, updating pattern switch treatments in Nightscout");

final RealmResults<PumpHistoryProfile> results = historyRealm
.where(PumpHistoryProfile.class)
.equalTo("profileSwitch", true)
final RealmResults<PumpHistoryPattern> results = historyRealm
.where(PumpHistoryPattern.class)
.findAll();
historyRealm.executeTransaction(new Realm.Transaction() {

if (results.size() > 0) {
Log.d(TAG, "NameBasalPatternChanged: Found " + results.size() + " pattern switch treatments to update");
userLogMessage("Basal Pattern Names changed, updating pattern switch treatments in Nightscout");

historyRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
for (PumpHistoryPattern record : results) {
record.setUploadREQ(true);
record.setUploadACK(true);
}
}
});

}

storeRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
for (PumpHistoryProfile record : results) {
record.setUploadREQ(true);
}
dataStore.setNameBasalPatternChanged(false);
}
});
}
}

public void checkGramsPerExchangeChanged() {

if (dataStore.isNsGramsPerExchangeChanged()) {

final RealmResults<PumpHistoryBolus> results = historyRealm
.where(PumpHistoryBolus.class)
.equalTo("programmed", true)
.equalTo("estimate", true)
.equalTo("carbUnits", PumpHistoryParser.CARB_UNITS.EXCHANGES.get())
.findAll();

if (results.size() > 0) {
Log.d(TAG, "GramsPerExchangeChanged: Found " + results.size() + " carb/bolus treatments to update");
userLogMessage("Grams per Exchange changed, updating carb/bolus treatments in Nightscout");

historyRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
for (PumpHistoryBolus record : results) {
record.setUploadREQ(true);
record.setUploadACK(true);
}
}
});
}

storeRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
dataStore.setNameBasalPatternChanged(false);
dataStore.setNsGramsPerExchangeChanged(false);
}
});
}

}

public void cgm(final PumpStatusEvent pumpRecord) {

// push the current sgv from status (always have latest sgv available even if there are comms errors after this)
if (pumpRecord.isValidSGV()) {
if (pumpRecord.isCgmActive()) {

final Date date = pumpRecord.getCgmDate();
final int rtc = pumpRecord.getCgmRTC();
Expand All @@ -317,7 +357,7 @@ public void cgm(final PumpStatusEvent pumpRecord) {
.where(PumpHistoryCGM.class)
.findAllSorted("eventDate", Sort.ASCENDING);

// sgv is available do we need the backfill?
// cgm is available do we need the backfill?
if (dataStore.isSysEnableCgmHistory()
&& results.size() == 0
|| (results.size() > 0 && date.getTime() - results.last().getEventDate().getTime() > 9 * 60 * 1000L)) {
Expand All @@ -335,6 +375,7 @@ public void execute(Realm realm) {
public void execute(Realm realm) {
PumpHistoryCGM.cgm(historyRealm, date, rtc, offset,
pumpRecord.getSgv(),
pumpRecord.getCgmExceptionType(),
pumpRecord.getCgmTrendString()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public void updateNotification(NOTIFICATION mode, long nextpoll) {
private void updateNotification() {
Log.d(TAG, "updateNotification called");

if (realm == null || storeRealm == null || historyRealm == null) {
Log.e(TAG, "unexpected null for Realm");
return;
}

long now = System.currentTimeMillis();

String poll = "";
Expand Down Expand Up @@ -150,7 +155,7 @@ private void updateNotification() {
if (sgvAge < 60 && deltaTime < 30) {
int diff = results.first().getSgv() - results.get(1).getSgv();
if (dataStore.isMmolxl())
delta += "Δ " + (diff > 0 ? "+" : "") + new BigDecimal(diff / MMOLXLFACTOR).setScale(2, BigDecimal.ROUND_HALF_UP);
delta += "Δ " + (diff > 0 ? "+" : "") + new BigDecimal(diff / MMOLXLFACTOR).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
else
delta += "Δ " + (diff > 0 ? "+" : "") + diff;
if (deltaTime > 6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ protected byte[] readFromPump(UsbHidDriver mDevice, MedtronicCnlSession pumpSess
expectedSegments = multipacketSession.packetsToFetch;
fetchMoreData = true;
} else if (MedtronicSendMessageRequestMessage.MessageType.MULTIPACKET_SEGMENT_TRANSMISSION.response(cmd)) {
if (multipacketSession == null) throw new UnexpectedMessageException("multipacketSession not initiated before segment received" + tag);
multipacketSession.addSegment(decrypted);
expectedSegments--;

Expand Down Expand Up @@ -447,23 +448,27 @@ private boolean payloadComplete() {
}

private void addSegment(byte[] data) throws UnexpectedMessageException {
int packetNumber = read16BEtoUInt(data, 0x0003);
int packetSize = data.length - 7;
segments[packetNumber] = true;

Log.d(TAG, "*** Got a Multipacket Segment: " + (packetNumber + 1) + " of " + this.packetsToFetch + ", count: " + segmentsFilled() + " [packetSize=" + packetSize + " " + this.packetSize + "/" + this.lastPacketSize + "]");

if (packetNumber == lastPacketNumber() &&
packetSize != this.lastPacketSize) {
throw new UnexpectedMessageException("Multipacket Transfer last packet size mismatch");
} else if (packetNumber != lastPacketNumber() &&
packetSize != this.packetSize) {
throw new UnexpectedMessageException("Multipacket Transfer packet size mismatch");
}
try {
int packetNumber = read16BEtoUInt(data, 0x0003);
int packetSize = data.length - 7;
segments[packetNumber] = true;

Log.d(TAG, "*** Got a Multipacket Segment: " + (packetNumber + 1) + " of " + this.packetsToFetch + ", count: " + segmentsFilled() + " [packetSize=" + packetSize + " " + this.packetSize + "/" + this.lastPacketSize + "]");

if (packetNumber == lastPacketNumber() &&
packetSize != this.lastPacketSize) {
throw new UnexpectedMessageException("Multipacket Transfer last packet size mismatch");
} else if (packetNumber != lastPacketNumber() &&
packetSize != this.packetSize) {
throw new UnexpectedMessageException("Multipacket Transfer packet size mismatch");
}

int to = (packetNumber * this.packetSize) + 1;
int from = 5;
while (from < packetSize + 5) this.response[to++] = data[from++];
int to = (packetNumber * this.packetSize) + 1;
int from = 5;
while (from < packetSize + 5) this.response[to++] = data[from++];
} catch (Exception e) {
throw new UnexpectedMessageException("Multipacket Transfer bad segment data received");
}
}

private byte[] missingSegments() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,24 @@ private void addHistoryBlock(byte[] payload) throws UnexpectedMessageException,

// Check that we have the correct number of bytes in this message
if (payload.length - HEADER_SIZE != historySizeCompressed) {
throw new UnexpectedMessageException("Unexpected update message size");
throw new UnexpectedMessageException("Unexpected message size");
}

byte[] blockPayload;

if (historyCompressed) {
blockPayload = new byte[historySizeUncompressed];

LzoAlgorithm algorithm = LzoAlgorithm.LZO1X;
LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(algorithm, null);
int lzoReturnCode = decompressor.decompress(payload, HEADER_SIZE, historySizeCompressed, blockPayload, 0, new lzo_uintp(historySizeUncompressed));
try {
LzoAlgorithm algorithm = LzoAlgorithm.LZO1X;
LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(algorithm, null);
int lzoReturnCode = decompressor.decompress(payload, HEADER_SIZE, historySizeCompressed, blockPayload, 0, new lzo_uintp(historySizeUncompressed));
if (lzoReturnCode != LzoTransformer.LZO_E_OK)
throw new UnexpectedMessageException("Error trying to decompress message (" + decompressor.toErrorString(lzoReturnCode) + ")");
} catch (Exception e) {
throw new UnexpectedMessageException("Error trying to decompress message");
}

if (lzoReturnCode != LzoTransformer.LZO_E_OK)
throw new UnexpectedMessageException("Error trying to decompress update message (" + decompressor.toErrorString(lzoReturnCode) + ")");
} else {
blockPayload = Arrays.copyOfRange(payload, HEADER_SIZE, payload.length);
}
Expand Down
Loading

0 comments on commit 13b787f

Please sign in to comment.