From 2ae7d0a2928d2f3a67d0a4e1f946384f75d9e240 Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Sun, 5 May 2019 17:22:10 +0100 Subject: [PATCH] - fixed problem with TDD - added new command/action for Reset RileyLink (not working yet) - added Hardware encoding for RileyLink --- app/build.gradle | 6 +- .../androidaps/db/DatabaseHelper.java | 3 +- .../general/actions/ActionsFragment.java | 3 - .../general/actions/defs/CustomAction.java | 3 + .../pump/common/hw/rileylink/ble/RFSpy.java | 50 ++++-- .../hw/rileylink/ble/command/Reset.java | 22 +++ .../ble/command/ResetRadioConfig.java | 22 +++ .../service/tasks/ResetRileyLinkTask.java | 35 ++++ .../pump/common/utils/DateTimeUtil.java | 6 +- .../pump/medtronic/MedtronicPumpPlugin.java | 29 +++- .../pump/MedtronicPumpHistoryDecoder.java | 91 ++--------- .../medtronic/data/MedtronicHistoryData.java | 151 +++--------------- .../medtronic/data/dto/DailyTotalsDTO.java | 7 +- .../medtronic/driver/MedtronicPumpStatus.java | 4 +- .../service/RileyLinkMedtronicService.java | 5 + .../pump/medtronic/util/MedtronicConst.java | 1 + app/src/main/res/values/strings.xml | 5 +- .../history/pump/PumpHistoryEntryUTest.java | 23 +++ 18 files changed, 222 insertions(+), 244 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/command/Reset.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/command/ResetRadioConfig.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ResetRileyLinkTask.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryUTest.java diff --git a/app/build.gradle b/app/build.gradle index ddc6dfbbc4..3d21ce06a6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -101,11 +101,11 @@ android { defaultConfig { minSdkVersion 21 - targetSdkVersion 25 + targetSdkVersion 26 multiDexEnabled true versionCode 1500 // dev_version: 2.3.1-dev - version "medtronic-0.9.6-SNAPSHOT" + version "medtronic-0.9.7-SNAPSHOT" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "HEAD", '"' + generateGitBuild() + '"' @@ -261,6 +261,7 @@ dependencies { implementation "uk.com.robust-it:cloning:1.9.9" implementation 'org.mozilla:rhino:1.7.7.2' + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "com.jakewharton:butterknife:${butterknifeVersion}" annotationProcessor "com.jakewharton:butterknife-compiler:${butterknifeVersion}" @@ -282,7 +283,6 @@ dependencies { androidTestImplementation "org.mockito:mockito-core:2.8.47" androidTestImplementation "com.google.dexmaker:dexmaker:${dexmakerVersion}" androidTestImplementation "com.google.dexmaker:dexmaker-mockito:${dexmakerVersion}" - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" } task unzip(type: Copy) { diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 690bc4a0bb..d278db98a0 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -542,14 +542,13 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { GregorianCalendar gc = new GregorianCalendar(); gc.add(Calendar.DAY_OF_YEAR, (-1) * days); - Date d = new Date(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DAY_OF_MONTH), 0, 0, 0); + Date d = new Date((gc.get(Calendar.YEAR)-1900), gc.get(Calendar.MONTH), gc.get(Calendar.DAY_OF_MONTH), 0, 0, 0); try { QueryBuilder queryBuilder = getDaoTDD().queryBuilder(); queryBuilder.orderBy("date", false); Where where = queryBuilder.where(); where.ge("date", d.getTime()); - queryBuilder.limit(10L); PreparedQuery preparedQuery = queryBuilder.prepare(); tddList = getDaoTDD().query(preparedQuery); } catch (SQLException e) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.java index dc2662d883..d0ea388f22 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.java @@ -273,10 +273,8 @@ public class ActionsFragment extends SubscriberFragment implements View.OnClickL this.pumpCustomActions.put(MainApp.gs(customAction.getName()), customAction); this.pumpCustomButtons.add(btn); - } } - } @@ -292,7 +290,6 @@ public class ActionsFragment extends SubscriberFragment implements View.OnClickL } pumpCustomButtons.clear(); - pumpCustomActions.clear(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomAction.java index af1fde8c19..5e5f707928 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomAction.java @@ -24,6 +24,9 @@ public class CustomAction { this(nameResourceId, actionType, iconResourceId, true); } + public CustomAction(int nameResourceId, CustomActionType actionType, boolean enabled) { + this(nameResourceId, actionType, R.drawable.icon_actions_profileswitch, enabled); + } public CustomAction(int nameResourceId, CustomActionType actionType, int iconResourceId, boolean enabled) { this.name = nameResourceId; 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 b08e5bfd56..2723ac1817 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 @@ -7,7 +7,13 @@ import org.slf4j.LoggerFactory; import android.os.SystemClock; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.Reset; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.ResetRadioConfig; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.RileyLinkCommand; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.SendAndListen; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command.SetHardwareEncoding; @@ -23,10 +29,15 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.Rile import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkFirmwareVersion; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkTargetFrequency; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.operations.BLECommOperationResult; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkTargetDevice; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.common.utils.HexDump; import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil; import info.nightscout.androidaps.plugins.pump.common.utils.ThreadUtil; +import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; +import info.nightscout.androidaps.plugins.pump.medtronic.defs.MedtronicCustomActionType; +import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicConst; +import info.nightscout.androidaps.utils.SP; /** * Created by geoff on 5/26/16. @@ -84,6 +95,12 @@ public class RFSpy { bleVersion = getVersion(); firmwareVersion = getFirmwareVersion(); RileyLinkUtil.setFirmwareVersion(firmwareVersion); + + if (RileyLinkFirmwareVersion.isSameVersion(firmwareVersion, RileyLinkFirmwareVersion.Version2AndHigher)) { + if (RileyLinkUtil.getRileyLinkServiceData().targetDevice == RileyLinkTargetDevice.MedtronicPump) { + //MedtronicPumpPlugin.getPlugin().setEnableCustomAction(MedtronicCustomActionType.ResetRileyLink, true); + } + } } @@ -278,9 +295,6 @@ public class RFSpy { } - // FIXME: to be able to work with Omnipod we need to support preamble extensions so we should create a class for the - // SnedAndListen RL command - // To avoid snedAndListen command assembly magic public RFSpyResponse transmitThenReceive(RadioPacket pkt, byte sendChannel, byte repeatCount, byte delay_ms, byte listenChannel, int timeout_ms, byte retryCount, Integer extendPreamble_ms) { @@ -305,7 +319,7 @@ public class RFSpy { updateRegister(CC111XRegister.freq0, (byte)(value & 0xff)); updateRegister(CC111XRegister.freq1, (byte)((value >> 8) & 0xff)); updateRegister(CC111XRegister.freq2, (byte)((value >> 16) & 0xff)); - LOG.warn("Set frequency to {}", freqMHz); + LOG.info("Set frequency to {}", freqMHz); configureRadioForRegion(RileyLinkUtil.getRileyLinkTargetFrequency()); } @@ -314,8 +328,6 @@ public class RFSpy { private void configureRadioForRegion(RileyLinkTargetFrequency frequency) { // we update registers only on first run, or if region changed - if (selectedTargetFrequency == frequency) - return; switch (frequency) { case Medtronic_WorldWide: { @@ -386,11 +398,15 @@ public class RFSpy { private void setMedtronicEncoding() { - // FIXME + if (RileyLinkFirmwareVersion.isSameVersion(this.firmwareVersion, RileyLinkFirmwareVersion.Version2AndHigher)) { - // check settings if RileyLink_4b6b is enabled, and then check if we have version 2.2 or higher, if both - // are yes then we set encoding on RileyLink and set it in RileyLinkUtil. + if (SP.getString(MedtronicConst.Prefs.Encoding, "None").equals(MainApp.gs(R.string.medtronic_pump_encoding_4b6b_rileylink))) { + setSoftwareEncoding(RileyLinkEncodingType.FourByteSixByteRileyLink); + RileyLinkUtil.setEncoding(RileyLinkEncodingType.FourByteSixByteRileyLink); + } + } + LOG.debug("Set Encoding for Medtronic: " + RileyLinkUtil.getEncoding().name()); } @@ -419,4 +435,20 @@ public class RFSpy { updateRegister(CC111XRegister.mdmcfg4, (byte)(chanbw | drate_e)); } + public RFSpyResponse resetRileyLinkDevice() { + // FIXME not working correctly yet + RFSpyResponse resp = null; + try { + resp = writeToData(new Reset(), EXPECTED_MAX_BLUETOOTH_LATENCY_MS); + if (isLogEnabled()) + LOG.debug("Reset command send, response: {}", resp); + } catch (Exception e) { + e.toString(); + } + return resp; + } + + private boolean isLogEnabled() { + return L.isEnabled(L.PUMPCOMM); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/command/Reset.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/command/Reset.java new file mode 100644 index 0000000000..bd556d6695 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/command/Reset.java @@ -0,0 +1,22 @@ +package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command; + +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkCommandType; + +public class Reset extends RileyLinkCommand { + + public Reset() { + super(); + } + + + @Override + public RileyLinkCommandType getCommandType() { + return RileyLinkCommandType.Reset; + } + + + @Override + public byte[] getRaw() { + return super.getRawSimple(); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/command/ResetRadioConfig.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/command/ResetRadioConfig.java new file mode 100644 index 0000000000..c37d4da35c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/command/ResetRadioConfig.java @@ -0,0 +1,22 @@ +package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.command; + +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkCommandType; + +public class ResetRadioConfig extends RileyLinkCommand { + + public ResetRadioConfig() { + super(); + } + + + @Override + public RileyLinkCommandType getCommandType() { + return RileyLinkCommandType.ResetRadioConfig; + } + + + @Override + public byte[] getRaw() { + return super.getRawSimple(); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ResetRileyLinkTask.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ResetRileyLinkTask.java new file mode 100644 index 0000000000..cf2304e98f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ResetRileyLinkTask.java @@ -0,0 +1,35 @@ +package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks; + +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RFSpy; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport; +import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicFragment; +import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; +import info.nightscout.androidaps.plugins.pump.medtronic.service.RileyLinkMedtronicService; + +/** + * Created by geoff on 7/16/16. + */ +public class ResetRileyLinkTask extends PumpTask { + + private static final String TAG = "ResetRileyLinkTask"; + + + public ResetRileyLinkTask() { + } + + + public ResetRileyLinkTask(ServiceTransport transport) { + super(transport); + } + + + @Override + public void run() { + MedtronicFragment.refreshButtonEnabled(false); + MedtronicPumpPlugin.isBusy = true; + RileyLinkMedtronicService.getInstance().resetRileyLinkDevice(); + MedtronicPumpPlugin.isBusy = false; + MedtronicFragment.refreshButtonEnabled(true); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java index deac533ab1..8bd986f89a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java @@ -116,9 +116,9 @@ public class DateTimeUtil { long atechDateTime = 0L; - atechDateTime += date.getYear() * 10000000000L; - atechDateTime += date.getMonth() * 100000000L; - atechDateTime += date.getDay() * 1000000L; + atechDateTime += (date.getYear() + 1900) * 10000000000L; + atechDateTime += (date.getMonth() +1) * 100000000L; + atechDateTime += date.getDate() * 1000000L; atechDateTime += date.getHours() * 10000L; atechDateTime += date.getMinutes() * 100L; atechDateTime += date.getSeconds(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java index 7b98fc16f8..9f69caf873 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java @@ -48,6 +48,7 @@ import info.nightscout.androidaps.plugins.pump.common.defs.PumpDriverState; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState; +import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.ResetRileyLinkTask; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.ServiceTaskExecutor; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.WakeAndTuneTask; import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil; @@ -767,8 +768,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter long time = System.currentTimeMillis() + (bolusTime * 1000); this.busyTimestamps.add(time); - this.customActionClearBolusBlock.setEnabled(true); - refreshCustomActionsList(); + setEnableCustomAction(MedtronicCustomActionType.ClearBolusBlock, true); } return new PumpEnactResult().success(true) // @@ -1315,7 +1315,6 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter LOG.info(getLogPrefix() + "Basal Profile was set: " + response); if (response) { - // medtronicHistoryData.setBasalProfileChanged(); return new PumpEnactResult().success(true).enacted(true); } else { return new PumpEnactResult().success(response).enacted(response) // @@ -1378,14 +1377,17 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter MedtronicCustomActionType.WakeUpAndTune); private CustomAction customActionClearBolusBlock = new CustomAction( - R.string.medtronic_custom_action_clear_bolus_block, MedtronicCustomActionType.ClearBolusBlock); + R.string.medtronic_custom_action_clear_bolus_block, MedtronicCustomActionType.ClearBolusBlock, false); + + private CustomAction customActionReset = new CustomAction( + R.string.medtronic_custom_action_reset_rileylink, MedtronicCustomActionType.ResetRileyLink, false); @Override public List getCustomActions() { if (customActions == null) { - this.customActions = Arrays.asList(customActionWakeUpAndTune, customActionClearBolusBlock); + this.customActions = Arrays.asList(customActionWakeUpAndTune, customActionClearBolusBlock, customActionReset); } return this.customActions; @@ -1410,6 +1412,10 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter } break; + case ResetRileyLink: { + ServiceTaskExecutor.startTask(new ResetRileyLinkTask()); + } break; + default: break; } @@ -1421,4 +1427,17 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter MainApp.bus().post(new EventCustomActionsChanged()); } + + public void setEnableCustomAction(MedtronicCustomActionType customAction, boolean isEnabled) { + + if (customAction == MedtronicCustomActionType.ClearBolusBlock) { + this.customActionClearBolusBlock.setEnabled(isEnabled); + } else if (customAction == MedtronicCustomActionType.ResetRileyLink) { + this.customActionReset.setEnabled(isEnabled); + } + + refreshCustomActionsList(); + } + + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.java index d76c67cbec..95e330ee43 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.java @@ -357,11 +357,6 @@ public class MedtronicPumpHistoryDecoder extends MedtronicHistoryDecoder> 7; - // int bg = bitUtils.toInt(high, getUnsignedInt(entry.getHead()[0])); - // - // writeData(PumpBaseType.AdditionalData, PumpAdditionalDataType.BloodGlucose, "" + bg, entry.getATechDate()); - // } - - // masks = [ ( 0x80, 7), (0x40, 6), (0x20, 5), (0x10, 4) ] - // nibbles = [ ] - // for mask, shift in masks: - // nibbles.append( ( (year & mask) >> shift ) ) - // return nibbles - - // FIXME private void decodeChangeTime(PumpHistoryEntry entry) { if (changeTimeRecord == null) return; - // String timeChange = String.format(PumpEventType.DateTimeChanged.getValueTemplate(), - // this.changeTimeRecord.getATechDate().getDateTimeString(), entry.getATechDate().getDateTimeString()); - - // writeData(PumpBaseType.Event, PumpEventType.DateTimeChanged, timeChange, entry.getATechDate()); - entry.setDisplayableValue(entry.getDateTimeString()); this.changeTimeRecord = null; @@ -481,25 +420,11 @@ public class MedtronicPumpHistoryDecoder extends MedtronicHistoryDecoder 90) { year += 1900; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java index 3787801215..fdd1f198e8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java @@ -40,9 +40,11 @@ import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.ClockDTO; import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.DailyTotalsDTO; import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.TempBasalPair; import info.nightscout.androidaps.plugins.pump.medtronic.driver.MedtronicPumpStatus; +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.SP; //import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; @@ -57,12 +59,11 @@ public class MedtronicHistoryData { private List allHistory = null; private List newHistory = null; - // private LocalDateTime previousLastHistoryRecordTime; private Long lastHistoryRecordTime; private boolean isInit = false; private Gson gsonPretty; - private List fakeTBRs; + //private List fakeTBRs; DatabaseHelper databaseHelper = MainApp.getDbHelper(); @@ -137,10 +138,8 @@ public class MedtronicHistoryData { } else { newHistory2.add(pumpHistoryEntry); } - } } - } TBRs = preProcessTBRs(TBRs); @@ -170,13 +169,17 @@ public class MedtronicHistoryData { this.allHistory.add(pumpHistoryEntry); } - if (pumpHistoryEntry.isAfter(pheLast.atechDateTime)) { + if (pumpHistoryEntry.atechDateTime!=null && pumpHistoryEntry.isAfter(pheLast.atechDateTime)) { pheLast = pumpHistoryEntry; } } + if (pheLast==null) // if we don't have any valid record we don't do the filtering and setting + return; + this.setLastHistoryRecordTime(pheLast.atechDateTime); + SP.putLong(MedtronicConst.Statistics.LastPumpHistoryEntry, pheLast.atechDateTime); LocalDateTime dt = null; @@ -212,12 +215,10 @@ public class MedtronicHistoryData { } this.newHistory.clear(); - } public boolean hasRelevantConfigurationChanged() { - return getStateFromFilteredList( // PumpHistoryEntryType.ChangeBasalPattern, // PumpHistoryEntryType.ClearSettings, // @@ -301,7 +302,6 @@ public class MedtronicHistoryData { } return newAndAll2; - } @@ -369,16 +369,13 @@ public class MedtronicHistoryData { ClockDTO pumpTime; - public void processTDDs(List tddsIn) { + private void processTDDs(List tddsIn) { List tdds = filterTDDs(tddsIn); - // /pumpTime = MedtronicUtil.getPumpTime(); + LOG.error(getLogPrefix() + "TDDs found: {}.\n{}", tdds.size(), gsonPretty.toJson(tdds)); - LOG.error(getLogPrefix() + "TDDs found: {}. Not processed.\n{}", tdds.size(), gsonPretty.toJson(tdds)); - - // FIXME tdd - List tddsDb = MainApp.getDbHelper().getTDDs(); // .getTDDsForLastXDays(3); + List tddsDb = databaseHelper.getTDDsForLastXDays(3); for (PumpHistoryEntry tdd : tdds) { @@ -386,28 +383,27 @@ public class MedtronicHistoryData { DailyTotalsDTO totalsDTO = (DailyTotalsDTO) tdd.getDecodedData().get("Object"); - LOG.debug("DailtyTotals: {}", totalsDTO); + //LOG.debug("DailyTotals: {}", totalsDTO); if (tddDbEntry == null) { TDD tddNew = new TDD(); totalsDTO.setTDD(tddNew); - LOG.debug("TDD-Add: {}", tddNew); + LOG.debug("TDD Add: {}", tddNew); - MainApp.getDbHelper().createOrUpdateTDD(tddNew); + databaseHelper.createOrUpdateTDD(tddNew); } else { if (!totalsDTO.doesEqual(tddDbEntry)) { totalsDTO.setTDD(tddDbEntry); - LOG.debug("TDD-Edit: {}", tddDbEntry); + LOG.debug("TDD Edit: {}", tddDbEntry); - MainApp.getDbHelper().createOrUpdateTDD(tddDbEntry); + databaseHelper.createOrUpdateTDD(tddDbEntry); } } } - } @@ -673,14 +669,9 @@ public class MedtronicHistoryData { String.format("%d", temporaryBasalDb.percentRate), // temporaryBasalDb.isAbsolute ? "U/h" : "%", // temporaryBasalDb.durationInMinutes); - } - - - - // TODO needs to be implemented public void processSuspends(List treatments) { @@ -723,16 +714,9 @@ public class MedtronicHistoryData { LocalDateTime ldt = DateTimeUtil.toLocalDateTime(atechDateTime); - // LOG.debug("TOE. Time of Entry: " + atechDateTime); - // LOG.debug("TOE. Clock Pump: " + pumpTime.pumpTime.toString("HH:mm:ss")); - // LOG.debug("TOE. LocalTime: " + pumpTime.localDeviceTime.toString("HH:mm:ss")); - // LOG.debug("TOE. Difference(s): " + pumpTime.timeDifference); - ldt = ldt.plusSeconds(pumpTime.timeDifference); ldt = ldt.millisOfSecond().setCopy(000); - // LOG.debug("TOE. New Time Of Entry: " + ldt.toString("HH:mm:ss")); - LOG.debug("tryToGetByLocalTime: [TimeOfEntry={}, ClockPump={}, LocalTime={}, DifferenceSec={}, " + "NewTimeOfEntry={}, time={}", atechDateTime, pumpTime.pumpTime.toString("HH:mm:ss"), pumpTime.localDeviceTime.toString("HH:mm:ss"), pumpTime.timeDifference, ldt.toString("HH:mm:ss"), ldt @@ -747,6 +731,10 @@ public class MedtronicHistoryData { long dt = Long.MAX_VALUE; PumpHistoryEntry currentTreatment = null; + if (isCollectionEmpty(treatments)) { + return 10; // default return of 10 minutes + } + for (PumpHistoryEntry treatment : treatments) { if (treatment.atechDateTime < dt) { @@ -755,8 +743,6 @@ public class MedtronicHistoryData { } } - // LOG.debug("Oldest entry: {}, pumpTimeDifference={}", dt, this.pumpTime.timeDifference); - LocalDateTime oldestEntryTime = null; try { @@ -769,9 +755,9 @@ public class MedtronicHistoryData { } } catch (Exception ex) { LOG.error("Problem decoding date from last record: {}" + currentTreatment); + return 10; // default return of 10 minutes } - LocalDateTime now = new LocalDateTime(); Minutes minutes = Minutes.minutesBetween(oldestEntryTime, now); @@ -781,22 +767,9 @@ public class MedtronicHistoryData { this.pumpTime.timeDifference, oldestEntryTime, now, minutes.getMinutes()); return minutes.getMinutes(); - } - // private void processTreatments(List treatments) { - // - // // FIXME bolus and tbr - // - // LOG.error(getLogPrefix() + "Treatments found: {}. Not processed.\n", treatments.size()); - // - // //MainApp.getDbHelper().getTDDsForLastXDays() - // - // MedtronicHistoryData.showLogs(null, gsonInstancePretty.toJson(treatments)); - // - // } - private PumpHistoryEntryType getTDDType() { if (MedtronicUtil.getMedtronicPumpModel() == null) { @@ -823,40 +796,9 @@ public class MedtronicHistoryData { return PumpHistoryEntryType.EndResultTotals; } } - } - /* - * entryType == PumpHistoryEntryType.Bolus || // Treatments - * entryType == PumpHistoryEntryType.TempBasalRate || // - * entryType == PumpHistoryEntryType.TempBasalDuration || // - * - * entryType == PumpHistoryEntryType.Prime || // Pump Status Change - * entryType == PumpHistoryEntryType.PumpSuspend || // - * entryType == PumpHistoryEntryType.PumpResume || // - * entryType == PumpHistoryEntryType.Rewind || // - * entryType == PumpHistoryEntryType.NoDeliveryAlarm || // no delivery - * entryType == PumpHistoryEntryType.BasalProfileStart || // - * - * entryType == PumpHistoryEntryType.ChangeTime || // Time Change - * entryType == PumpHistoryEntryType.NewTimeSet || // - * - * entryType == PumpHistoryEntryType.SelectBasalProfile || // Configuration - * entryType == PumpHistoryEntryType.ClearSettings || // - * entryType == PumpHistoryEntryType.SaveSettings || // - * entryType == PumpHistoryEntryType.ChangeMaxBolus || // - * entryType == PumpHistoryEntryType.ChangeMaxBasal || // - * entryType == PumpHistoryEntryType.ChangeTempBasalType || // - * - * entryType == PumpHistoryEntryType.ChangeBasalProfile_NewProfile || // Basal profile - * - * entryType == PumpHistoryEntryType.DailyTotals512 || // Daily Totals - * entryType == PumpHistoryEntryType.DailyTotals522 || // - * entryType == PumpHistoryEntryType.DailyTotals523 || // - * entryType == PumpHistoryEntryType.EndResultTotals - */ - public boolean hasBasalProfileChanged() { List filteredItems = getFilteredItems(PumpHistoryEntryType.ChangeBasalProfile_NewProfile); @@ -864,14 +806,11 @@ public class MedtronicHistoryData { LOG.debug("hasBasalProfileChanged. Items: " + filteredItems); return (filteredItems.size() > 0); - } public void processLastBasalProfileChange(MedtronicPumpStatus mdtPumpStatus) { - // FIXME - List filteredItems = getFilteredItems(PumpHistoryEntryType.ChangeBasalProfile_NewProfile); LOG.debug("processLastBasalProfileChange. Items: " + filteredItems); @@ -897,23 +836,12 @@ public class MedtronicHistoryData { BasalProfile basalProfile = (BasalProfile) newProfile.getDecodedData().get("Object"); mdtPumpStatus.basalsByHour = basalProfile.getProfilesByHour(); } - - // boolean profileChanged = ((filteredItems.size() - basalProfileChangedInternally) > 0); - - // LOG.error("Profile changed:" + profileChanged); - - // this.basalProfileChangedInternally = 0; - - // return profileChanged; - } public boolean hasPumpTimeChanged() { - return getStateFromFilteredList(PumpHistoryEntryType.NewTimeSet, // PumpHistoryEntryType.ChangeTime); - } @@ -957,27 +885,7 @@ public class MedtronicHistoryData { private List getFilteredItems(PumpHistoryEntryType... entryTypes) { - return getFilteredItems(this.newHistory, entryTypes); - - } - - - // private List getFilteredListByPreviousLastRecord(PumpHistoryEntryType... entryTypes) { - // return getFilteredListByTime(this.previousLastHistoryRecordTime, entryTypes); - // } - - private List getFilteredListByLastRecord(PumpHistoryEntryType... entryTypes) { - return getFilteredListByTime(this.lastHistoryRecordTime, entryTypes); - } - - - private List getFilteredListByTime(Long lastRecordTime, PumpHistoryEntryType... entryTypes) { - if (lastRecordTime == null) { - return getFilteredItems(entryTypes); - } else { - return getFilteredItems(lastRecordTime, entryTypes); - } } @@ -994,14 +902,6 @@ public class MedtronicHistoryData { } - private List getFilteredItems(Long dateTime, PumpHistoryEntryType... entryTypes) { - - PumpHistoryResult phr = new PumpHistoryResult(null, dateTime); - return getFilteredItems(phr.getValidEntries(), entryTypes); - - } - - private List getFilteredItems(List inList, PumpHistoryEntryType... entryTypes) { // LOG.debug("InList: " + inList.size()); @@ -1027,7 +927,6 @@ public class MedtronicHistoryData { // LOG.debug("OutList: " + outList.size()); return outList; - } @@ -1036,14 +935,6 @@ public class MedtronicHistoryData { } - // public List getNewHistoryEntries() { - // return this.newHistory; - // } - - // public void setBasalProfileChanged() { - // this.basalProfileChangedInternally++; - // } - private String getLogPrefix() { return "MedtronicHistoryData::"; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/DailyTotalsDTO.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/DailyTotalsDTO.java index a6a2878df5..119a7d2b18 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/DailyTotalsDTO.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/DailyTotalsDTO.java @@ -108,8 +108,6 @@ public class DailyTotalsDTO { this.insulinTotal = totals; entry.addDecodedData("Totals", totals); - // entry.setDisplayableValue(getFormattedValue(totals, 3)); - } @@ -136,7 +134,6 @@ public class DailyTotalsDTO { k, j1, k1, k / 40.0d, j1 / 40.0d, k1 / 40.0d)); } - } @@ -178,8 +175,7 @@ public class DailyTotalsDTO { // Bolus=1.7[18,19], Fodd, Corr, Manual=1.7[27,28], // Num bOlus=1, food/corr, Food+corr, manual bolus=1 - LOG.debug("522: {}", toString()); - + //LOG.debug("522: {}", toString()); } @@ -207,7 +203,6 @@ public class DailyTotalsDTO { // Delivery Stats: Manual = 0.95, #Manual=5 //LOG.debug("523: {}", toString()); - } 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 60e3618d84..da1dab903f 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 @@ -198,8 +198,8 @@ public class MedtronicPumpStatus extends PumpStatus { this.isFrequencyUS = pumpFrequency.equals(frequencies[0]); RileyLinkTargetFrequency newTargetFrequency = this.isFrequencyUS ? // - RileyLinkTargetFrequency.Medtronic_US - : RileyLinkTargetFrequency.Medtronic_WorldWide; + RileyLinkTargetFrequency.Medtronic_US + : RileyLinkTargetFrequency.Medtronic_WorldWide; if (targetFrequency != newTargetFrequency) { RileyLinkUtil.setRileyLinkTargetFrequency(newTargetFrequency); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java index eee2480dd8..19b5514de6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java @@ -123,6 +123,11 @@ public class RileyLinkMedtronicService extends RileyLinkService { } + public void resetRileyLinkDevice() { + rfspy.resetRileyLinkDevice(); + } + + @Override public RileyLinkCommunicationManager getDeviceCommunicationManager() { return this.medtronicCommunicationManager; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicConst.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicConst.java index 5def148af2..63a9b440bb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicConst.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicConst.java @@ -17,6 +17,7 @@ public class MedtronicConst { public static final String MaxBolus = PrefPrefix + "max_bolus"; public static final String MaxBasal = PrefPrefix + "max_basal"; public static final String BolusDelay = PrefPrefix + "bolus_delay"; + public static final String Encoding = PrefPrefix + "encoding"; } public class Statistics { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9c01c4546d..0b03da8b1b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1354,11 +1354,12 @@ Medtronic Encoding US & Canada (916 MHz) Worldwide (868 Mhz) - Local 4b6b Encoding - RileyLink 4b6b Encoding + Software 4b6b Encoding + Hardware 4b6b Encoding RileyLink MAC Address Wake and Tune Up Clear Bolus Block + Reset RileyLink SCAN diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryUTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryUTest.java new file mode 100644 index 0000000000..e5da941848 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryUTest.java @@ -0,0 +1,23 @@ +package info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by andy on 4/9/19. + */ +public class PumpHistoryEntryUTest { + + @Test + public void checkIsAfter() { + + long dateObject = 20191010000000L; + long queryObject = 20191009000000L; + + PumpHistoryEntry phe = new PumpHistoryEntry(); + phe.atechDateTime = dateObject; + + Assert.assertTrue(phe.isAfter(queryObject)); + } + +}