0.10.2-SNAPSHOT

- added functionality so that encoding can be changed on the fly
- chnaged TretmentService, rewrote method for createOrUpdate entry (fix for #125)
This commit is contained in:
Andy Rozman 2019-05-18 14:49:44 +01:00
parent fe73eba2a2
commit a897a72142
6 changed files with 205 additions and 66 deletions

View file

@ -105,7 +105,7 @@ android {
multiDexEnabled true multiDexEnabled true
versionCode 1500 versionCode 1500
// dev_version: 2.3.1-dev // dev_version: 2.3.1-dev
version "medtronic-0.10.0-SNAPSHOT" version "medtronic-0.10.2-SNAPSHOT"
buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "VERSION", '"' + version + '"'
buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"'
buildConfigField "String", "HEAD", '"' + generateGitBuild() + '"' buildConfigField "String", "HEAD", '"' + generateGitBuild() + '"'

View file

@ -393,7 +393,7 @@ public class RFSpy {
} }
private RFSpyResponse setRileyLinkEncoding(RileyLinkEncodingType encoding) { public RFSpyResponse setRileyLinkEncoding(RileyLinkEncodingType encoding) {
RFSpyResponse resp = writeToData(new SetHardwareEncoding(encoding), EXPECTED_MAX_BLUETOOTH_LATENCY_MS); RFSpyResponse resp = writeToData(new SetHardwareEncoding(encoding), EXPECTED_MAX_BLUETOOTH_LATENCY_MS);
if (resp.isOK()) { if (resp.isOK()) {

View file

@ -268,4 +268,11 @@ public abstract class RileyLinkService extends Service {
private boolean isLogEnabled() { private boolean isLogEnabled() {
return L.isEnabled(L.PUMPCOMM); return L.isEnabled(L.PUMPCOMM);
} }
public void changeRileyLinkEncoding(RileyLinkEncodingType encodingType) {
if (rfspy!=null) {
rfspy.setRileyLinkEncoding(encodingType);
}
}
} }

View file

@ -1,40 +0,0 @@
package info.nightscout.androidaps.plugins.pump.medtronic.comm.message;
/**
* Created by geoff on 5/29/16.
*/
@Deprecated
public class GetPumpModelCarelinkMessageBody extends MessageBody {
@Override
public int getLength() {
return 1;
}
@Override
public void init(byte[] rxData) {
}
public byte[] getRxData() {
return new byte[] { 0 };
}
public void setRxData(byte[] rxData) {
}
@Override
public byte[] getTxData() {
return new byte[] { 0 };
}
public void setTxData(byte[] txData) {
}
}

View file

@ -59,18 +59,21 @@ public class MedtronicPumpStatus extends PumpStatus {
// fixme // fixme
public Integer tempBasalLength = 0; public Integer tempBasalLength = 0;
String regexMac = "([\\da-fA-F]{1,2}(?:\\:|$)){6}";
String regexSN = "[0-9]{6}"; private String regexMac = "([\\da-fA-F]{1,2}(?:\\:|$)){6}";
boolean serialChanged = false; private String regexSN = "[0-9]{6}";
boolean rileyLinkAddressChanged = false;
boolean encodingChanged = false; private boolean serialChanged = false;
RileyLinkEncodingType encodingType; private boolean rileyLinkAddressChanged = false;
private boolean encodingChanged = false;
private boolean targetFrequencyChanged = false;
private RileyLinkEncodingType encodingType;
private String[] frequencies; private String[] frequencies;
private boolean isFrequencyUS = false; private boolean isFrequencyUS = false;
private Map<String, PumpType> medtronicPumpMap = null; private Map<String, PumpType> medtronicPumpMap = null;
private Map<String, MedtronicDeviceType> medtronicDeviceTypeMap = null; private Map<String, MedtronicDeviceType> medtronicDeviceTypeMap = null;
private RileyLinkTargetFrequency targetFrequency; private RileyLinkTargetFrequency targetFrequency;
private boolean targetFrequencyChanged = false;
public BasalProfileStatus basalProfileStatus; public BasalProfileStatus basalProfileStatus;
@ -195,7 +198,6 @@ public class MedtronicPumpStatus extends PumpStatus {
this.errorDescription = MainApp.gs(R.string.medtronic_error_pump_frequency_invalid); this.errorDescription = MainApp.gs(R.string.medtronic_error_pump_frequency_invalid);
return; return;
} else { } else {
// if (this.pumpFrequency == null || !this.pumpFrequency.equals(pumpFrequency))
this.pumpFrequency = pumpFrequency; this.pumpFrequency = pumpFrequency;
this.isFrequencyUS = pumpFrequency.equals(frequencies[0]); this.isFrequencyUS = pumpFrequency.equals(frequencies[0]);
@ -206,7 +208,7 @@ public class MedtronicPumpStatus extends PumpStatus {
if (targetFrequency != newTargetFrequency) { if (targetFrequency != newTargetFrequency) {
RileyLinkUtil.setRileyLinkTargetFrequency(newTargetFrequency); RileyLinkUtil.setRileyLinkTargetFrequency(newTargetFrequency);
targetFrequency = newTargetFrequency; targetFrequency = newTargetFrequency;
// targetFrequencyChanged = true; targetFrequencyChanged = true;
} }
} }
@ -248,11 +250,19 @@ public class MedtronicPumpStatus extends PumpStatus {
//LOG.debug("Max Basal from AAPS settings is " + maxBasal); //LOG.debug("Max Basal from AAPS settings is " + maxBasal);
} }
//RileyLinkEncodingType encodingType = RileyLinkEncodingType.getValueByDescription()
String encodingTypeStr = SP.getString(MedtronicConst.Prefs.Encoding, null);
RileyLinkEncodingType newEncodingType = RileyLinkEncodingType.valueOf(encodingTypeStr);
startService(); if (this.encodingType == null) {
this.encodingType = newEncodingType;
} else if (this.encodingType != newEncodingType) {
this.encodingType = newEncodingType;
this.encodingChanged = true;
}
reconfigureService();
} catch (Exception ex) { } catch (Exception ex) {
this.errorDescription = ex.getMessage(); this.errorDescription = ex.getMessage();
@ -261,21 +271,26 @@ public class MedtronicPumpStatus extends PumpStatus {
} }
private boolean startService() { private boolean reconfigureService() {
// LOG.debug("MedtronicPumpStatus::startService"); if (!inPreInit && MedtronicUtil.getMedtronicService() != null) {
boolean ready = (!inPreInit && MedtronicUtil.getMedtronicService() != null); if (serialChanged) {
MedtronicUtil.getMedtronicService().setPumpIDString(this.serialNumber); // short operation
serialChanged = false;
}
if (serialChanged && ready) { if (rileyLinkAddressChanged) {
MedtronicUtil.getMedtronicService().setPumpIDString(this.serialNumber); // short operation MedtronicUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet);
serialChanged = false; rileyLinkAddressChanged = false;
}
if (encodingChanged) {
RileyLinkUtil.getRileyLinkService().changeRileyLinkEncoding(encodingType);
encodingChanged = false;
}
} }
if (rileyLinkAddressChanged && !inPreInit && MedtronicUtil.getMedtronicService() != null) {
MedtronicUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet);
rileyLinkAddressChanged = false;
}
// if (targetFrequencyChanged && !inPreInit && MedtronicUtil.getMedtronicService() != null) { // if (targetFrequencyChanged && !inPreInit && MedtronicUtil.getMedtronicService() != null) {
// RileyLinkUtil.setRileyLinkTargetFrequency(targetFrequency); // RileyLinkUtil.setRileyLinkTargetFrequency(targetFrequency);
@ -283,7 +298,7 @@ public class MedtronicPumpStatus extends PumpStatus {
// targetFrequencyChanged = false; // targetFrequencyChanged = false;
// } // }
return (!rileyLinkAddressChanged && !serialChanged); // && !targetFrequencyChanged); return (!rileyLinkAddressChanged && !serialChanged && !encodingChanged); // && !targetFrequencyChanged);
} }
@ -324,7 +339,7 @@ public class MedtronicPumpStatus extends PumpStatus {
public boolean setNotInPreInit() { public boolean setNotInPreInit() {
this.inPreInit = false; this.inPreInit = false;
return startService(); return reconfigureService();
} }

View file

@ -15,6 +15,7 @@ import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils; import com.j256.ormlite.table.TableUtils;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -38,6 +39,7 @@ import info.nightscout.androidaps.events.EventReloadTreatmentData;
import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData;
import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil;
import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.JsonHelper;
@ -251,8 +253,53 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
} }
} }
// return true if new record is created
public UpdateReturn createOrUpdate(Treatment treatment, boolean fromNightScout) { public UpdateReturn createOrUpdate(Treatment treatment, boolean fromNightScout) {
try {
treatment.date = DatabaseHelper.roundDateToSec(treatment.date);
Treatment existingTreatment = getRecord(treatment.pumpId, treatment.date);
if (existingTreatment==null) {
getDao().create(treatment);
if (L.isEnabled(L.DATATREATMENTS))
log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
DatabaseHelper.updateEarliestDataChange(treatment.date);
scheduleTreatmentChange(treatment);
return new UpdateReturn(true, true);
} else {
if (existingTreatment.date==treatment.date) {
// we will do update only, if entry changed
if (!optionalTreatmentCopy(existingTreatment, treatment, fromNightScout)) {
return new UpdateReturn(true, false);
}
getDao().update(existingTreatment);
DatabaseHelper.updateEarliestDataChange(existingTreatment.date);
scheduleTreatmentChange(treatment);
return new UpdateReturn(true, false);
} else {
// date is different, we need to remove entry
getDao().delete(existingTreatment);
optionalTreatmentCopy(existingTreatment, treatment, fromNightScout);
getDao().create(existingTreatment);
DatabaseHelper.updateEarliestDataChange(existingTreatment.date);
scheduleTreatmentChange(treatment);
return new UpdateReturn(true, false); //updating a pump treatment with another one from the pump is not counted as clash
}
}
} catch (SQLException e) {
log.error("Unhandled exception", e);
}
return new UpdateReturn(false, false);
}
// return true if new record is created
@Deprecated
public UpdateReturn createOrUpdateOld(Treatment treatment, boolean fromNightScout) {
try { try {
Treatment old; Treatment old;
treatment.date = DatabaseHelper.roundDateToSec(treatment.date); treatment.date = DatabaseHelper.roundDateToSec(treatment.date);
@ -379,6 +426,7 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
return new UpdateReturn(false, false); return new UpdateReturn(false, false);
} }
@Deprecated
private boolean preserveCarbsAndSMB(Treatment treatment, Treatment existingTreatment) { private boolean preserveCarbsAndSMB(Treatment treatment, Treatment existingTreatment) {
if (existingTreatment.isValid) { if (existingTreatment.isValid) {
if (existingTreatment.carbs > 0 && treatment.carbs == 0) { if (existingTreatment.carbs > 0 && treatment.carbs == 0) {
@ -393,10 +441,93 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
return false; return false;
} }
private boolean optionalTreatmentCopy(Treatment oldTreatment, Treatment newTreatment, boolean fromNightScout) {
log.debug("optionalTreatmentCopy [old={}, new={}]", oldTreatment.toString(), newTreatment.toString());
boolean changed = false;
if (oldTreatment.date != newTreatment.date) {
oldTreatment.date = newTreatment.date;
changed = true;
}
if (oldTreatment.isSMB != newTreatment.isSMB) {
if (!oldTreatment.isSMB) {
oldTreatment.isSMB = newTreatment.isSMB;
changed = true;
}
}
if (!isSame(oldTreatment.carbs, newTreatment.carbs)) {
if (isSame(oldTreatment.carbs, 0.0d)) {
oldTreatment.carbs = newTreatment.carbs;
changed = true;
}
}
if (oldTreatment.mealBolus != (oldTreatment.carbs > 0)) {
oldTreatment.mealBolus = (oldTreatment.carbs > 0);
changed = true;
}
if (!isSame(oldTreatment.insulin, newTreatment.insulin)) {
if (!fromNightScout) {
oldTreatment.insulin = newTreatment.insulin;
changed = true;
}
}
if (!oldTreatment._id.equals(newTreatment._id)) {
if (StringUtils.isBlank(oldTreatment._id)) {
oldTreatment._id = newTreatment._id;
changed = true;
}
}
int source = Source.NONE;
if (oldTreatment.pumpId==0) {
if (newTreatment.pumpId > 0) {
oldTreatment.pumpId=newTreatment.pumpId;
source = Source.PUMP;
changed = true;
}
}
if (source==Source.NONE) {
if (oldTreatment.source == newTreatment.source) {
source = oldTreatment.source;
} else {
source = (oldTreatment.source == Source.NIGHTSCOUT || newTreatment.source == Source.NIGHTSCOUT) ? Source.NIGHTSCOUT : Source.USER;
}
}
if (oldTreatment.source != source) {
oldTreatment.source = source;
changed = true;
}
log.debug("optionalTreatmentCopy [changed={}, newAfterChange={}]", changed, oldTreatment.toString());
return changed;
}
public static boolean isSame(Double d1, Double d2) {
double diff = d1 - d2;
return (Math.abs(diff) <= 0.00001);
}
@Deprecated
private void treatmentCopy(Treatment oldTreatment, Treatment newTreatment, boolean fromNightScout) { private void treatmentCopy(Treatment oldTreatment, Treatment newTreatment, boolean fromNightScout) {
log.debug("treatmentCopy [old={}, new={}]", oldTreatment.toString(), newTreatment.toString()); log.debug("treatmentCopy [old={}, new={}]", oldTreatment.toString(), newTreatment.toString());
if (fromNightScout) { if (fromNightScout) {
long pumpId_old = oldTreatment.pumpId; long pumpId_old = oldTreatment.pumpId;
boolean isSMB = (oldTreatment.isSMB || newTreatment.isSMB); boolean isSMB = (oldTreatment.isSMB || newTreatment.isSMB);
@ -421,6 +552,32 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
} }
public Treatment getRecord(long pumpId, long date) {
Treatment record = null;
if (pumpId>0) {
record = getPumpRecordById(pumpId);
if (record != null) {
return record;
}
}
try {
record = getDao().queryForId(date);
} catch (SQLException ex) {
log.error("Error getting entry by id ({}", date);
}
return record;
}
/** /**
* Returns the record for the given id, null if none, throws RuntimeException * Returns the record for the given id, null if none, throws RuntimeException
* if multiple records with the same pump id exist. * if multiple records with the same pump id exist.