From 403c60931901f0d7f4453f47efb01cf2d322bca9 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 25 Apr 2020 21:52:20 +0200 Subject: [PATCH 1/5] migrate to ViewPager2 --- app/build.gradle | 1 + .../nightscout/androidaps/MainActivity.kt | 83 ++++++++---------- .../androidaps/tabs/TabPageAdapter.java | 84 ------------------- .../androidaps/utils/tabs/TabPageAdapter.kt | 24 ++++++ app/src/main/res/layout/activity_main.xml | 8 +- .../res/layout/rileylink_scan_activity.xml | 1 - 6 files changed, 64 insertions(+), 137 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/tabs/TabPageAdapter.java create mode 100644 app/src/main/java/info/nightscout/androidaps/utils/tabs/TabPageAdapter.kt diff --git a/app/build.gradle b/app/build.gradle index 1d4ff6b1a0..0867f61e70 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -123,6 +123,7 @@ tasks.matching { it instanceof Test }.all { android { compileSdkVersion 28 + ndkVersion "21.1.6352462" defaultConfig { minSdkVersion 23 diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.kt b/app/src/main/java/info/nightscout/androidaps/MainActivity.kt index 3972faefba..778d2cf985 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.kt @@ -23,10 +23,8 @@ import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AlertDialog import androidx.appcompat.widget.Toolbar import androidx.core.app.ActivityCompat -import androidx.drawerlayout.widget.DrawerLayout -import androidx.viewpager.widget.ViewPager -import com.google.android.material.navigation.NavigationView -import com.google.android.material.tabs.TabLayout +import androidx.viewpager2.widget.ViewPager2 +import com.google.android.material.tabs.TabLayoutMediator import com.joanzapata.iconify.Iconify import com.joanzapata.iconify.fonts.FontAwesomeModule import info.nightscout.androidaps.activities.NoSplashAppCompatActivity @@ -47,10 +45,10 @@ import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionChec import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin import info.nightscout.androidaps.setupwizard.SetupWizardActivity -import info.nightscout.androidaps.tabs.TabPageAdapter +import info.nightscout.androidaps.utils.tabs.TabPageAdapter import info.nightscout.androidaps.utils.AndroidPermission import info.nightscout.androidaps.utils.FabricPrivacy -import info.nightscout.androidaps.utils.LocaleHelper.update +import info.nightscout.androidaps.utils.LocaleHelper import info.nightscout.androidaps.utils.alertDialogs.OKDialog import info.nightscout.androidaps.utils.buildHelper.BuildHelper import info.nightscout.androidaps.utils.extensions.isRunningRealPumpTest @@ -86,50 +84,44 @@ class MainActivity : NoSplashAppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Iconify.with(FontAwesomeModule()) - update(applicationContext) + LocaleHelper.update(applicationContext) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) supportActionBar?.setDisplayShowTitleEnabled(false) supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setHomeButtonEnabled(true) - actionBarDrawerToggle = ActionBarDrawerToggle(this, drawer_layout, R.string.open_navigation, R.string.close_navigation).also { - drawer_layout.addDrawerListener(it) + actionBarDrawerToggle = ActionBarDrawerToggle(this, main_drawer_layout, R.string.open_navigation, R.string.close_navigation).also { + main_drawer_layout.addDrawerListener(it) it.syncState() } // initialize screen wake lock processPreferenceChange(EventPreferenceChange(resourceHelper.gs(R.string.key_keep_screen_on))) - pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { + main_pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { + override fun onPageScrollStateChanged(state: Int) {} override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {} override fun onPageSelected(position: Int) { - checkPluginPreferences(pager) + checkPluginPreferences(main_pager) } - - override fun onPageScrollStateChanged(state: Int) {} }) //Check here if loop plugin is disabled. Else check via constraints if (!loopPlugin.isEnabled(PluginType.LOOP)) versionCheckerUtils.triggerCheckVersion() fabricPrivacy.setUserStats() - setupTabs() setupViews() disposable.add(rxBus .toObservable(EventRebuildTabs::class.java) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ - update(applicationContext) if (it.recreate) recreate() - else { - setupTabs() - setupViews() - } + else setupViews() setWakeLock() - }) { fabricPrivacy.logException(it) } + }) { fabricPrivacy::logException } ) disposable.add(rxBus .toObservable(EventPreferenceChange::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ processPreferenceChange(it) }) { fabricPrivacy.logException(it) } + .subscribe({ processPreferenceChange(it) }) { fabricPrivacy::logException } ) if (!sp.getBoolean(R.string.key_setupwizard_processed, false) && !isRunningRealPumpTest()) { val intent = Intent(this, SetupWizardActivity::class.java) @@ -144,7 +136,7 @@ class MainActivity : NoSplashAppCompatActivity() { } } - private fun checkPluginPreferences(viewPager: ViewPager) { + private fun checkPluginPreferences(viewPager: ViewPager2) { pluginPreferencesMenuItem?.isEnabled = (viewPager.adapter as TabPageAdapter).getPluginAt(viewPager.currentItem).preferencesId != -1 } @@ -179,50 +171,46 @@ class MainActivity : NoSplashAppCompatActivity() { } private fun setupViews() { - val pageAdapter = TabPageAdapter(supportFragmentManager, this) - val navigationView = findViewById(R.id.navigation_view) - navigationView.setNavigationItemSelectedListener { true } - val menu = navigationView.menu.also { it.clear() } + // Menu + val pageAdapter = TabPageAdapter(this) + main_navigation_view.setNavigationItemSelectedListener { true } + val menu = main_navigation_view.menu.also { it.clear() } for (p in activePlugin.pluginsList) { pageAdapter.registerNewFragment(p) - if (p.hasFragment() && !p.isFragmentVisible() && p.isEnabled(p.pluginDescription.type) && !p.pluginDescription.neverVisible) { + if (p.isEnabled() && p.hasFragment() && !p.isFragmentVisible() && !p.pluginDescription.neverVisible) { val menuItem = menu.add(p.name) menuItem.isCheckable = true menuItem.setOnMenuItemClickListener { val intent = Intent(this, SingleFragmentActivity::class.java) intent.putExtra("plugin", activePlugin.pluginsList.indexOf(p)) startActivity(intent) - (findViewById(R.id.drawer_layout) as DrawerLayout).closeDrawers() + main_drawer_layout.closeDrawers() true } } } - val mPager = findViewById(R.id.pager) - mPager.adapter = pageAdapter - //if (switchToLast) - // mPager.setCurrentItem(pageAdapter.getCount() - 1, false); - checkPluginPreferences(mPager) - } + main_pager.adapter = pageAdapter + checkPluginPreferences(main_pager) - private fun setupTabs() { - val viewPager = findViewById(R.id.pager) - val normalTabs = findViewById(R.id.tabs_normal) - normalTabs.setupWithViewPager(viewPager, true) - val compactTabs = findViewById(R.id.tabs_compact) - compactTabs.setupWithViewPager(viewPager, true) - val toolbar = findViewById(R.id.toolbar) + // Tabs if (sp.getBoolean(R.string.key_short_tabtitles, false)) { - normalTabs.visibility = View.GONE - compactTabs.visibility = View.VISIBLE + tabs_normal.visibility = View.GONE + tabs_compact.visibility = View.VISIBLE toolbar.layoutParams = LinearLayout.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, resources.getDimension(R.dimen.compact_height).toInt()) + TabLayoutMediator(tabs_compact, main_pager) { tab, position -> + tab.text = (main_pager.adapter as TabPageAdapter).getPluginAt(position).nameShort + }.attach() } else { - normalTabs.visibility = View.VISIBLE - compactTabs.visibility = View.GONE + tabs_normal.visibility = View.VISIBLE + tabs_compact.visibility = View.GONE val typedValue = TypedValue() if (theme.resolveAttribute(R.attr.actionBarSize, typedValue, true)) { toolbar.layoutParams = LinearLayout.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, TypedValue.complexToDimensionPixelSize(typedValue.data, resources.displayMetrics)) } + TabLayoutMediator(tabs_normal, main_pager) { tab, position -> + tab.text = (main_pager.adapter as TabPageAdapter).getPluginAt(position).name + }.attach() } } @@ -260,7 +248,7 @@ class MainActivity : NoSplashAppCompatActivity() { override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu_main, menu) pluginPreferencesMenuItem = menu.findItem(R.id.nav_plugin_preferences) - checkPluginPreferences(findViewById(R.id.pager)) + checkPluginPreferences(main_pager) return true } @@ -314,8 +302,7 @@ class MainActivity : NoSplashAppCompatActivity() { } R.id.nav_plugin_preferences -> { - val viewPager = findViewById(R.id.pager) - val plugin = (viewPager.adapter as TabPageAdapter).getPluginAt(viewPager.currentItem) + val plugin = (main_pager.adapter as TabPageAdapter).getPluginAt(main_pager.currentItem) protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, Runnable { val i = Intent(this, PreferencesActivity::class.java) i.putExtra("id", plugin.preferencesId) diff --git a/app/src/main/java/info/nightscout/androidaps/tabs/TabPageAdapter.java b/app/src/main/java/info/nightscout/androidaps/tabs/TabPageAdapter.java deleted file mode 100644 index 9fee472cf0..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/tabs/TabPageAdapter.java +++ /dev/null @@ -1,84 +0,0 @@ -package info.nightscout.androidaps.tabs; - -import android.content.Context; -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentPagerAdapter; -import android.view.ViewGroup; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; - -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; -import info.nightscout.androidaps.utils.SP; - -/** - * Created by mike on 30.05.2016. - */ -public class TabPageAdapter extends FragmentPagerAdapter { - - ArrayList visibleFragmentList = new ArrayList<>(); - - Context context; - - private static Logger log = StacktraceLoggerWrapper.getLogger(L.CORE); - - public TabPageAdapter(FragmentManager fm, Context context) { - super(fm); - this.context = context; - } - - @Override - @Nullable - public Fragment getItem(int position) { - //Fragment fragment = (Fragment) visibleFragmentList.get(position); - return Fragment.instantiate(context, visibleFragmentList.get(position).getPluginDescription().getFragmentClass()); - } - - public PluginBase getPluginAt(int position) { - return visibleFragmentList.get(position); - } - - @Override - public void finishUpdate(ViewGroup container) { - try { - super.finishUpdate(container); - } catch (NullPointerException nullPointerException) { - System.out.println("Catch the NullPointerException in FragmentStatePagerAdapter.finishUpdate"); - } catch (IllegalStateException e) { - log.error("Unhandled exception", e); - } - } - - @Override - public CharSequence getPageTitle(int position) { - if (SP.getBoolean(R.string.key_short_tabtitles, false)) { - return visibleFragmentList.get(position).getNameShort(); - } - return visibleFragmentList.get(position).getName(); - - } - - @Override - public int getCount() { - return visibleFragmentList.size(); - } - - public void registerNewFragment(PluginBase plugin) { - if (plugin.hasFragment() && plugin.isFragmentVisible()) { - visibleFragmentList.add(plugin); - notifyDataSetChanged(); - } - } - - @Override - public long getItemId(int position) { - return System.identityHashCode(visibleFragmentList.get(position)); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/utils/tabs/TabPageAdapter.kt b/app/src/main/java/info/nightscout/androidaps/utils/tabs/TabPageAdapter.kt new file mode 100644 index 0000000000..983593759f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/tabs/TabPageAdapter.kt @@ -0,0 +1,24 @@ +package info.nightscout.androidaps.utils.tabs + +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment +import androidx.viewpager2.adapter.FragmentStateAdapter +import info.nightscout.androidaps.interfaces.PluginBase +import java.util.* + +class TabPageAdapter(private val activity: AppCompatActivity) : FragmentStateAdapter(activity) { + + private val visibleFragmentList = ArrayList() + + override fun getItemCount(): Int = visibleFragmentList.size + override fun createFragment(position: Int): Fragment = + activity.supportFragmentManager.fragmentFactory.instantiate(ClassLoader.getSystemClassLoader(), visibleFragmentList[position].pluginDescription.fragmentClass) + + fun getPluginAt(position: Int): PluginBase = visibleFragmentList[position] + + fun registerNewFragment(plugin: PluginBase) { + if (plugin.hasFragment() && plugin.isFragmentVisible()) { + visibleFragmentList.add(plugin) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 268dd336f3..28164e3ef3 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -36,18 +36,18 @@ - From 973403e944955107c731f23d59e1c38c38b7bb3f Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Sat, 25 Apr 2020 21:10:58 +0100 Subject: [PATCH 2/5] - changes from omnipod tree - changed PumpInterface for TimeDateOrTZChangeReceiver changes, and changes on all the pumps (method name and parameters) - some RL changes - added something to profile --- app/build.gradle | 2 +- .../info/nightscout/androidaps/MainApp.java | 1 - .../nightscout/androidaps/data/Profile.java | 35 ++++++++ .../interfaces/PumpDescription.java | 8 ++ .../androidaps/interfaces/PumpInterface.java | 8 +- .../plugins/pump/combo/ComboPlugin.java | 6 +- .../pump/common/PumpPluginAbstract.java | 40 +++++++++ .../plugins/pump/common/data/PumpStatus.java | 16 ++-- .../pump/common/data/TempBasalPair.java | 79 +++++++++++++++++ .../pump/common/defs/PumpCapability.java | 3 +- .../defs}/PumpHistoryEntryGroup.java | 4 +- .../plugins/pump/common/defs/PumpType.java | 86 ++++++++++++++++--- .../RileyLinkCommunicationManager.java | 54 ++++++++---- .../pump/common/hw/rileylink/ble/RFSpy.java | 7 ++ .../common/hw/rileylink/ble/RFSpyReader.java | 6 +- .../ble/RileyLinkCommunicationException.java | 3 + .../hw/rileylink/data/RLHistoryItem.java | 11 ++- .../dialog/RileyLinkStatusHistory.java | 9 +- .../rileylink/service/RileyLinkService.java | 3 - .../plugins/pump/common/utils/ByteUtil.java | 24 ++++++ .../pump/common/utils/DateTimeUtil.java | 25 ++++++ .../pump/common/utils/ProfileUtil.java | 54 ++++++++++++ .../plugins/pump/common/utils/StringUtil.java | 5 +- .../pump/danaR/AbstractDanaRPlugin.java | 4 +- .../plugins/pump/danaRS/DanaRSPlugin.java | 4 +- .../pump/insight/LocalInsightPlugin.java | 6 +- .../plugins/pump/mdi/MDIPlugin.java | 3 +- .../pump/medtronic/MedtronicPumpPlugin.java | 13 +-- .../comm/MedtronicCommunicationManager.java | 5 +- .../history/pump/PumpHistoryEntryType.java | 1 + .../medtronic/data/dto/TempBasalPair.java | 50 ++--------- .../dialog/MedtronicHistoryActivity.java | 3 +- .../service/RileyLinkMedtronicService.java | 2 +- .../plugins/pump/virtual/VirtualPumpPlugin.kt | 6 +- .../receivers/TimeDateOrTZChangeReceiver.kt | 59 +++++++++++-- .../nightscout/androidaps/utils/Round.java | 9 ++ .../androidaps/utils/TimeChangeType.java | 8 ++ app/src/main/res/values/strings.xml | 3 +- .../MedtronicPumpHistoryDecoderUTest.java | 1 + 39 files changed, 543 insertions(+), 123 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/TempBasalPair.java rename app/src/main/java/info/nightscout/androidaps/plugins/pump/{medtronic/comm/history/pump => common/defs}/PumpHistoryEntryGroup.java (95%) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/ProfileUtil.java create mode 100644 app/src/main/java/info/nightscout/androidaps/utils/TimeChangeType.java diff --git a/app/build.gradle b/app/build.gradle index 97459c6d22..1d4ff6b1a0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -129,7 +129,7 @@ android { targetSdkVersion 28 multiDexEnabled true versionCode 1500 - version "2.6.5-mdt-dev" + version "2.6.5-dev" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"' diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 22b278abba..d3701c94b0 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -186,7 +186,6 @@ public class MainApp extends DaggerApplication { filter = new IntentFilter(); filter.addAction(Intent.ACTION_TIME_CHANGED); - filter.addAction(Intent.ACTION_DATE_CHANGED); filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); registerReceiver(new TimeDateOrTZChangeReceiver(), filter); diff --git a/app/src/main/java/info/nightscout/androidaps/data/Profile.java b/app/src/main/java/info/nightscout/androidaps/data/Profile.java index b6f22aae07..f1204f8b72 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Profile.java +++ b/app/src/main/java/info/nightscout/androidaps/data/Profile.java @@ -29,6 +29,7 @@ import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.MidnightTime; +import info.nightscout.androidaps.utils.Round; import info.nightscout.androidaps.utils.resources.ResourceHelper; public class Profile { @@ -496,6 +497,18 @@ public class Profile { public int timeAsSeconds; public double value; + + + public boolean equals(Object otherObject) { + if (!(otherObject instanceof ProfileValue)) { + return false; + } + + ProfileValue otherProfileValue = (ProfileValue) otherObject; + + return (timeAsSeconds == otherProfileValue.timeAsSeconds) && Round.isSame(value, otherProfileValue.value); + + } } public synchronized ProfileValue[] getBasalValues() { @@ -818,4 +831,26 @@ public class Profile { } return new Profile(injector, o); } + + + public boolean areProfileBasalPatternsSame(Profile otherProfile) { + + if (!Round.isSame(this.baseBasalSum(), otherProfile.baseBasalSum())) + return false; + + ProfileValue[] basalValues = this.getBasalValues(); + ProfileValue[] otherBasalValues = otherProfile.getBasalValues(); + + if (basalValues.length != otherBasalValues.length) + return false; + + for (int i = 0; i < basalValues.length; i++) { + if (!basalValues[i].equals(otherBasalValues[i])) { + return false; + } + } + + return true; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java index e4a02883e8..0b83944d94 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java @@ -55,6 +55,8 @@ public class PumpDescription { public boolean supportsTDDs; public boolean needsManualTDDLoad; + public boolean hasFixedUnreachableAlert; + public boolean hasCustomUnreachableAlertCheck; public void resetSettings() { isBolusCapable = true; @@ -87,6 +89,9 @@ public class PumpDescription { supportsTDDs = false; needsManualTDDLoad = true; + + hasFixedUnreachableAlert = false; + hasCustomUnreachableAlertCheck = false; } public void setPumpDescription(PumpType pumpType) { @@ -134,6 +139,9 @@ public class PumpDescription { needsManualTDDLoad = pumpCapability.hasCapability(PumpCapability.ManualTDDLoad); is30minBasalRatesCapable = pumpCapability.hasCapability(PumpCapability.BasalRate30min); + + hasFixedUnreachableAlert = pumpType.getHasFixedUnreachableAlert(); + hasCustomUnreachableAlertCheck = pumpType.getHasCustomUnreachableAlertCheck(); } } diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index aab6d4089a..d85a383752 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -14,6 +14,7 @@ import info.nightscout.androidaps.plugins.common.ManufacturerType; import info.nightscout.androidaps.plugins.general.actions.defs.CustomAction; import info.nightscout.androidaps.plugins.general.actions.defs.CustomActionType; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; +import info.nightscout.androidaps.utils.TimeChangeType; /** * Created by mike on 04.06.2016. @@ -115,5 +116,10 @@ public interface PumpInterface { * This method will be called when time or Timezone changes, and pump driver can then do a specific action (for * example update clock on pump). */ - void timeDateOrTimeZoneChanged(); + void timezoneOrDSTChanged(TimeChangeType timeChangeType); + + /* Only used for pump types where hasCustomUnreachableAlertCheck=true */ + default boolean isUnreachableAlertTimeoutExceeded(long alertTimeoutMilliseconds) { + return false; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java index c4fb00cc4c..0ec21a8014 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java @@ -69,6 +69,7 @@ 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.InstanceId; +import info.nightscout.androidaps.utils.TimeChangeType; import info.nightscout.androidaps.utils.resources.ResourceHelper; import info.nightscout.androidaps.utils.sharedPreferences.SP; @@ -1404,9 +1405,8 @@ public class ComboPlugin extends PumpPluginBase implements PumpInterface, Constr } @Override - public void timeDateOrTimeZoneChanged() { - + public void timezoneOrDSTChanged(TimeChangeType changeType) { } -} \ No newline at end of file +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java index 00ecd5ef0c..ebea05f906 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java @@ -6,6 +6,7 @@ import android.content.ServiceConnection; import androidx.annotation.NonNull; +import org.jetbrains.annotations.NotNull; import org.json.JSONException; import org.json.JSONObject; @@ -20,6 +21,7 @@ import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.events.EventAppExit; +import info.nightscout.androidaps.events.EventCustomActionsChanged; import info.nightscout.androidaps.interfaces.ActivePluginProvider; import info.nightscout.androidaps.interfaces.CommandQueueProvider; import info.nightscout.androidaps.interfaces.ConstraintsInterface; @@ -30,6 +32,7 @@ import info.nightscout.androidaps.interfaces.PumpPluginBase; import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.bus.RxBusWrapper; +import info.nightscout.androidaps.plugins.common.ManufacturerType; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.pump.common.data.PumpStatus; import info.nightscout.androidaps.plugins.pump.common.defs.PumpDriverState; @@ -40,6 +43,7 @@ import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.resources.ResourceHelper; +import info.nightscout.androidaps.utils.sharedPreferences.SP; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.schedulers.Schedulers; @@ -57,6 +61,7 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI protected ActivePluginProvider activePlugin; protected Context context; protected FabricPrivacy fabricPrivacy; + protected SP sp; /* protected static final PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult().success(false) @@ -71,6 +76,7 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI // protected boolean isInitialized = false; protected PumpDriverState pumpState = PumpDriverState.NotInitialized; protected boolean displayConnectionMessages = false; + protected PumpType pumpType; protected PumpPluginAbstract( @@ -82,6 +88,7 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI CommandQueueProvider commandQueue, RxBusWrapper rxBus, ActivePluginProvider activePlugin, + SP sp, Context context, FabricPrivacy fabricPrivacy ) { @@ -92,8 +99,10 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI this.activePlugin = activePlugin; this.context = context; this.fabricPrivacy = fabricPrivacy; + this.sp = sp; pumpDescription.setPumpDescription(pumpType); + this.pumpType = pumpType; } @@ -435,6 +444,37 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI } + + protected void refreshCustomActionsList() { + rxBus.send(new EventCustomActionsChanged()); + } + + + public ManufacturerType manufacturer() { + return pumpType.getManufacturer() ; + } + + @NotNull + public PumpType model() { + return pumpType; + } + + + public PumpType getPumpType() { + return pumpType; + } + + + public void setPumpType(PumpType pumpType) { + this.pumpType = pumpType; + } + + + public boolean canHandleDST() { + return false; + } + + protected abstract PumpEnactResult deliverBolus(DetailedBolusInfo detailedBolusInfo); protected abstract void triggerUIChange(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.java index 11fd84a016..fee0ac914d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.java @@ -1,9 +1,9 @@ package info.nightscout.androidaps.plugins.pump.common.data; -import java.util.Date; - import org.joda.time.LocalDateTime; +import java.util.Date; + import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.plugins.pump.common.defs.PumpStatusType; @@ -19,7 +19,9 @@ public abstract class PumpStatus { public LocalDateTime lastDataTime; public long lastConnection = 0L; public long previousConnection = 0L; // here should be stored last connection of previous session (so needs to be - // read before lastConnection is modified for first time). + // read before lastConnection is modified for first time). + + public long lastErrorConnection = 0L; // last bolus public Date lastBolusTime; @@ -27,10 +29,10 @@ public abstract class PumpStatus { // other pump settings public String activeProfileName = "0"; - public double reservoirRemainingUnits = 0d; + public double reservoirRemainingUnits = 0.0d; public int reservoirFullUnits = 0; public int batteryRemaining = 0; // percent, so 0-100 - public Double batteryVoltage = null; + public Double batteryVoltage = null; // iob @@ -68,6 +70,10 @@ public abstract class PumpStatus { this.lastConnection = System.currentTimeMillis(); } + public void setLastFailedCommunicationToNow() { + this.lastErrorConnection = System.currentTimeMillis(); + } + public abstract String getErrorInfo(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/TempBasalPair.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/TempBasalPair.java new file mode 100644 index 0000000000..1e53734b5f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/TempBasalPair.java @@ -0,0 +1,79 @@ +package info.nightscout.androidaps.plugins.pump.common.data; + +import com.google.gson.annotations.Expose; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.logging.L; + +public class TempBasalPair { + + private static final Logger LOG = LoggerFactory.getLogger(L.PUMPCOMM); + + @Expose + protected double insulinRate = 0.0d; + @Expose + protected int durationMinutes = 0; + @Expose + protected boolean isPercent = false; + + private Long start; + private Long end; + + public TempBasalPair() { + } + + + public TempBasalPair(double insulinRate, boolean isPercent, int durationMinutes) { + this.insulinRate = insulinRate; + this.isPercent = isPercent; + this.durationMinutes = durationMinutes; + } + + + public double getInsulinRate() { + return insulinRate; + } + + + public void setInsulinRate(double insulinRate) { + this.insulinRate = insulinRate; + } + + + public int getDurationMinutes() { + return durationMinutes; + } + + + public void setDurationMinutes(int durationMinutes) { + this.durationMinutes = durationMinutes; + } + + + public boolean isPercent() { + return isPercent; + } + + + public void setIsPercent(boolean yesIsPercent) { + this.isPercent = yesIsPercent; + } + + public void setStartTime(Long startTime) { + this.start = startTime; + } + + + public void setEndTime(Long endTime) { + this.end = endTime; + } + + + @Override + public String toString() { + return "TempBasalPair [" + "Rate=" + insulinRate + ", DurationMinutes=" + durationMinutes + ", IsPercent=" + + isPercent + "]"; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpCapability.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpCapability.java index 074670f360..8917d3e834 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpCapability.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpCapability.java @@ -23,6 +23,7 @@ public enum PumpCapability { DanaWithHistoryCapabilities(Bolus, ExtendedBolus, TempBasal, BasalProfileSet, Refill, StoreCarbInfo, TDD, ManualTDDLoad), // InsightCapabilities(Bolus, ExtendedBolus, TempBasal, BasalProfileSet, Refill,TDD,BasalRate30min), // MedtronicCapabilities(Bolus, TempBasal, BasalProfileSet, Refill, TDD), // + OmnipodCapabilities(Bolus, TempBasal, BasalProfileSet, BasalRate30min), // // BasalRates (separately grouped) BasalRate_Duration15minAllowed, // @@ -69,4 +70,4 @@ public enum PumpCapability { -} \ No newline at end of file +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryGroup.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpHistoryEntryGroup.java similarity index 95% rename from app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryGroup.java rename to app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpHistoryEntryGroup.java index 33a7c89870..48d1df3b9d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryGroup.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpHistoryEntryGroup.java @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump; +package info.nightscout.androidaps.plugins.pump.common.defs; import java.util.ArrayList; import java.util.List; @@ -9,7 +9,7 @@ import info.nightscout.androidaps.R; /** * This file was taken from GGC - GNU Gluco Control (ggc.sourceforge.net), application for diabetes * management and modified/extended for AAPS. - * + *

* Author: Andy {andy.rozman@gmail.com} */ diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpType.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpType.java index 9e47bf71dd..4cd32df3f7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpType.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpType.java @@ -57,7 +57,14 @@ public enum PumpType { new DoseSettings(0.01d, 15, 24 * 60, 0.05d), // PumpTempBasalType.Percent, new DoseSettings(10, 15, 24 * 60, 0d, 250d), PumpCapability.BasalRate_Duration15and30minAllowed, // - 0.02d, 0.01d, DoseStepSize.InsightBolus, PumpCapability.InsightCapabilities), // + 0.02d, null, 0.01d, DoseStepSize.InsightBolus, PumpCapability.InsightCapabilities, false, false), // + + AccuChekSolo("Accu-Chek Solo", ManufacturerType.Roche, "Solo", 0.01d, null, // + new DoseSettings(0.01d, 15, 24 * 60, 0.05d), // + PumpTempBasalType.Percent, + new DoseSettings(10, 15, 24 * 60, 0d, 250d), PumpCapability.BasalRate_Duration15and30minAllowed, // + 0.02d, null, 0.01d, DoseStepSize.InsightBolus, PumpCapability.InsightCapabilities, false, false), // + // Animas AnimasVibe("Animas Vibe", ManufacturerType.Animas, "Vibe", 0.05d, null, // AnimasBolus? @@ -91,11 +98,17 @@ public enum PumpType { // Insulet - Insulet_Omnipod("Insulet Omnipod", ManufacturerType.Insulet, "Omnipod", 0.05d, null, // + Insulet_Omnipod("Insulet Omnipod", ManufacturerType.Insulet, "Omnipod (Eros)", 0.05d, null, // new DoseSettings(0.05d, 30, 8 * 60, 0.05d), // PumpTempBasalType.Absolute, // - new DoseSettings(0.05d, 30, 12 * 60, 0d, 30.0d), PumpCapability.BasalRate_Duration30minAllowed, // cannot exceed max basal rate 30u/hr - 0.05d, 0.05d, null, PumpCapability.VirtualPumpCapabilities), + new DoseSettings(0.05d, 30, 12 * 60, 0d, 30.0d), PumpCapability.BasalRate_Duration30minAllowed, // + 0.05d, null, 0.05d, null, PumpCapability.OmnipodCapabilities, true, true), + + Insulet_Omnipod_Dash("Insulet Omnipod Dash", ManufacturerType.Insulet, "Omnipod Dash", 0.05d, null, // + new DoseSettings(0.05d, 30, 8 * 60, 0.05d), // + PumpTempBasalType.Absolute, // + new DoseSettings(0.05d, 30, 12 * 60, 0d, 30.0d), PumpCapability.BasalRate_Duration30minAllowed, // + 0.05d, null, 0.05d, null, PumpCapability.OmnipodCapabilities, true, true), // TODO just copied OmniPod for now // Medtronic Medtronic_512_712("Medtronic 512/712", ManufacturerType.Medtronic, "512/712", 0.1d, null, // @@ -150,6 +163,8 @@ public enum PumpType { private double baseBasalStep; // private DoseStepSize baseBasalSpecialSteps; // private PumpCapability pumpCapability; + private boolean hasFixedUnreachableAlert; + private boolean hasCustomUnreachableAlertCheck; private PumpType parent; private static Map mapByDescription; @@ -184,17 +199,58 @@ public enum PumpType { parent.model = model; } - PumpType(String description, ManufacturerType manufacturer, String model, double bolusSize, DoseStepSize specialBolusSize, // + PumpType(String description, + ManufacturerType manufacturer, + String model, + double bolusSize, + DoseStepSize specialBolusSize, // DoseSettings extendedBolusSettings, // - PumpTempBasalType pumpTempBasalType, DoseSettings tbrSettings, PumpCapability specialBasalDurations, // - double baseBasalMinValue, double baseBasalStep, DoseStepSize baseBasalSpecialSteps, PumpCapability pumpCapability) { + PumpTempBasalType pumpTempBasalType, + DoseSettings tbrSettings, + PumpCapability specialBasalDurations, // + double baseBasalMinValue, + double baseBasalStep, + DoseStepSize baseBasalSpecialSteps, + PumpCapability pumpCapability) { this(description, manufacturer, model, bolusSize, specialBolusSize, extendedBolusSettings, pumpTempBasalType, tbrSettings, specialBasalDurations, baseBasalMinValue, null, baseBasalStep, baseBasalSpecialSteps, pumpCapability); } - PumpType(String description, ManufacturerType manufacturer, String model, double bolusSize, DoseStepSize specialBolusSize, // + PumpType(String description, + ManufacturerType manufacturer, + String model, + double bolusSize, + DoseStepSize specialBolusSize, // DoseSettings extendedBolusSettings, // - PumpTempBasalType pumpTempBasalType, DoseSettings tbrSettings, PumpCapability specialBasalDurations, // - double baseBasalMinValue, Double baseBasalMaxValue, double baseBasalStep, DoseStepSize baseBasalSpecialSteps, PumpCapability pumpCapability) { + PumpTempBasalType pumpTempBasalType, + DoseSettings tbrSettings, + PumpCapability specialBasalDurations, // + double baseBasalMinValue, + Double baseBasalMaxValue, + double baseBasalStep, + DoseStepSize baseBasalSpecialSteps, + PumpCapability pumpCapability) { + this(description, manufacturer, model, bolusSize, specialBolusSize, extendedBolusSettings, pumpTempBasalType, + tbrSettings, specialBasalDurations, baseBasalMinValue, null, baseBasalStep, + baseBasalSpecialSteps, pumpCapability, false, false); + } + + + PumpType(String description, + ManufacturerType manufacturer, + String model, + double bolusSize, + DoseStepSize specialBolusSize, // + DoseSettings extendedBolusSettings, // + PumpTempBasalType pumpTempBasalType, + DoseSettings tbrSettings, + PumpCapability specialBasalDurations, // + double baseBasalMinValue, + Double baseBasalMaxValue, + double baseBasalStep, + DoseStepSize baseBasalSpecialSteps, // + PumpCapability pumpCapability, + boolean hasFixedUnreachableAlert, + boolean hasCustomUnreachableAlertCheck) { this.description = description; this.manufacturer = manufacturer; this.model = model; @@ -209,9 +265,19 @@ public enum PumpType { this.baseBasalStep = baseBasalStep; this.baseBasalSpecialSteps = baseBasalSpecialSteps; this.pumpCapability = pumpCapability; + this.hasFixedUnreachableAlert = hasFixedUnreachableAlert; + this.hasCustomUnreachableAlertCheck = hasCustomUnreachableAlertCheck; } + public boolean getHasFixedUnreachableAlert() { + return hasFixedUnreachableAlert; + } + + public boolean getHasCustomUnreachableAlertCheck() { + return hasFixedUnreachableAlert; + } + public String getDescription() { return description; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkCommunicationManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkCommunicationManager.java index 5a30a75fe1..47d2181252 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkCommunicationManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkCommunicationManager.java @@ -3,8 +3,6 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import android.content.Context; - import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; import info.nightscout.androidaps.plugins.pump.common.data.PumpStatus; @@ -39,7 +37,6 @@ public abstract class RileyLinkCommunicationManager { private static final int ALLOWED_PUMP_UNREACHABLE = 10 * 60 * 1000; // 10 minutes protected final RFSpy rfspy; - protected final Context context; protected int receiverDeviceAwakeForMinutes = 1; // override this in constructor of specific implementation protected String receiverDeviceID; // String representation of receiver device (ex. Pump (xxxxxx) or Pod (yyyyyy)) protected long lastGoodReceiverCommunicationTime = 0; @@ -52,8 +49,7 @@ public abstract class RileyLinkCommunicationManager { private int timeoutCount = 0; - public RileyLinkCommunicationManager(Context context, RFSpy rfspy) { - this.context = context; + public RileyLinkCommunicationManager(RFSpy rfspy) { this.rfspy = rfspy; this.rileyLinkServiceData = RileyLinkUtil.getRileyLinkServiceData(); RileyLinkUtil.setRileyLinkCommunicationManager(this); @@ -66,7 +62,23 @@ public abstract class RileyLinkCommunicationManager { // All pump communications go through this function. - protected E sendAndListen(RLMessage msg, int timeout_ms, Class clazz) + public E sendAndListen(RLMessage msg, int timeout_ms, Class clazz) + throws RileyLinkCommunicationException { + return sendAndListen(msg, timeout_ms, null, clazz); + } + + public E sendAndListen(RLMessage msg, int timeout_ms, Integer extendPreamble_ms, Class clazz) + throws RileyLinkCommunicationException { + return sendAndListen(msg, timeout_ms, 0, extendPreamble_ms, clazz); + } + + // For backward compatibility + public E sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, Integer extendPreamble_ms, Class clazz) + throws RileyLinkCommunicationException { + return sendAndListen(msg, timeout_ms, repeatCount, 0, extendPreamble_ms, clazz); + } + + public E sendAndListen(RLMessage msg, int timeout_ms, int repeatCount, int retryCount, Integer extendPreamble_ms, Class clazz) throws RileyLinkCommunicationException { if (showPumpMessages) { @@ -74,11 +86,11 @@ public abstract class RileyLinkCommunicationManager { LOG.info("Sent:" + ByteUtil.shortHexString(msg.getTxData())); } - RFSpyResponse rfSpyResponse = rfspy.transmitThenReceive(new RadioPacket(msg.getTxData()), timeout_ms); + RFSpyResponse rfSpyResponse = rfspy.transmitThenReceive(new RadioPacket(msg.getTxData()), + (byte)0, (byte)repeatCount, (byte)0, (byte)0, timeout_ms, (byte)retryCount, extendPreamble_ms); RadioResponse radioResponse = rfSpyResponse.getRadioResponse(); - - E response = createResponseMessage(rfSpyResponse.getRadioResponse().getPayload(), clazz); + E response = createResponseMessage(radioResponse.getPayload(), clazz); if (response.isValid()) { // Mark this as the last time we heard from the pump. @@ -88,14 +100,16 @@ public abstract class RileyLinkCommunicationManager { rfSpyResponse.wasTimeout(), rfSpyResponse.isUnknownCommand(), rfSpyResponse.isInvalidParam()); if (rfSpyResponse.wasTimeout()) { - timeoutCount++; + if (hasTunning()) { + timeoutCount++; - long diff = System.currentTimeMillis() - pumpStatus.lastConnection; + long diff = System.currentTimeMillis() - pumpStatus.lastConnection; - if (diff > ALLOWED_PUMP_UNREACHABLE) { - LOG.warn("We reached max time that Pump can be unreachable. Starting Tuning."); - ServiceTaskExecutor.startTask(new WakeAndTuneTask()); - timeoutCount = 0; + if (diff > ALLOWED_PUMP_UNREACHABLE) { + LOG.warn("We reached max time that Pump can be unreachable. Starting Tuning."); + ServiceTaskExecutor.startTask(new WakeAndTuneTask()); + timeoutCount = 0; + } } throw new RileyLinkCommunicationException(RileyLinkBLEError.Timeout); @@ -125,6 +139,9 @@ public abstract class RileyLinkCommunicationManager { return rfspy != null ? rfspy.notConnectedCount : 0; } + public boolean hasTunning() { + return true; + } // FIXME change wakeup @@ -405,7 +422,9 @@ public abstract class RileyLinkCommunicationManager { lastGoodReceiverCommunicationTime = System.currentTimeMillis(); SP.putLong(RileyLinkConst.Prefs.LastGoodDeviceCommunicationTime, lastGoodReceiverCommunicationTime); - pumpStatus.setLastCommunicationToNow(); + if(pumpStatus != null) { + pumpStatus.setLastCommunicationToNow(); + } } @@ -437,4 +456,7 @@ public abstract class RileyLinkCommunicationManager { return L.isEnabled(L.PUMPCOMM); } + public void setPumpStatus(PumpStatus pumpStatus) { + this.pumpStatus = pumpStatus; + } } 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 031de05e23..bb5f435f5f 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 @@ -32,6 +32,7 @@ import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; 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.util.MedtronicConst; + import info.nightscout.androidaps.utils.SP; /** @@ -116,6 +117,12 @@ public class RFSpy { } } + public boolean isRileyLinkStillAvailable() { + RileyLinkFirmwareVersion firmwareVersion = getFirmwareVersion(); + + return (firmwareVersion!= RileyLinkFirmwareVersion.UnknownVersion); + } + public RileyLinkFirmwareVersion getFirmwareVersion() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpyReader.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpyReader.java index 69b1f245fd..595da51847 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpyReader.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RFSpyReader.java @@ -64,7 +64,7 @@ public class RFSpyReader { LOG.trace(ThreadUtil.sig() + "Entering poll at t==" + SystemClock.uptimeMillis() + ", timeout is " + timeout_ms + " mDataQueue size is " + mDataQueue.size()); - if (mDataQueue.isEmpty()) + if (mDataQueue.isEmpty()) { try { // block until timeout or data available. // returns null if timeout. @@ -72,7 +72,7 @@ public class RFSpyReader { if (dataFromQueue != null) { if (isLogEnabled()) LOG.debug("Got data [" + ByteUtil.shortHexString(dataFromQueue) + "] at t==" - + SystemClock.uptimeMillis()); + + SystemClock.uptimeMillis()); } else { if (isLogEnabled()) LOG.debug("Got data [null] at t==" + SystemClock.uptimeMillis()); @@ -81,6 +81,8 @@ public class RFSpyReader { } catch (InterruptedException e) { LOG.error("poll: Interrupted waiting for data"); } + } + return null; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkCommunicationException.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkCommunicationException.java index e694609571..4a1ff7bdb0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkCommunicationException.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkCommunicationException.java @@ -27,4 +27,7 @@ public class RileyLinkCommunicationException extends Exception { // this.extendedErrorText = extendedErrorText; } + public RileyLinkBLEError getErrorCode() { + return errorCode; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/data/RLHistoryItem.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/data/RLHistoryItem.java index a887fede5c..be6cfb3e2d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/data/RLHistoryItem.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/data/RLHistoryItem.java @@ -97,7 +97,8 @@ public class RLHistoryItem { public enum RLHistoryItemSource { RileyLink("RileyLink"), // MedtronicPump("Medtronic"), // - MedtronicCommand("Medtronic"); + MedtronicCommand("Medtronic"), // + OmnipodCommand("Omnipod"); private String desc; @@ -112,4 +113,12 @@ public class RLHistoryItem { } } + public static class Comparator implements java.util.Comparator { + + @Override + public int compare(RLHistoryItem o1, RLHistoryItem o2) { + return o2.dateTime.compareTo(o1.dateTime); + } + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusHistory.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusHistory.java index d6af799550..ae32018111 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusHistory.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusHistory.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.dialog; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import android.os.Bundle; @@ -12,12 +13,15 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import org.jetbrains.annotations.NotNull; + import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.pump.common.dialog.RefreshableInterface; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.data.RLHistoryItem; import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil; import info.nightscout.androidaps.plugins.pump.medtronic.defs.PumpDeviceState; +import info.nightscout.androidaps.utils.DateUtil; /** * Created by andy on 5/19/18. @@ -83,6 +87,8 @@ public class RileyLinkStatusHistory extends Fragment implements RefreshableInter public void addItemsAndClean(List items) { this.historyList.clear(); + Collections.sort(items, new RLHistoryItem.Comparator()); + for (RLHistoryItem item : items) { if (!historyList.contains(item) && isValidItem(item)) { @@ -110,6 +116,7 @@ public class RileyLinkStatusHistory extends Fragment implements RefreshableInter } + @NotNull @Override public RecyclerViewAdapter.HistoryViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.rileylink_status_history_item, // @@ -123,7 +130,7 @@ public class RileyLinkStatusHistory extends Fragment implements RefreshableInter RLHistoryItem item = historyList.get(position); if (item != null) { - holder.timeView.setText(StringUtil.toDateTimeString(item.getDateTime())); + holder.timeView.setText(DateUtil.dateAndTimeAndSecondsString(item.getDateTime().toDateTime().getMillis())); holder.typeView.setText(item.getSource().getDesc()); holder.valueView.setText(item.getDescription()); } 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 0b697fe6eb..41c4028f96 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 @@ -48,7 +48,6 @@ public abstract class RileyLinkService extends DaggerService { @Override public void onCreate() { super.onCreate(); - //LOG.debug("onCreate"); RileyLinkUtil.setContext(this.context); RileyLinkUtil.setRileyLinkService(this); @@ -61,8 +60,6 @@ public abstract class RileyLinkService extends DaggerService { bluetoothStateReceiver = new RileyLinkBluetoothStateReceiver(); bluetoothStateReceiver.registerBroadcasts(this); - - //LOG.debug("onCreate(): It's ALIVE!"); } /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/ByteUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/ByteUtil.java index 5451689ebe..6923b42020 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/ByteUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/ByteUtil.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.pump.common.utils; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; @@ -28,6 +29,14 @@ public class ByteUtil { return (b < 0) ? b + 256 : b; } + public static byte[] getBytesFromInt16(int value) { + byte[] array = getBytesFromInt(value); + return new byte[] {array[2], array[3]}; + } + + public static byte[] getBytesFromInt(int value) { + return ByteBuffer.allocate(4).putInt(value).array(); + } /* For Reference: static void System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) */ @@ -107,6 +116,21 @@ public class ByteUtil { return rval; } + public static String shortHexStringWithoutSpaces(byte[] byteArray) { + String hexString = ""; + if (byteArray == null) { + return hexString; + } + if (byteArray.length == 0) { + return hexString; + } + for (byte b : byteArray) { + hexString = hexString + HEX_DIGITS[(b & 0xF0) >> 4]; + hexString = hexString + HEX_DIGITS[(b & 0x0F)]; + } + return hexString; + } + public static String shortHexString(List list) { byte[] abyte0 = getByteArrayFromList(list); 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 17dfd96083..7db9fcfb5a 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 @@ -120,6 +120,14 @@ public class DateTimeUtil { } + public static long toATechDate(long timeInMillis) { + GregorianCalendar gc = new GregorianCalendar(); + gc.setTimeInMillis(timeInMillis); + + return toATechDate(gc); + } + + public static boolean isSameDay(LocalDateTime ldt1, LocalDateTime ldt2) { return (ldt1.getYear() == ldt2.getYear() && // @@ -276,4 +284,21 @@ public class DateTimeUtil { } + public static long getTimeInFutureFromMinutes(long startTime, int minutes) { + return startTime + getTimeInMs(minutes); + } + + public static long getTimeInFutureFromMinutes(int minutes) { + return System.currentTimeMillis() + getTimeInMs(minutes); + } + + + public static long getTimeInMs(int minutes) { + return getTimeInS(minutes) * 1000L; + } + + public static int getTimeInS(int minutes) { + return minutes * 60; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/ProfileUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/ProfileUtil.java new file mode 100644 index 0000000000..5f1dd3c6ef --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/ProfileUtil.java @@ -0,0 +1,54 @@ +package info.nightscout.androidaps.plugins.pump.common.utils; + +import java.util.Locale; + +import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; + +public class ProfileUtil { + + + public static String getProfileDisplayable(Profile profile, PumpType pumpType) { + + StringBuilder stringBuilder = new StringBuilder(); + + for (Profile.ProfileValue basalValue : profile.getBasalValues()) { + + double basalValueValue = pumpType.determineCorrectBasalSize(basalValue.value); + + int hour = basalValue.timeAsSeconds / (60 * 60); + + stringBuilder.append((hour < 10 ? "0" : "") + hour + ":00"); + + stringBuilder.append(String.format(Locale.ENGLISH, "%.3f", basalValueValue)); + stringBuilder.append(", "); + } + if (stringBuilder.length() > 3) + return stringBuilder.toString().substring(0, stringBuilder.length() - 2); + else + return stringBuilder.toString(); + } + + public static String getBasalProfilesDisplayable(Profile.ProfileValue[] profiles, PumpType pumpType) { + + StringBuilder stringBuilder = new StringBuilder(); + + for (Profile.ProfileValue basalValue : profiles) { + + double basalValueValue = pumpType.determineCorrectBasalSize(basalValue.value); + + int hour = basalValue.timeAsSeconds / (60 * 60); + + stringBuilder.append((hour < 10 ? "0" : "") + hour + ":00"); + + stringBuilder.append(String.format(Locale.ENGLISH, "%.3f", basalValueValue)); + stringBuilder.append(", "); + } + if (stringBuilder.length() > 3) + return stringBuilder.toString().substring(0, stringBuilder.length() - 2); + else + return stringBuilder.toString(); + } + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/StringUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/StringUtil.java index ed7e4e2634..f7d86e5774 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/StringUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/StringUtil.java @@ -7,6 +7,8 @@ import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; +import info.nightscout.androidaps.utils.DateUtil; + /** * Created by geoff on 4/28/15. * modified by Andy @@ -83,7 +85,8 @@ public class StringUtil { public static String toDateTimeString(LocalDateTime localDateTime) { - return localDateTime.toString("dd.MM.yyyy HH:mm:ss"); + return DateUtil.dateAndTimeAndSecondsString(localDateTime.toDateTime().getMillis()); + //return localDateTime.toString("dd.MM.yyyy HH:mm:ss"); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/AbstractDanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/AbstractDanaRPlugin.java index b448990575..4fe2815bdc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/AbstractDanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/AbstractDanaRPlugin.java @@ -40,6 +40,7 @@ import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.Round; +import info.nightscout.androidaps.utils.TimeChangeType; import info.nightscout.androidaps.utils.resources.ResourceHelper; import info.nightscout.androidaps.utils.sharedPreferences.SP; @@ -485,8 +486,7 @@ public abstract class AbstractDanaRPlugin extends PumpPluginBase implements Pump } @Override - public void timeDateOrTimeZoneChanged() { - + public void timezoneOrDSTChanged(TimeChangeType timeChangeType) { } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.java index 13c41933f5..2227c905a9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.java @@ -61,6 +61,7 @@ import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.Round; import info.nightscout.androidaps.utils.T; +import info.nightscout.androidaps.utils.TimeChangeType; import info.nightscout.androidaps.utils.resources.ResourceHelper; import info.nightscout.androidaps.utils.sharedPreferences.SP; import io.reactivex.disposables.CompositeDisposable; @@ -826,8 +827,7 @@ public class DanaRSPlugin extends PumpPluginBase implements PumpInterface, DanaR } @Override - public void timeDateOrTimeZoneChanged() { - + public void timezoneOrDSTChanged(TimeChangeType changeType) { } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java index efc14fed0d..0b3a513bf5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java @@ -135,6 +135,7 @@ import info.nightscout.androidaps.plugins.pump.insight.utils.ParameterBlockUtil; 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.TimeChangeType; import info.nightscout.androidaps.utils.resources.ResourceHelper; import info.nightscout.androidaps.utils.sharedPreferences.SP; @@ -1684,8 +1685,7 @@ public class LocalInsightPlugin extends PumpPluginBase implements PumpInterface, } @Override - public void timeDateOrTimeZoneChanged() { - + public void timezoneOrDSTChanged(TimeChangeType changeType) { } -} \ No newline at end of file +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/mdi/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/mdi/MDIPlugin.java index d494912990..b5d7fafb4e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/mdi/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/mdi/MDIPlugin.java @@ -32,6 +32,7 @@ import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.InstanceId; +import info.nightscout.androidaps.utils.TimeChangeType; import info.nightscout.androidaps.utils.resources.ResourceHelper; @@ -289,7 +290,7 @@ public class MDIPlugin extends PumpPluginBase implements PumpInterface { } @Override - public void timeDateOrTimeZoneChanged() { + public void timezoneOrDSTChanged(TimeChangeType changeType) { } 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 19e6abebe1..523a9c5f53 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 @@ -81,6 +81,7 @@ import info.nightscout.androidaps.plugins.pump.medtronic.service.RileyLinkMedtro import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicConst; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; import info.nightscout.androidaps.utils.FabricPrivacy; +import info.nightscout.androidaps.utils.TimeChangeType; import info.nightscout.androidaps.utils.resources.ResourceHelper; import info.nightscout.androidaps.utils.sharedPreferences.SP; @@ -94,8 +95,6 @@ import static info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUt @Singleton public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInterface { - private final SP sp; - protected static MedtronicPumpPlugin plugin = null; private RileyLinkMedtronicService medtronicService; private MedtronicPumpStatus pumpStatusLocal = null; @@ -135,7 +134,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter .shortName(R.string.medtronic_name_short) // .preferencesId(R.xml.pref_medtronic).description(R.string.description_pump_medtronic), // PumpType.Medtronic_522_722, // we default to most basic model, correct model from config is loaded later - injector, resourceHelper, aapsLogger, commandQueue, rxBus, activePlugin, context, fabricPrivacy + injector, resourceHelper, aapsLogger, commandQueue, rxBus, activePlugin, sp, context, fabricPrivacy ); this.plugin = this; @@ -1569,17 +1568,13 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter } @Override - public void timeDateOrTimeZoneChanged() { + public void timezoneOrDSTChanged(TimeChangeType changeType) { - aapsLogger.warn(LTag.PUMP, getLogPrefix() + "Time, Date and/or TimeZone changed. "); + aapsLogger.warn(LTag.PUMP, getLogPrefix() + "Time or TimeZone changed. "); this.hasTimeDateOrTimeZoneChanged = true; } - private void refreshCustomActionsList() { - rxBus.send(new EventCustomActionsChanged()); - } - public void setEnableCustomAction(MedtronicCustomActionType customAction, boolean isEnabled) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java index 81d1425e91..261e53e7ab 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.pump.medtronic.comm; -import android.content.Context; import android.os.SystemClock; import org.joda.time.LocalDateTime; @@ -72,8 +71,8 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager private boolean doWakeUpBeforeCommand = true; - public MedtronicCommunicationManager(Context context, RFSpy rfspy) { - super(context, rfspy); + public MedtronicCommunicationManager(RFSpy rfspy) { + super(rfspy); medtronicCommunicationManager = this; this.medtronicConverter = new MedtronicConverter(); this.pumpHistoryDecoder = new MedtronicPumpHistoryDecoder(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryType.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryType.java index 90cb11f71f..51b8993b05 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryType.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryType.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import info.nightscout.androidaps.plugins.pump.common.defs.PumpHistoryEntryGroup; import info.nightscout.androidaps.plugins.pump.medtronic.defs.MedtronicDeviceType; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalPair.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalPair.java index 850df2ccc0..83ee3baf03 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalPair.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalPair.java @@ -1,7 +1,5 @@ package info.nightscout.androidaps.plugins.pump.medtronic.data.dto; -import com.google.gson.annotations.Expose; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,18 +17,10 @@ import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; *

* Just need a class to keep the pair together, for parcel transport. */ -public class TempBasalPair { +public class TempBasalPair extends info.nightscout.androidaps.plugins.pump.common.data.TempBasalPair { private static final Logger LOG = StacktraceLoggerWrapper.getLogger(L.PUMPCOMM); - @Expose - private double insulinRate = 0.0d; - @Expose - private int durationMinutes = 0; - @Expose - private boolean isPercent = false; - - public TempBasalPair() { } @@ -43,6 +33,8 @@ public class TempBasalPair { * @param isPercent */ public TempBasalPair(byte rateByte, int startTimeByte, boolean isPercent) { + super(); + int rateInt = ByteUtil.asUINT8(rateByte); if (isPercent) @@ -54,13 +46,6 @@ public class TempBasalPair { } - public TempBasalPair(double insulinRate, boolean isPercent, int durationMinutes) { - this.insulinRate = insulinRate; - this.isPercent = isPercent; - this.durationMinutes = durationMinutes; - } - - public TempBasalPair(byte[] response) { if (L.isEnabled(L.PUMPCOMM)) @@ -87,33 +72,8 @@ public class TempBasalPair { } - public double getInsulinRate() { - return insulinRate; - } - - - public void setInsulinRate(double insulinRate) { - this.insulinRate = insulinRate; - } - - - public int getDurationMinutes() { - return durationMinutes; - } - - - public void setDurationMinutes(int durationMinutes) { - this.durationMinutes = durationMinutes; - } - - - public boolean isPercent() { - return isPercent; - } - - - public void setIsPercent(boolean yesIsPercent) { - this.isPercent = yesIsPercent; + public TempBasalPair(double insulinRate, boolean isPercent, int durationMinutes) { + super(insulinRate, isPercent, durationMinutes); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/MedtronicHistoryActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/MedtronicHistoryActivity.java index 75577098d0..e4673fbc5d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/MedtronicHistoryActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/MedtronicHistoryActivity.java @@ -20,9 +20,10 @@ import javax.inject.Inject; import info.nightscout.androidaps.R; import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; +import info.nightscout.androidaps.plugins.pump.common.defs.PumpHistoryEntryGroup; import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.PumpHistoryEntry; -import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.PumpHistoryEntryGroup; + public class MedtronicHistoryActivity extends NoSplashAppCompatActivity { 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 255696aba4..0de5e3f355 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 @@ -102,7 +102,7 @@ public class RileyLinkMedtronicService extends RileyLinkService { RileyLinkUtil.setRileyLinkBLE(rileyLinkBLE); // init rileyLinkCommunicationManager - medtronicCommunicationManager = new MedtronicCommunicationManager(context, rfspy); + medtronicCommunicationManager = new MedtronicCommunicationManager(rfspy); aapsLogger.debug(LTag.PUMPCOMM, "RileyLinkMedtronicService newly constructed"); MedtronicUtil.setMedtronicService(this); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpPlugin.kt index 304d8177e3..4854112fc3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpPlugin.kt @@ -29,6 +29,7 @@ import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.InstanceId.instanceId +import info.nightscout.androidaps.utils.TimeChangeType import info.nightscout.androidaps.utils.extensions.plusAssign import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP @@ -409,5 +410,6 @@ class VirtualPumpPlugin @Inject constructor( pumpType = pumpTypeNew } - override fun timeDateOrTimeZoneChanged() {} -} \ No newline at end of file + override fun timezoneOrDSTChanged(timeChangeType: TimeChangeType?) {} + +} diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/TimeDateOrTZChangeReceiver.kt b/app/src/main/java/info/nightscout/androidaps/receivers/TimeDateOrTZChangeReceiver.kt index 3aa1c2122d..46cf58d99f 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/TimeDateOrTZChangeReceiver.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/TimeDateOrTZChangeReceiver.kt @@ -2,25 +2,74 @@ package info.nightscout.androidaps.receivers import android.content.Context import android.content.Intent +import com.google.gson.Gson import dagger.android.DaggerBroadcastReceiver import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.interfaces.PumpInterface import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.utils.TimeChangeType +import java.util.* import javax.inject.Inject class TimeDateOrTZChangeReceiver : DaggerBroadcastReceiver() { @Inject lateinit var aapsLogger: AAPSLogger @Inject lateinit var activePlugin: ActivePluginProvider + var gson: Gson + + private var isDST = false + + init { + isDST = calculateDST() + gson = Gson() + } + + private fun calculateDST(): Boolean { + val timeZone = TimeZone.getDefault() + val nowDate = Date() + return if (timeZone.useDaylightTime()) { + timeZone.inDaylightTime(nowDate) + } else { + false + } + } override fun onReceive(context: Context, intent: Intent) { super.onReceive(context, intent) val action = intent.action val activePump: PumpInterface = activePlugin.activePump - aapsLogger.debug(LTag.PUMP, "Date, Time and/or TimeZone changed.") - if (action != null) { - aapsLogger.debug(LTag.PUMP, "Date, Time and/or TimeZone changed. Notifying pump driver.") - activePump.timeDateOrTimeZoneChanged() + if (activePump == null) { + aapsLogger.debug(LTag.PUMP,"TimeDateOrTZChangeReceiver::Time and/or TimeZone changed. [action={}]. Pump is null, exiting.", action) + return + } + + aapsLogger.debug(LTag.PUMP,"TimeDateOrTZChangeReceiver::Date, Time and/or TimeZone changed. [action={}]", action) + aapsLogger.debug(LTag.PUMP,"TimeDateOrTZChangeReceiver::Intent::{}", gson.toJson(intent)) + + if (action == null) { + aapsLogger.error(LTag.PUMP,"TimeDateOrTZChangeReceiver::Action is null. Exiting.") + } else if (Intent.ACTION_TIMEZONE_CHANGED == action) { + aapsLogger.info(LTag.PUMP,"TimeDateOrTZChangeReceiver::Timezone changed. Notifying pump driver.") + activePump.timezoneOrDSTChanged(TimeChangeType.TimezoneChange) + } else if (Intent.ACTION_TIME_CHANGED == action) { + val currentDst = calculateDST() + if (currentDst == isDST) { + aapsLogger.info(LTag.PUMP,"TimeDateOrTZChangeReceiver::Time changed (manual). Notifying pump driver.") + activePump.timezoneOrDSTChanged(TimeChangeType.ManualTimeChange) + } else { + if (currentDst) { + aapsLogger.info(LTag.PUMP,"TimeDateOrTZChangeReceiver::DST started. Notifying pump driver.") + activePump.timezoneOrDSTChanged(TimeChangeType.DST_Started) + } else { + aapsLogger.info(LTag.PUMP,"TimeDateOrTZChangeReceiver::DST ended. Notifying pump driver.") + activePump.timezoneOrDSTChanged(TimeChangeType.DST_Ended) + } + } + isDST = currentDst + } else { + aapsLogger.error(LTag.PUMP,"TimeDateOrTZChangeReceiver::Unknown action received [name={}]. Exiting.", action) } } -} \ No newline at end of file + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/utils/Round.java b/app/src/main/java/info/nightscout/androidaps/utils/Round.java index 3e312a20a9..bc3c5d8b9c 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/Round.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/Round.java @@ -19,16 +19,25 @@ public class Round { return newCalc; } + public static Double floorTo(Double x, Double step) { if (x != 0d) { return Math.floor(x / step) * step; } return 0d; } + public static Double ceilTo(Double x, Double step) { if (x != 0d) { return Math.ceil(x / step) * step; } return 0d; } + + public static boolean isSame(Double d1, Double d2) { + double diff = d1 - d2; + + return (Math.abs(diff) <= 0.000001); + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/TimeChangeType.java b/app/src/main/java/info/nightscout/androidaps/utils/TimeChangeType.java new file mode 100644 index 0000000000..720ecc1a1b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/TimeChangeType.java @@ -0,0 +1,8 @@ +package info.nightscout.androidaps.utils; + +public enum TimeChangeType { + TimezoneChange, + DST_Started, + DST_Ended, + ManualTimeChange +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bb5868cd66..b0f6821b69 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1557,7 +1557,7 @@ Scanning Scanning finished Scanning error: %1$d - + Never Settings @@ -1710,6 +1710,7 @@ Clear started Time detection Do you want reset objective start? You may lose your progress. + Time and/or Timezone change on pump No pump selected Select units you want to display values in ns_uploadlocalprofile diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoderUTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoderUTest.java index a8a8b840a2..3233d4e1e4 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoderUTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoderUTest.java @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump; import org.junit.Before; import org.junit.Ignore; +import info.nightscout.androidaps.plugins.pump.common.defs.PumpHistoryEntryGroup; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; /** From 3bd4e4d394abc8b4b6ba9f075f65d587875ba659 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sat, 25 Apr 2020 22:31:39 +0200 Subject: [PATCH 3/5] plugin as map not set --- .../nightscout/androidaps/dependencyInjection/PluginsModule.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/PluginsModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/PluginsModule.kt index 506a37a741..2861829d7f 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/PluginsModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/PluginsModule.kt @@ -214,7 +214,8 @@ abstract class PluginsModule { @Binds @AllConfigs - @IntoSet + @IntoMap + @IntKey(265) abstract fun bindSafetyPlugin(plugin: SafetyPlugin): PluginBase @Binds From d86c5f1dd2c525f6ff8e555bf8003cb8589fb807 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 25 Apr 2020 22:59:22 +0200 Subject: [PATCH 4/5] BatteryLevel -> ChargingStateReceiver --- .../androidaps/events/EventChargingState.kt | 2 +- .../plugins/aps/loop/LoopPlugin.java | 26 ++++++++------ .../dataBroadcaster/DataBroadcastPlugin.kt | 23 ++++++------ .../plugins/general/nsclient/NSUpload.java | 8 ++--- .../wearintegration/WatchUpdaterService.java | 5 +-- .../receivers/ChargingStateReceiver.kt | 18 +++++++--- .../androidaps/receivers/KeepAliveReceiver.kt | 3 +- .../receivers/ReceiverStatusStore.kt | 3 ++ .../androidaps/utils/BatteryLevel.java | 35 ------------------- 9 files changed, 52 insertions(+), 71 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/utils/BatteryLevel.java diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventChargingState.kt b/app/src/main/java/info/nightscout/androidaps/events/EventChargingState.kt index f9ff60a71d..71c36ca387 100644 --- a/app/src/main/java/info/nightscout/androidaps/events/EventChargingState.kt +++ b/app/src/main/java/info/nightscout/androidaps/events/EventChargingState.kt @@ -1,3 +1,3 @@ package info.nightscout.androidaps.events -class EventChargingState(val isCharging: Boolean) : Event() +class EventChargingState(val isCharging: Boolean, val batterLevel: Int) : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java index 24e86220c2..fefd50f132 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java @@ -17,8 +17,6 @@ import org.jetbrains.annotations.Nullable; import org.json.JSONException; import org.json.JSONObject; -import java.util.Date; - import javax.inject.Inject; import javax.inject.Singleton; @@ -49,7 +47,6 @@ import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.AAPSLogger; -import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopSetLastRunGui; import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopUpdateGui; @@ -65,6 +62,7 @@ import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.commands.Command; +import info.nightscout.androidaps.receivers.ReceiverStatusStore; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.T; @@ -88,6 +86,8 @@ public class LoopPlugin extends PluginBase { private final VirtualPumpPlugin virtualPumpPlugin; private final Lazy actionStringHandler; private final IobCobCalculatorPlugin iobCobCalculatorPlugin; + private final ReceiverStatusStore receiverStatusStore; + private final FabricPrivacy fabricPrivacy; private CompositeDisposable disposable = new CompositeDisposable(); @@ -105,7 +105,7 @@ public class LoopPlugin extends PluginBase { public PumpEnactResult tbrSetByPump = null; public PumpEnactResult smbSetByPump = null; public String source = null; - public long lastAPSRun = DateUtil.now(); + public long lastAPSRun = DateUtil.now(); public long lastTBREnact = 0; public long lastSMBEnact = 0; public long lastTBRRequest = 0; @@ -130,7 +130,9 @@ public class LoopPlugin extends PluginBase { TreatmentsPlugin treatmentsPlugin, VirtualPumpPlugin virtualPumpPlugin, Lazy actionStringHandler, // TODO Adrian use RxBus instead of Lazy - IobCobCalculatorPlugin iobCobCalculatorPlugin + IobCobCalculatorPlugin iobCobCalculatorPlugin, + ReceiverStatusStore receiverStatusStore, + FabricPrivacy fabricPrivacy ) { super(new PluginDescription() .mainType(PluginType.LOOP) @@ -154,6 +156,8 @@ public class LoopPlugin extends PluginBase { this.virtualPumpPlugin = virtualPumpPlugin; this.actionStringHandler = actionStringHandler; this.iobCobCalculatorPlugin = iobCobCalculatorPlugin; + this.receiverStatusStore = receiverStatusStore; + this.fabricPrivacy = fabricPrivacy; loopSuspendedTill = sp.getLong("loopSuspendedTill", 0L); isSuperBolus = sp.getBoolean("isSuperBolus", false); @@ -167,7 +171,7 @@ public class LoopPlugin extends PluginBase { disposable.add(rxBus .toObservable(EventTempTargetChange.class) .observeOn(Schedulers.io()) - .subscribe(event -> invoke("EventTempTargetChange", true), exception -> FabricPrivacy.getInstance().logException(exception)) + .subscribe(event -> invoke("EventTempTargetChange", true), fabricPrivacy::logException) ); /** * This method is triggered once autosens calculation has completed, so the LoopPlugin @@ -191,7 +195,7 @@ public class LoopPlugin extends PluginBase { lastBgTriggeredRun = bgReading.date; invoke("AutosenseCalculation for " + bgReading, true); - }, exception -> FabricPrivacy.getInstance().logException(exception)) + }, fabricPrivacy::logException) ); } @@ -391,7 +395,7 @@ public class LoopPlugin extends PluginBase { lastRun.lastSMBEnact = 0; lastRun.lastSMBRequest = 0; - NSUpload.uploadDeviceStatus(this, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump()); + NSUpload.uploadDeviceStatus(this, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump(), receiverStatusStore); if (isSuspended()) { getAapsLogger().debug(LTag.APS, resourceHelper.gs(R.string.loopsuspended)); @@ -418,7 +422,7 @@ public class LoopPlugin extends PluginBase { if (resultAfterConstraints.bolusRequested) lastRun.smbSetByPump = waiting; rxBus.send(new EventLoopUpdateGui()); - FabricPrivacy.getInstance().logCustom("APSRequest"); + fabricPrivacy.logCustom("APSRequest"); applyTBRRequest(resultAfterConstraints, profile, new Callback() { @Override public void run() { @@ -516,13 +520,13 @@ public class LoopPlugin extends PluginBase { lastRun.lastTBRRequest = lastRun.lastAPSRun; lastRun.lastTBREnact = DateUtil.now(); lastRun.lastOpenModeAccept = DateUtil.now(); - NSUpload.uploadDeviceStatus(lp, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump()); + NSUpload.uploadDeviceStatus(lp, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump(), receiverStatusStore); sp.incInt(R.string.key_ObjectivesmanualEnacts); } rxBus.send(new EventAcceptOpenLoopChange()); } }); - FabricPrivacy.getInstance().logCustom("AcceptTemp"); + fabricPrivacy.logCustom("AcceptTemp"); } /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/dataBroadcaster/DataBroadcastPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/dataBroadcaster/DataBroadcastPlugin.kt index 6380bd5fcf..63a32edc41 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/dataBroadcaster/DataBroadcastPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/dataBroadcaster/DataBroadcastPlugin.kt @@ -4,7 +4,6 @@ import android.content.Context import android.content.Intent import android.content.pm.ResolveInfo import android.os.Bundle -import dagger.Lazy import dagger.android.HasAndroidInjector import info.nightscout.androidaps.Config import info.nightscout.androidaps.R @@ -30,8 +29,8 @@ import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewB import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished +import info.nightscout.androidaps.receivers.ReceiverStatusStore import info.nightscout.androidaps.services.Intents -import info.nightscout.androidaps.utils.BatteryLevel import info.nightscout.androidaps.utils.DefaultValueHelper import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.resources.ResourceHelper @@ -52,8 +51,10 @@ class DataBroadcastPlugin @Inject constructor( private val profileFunction: ProfileFunction, private val defaultValueHelper: DefaultValueHelper, private val nsDeviceStatus: NSDeviceStatus, - private val lazyLoopPlugin: Lazy, - private val activePlugin: ActivePluginProvider + private val loopPlugin: LoopPlugin, + private val activePlugin: ActivePluginProvider, + private var receiverStatusStore: ReceiverStatusStore + ) : PluginBase(PluginDescription() .mainType(PluginType.GENERAL) @@ -154,16 +155,16 @@ class DataBroadcastPlugin @Inject constructor( private fun loopStatus(bundle: Bundle) { //batteries - bundle.putInt("phoneBattery", BatteryLevel.getBatteryLevel()) + bundle.putInt("phoneBattery", receiverStatusStore.batteryLevel) bundle.putInt("rigBattery", nsDeviceStatus.uploaderStatus.replace("%", "").trim { it <= ' ' }.toInt()) - if (Config.APS && lazyLoopPlugin.get().lastRun?.lastTBREnact != 0L) { //we are AndroidAPS - bundle.putLong("suggestedTimeStamp", lazyLoopPlugin.get().lastRun?.lastAPSRun ?: -1L) - bundle.putString("suggested", lazyLoopPlugin.get().lastRun?.request?.json().toString()) - if (lazyLoopPlugin.get().lastRun?.tbrSetByPump != null && lazyLoopPlugin.get().lastRun?.tbrSetByPump?.enacted == true) { - bundle.putLong("enactedTimeStamp", lazyLoopPlugin.get().lastRun?.lastTBREnact + if (Config.APS && loopPlugin.lastRun?.lastTBREnact != 0L) { //we are AndroidAPS + bundle.putLong("suggestedTimeStamp", loopPlugin.lastRun?.lastAPSRun ?: -1L) + bundle.putString("suggested", loopPlugin.lastRun?.request?.json().toString()) + if (loopPlugin.lastRun?.tbrSetByPump != null && loopPlugin.lastRun?.tbrSetByPump?.enacted == true) { + bundle.putLong("enactedTimeStamp", loopPlugin.lastRun?.lastTBREnact ?: -1L) - bundle.putString("enacted", lazyLoopPlugin.get().lastRun?.request?.json().toString()) + bundle.putString("enacted", loopPlugin.lastRun?.request?.json().toString()) } } else { //NSClient or remote val data = NSDeviceStatus.deviceStatusOpenAPSData diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java index 44ec38041f..5b2fd496c7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java @@ -39,11 +39,9 @@ import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; import info.nightscout.androidaps.plugins.aps.loop.APSResult; import info.nightscout.androidaps.plugins.aps.loop.DeviceStatus; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.configBuilder.PluginStore; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; -import info.nightscout.androidaps.utils.BatteryLevel; +import info.nightscout.androidaps.receivers.ReceiverStatusStore; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.SP; @@ -160,7 +158,7 @@ public class NSUpload { } } - public static void uploadDeviceStatus(LoopPlugin loopPlugin, IobCobCalculatorPlugin iobCobCalculatorPlugin, ProfileFunction profileFunction, PumpInterface pumpInterface) { + public static void uploadDeviceStatus(LoopPlugin loopPlugin, IobCobCalculatorPlugin iobCobCalculatorPlugin, ProfileFunction profileFunction, PumpInterface pumpInterface, ReceiverStatusStore receiverStatusStore) { Profile profile = profileFunction.getProfile(); String profileName = profileFunction.getProfileName(); @@ -216,7 +214,7 @@ public class NSUpload { deviceStatus.pump = pumpstatus; } - int batteryLevel = BatteryLevel.getBatteryLevel(); + int batteryLevel = receiverStatusStore.getBatteryLevel(); deviceStatus.uploaderBattery = batteryLevel; deviceStatus.created_at = DateUtil.toISOString(new Date()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/wearintegration/WatchUpdaterService.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/wearintegration/WatchUpdaterService.java index 11eb0b39c8..b60dd1f1fd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/wearintegration/WatchUpdaterService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/wearintegration/WatchUpdaterService.java @@ -50,7 +50,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.utils.BatteryLevel; +import info.nightscout.androidaps.receivers.ReceiverStatusStore; import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.DefaultValueHelper; import info.nightscout.androidaps.utils.ToastUtils; @@ -71,6 +71,7 @@ public class WatchUpdaterService extends WearableListenerService implements Goog @Inject public IobCobCalculatorPlugin iobCobCalculatorPlugin; @Inject public TreatmentsPlugin treatmentsPlugin; @Inject public ActionStringHandler actionStringHandler; + @Inject ReceiverStatusStore receiverStatusStore; public static final String ACTION_RESEND = WatchUpdaterService.class.getName().concat(".Resend"); public static final String ACTION_OPEN_SETTINGS = WatchUpdaterService.class.getName().concat(".OpenSettings"); @@ -705,7 +706,7 @@ public class WatchUpdaterService extends WearableListenerService implements Goog //batteries - int phoneBattery = BatteryLevel.getBatteryLevel(); + int phoneBattery = receiverStatusStore.getBatteryLevel(); String rigBattery = nsDeviceStatus.getUploaderStatus().trim(); diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.kt b/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.kt index f6456fc749..674a84b2c8 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.kt @@ -19,16 +19,24 @@ class ChargingStateReceiver : DaggerBroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { super.onReceive(context, intent) rxBus.send(grabChargingState(context)) - aapsLogger.debug(LTag.CORE, receiverStatusStore.lastChargingEvent!!.toString()) + aapsLogger.debug(LTag.CORE, receiverStatusStore.lastChargingEvent?.toString() + ?: "Unknown charging state") } private fun grabChargingState(context: Context): EventChargingState { - val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { iFilter -> - context.registerReceiver(null, iFilter) - } + val batteryStatus = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED)) + + // Level + var batteryLevel = 0 + val level = batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1 + val scale = batteryStatus?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1 + if (level != -1 && scale != -1) + batteryLevel = (level.toFloat() / scale.toFloat() * 100.0f).toInt() + // Status val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1 val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL - return EventChargingState(isCharging).also { receiverStatusStore.lastChargingEvent = it } + + return EventChargingState(isCharging, batteryLevel).also { receiverStatusStore.lastChargingEvent = it } } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt index 521537e2d4..2c99b1e4f1 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt @@ -37,6 +37,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() { @Inject lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin @Inject lateinit var localAlertUtils: LocalAlertUtils @Inject lateinit var fabricPrivacy: FabricPrivacy + @Inject lateinit var receiverStatusStore: ReceiverStatusStore companion object { @@ -106,7 +107,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() { else if (DateUtil.isOlderThan(activePlugin.activeAPS.lastAPSRun, 5)) shouldUploadStatus = true if (DateUtil.isOlderThan(lastIobUpload, IOB_UPDATE_FREQUENCY) && shouldUploadStatus) { lastIobUpload = DateUtil.now() - NSUpload.uploadDeviceStatus(loopPlugin, iobCobCalculatorPlugin, profileFunction, activePlugin.activePump) + NSUpload.uploadDeviceStatus(loopPlugin, iobCobCalculatorPlugin, profileFunction, activePlugin.activePump, receiverStatusStore) } } diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/ReceiverStatusStore.kt b/app/src/main/java/info/nightscout/androidaps/receivers/ReceiverStatusStore.kt index a55b95f650..30090d386f 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/ReceiverStatusStore.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/ReceiverStatusStore.kt @@ -28,6 +28,9 @@ class ReceiverStatusStore @Inject constructor(val context: Context, val rxBus: R val isCharging: Boolean get() = lastChargingEvent?.isCharging ?: false + val batteryLevel: Int + get() = lastChargingEvent?.batterLevel ?: 0 + fun broadcastChargingState() { lastChargingEvent?.let { rxBus.send(it) } } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/BatteryLevel.java b/app/src/main/java/info/nightscout/androidaps/utils/BatteryLevel.java deleted file mode 100644 index b346125e6f..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/utils/BatteryLevel.java +++ /dev/null @@ -1,35 +0,0 @@ -package info.nightscout.androidaps.utils; - -import android.content.Intent; -import android.content.IntentFilter; -import android.os.BatteryManager; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; - -/** - * Created by mike on 20.02.2017. - */ - -public class BatteryLevel { - private static Logger log = StacktraceLoggerWrapper.getLogger(BatteryLevel.class); - static public int lastUploadedLevel = 0; - - static public int getBatteryLevel() { - int batteryLevel = 0; - Intent batteryIntent = MainApp.instance().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); - if (batteryIntent != null) { - int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); - int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); - if (level != -1 && scale != -1) { - batteryLevel = (int) (((float) level / (float) scale) * 100.0f); - } - } - log.debug("Battery level: " + batteryLevel); - return batteryLevel; - } - -} From 6e83f9e1533019993a38d82323a6707a14c1ddbc Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 26 Apr 2020 10:24:25 +0200 Subject: [PATCH 5/5] fix tests --- .../androidaps/interfaces/ConstraintsCheckerTest.kt | 9 ++++++--- .../androidaps/plugins/aps/loop/LoopPluginTest.kt | 9 ++++++--- .../general/nsclient/NsClientReceiverDelegateTest.kt | 8 ++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt index 1daab6553e..cd090dbde4 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt @@ -26,7 +26,9 @@ import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin import info.nightscout.androidaps.plugins.source.GlimpPlugin import info.nightscout.androidaps.plugins.treatments.TreatmentService import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.HardLimits +import info.nightscout.androidaps.utils.Profiler import info.nightscout.androidaps.utils.buildHelper.BuildHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import org.junit.Assert @@ -43,7 +45,7 @@ import java.util.* * Created by mike on 18.03.2018. */ @RunWith(PowerMockRunner::class) -@PrepareForTest(MainApp::class, ConfigBuilderPlugin::class, ConstraintChecker::class, SP::class, Context::class, OpenAPSAMAPlugin::class, OpenAPSSMBPlugin::class, TreatmentsPlugin::class, TreatmentService::class, VirtualPumpPlugin::class, DetailedBolusInfoStorage::class, GlimpPlugin::class) +@PrepareForTest(MainApp::class, ConfigBuilderPlugin::class, ConstraintChecker::class, SP::class, Context::class, OpenAPSAMAPlugin::class, OpenAPSSMBPlugin::class, TreatmentsPlugin::class, TreatmentService::class, VirtualPumpPlugin::class, DetailedBolusInfoStorage::class, GlimpPlugin::class, Profiler::class) class ConstraintsCheckerTest : TestBaseWithProfile() { @Mock lateinit var activePlugin: ActivePluginProvider @@ -55,6 +57,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { @Mock lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin @Mock lateinit var glimpPlugin: GlimpPlugin @Mock lateinit var sensitivityOref1Plugin: SensitivityOref1Plugin + @Mock lateinit var profiler: Profiler private var buildHelper = BuildHelper() lateinit var danaRPump: DanaRPump @@ -114,8 +117,8 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { danaRPlugin = DanaRPlugin(injector, aapsLogger, rxBus, context, resourceHelper, constraintChecker, treatmentsPlugin, sp, commandQueue, danaRPump) danaRSPlugin = DanaRSPlugin(injector, aapsLogger, rxBus, context, resourceHelper, constraintChecker, profileFunction, treatmentsPlugin, sp, commandQueue, danaRPump, detailedBolusInfoStorage) insightPlugin = LocalInsightPlugin(injector, aapsLogger, rxBus, resourceHelper, treatmentsPlugin, sp, commandQueue, profileFunction, context) - openAPSSMBPlugin = OpenAPSSMBPlugin(injector, aapsLogger, rxBus, constraintChecker, resourceHelper, profileFunction, context, activePlugin, treatmentsPlugin, iobCobCalculatorPlugin, hardLimits) - openAPSAMAPlugin = OpenAPSAMAPlugin(injector, aapsLogger, rxBus, constraintChecker, resourceHelper, profileFunction, context, activePlugin, treatmentsPlugin, iobCobCalculatorPlugin, hardLimits) + openAPSSMBPlugin = OpenAPSSMBPlugin(injector, aapsLogger, rxBus, constraintChecker, resourceHelper, profileFunction, context, activePlugin, treatmentsPlugin, iobCobCalculatorPlugin, hardLimits, profiler, fabricPrivacy) + openAPSAMAPlugin = OpenAPSAMAPlugin(injector, aapsLogger, rxBus, constraintChecker, resourceHelper, profileFunction, context, activePlugin, treatmentsPlugin, iobCobCalculatorPlugin, hardLimits, profiler, fabricPrivacy) safetyPlugin = SafetyPlugin(injector, aapsLogger, resourceHelper, sp, rxBus, constraintChecker, openAPSAMAPlugin, openAPSSMBPlugin, sensitivityOref1Plugin, activePlugin, hardLimits, buildHelper, treatmentsPlugin) val constraintsPluginsList = ArrayList() constraintsPluginsList.add(safetyPlugin) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt index 804f68a91f..dbfa209d11 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt @@ -10,7 +10,6 @@ import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.interfaces.CommandQueueProvider import info.nightscout.androidaps.interfaces.PluginType import info.nightscout.androidaps.interfaces.PumpDescription -import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction @@ -18,6 +17,8 @@ import info.nightscout.androidaps.plugins.general.wear.ActionStringHandler import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.receivers.ReceiverStatusStore +import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import org.junit.Assert @@ -30,7 +31,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner @RunWith(PowerMockRunner::class) -@PrepareForTest(ConstraintChecker::class, VirtualPumpPlugin::class) +@PrepareForTest(ConstraintChecker::class, VirtualPumpPlugin::class, FabricPrivacy::class, ReceiverStatusStore::class) class LoopPluginTest : TestBase() { @Mock lateinit var sp: SP @@ -45,12 +46,14 @@ class LoopPluginTest : TestBase() { @Mock lateinit var virtualPumpPlugin: VirtualPumpPlugin @Mock lateinit var actionStringHandler: Lazy @Mock lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin + @Mock lateinit var fabricPrivacy: FabricPrivacy + @Mock lateinit var receiverStatusStore: ReceiverStatusStore lateinit var loopPlugin: LoopPlugin val injector = HasAndroidInjector { AndroidInjector { } } @Before fun prepareMock() { - loopPlugin = LoopPlugin(injector, aapsLogger, rxBus, sp, constraintChecker, resourceHelper, profileFunction, context, commandQueue, activePlugin, treatmentsPlugin, virtualPumpPlugin, actionStringHandler, iobCobCalculatorPlugin) + loopPlugin = LoopPlugin(injector, aapsLogger, rxBus, sp, constraintChecker, resourceHelper, profileFunction, context, commandQueue, activePlugin, treatmentsPlugin, virtualPumpPlugin, actionStringHandler, iobCobCalculatorPlugin, receiverStatusStore, fabricPrivacy) `when`(activePlugin.getActivePump()).thenReturn(virtualPumpPlugin) } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt index 7388178df6..b0441c289f 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.kt @@ -52,14 +52,14 @@ class NsClientReceiverDelegateTest : TestBase() { @Test fun testCalculateStatusChargingState() { PowerMockito.mockStatic(SP::class.java) Mockito.`when`(sp.getBoolean(ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean())).thenReturn(false) - var ev = EventChargingState(true) + var ev = EventChargingState(true, 0) Assert.assertTrue(sut!!.calculateStatus(ev)) - ev = EventChargingState(false) + ev = EventChargingState(false, 0) Assert.assertTrue(sut!!.calculateStatus(ev)) Mockito.`when`(sp.getBoolean(ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean())).thenReturn(true) - ev = EventChargingState(true) + ev = EventChargingState(true, 0) Assert.assertTrue(sut!!.calculateStatus(ev)) - ev = EventChargingState(false) + ev = EventChargingState(false, 0) Assert.assertTrue(!sut!!.calculateStatus(ev)) }