diff --git a/app/build.gradle b/app/build.gradle index 8d56c75c33..ea7574a3d5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -105,7 +105,7 @@ android { multiDexEnabled true versionCode 1500 // dev_version: 2.3.1-dev - version "medtronic-0.10.0-SNAPSHOT" + version "medtronic-0.10.2-SNAPSHOT" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "HEAD", '"' + generateGitBuild() + '"' diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpy.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpy.java index 3b6f88609a..aef27b961d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpy.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpy.java @@ -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); if (resp.isOK()) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java index 06b7baf45f..4ad5f0f75f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java @@ -268,4 +268,11 @@ public abstract class RileyLinkService extends Service { private boolean isLogEnabled() { return L.isEnabled(L.PUMPCOMM); } + + + public void changeRileyLinkEncoding(RileyLinkEncodingType encodingType) { + if (rfspy!=null) { + rfspy.setRileyLinkEncoding(encodingType); + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/message/GetPumpModelCarelinkMessageBody.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/message/GetPumpModelCarelinkMessageBody.java deleted file mode 100644 index 15a1fb9890..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/message/GetPumpModelCarelinkMessageBody.java +++ /dev/null @@ -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) { - - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java index d93866a7b6..d6fd32d54e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java @@ -59,18 +59,21 @@ public class MedtronicPumpStatus extends PumpStatus { // fixme public Integer tempBasalLength = 0; - String regexMac = "([\\da-fA-F]{1,2}(?:\\:|$)){6}"; - String regexSN = "[0-9]{6}"; - boolean serialChanged = false; - boolean rileyLinkAddressChanged = false; - boolean encodingChanged = false; - RileyLinkEncodingType encodingType; + + private String regexMac = "([\\da-fA-F]{1,2}(?:\\:|$)){6}"; + private String regexSN = "[0-9]{6}"; + + private boolean serialChanged = false; + private boolean rileyLinkAddressChanged = false; + private boolean encodingChanged = false; + private boolean targetFrequencyChanged = false; + + private RileyLinkEncodingType encodingType; private String[] frequencies; private boolean isFrequencyUS = false; private Map medtronicPumpMap = null; private Map medtronicDeviceTypeMap = null; private RileyLinkTargetFrequency targetFrequency; - private boolean targetFrequencyChanged = false; public BasalProfileStatus basalProfileStatus; @@ -195,7 +198,6 @@ public class MedtronicPumpStatus extends PumpStatus { this.errorDescription = MainApp.gs(R.string.medtronic_error_pump_frequency_invalid); return; } else { - // if (this.pumpFrequency == null || !this.pumpFrequency.equals(pumpFrequency)) this.pumpFrequency = pumpFrequency; this.isFrequencyUS = pumpFrequency.equals(frequencies[0]); @@ -206,7 +208,7 @@ public class MedtronicPumpStatus extends PumpStatus { if (targetFrequency != newTargetFrequency) { RileyLinkUtil.setRileyLinkTargetFrequency(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); } - //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) { 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) { - MedtronicUtil.getMedtronicService().setPumpIDString(this.serialNumber); // short operation - serialChanged = false; + if (rileyLinkAddressChanged) { + MedtronicUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet); + 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) { // RileyLinkUtil.setRileyLinkTargetFrequency(targetFrequency); @@ -283,7 +298,7 @@ public class MedtronicPumpStatus extends PumpStatus { // targetFrequencyChanged = false; // } - return (!rileyLinkAddressChanged && !serialChanged); // && !targetFrequencyChanged); + return (!rileyLinkAddressChanged && !serialChanged && !encodingChanged); // && !targetFrequencyChanged); } @@ -324,7 +339,7 @@ public class MedtronicPumpStatus extends PumpStatus { public boolean setNotInPreInit() { this.inPreInit = false; - return startService(); + return reconfigureService(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java index 7c3b871ae1..5779ecca4d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java @@ -15,6 +15,7 @@ import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; import com.squareup.otto.Subscribe; +import org.apache.commons.lang3.StringUtils; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; @@ -38,6 +39,7 @@ import info.nightscout.androidaps.events.EventReloadTreatmentData; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData; +import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; import info.nightscout.androidaps.utils.JsonHelper; @@ -251,8 +253,53 @@ public class TreatmentService extends OrmLiteBaseService { } } - // return true if new record is created + 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 { Treatment old; treatment.date = DatabaseHelper.roundDateToSec(treatment.date); @@ -379,6 +426,7 @@ public class TreatmentService extends OrmLiteBaseService { return new UpdateReturn(false, false); } + @Deprecated private boolean preserveCarbsAndSMB(Treatment treatment, Treatment existingTreatment) { if (existingTreatment.isValid) { if (existingTreatment.carbs > 0 && treatment.carbs == 0) { @@ -393,10 +441,93 @@ public class TreatmentService extends OrmLiteBaseService { 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) { log.debug("treatmentCopy [old={}, new={}]", oldTreatment.toString(), newTreatment.toString()); + + if (fromNightScout) { long pumpId_old = oldTreatment.pumpId; boolean isSMB = (oldTreatment.isSMB || newTreatment.isSMB); @@ -421,6 +552,32 @@ public class TreatmentService extends OrmLiteBaseService { } + + 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 * if multiple records with the same pump id exist.