diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b186abbbed..d54fefbd79 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -80,6 +80,10 @@ android:name=".plugins.DanaR.Services.ExecutionService" android:enabled="true" android:exported="false" /> + diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index faf0c259fe..eaef4449db 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -90,12 +90,17 @@ public class MainActivity extends AppCompatActivity { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String lang = SP.getString("language", "en"); LocaleHelper.setLocale(getApplicationContext(), lang); - recreate(); - try { // activity may be destroyed - setUpTabs(ev.isSwitchToLast()); - } catch (IllegalStateException e) { - e.printStackTrace(); - } + runOnUiThread(new Runnable() { + @Override + public void run() { + recreate(); + try { // activity may be destroyed + setUpTabs(ev.isSwitchToLast()); + } catch (IllegalStateException e) { + e.printStackTrace(); + } + } + }); } private void setUpTabs(boolean switchToLast) { diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index b2f46cddb8..638128af10 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -22,8 +22,8 @@ import info.nightscout.androidaps.plugins.CircadianPercentageProfile.CircadianPe import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanFragment; import info.nightscout.androidaps.plugins.Loop.LoopFragment; -import info.nightscout.androidaps.plugins.MM640g.MM640gFragment; import info.nightscout.androidaps.plugins.NSProfileViewer.NSProfileViewerFragment; import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment; import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAFragment; @@ -70,7 +70,7 @@ public class MainApp extends Application { pluginsList.add(OverviewFragment.getPlugin()); pluginsList.add(ActionsFragment.getPlugin()); if (Config.DANAR) pluginsList.add(DanaRFragment.getPlugin()); - if (Config.MM640G) pluginsList.add(MM640gFragment.getPlugin()); + if (Config.DANAR) pluginsList.add(DanaRKoreanFragment.getPlugin()); if (Config.CAREPORTALENABLED) pluginsList.add(CareportalFragment.getPlugin()); pluginsList.add(VirtualPumpFragment.getPlugin()); if (Config.LOOPENABLED) pluginsList.add(LoopFragment.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java index 0934e68ec3..8865bba6a9 100644 --- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java @@ -17,6 +17,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.DanaR.BluetoothDevicePreference; import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; import info.nightscout.utils.LocaleHelper; public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { @@ -89,7 +90,8 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre addPreferencesFromResource(R.xml.pref_nightscout); if (Config.DANAR) { DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); - if (danaRPlugin.isEnabled(PluginBase.PUMP)) { + DanaRKoreanPlugin danaRKoreanPlugin = (DanaRKoreanPlugin) MainApp.getSpecificPlugin(DanaRKoreanPlugin.class); + if (danaRPlugin.isEnabled(PluginBase.PUMP) || danaRKoreanPlugin.isEnabled(PluginBase.PUMP)) { addPreferencesFromResource(R.xml.pref_danar); addPreferencesFromResource(R.xml.pref_danarprofile); } diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index c38724d6bd..4ac7ee70fd 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -96,11 +96,10 @@ public class DataService extends IntentService { ObjectivesPlugin.bgIsAvailableInNS = true; ObjectivesPlugin.saveProgress(); } else if (isNSProfile && Intents.ACTION_NEW_PROFILE.equals(action)){ - // always handle Profili if NSProfile is enabled without looking at nsUploadOnly + // always handle Profile if NSProfile is enabled without looking at nsUploadOnly handleNewDataFromNSClient(intent); } else if (!nsUploadOnly && - (Intents.ACTION_NEW_PROFILE.equals(action) || - Intents.ACTION_NEW_TREATMENT.equals(action) || + (Intents.ACTION_NEW_TREATMENT.equals(action) || Intents.ACTION_CHANGED_TREATMENT.equals(action) || Intents.ACTION_REMOVED_TREATMENT.equals(action) || Intents.ACTION_NEW_STATUS.equals(action) || diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 4717f6e237..faafbb39fc 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -196,8 +196,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { QueryBuilder queryBuilder = daoBgreadings.queryBuilder(); queryBuilder.orderBy("timeIndex", true); Where where = queryBuilder.where(); - where.ge("timeIndex", mills); - queryBuilder.where().gt("value", 38); + where.ge("timeIndex", mills).and().gt("value", 38); PreparedQuery preparedQuery = queryBuilder.prepare(); bgReadings = daoBgreadings.query(preparedQuery); return bgReadings; diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java new file mode 100644 index 0000000000..b350db88ea --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpDescription.java @@ -0,0 +1,37 @@ +package info.nightscout.androidaps.interfaces; + +/** + * Created by mike on 08.12.2016. + */ + +public class PumpDescription { + public static final int PERCENT = 1; + public static final int ABSOLUTE = 2; + public static final int EXTENDED = 4; + + public boolean isBolusCapable = true; + public double bolusStep = 0.1d; + + public boolean isExtendedBolusCapable = true; + public double extendedBolusStep = 0.1d; + + public boolean isTempBasalCapable = true; + public int lowTempBasalStyle = PERCENT; + public int highTempBasalStyle = PERCENT; + public double maxHighTempPercent = 200; + public double maxHighTempAbsolute = 0; // zero = no limit + public double lowTempPercentStep = 10; + public double lowTempAbsoluteStep = 0.05d; + public int lowTempPercentDuration = 30; + public int lowTempAbsoluteDuration = 30; + public double highTempPercentStep = 10; + public double highTempAbsoluteStep = 0.05d; + public int highTempPercentDuration = 30; + public int highTempAbsoluteDuration = 30; + + public boolean isSetBasalProfileCapable = true; + public double basalStep = 0.01d; + public double basalMinimumRate = 0.04d; + + public boolean isRefillingCapable = false; +} 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 2b413fbe43..9f57af50db 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -42,4 +42,6 @@ public interface PumpInterface { // Status to be passed to NS JSONObject getJSONStatus(); String deviceID(); + + PumpDescription getPumpDescription(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java index f263adcc99..ee3de3267e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java @@ -7,8 +7,13 @@ import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; +import com.squareup.otto.Subscribe; + +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.interfaces.FragmentBase; import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; @@ -22,10 +27,16 @@ import info.nightscout.androidaps.plugins.Actions.dialogs.NewTempBasalDialog; public class ActionsFragment extends Fragment implements FragmentBase, View.OnClickListener { static ActionsPlugin actionsPlugin = new ActionsPlugin(); + static public ActionsPlugin getPlugin() { return actionsPlugin; } + Button profileSwitch; + Button extendedBolus; + Button tempBasal; + Button fill; + public ActionsFragment() { } @@ -35,14 +46,57 @@ public class ActionsFragment extends Fragment implements FragmentBase, View.OnCl Bundle savedInstanceState) { View view = inflater.inflate(R.layout.actions_fragment, container, false); - view.findViewById(R.id.actions_profileswitch).setOnClickListener(this); - view.findViewById(R.id.actions_extendedbolus).setOnClickListener(this); - view.findViewById(R.id.actions_settempbasal).setOnClickListener(this); - view.findViewById(R.id.actions_fill).setOnClickListener(this); + profileSwitch = (Button) view.findViewById(R.id.actions_profileswitch); + extendedBolus = (Button) view.findViewById(R.id.actions_extendedbolus); + tempBasal = (Button) view.findViewById(R.id.actions_settempbasal); + fill = (Button) view.findViewById(R.id.actions_fill); + profileSwitch.setOnClickListener(this); + extendedBolus.setOnClickListener(this); + tempBasal.setOnClickListener(this); + fill.setOnClickListener(this); + + updateGUIIfVisible(); return view; } + @Override + public void onPause() { + super.onPause(); + MainApp.bus().unregister(this); + } + + @Override + public void onResume() { + super.onResume(); + MainApp.bus().register(this); + } + + @Subscribe + public void onStatusEvent(final EventRefreshGui ev) { + updateGUIIfVisible(); + } + + void updateGUIIfVisible() { + if (!MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable) + profileSwitch.setVisibility(View.GONE); + else + profileSwitch.setVisibility(View.VISIBLE); + if (!MainApp.getConfigBuilder().getPumpDescription().isExtendedBolusCapable) + extendedBolus.setVisibility(View.GONE); + else + extendedBolus.setVisibility(View.VISIBLE); + if (!MainApp.getConfigBuilder().getPumpDescription().isTempBasalCapable) + tempBasal.setVisibility(View.GONE); + else + tempBasal.setVisibility(View.VISIBLE); + if (!MainApp.getConfigBuilder().getPumpDescription().isRefillingCapable) + fill.setVisibility(View.GONE); + else + fill.setVisibility(View.VISIBLE); + } + + @Override public void onClick(View view) { FragmentManager manager = getFragmentManager(); @@ -57,11 +111,11 @@ public class ActionsFragment extends Fragment implements FragmentBase, View.OnCl case R.id.actions_extendedbolus: NewExtendedBolusDialog newExtendedDialog = new NewExtendedBolusDialog(); newExtendedDialog.show(manager, "NewExtendedDialog"); - break; + break; case R.id.actions_settempbasal: NewTempBasalDialog newTempDialog = new NewTempBasalDialog(); newTempDialog.show(manager, "NewTempDialog"); - break; + break; case R.id.actions_fill: FillDialog fillDialog = new FillDialog(); fillDialog.show(manager, "FillDialog"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java index 047dd3e7c5..05245bd0b1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java @@ -16,9 +16,12 @@ import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialo public class CareportalFragment extends Fragment implements FragmentBase, View.OnClickListener { - static CareportalPlugin careportalPlugin = new CareportalPlugin(); + static CareportalPlugin careportalPlugin; static public CareportalPlugin getPlugin() { + if (careportalPlugin == null) { + careportalPlugin = new CareportalPlugin(); + } return careportalPlugin; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 00d6beafcf..f8a311e0c0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -35,6 +35,7 @@ import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; +import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.TempBasalsInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; @@ -594,6 +595,21 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain else return "Unknown"; } + @Override + public PumpDescription getPumpDescription() { + if (activePump != null) + return activePump.getPumpDescription(); + else { + PumpDescription emptyDescription = new PumpDescription(); + emptyDescription.isBolusCapable = false; + emptyDescription.isExtendedBolusCapable = false; + emptyDescription.isSetBasalProfileCapable = false; + emptyDescription.isTempBasalCapable = false; + emptyDescription.isRefillingCapable = false; + return emptyDescription; + } + } + /** * Constraints interface **/ diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java index 3595193ce1..4ec54cb37e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java @@ -38,9 +38,12 @@ import info.nightscout.utils.SetWarnColor; public class DanaRFragment extends Fragment implements FragmentBase { private static Logger log = LoggerFactory.getLogger(DanaRFragment.class); - private static DanaRPlugin danaRPlugin = new DanaRPlugin(); + private static DanaRPlugin danaRPlugin; public static DanaRPlugin getPlugin() { + if(danaRPlugin==null){ + danaRPlugin = new DanaRPlugin(); + } return danaRPlugin; } @@ -204,20 +207,20 @@ public class DanaRFragment extends Fragment implements FragmentBase { Long agoMsec = new Date().getTime() - DanaRPlugin.getDanaRPump().lastBolusTime.getTime(); double agoHours = agoMsec / 60d / 60d / 1000d; if (agoHours < 6) // max 6h back - lastBolusView.setText(formatTime.format(DanaRPlugin.getDanaRPump().lastBolusTime) + " (" + DecimalFormatter.to1Decimal(agoHours) + " " + getString(R.string.hoursago) + ") " + DecimalFormatter.to2Decimal(danaRPlugin.getDanaRPump().lastBolusAmount) + " U"); + lastBolusView.setText(formatTime.format(DanaRPlugin.getDanaRPump().lastBolusTime) + " (" + DecimalFormatter.to1Decimal(agoHours) + " " + getString(R.string.hoursago) + ") " + DecimalFormatter.to2Decimal(getPlugin().getDanaRPump().lastBolusAmount) + " U"); else lastBolusView.setText(""); } dailyUnitsView.setText(DecimalFormatter.to0Decimal(DanaRPlugin.getDanaRPump().dailyTotalUnits) + " / " + DanaRPlugin.getDanaRPump().maxDailyTotalUnits + " U"); SetWarnColor.setColor(dailyUnitsView, DanaRPlugin.getDanaRPump().dailyTotalUnits, DanaRPlugin.getDanaRPump().maxDailyTotalUnits * 0.75d, DanaRPlugin.getDanaRPump().maxDailyTotalUnits * 0.9d); - basaBasalRateView.setText("( " + (DanaRPlugin.getDanaRPump().activeProfile + 1) + " ) " + DecimalFormatter.to2Decimal(danaRPlugin.getBaseBasalRate()) + " U/h"); - if (danaRPlugin.isRealTempBasalInProgress()) { - tempBasalView.setText(danaRPlugin.getRealTempBasal().toString()); + basaBasalRateView.setText("( " + (DanaRPlugin.getDanaRPump().activeProfile + 1) + " ) " + DecimalFormatter.to2Decimal(getPlugin().getBaseBasalRate()) + " U/h"); + if (getPlugin().isRealTempBasalInProgress()) { + tempBasalView.setText(getPlugin().getRealTempBasal().toString()); } else { tempBasalView.setText(""); } - if (danaRPlugin.isExtendedBoluslInProgress()) { - extendedBolusView.setText(danaRPlugin.getExtendedBolus().toString()); + if (getPlugin().isExtendedBoluslInProgress()) { + extendedBolusView.setText(getPlugin().getExtendedBolus().toString()); } else { extendedBolusView.setText(""); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java index 5501319e99..b039a84f94 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java @@ -16,7 +16,6 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.text.DateFormat; import java.util.Date; import java.util.Objects; @@ -33,6 +32,7 @@ import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; +import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService; @@ -63,6 +63,8 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf private static DanaRPump sDanaRPump = new DanaRPump(); private static boolean useExtendedBoluses = false; + private static PumpDescription pumpDescription = new PumpDescription(); + public static DanaRPump getDanaRPump() { return sDanaRPump; } @@ -75,6 +77,32 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf Intent intent = new Intent(context, ExecutionService.class); context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); MainApp.bus().register(this); + + pumpDescription.isBolusCapable = true; // TODO: use description in setTempBasalAbsolute + pumpDescription.bolusStep = 0.05d; + + pumpDescription.isExtendedBolusCapable = true; + pumpDescription.extendedBolusStep = 0.1d; + + pumpDescription.isTempBasalCapable = true; + pumpDescription.lowTempBasalStyle = PumpDescription.PERCENT; + pumpDescription.highTempBasalStyle = useExtendedBoluses ? PumpDescription.EXTENDED : PumpDescription.PERCENT; + pumpDescription.maxHighTempPercent = 200; + pumpDescription.maxHighTempAbsolute = 0; + pumpDescription.lowTempPercentStep = 10; + pumpDescription.lowTempAbsoluteStep = 0; + pumpDescription.lowTempPercentDuration = 60; + pumpDescription.lowTempAbsoluteDuration = 60; + pumpDescription.highTempPercentStep = 10; + pumpDescription.highTempAbsoluteStep = 0.05d; + pumpDescription.highTempPercentDuration = 60; + pumpDescription.highTempAbsoluteDuration = 30; + + pumpDescription.isSetBasalProfileCapable = true; + pumpDescription.basalStep = 0.01d; + pumpDescription.basalMinimumRate = 0.04d; + + pumpDescription.isRefillingCapable = true; } ServiceConnection mConnection = new ServiceConnection() { @@ -99,11 +127,16 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf @Subscribe public void onStatusEvent(final EventPreferenceChange s) { - boolean previousValue = useExtendedBoluses; - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false); - if (useExtendedBoluses != previousValue && isExtendedBoluslInProgress()) { - sExecutionService.extendedBolusStop(); + if (isEnabled(PUMP)) { + boolean previousValue = useExtendedBoluses; + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false); + + pumpDescription.highTempBasalStyle = useExtendedBoluses ? PumpDescription.EXTENDED : PumpDescription.PERCENT; + + if (useExtendedBoluses != previousValue && isExtendedBoluslInProgress()) { + sExecutionService.extendedBolusStop(); + } } } @@ -585,6 +618,10 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf return sExecutionService != null && sExecutionService.isConnecting(); } + public static void doDisconnect(String from) { + if (sExecutionService != null) sExecutionService.disconnect(from); + } + @Override public JSONObject getJSONStatus() { if (getDanaRPump().lastConnection.getTime() + 5 * 60 * 1000L < new Date().getTime()) { @@ -629,6 +666,11 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf return getDanaRPump().serialNumber; } + @Override + public PumpDescription getPumpDescription() { + return pumpDescription; + } + /** * Constraint interface */ diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java index f1e2a91f87..f088aca1b0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java @@ -22,6 +22,11 @@ public class DanaRPump { public static final int UNITS_MGDL = 0; public static final int UNITS_MMOL = 1; + public static final int DELIVERY_PRIME = 0x01; + public static final int DELIVERY_STEP_BOLUS = 0x02; + public static final int DELIVERY_BASAL = 0x04; + public static final int DELIVERY_EXT_BOLUS = 0x08; + public static final String PROFILE_PREFIX = "DanaR-"; public Date lastConnection = new Date(0); @@ -35,12 +40,21 @@ public class DanaRPump { public int password = -1; public Date pumpTime = new Date(0); + public static final int DOMESTIC_MODEL = 0x01; + public static final int EXPORT_MODEL = 0x03; + public int model; + public int protocol; + public int productCode; + + // Status public boolean pumpSuspended; public boolean calculatorEnabled; public double dailyTotalUnits; public int maxDailyTotalUnits; + public double bolusStep; + public double iob; public double reservoirRemainingUnits; @@ -58,6 +72,7 @@ public class DanaRPump { public int tempBasalTotalSec; public Date tempBasalStart; + public boolean isDualBolusInProgress; public boolean isExtendedInProgress; public int extendedBolusMinutes; public double extendedBolusAmount; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java index e9151a53c3..a3024cc593 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java @@ -58,6 +58,7 @@ import info.nightscout.androidaps.plugins.DanaR.comm.MsgSetExtendedBolusStart; import info.nightscout.androidaps.plugins.DanaR.comm.MsgSetExtendedBolusStop; import info.nightscout.androidaps.plugins.DanaR.comm.MsgSetTempBasalStart; import info.nightscout.androidaps.plugins.DanaR.comm.MsgSetTempBasalStop; +import info.nightscout.androidaps.plugins.DanaR.comm.MsgSetTime; import info.nightscout.androidaps.plugins.DanaR.comm.MsgSettingActiveProfile; import info.nightscout.androidaps.plugins.DanaR.comm.MsgSettingBasal; import info.nightscout.androidaps.plugins.DanaR.comm.MsgSettingGlucose; @@ -174,6 +175,11 @@ public class ExecutionService extends Service { return connectionInProgress; } + public void disconnect(String from) { + if (mSerialIOThread != null) + mSerialIOThread.disconnect(from); + } + public void connect(String from) { if (danaRPump.password != -1 && danaRPump.password != SafeParse.stringToInt(SP.getString("danar_password", "-1"))) { ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.wrongpumppassword), R.raw.error); @@ -270,11 +276,11 @@ public class ExecutionService extends Service { MsgStatusBolusExtended exStatusMsg = new MsgStatusBolusExtended(); + mSerialIOThread.sendMessage(new MsgSettingShippingInfo()); // TODO: show it somewhere mSerialIOThread.sendMessage(tempStatusMsg); // do this before statusBasic because here is temp duration mSerialIOThread.sendMessage(exStatusMsg); mSerialIOThread.sendMessage(statusMsg); mSerialIOThread.sendMessage(statusBasicMsg); - mSerialIOThread.sendMessage(new MsgSettingShippingInfo()); // TODO: show it somewhere if (danaRPump.isNewPump) { mSerialIOThread.sendMessage(new MsgCheckValue()); @@ -315,6 +321,7 @@ public class ExecutionService extends Service { mSerialIOThread.sendMessage(new MsgSettingActiveProfile()); mSerialIOThread.sendMessage(new MsgSettingProfileRatios()); mSerialIOThread.sendMessage(new MsgSettingProfileRatiosAll()); + mSerialIOThread.sendMessage(new MsgSetTime(new Date())); danaRPump.lastSettingsRead = now; } @@ -466,6 +473,7 @@ public class ExecutionService extends Service { mSerialIOThread.sendMessage(msgSet); MsgSetActivateBasalProfile msgActivate = new MsgSetActivateBasalProfile((byte) 0); mSerialIOThread.sendMessage(msgActivate); + danaRPump.lastSettingsRead = new Date(0); // force read full settings getPumpStatus(); return true; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageBase.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageBase.java index 3fc4093119..1d97a02ba1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageBase.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageBase.java @@ -51,6 +51,15 @@ public class MessageBase { AddParamByte((byte) (date.get(Calendar.MINUTE))); } + public void AddParamDateTime(Date date) { + AddParamByte((byte) (date.getSeconds())); + AddParamByte((byte) (date.getMinutes())); + AddParamByte((byte) (date.getHours())); + AddParamByte((byte) (date.getDate())); + AddParamByte((byte) (date.getMonth() + 1)); + AddParamByte((byte) (date.getYear() - 100)); + } + public byte[] getRawMessageBytes() { this.buffer[0] = (byte) 0x7E; this.buffer[1] = (byte) 0x7E; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageHashTable.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageHashTable.java index 5081bf81d6..8650ea5e8b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageHashTable.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageHashTable.java @@ -33,7 +33,7 @@ public class MessageHashTable { put(new MsgSetTempBasalStop()); // 0x0403 CMD_PUMPSET_EXERCISE_STOP put(new MsgSetExtendedBolusStop()); // 0x0406 CMD_PUMPSET_EXPANS_INS_STOP put(new MsgSetExtendedBolusStart()); // 0x0407 CMD_PUMPSET_EXPANS_INS_S - put(new MsgError()); // 0x0601 CMD_PUMPOWAY_SYSTEM_STATUS + put(new MsgError()); // 0x0601 CMD_PUMPOWAY_SYSTEM_STATUS put(new MsgPCCommStart()); // 0x3001 CMD_CONNECT put(new MsgPCCommStop()); // 0x3002 CMD_DISCONNECT put(new MsgHistoryBolus()); // 0x3101 CMD_HISTORY_MEAL_INS @@ -56,6 +56,7 @@ public class MessageHashTable { put(new MsgSettingUserOptions()); // 0x320B CMD_SETTING_V_USER_OPTIONS put(new MsgSettingActiveProfile()); // 0x320C CMD_SETTING_V_PROFILE_NUMBER put(new MsgSettingProfileRatiosAll()); // 0x320D CMD_SETTING_V_CIR_CF_VALUE + put(new MsgSetSingleBasalProfile()); // 0x3302 CMD_SETTING_BASAL_INS_S put(new MsgSetBasalProfile()); // 0x3306 CMD_SETTING_BASAL_PROFILE_S put(new MsgSetActivateBasalProfile()); // 0x330C CMD_SETTING_PROFILE_NUMBER_S put(new MsgHistoryAllDone()); // 0x41F1 CMD_HISTORY_ALL_DONE diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageOriginalNames.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageOriginalNames.java index 66f4ac71bf..5aba354812 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageOriginalNames.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MessageOriginalNames.java @@ -62,7 +62,7 @@ public class MessageOriginalNames { messageNames.put(0x320d, "CMD_SETTING_V_CIR_CF_VALUE"); messageNames.put(0x3301, "CMD_SETTING_MEAL_INS_S"); - messageNames.put(0x3302, "CMD_SETTING_Based_INS_S"); + messageNames.put(0x3302, "CMD_SETTING_BASAL_INS_S"); messageNames.put(0x3303, "CMD_SETTING_MEAL_SETTING_S"); messageNames.put(0x3304, "CMD_SETTING_CCC_S"); messageNames.put(0x3305, "CMD_SETTING_MAX_VALUE_S"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgCheckValue.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgCheckValue.java index 954342a8a7..775de6eb29 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgCheckValue.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgCheckValue.java @@ -4,6 +4,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.DanaR.DanaRPump; +import info.nightscout.utils.ToastUtils; /** * Created by mike on 30.06.2016. @@ -17,15 +22,22 @@ public class MsgCheckValue extends MessageBase { @Override public void handleMessage(byte[] bytes) { - int a = intFromBuff(bytes, 0, 1); - int b = intFromBuff(bytes, 1, 1); - if (a != 3 || b <= 0) { - // another message will follow - } else { + DanaRPump pump = DanaRPlugin.getDanaRPump(); + pump.model = intFromBuff(bytes, 0, 1); + pump.protocol = intFromBuff(bytes, 1, 1); + pump.productCode = intFromBuff(bytes, 2, 1); + if (pump.model != DanaRPump.EXPORT_MODEL) { + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.wrongpumpdriverselected), R.raw.error); + ((DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class)).doDisconnect("Wrong Model"); + log.debug("Wrong model selected"); + } + + if (Config.logDanaMessageDetail) { + log.debug("Model: " + String.format("%02X ", pump.model)); + log.debug("Protocol: " + String.format("%02X ", pump.protocol)); + log.debug("Product Code: " + String.format("%02X ", pump.productCode)); } - if (Config.logDanaMessageDetail) - log.debug("Response: " + String.format("%02X ", a) + String.format("%02X ", b)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusBasic.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusBasic.java index 33a95554f7..6303391d4a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusBasic.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusBasic.java @@ -3,6 +3,10 @@ package info.nightscout.androidaps.plugins.DanaR.comm; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.DanaR.DanaRPump; + public class MsgInitConnStatusBasic extends MessageBase { private static Logger log = LoggerFactory.getLogger(MsgInitConnStatusBasic.class); @@ -12,25 +16,51 @@ public class MsgInitConnStatusBasic extends MessageBase { @Override public void handleMessage(byte[] bytes) { - int a = intFromBuff(bytes, 0, 1); - int b = intFromBuff(bytes, 1, 1); - int c = intFromBuff(bytes, 2, 1); - int d = intFromBuff(bytes, 3, 1); - int e = intFromBuff(bytes, 4, 1); - int f = intFromBuff(bytes, 5, 1); - int g = intFromBuff(bytes, 6, 1); - int h = intFromBuff(bytes, 7, 1); - int i = intFromBuff(bytes, 8, 1); - int j = intFromBuff(bytes, 9, 1); - int k = intFromBuff(bytes, 10, 1); - int l = intFromBuff(bytes, 11, 1); - int m = intFromBuff(bytes, 12, 1); - int n = intFromBuff(bytes, 13, 1); - int o; + if (bytes.length - 10 < 21) { + return; + } + DanaRPump pump = DanaRPlugin.getDanaRPump(); + pump.pumpSuspended = intFromBuff(bytes, 0, 1) == 1; + pump.calculatorEnabled = intFromBuff(bytes, 1, 1) == 1; + pump.dailyTotalUnits = intFromBuff(bytes, 2, 3) / 750d; + pump.maxDailyTotalUnits = intFromBuff(bytes, 5, 2) / 100; + pump.reservoirRemainingUnits = intFromBuff(bytes, 7, 3) / 750d; + pump.bolusBlocked = intFromBuff(bytes, 10, 1) == 1; + pump.currentBasal = intFromBuff(bytes, 11, 2) / 100d; + pump.tempBasalPercent = intFromBuff(bytes, 13, 1); + pump.isExtendedInProgress = intFromBuff(bytes, 14, 1) == 1; + pump.isTempBasalInProgress = intFromBuff(bytes, 15, 1) == 1; + int statusBasalUDOption = intFromBuff(bytes, 16, 1); + pump.isDualBolusInProgress = intFromBuff(bytes, 17, 1) == 1; + double extendedBolusRate = intFromBuff(bytes, 18, 2) / 100d; + pump.batteryRemaining = intFromBuff(bytes, 20, 1); try { - o = intFromBuff(bytes, 21, 1); - } catch (Exception ex) { - o = 0; + int bolusConfig = intFromBuff(bytes, 21, 1); + boolean deliveryPrime = (bolusConfig & DanaRPump.DELIVERY_PRIME) != 0; + boolean deliveryStepBolus = (bolusConfig & DanaRPump.DELIVERY_STEP_BOLUS) != 0; + boolean deliveryBasal = (bolusConfig & DanaRPump.DELIVERY_BASAL) != 0; + boolean deliveryExtBolus = (bolusConfig & DanaRPump.DELIVERY_EXT_BOLUS) != 0; + log.debug("Delivery prime: " + deliveryPrime); + log.debug("Delivery step bolus: " + deliveryStepBolus); + log.debug("Delivery basal: " + deliveryBasal); + log.debug("Delivery ext bolus: " + deliveryExtBolus); + } catch (Exception e) { + } + + if (Config.logDanaMessageDetail) { + log.debug("Pump suspended: " + pump.pumpSuspended); + log.debug("Calculator enabled: " + pump.calculatorEnabled); + log.debug("Daily total units: " + pump.dailyTotalUnits); + log.debug("Max daily total units: " + pump.maxDailyTotalUnits); + log.debug("Reservoir remaining units: " + pump.reservoirRemainingUnits); + log.debug("Bolus blocked: " + pump.bolusBlocked); + log.debug("Current basal: " + pump.currentBasal); + log.debug("Current temp basal percent: " + pump.tempBasalPercent); + log.debug("Is extended bolus running: " + pump.isExtendedInProgress); + log.debug("statusBasalUDOption: " + statusBasalUDOption); + log.debug("Is dual bolus running: " + pump.isDualBolusInProgress); + log.debug("Extended bolus rate: " + extendedBolusRate); + log.debug("Battery remaining: " + pump.batteryRemaining); } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusBolus.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusBolus.java index 3a3ce99349..abc9607fba 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusBolus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusBolus.java @@ -3,6 +3,10 @@ package info.nightscout.androidaps.plugins.DanaR.comm; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.DanaR.DanaRPump; + /** * Created by mike on 28.05.2016. */ @@ -15,10 +19,27 @@ public class MsgInitConnStatusBolus extends MessageBase { @Override public void handleMessage(byte[] bytes) { - int a1 = intFromBuff(bytes, 0, 1); - int a2 = intFromBuff(bytes, 1, 1); - int c = intFromBuff(bytes, 8, 2); - int d = c / 100; - int e = intFromBuff(bytes, 10, 2); + if (bytes.length - 10 > 12) { + return; + } + DanaRPump pump = DanaRPlugin.getDanaRPump(); + int bolusConfig = intFromBuff(bytes, 0, 1); + boolean deliveryPrime = (bolusConfig & DanaRPump.DELIVERY_PRIME) != 0; + boolean deliveryStepBolus = (bolusConfig & DanaRPump.DELIVERY_STEP_BOLUS) != 0; + boolean deliveryBasal = (bolusConfig & DanaRPump.DELIVERY_BASAL) != 0; + boolean deliveryExtBolus = (bolusConfig & DanaRPump.DELIVERY_EXT_BOLUS) != 0; + + pump.bolusStep = intFromBuff(bytes, 1, 1) / 100d; + pump.maxBolus = intFromBuff(bytes, 2, 2) / 100d; + //int bolusRate = intFromBuff(bytes, 4, 8); + + if (Config.logDanaMessageDetail) { + log.debug("Delivery prime: " + deliveryPrime); + log.debug("Delivery step bolus: " + deliveryStepBolus); + log.debug("Delivery basal: " + deliveryBasal); + log.debug("Delivery ext bolus: " + deliveryExtBolus); + log.debug("Bolus increment: " + pump.bolusStep); + log.debug("Bolus max: " + pump.maxBolus); + } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusOption.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusOption.java index b8049c23ee..2b424a949b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusOption.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusOption.java @@ -18,15 +18,15 @@ public class MsgInitConnStatusOption extends MessageBase { @Override public void handleMessage(byte[] bytes) { - int a = intFromBuff(bytes, 0, 1); - int b = intFromBuff(bytes, 1, 1); - int c = intFromBuff(bytes, 2, 1); - int d = intFromBuff(bytes, 3, 1); - int e = intFromBuff(bytes, 4, 1); - int f = intFromBuff(bytes, 5, 1); - int g = intFromBuff(bytes, 6, 1); - int h = intFromBuff(bytes, 7, 1); - int i = intFromBuff(bytes, 8, 1); + int status1224Clock = intFromBuff(bytes, 0, 1); + int isStatusButtonScroll = intFromBuff(bytes, 1, 1); + int soundVibration = intFromBuff(bytes, 2, 1); + int glucoseUnit = intFromBuff(bytes, 3, 1); + int lcdTimeout = intFromBuff(bytes, 4, 1); + int backlightgTimeout = intFromBuff(bytes, 5, 1); + int languageOption = intFromBuff(bytes, 6, 1); + int lowReservoirAlarmBoundary = intFromBuff(bytes, 7, 1); + //int none = intFromBuff(bytes, 8, 1); if (bytes.length >= 21) { DanaRPlugin.getDanaRPump().password = intFromBuff(bytes, 9, 2) ^ 0x3463; if (Config.logDanaMessageDetail) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusTime.java index 7e6b1a7652..8f5ef43c41 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusTime.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgInitConnStatusTime.java @@ -6,6 +6,14 @@ import org.slf4j.LoggerFactory; import java.util.Date; import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.DanaR.DanaRPump; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.utils.ToastUtils; public class MsgInitConnStatusTime extends MessageBase { private static Logger log = LoggerFactory.getLogger(MsgInitConnStatusTime.class); @@ -16,9 +24,25 @@ public class MsgInitConnStatusTime extends MessageBase { @Override public void handleMessage(byte[] bytes) { - Date time = dateTimeSecFromBuff(bytes, 0); + if (bytes.length - 10 > 7) { + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(),MainApp.sResources.getString(R.string.wrongpumpdriverselected), R.raw.error); + ((DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class)).doDisconnect("Wrong Model"); + log.debug("Wrong model selected. Switching to Korean DanaR"); + ((DanaRKoreanPlugin)MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setFragmentEnabled(PluginBase.PUMP, true); + ((DanaRKoreanPlugin)MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setFragmentVisible(PluginBase.PUMP, true); + ((DanaRPlugin)MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentEnabled(PluginBase.PUMP, false); + ((DanaRPlugin)MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentVisible(PluginBase.PUMP, false); + MainApp.getConfigBuilder().storeSettings(); + MainApp.bus().post(new EventRefreshGui(false)); + return; + } - if (Config.logDanaMessageDetail) + Date time = dateTimeSecFromBuff(bytes, 0); + int versionCode = intFromBuff(bytes, 6, 1); + + if (Config.logDanaMessageDetail) { log.debug("Pump time: " + time); + log.debug("Version code: " + versionCode); + } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSetBasalProfile.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSetBasalProfile.java index 97eb3844da..9acd5ec824 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSetBasalProfile.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSetBasalProfile.java @@ -4,6 +4,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.Overview.Notification; +import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; public class MsgSetBasalProfile extends MessageBase { private static Logger log = LoggerFactory.getLogger(MsgSetBasalProfile.class); @@ -29,9 +33,13 @@ public class MsgSetBasalProfile extends MessageBase { if (result != 1) { failed = true; log.debug("Set basal profile result: " + result + " FAILED!!!"); + Notification reportFail = new Notification(Notification.PROFILE_SET_FAILED, MainApp.sResources.getString(R.string.profile_set_failed), Notification.URGENT); + MainApp.bus().post(new EventNewNotification(reportFail)); } else { if (Config.logDanaMessageDetail) log.debug("Set basal profile result: " + result); + Notification reportOK = new Notification(Notification.PROFILE_SET_OK, MainApp.sResources.getString(R.string.profile_set_ok), Notification.INFO, 60); + MainApp.bus().post(new EventNewNotification(reportOK)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSetSingleBasalProfile.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSetSingleBasalProfile.java new file mode 100644 index 0000000000..4e57712b04 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSetSingleBasalProfile.java @@ -0,0 +1,48 @@ +package info.nightscout.androidaps.plugins.DanaR.comm; + +import com.j256.ormlite.stmt.query.Not; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.Overview.Notification; +import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; + +public class MsgSetSingleBasalProfile extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSetSingleBasalProfile.class); + + public MsgSetSingleBasalProfile() { + SetCommand(0x3302); + } + + // index 0-3 + public MsgSetSingleBasalProfile(double[] values) { + this(); + for (Integer i = 0; i < 24; i++) { + AddParamInt((int) (values[i] * 100)); + } + if (Config.logDanaMessageDetail) + log.debug("Set basal profile"); + } + + @Override + public void handleMessage(byte[] bytes) { + int result = intFromBuff(bytes, 0, 1); + if (result != 1) { + failed = true; + log.debug("Set basal profile result: " + result + " FAILED!!!"); + Notification reportFail = new Notification(Notification.PROFILE_SET_FAILED, MainApp.sResources.getString(R.string.profile_set_failed), Notification.URGENT); + MainApp.bus().post(new EventNewNotification(reportFail)); + } else { + if (Config.logDanaMessageDetail) + log.debug("Set basal profile result: " + result); + Notification reportOK = new Notification(Notification.PROFILE_SET_OK, MainApp.sResources.getString(R.string.profile_set_ok), Notification.INFO, 60); + MainApp.bus().post(new EventNewNotification(reportOK)); + } + } + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSetTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSetTime.java new file mode 100644 index 0000000000..827958608c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSetTime.java @@ -0,0 +1,28 @@ +package info.nightscout.androidaps.plugins.DanaR.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; + +import info.nightscout.androidaps.Config; + +/** + * Created by mike on 09.12.2016. + */ + +public class MsgSetTime extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSetTime.class); + + public MsgSetTime(Date time) { + SetCommand(0x330a); + AddParamDateTime(time); + } + + public void handleMessage(byte[] bytes) { + int result = intFromBuff(bytes, 0, 1); + + if (Config.logDanaMessageDetail) + log.debug("Result: " + result); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSettingShippingInfo.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSettingShippingInfo.java index 81b1a11eb6..9b71f0060b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSettingShippingInfo.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgSettingShippingInfo.java @@ -7,6 +7,7 @@ import java.util.Date; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.DanaR.DanaRPump; /** * Created by mike on 05.07.2016. @@ -19,17 +20,18 @@ public class MsgSettingShippingInfo extends MessageBase { } public void handleMessage(byte[] bytes) { - DanaRPlugin.getDanaRPump().serialNumber = stringFromBuff(bytes, 0, 10); - DanaRPlugin.getDanaRPump().shippingDate = dateFromBuff(bytes, 10); - DanaRPlugin.getDanaRPump().shippingCountry = asciiStringFromBuff(bytes, 13, 3); - if (DanaRPlugin.getDanaRPump().shippingDate.getTime() > new Date(116, 4, 1).getTime()) { - DanaRPlugin.getDanaRPump().isNewPump = true; + DanaRPump pump = DanaRPlugin.getDanaRPump(); + pump.serialNumber = stringFromBuff(bytes, 0, 10); + pump.shippingDate = dateFromBuff(bytes, 10); + pump.shippingCountry = asciiStringFromBuff(bytes, 13, 3); + if (pump.shippingDate.getTime() > new Date(116, 4, 1).getTime()) { + pump.isNewPump = true; } else - DanaRPlugin.getDanaRPump().isNewPump = false; + pump.isNewPump = false; if (Config.logDanaMessageDetail) { - log.debug("Serial number: " + DanaRPlugin.getDanaRPump().serialNumber); - log.debug("Shipping date: " + DanaRPlugin.getDanaRPump().shippingDate); - log.debug("Shipping country: " + DanaRPlugin.getDanaRPump().shippingCountry); + log.debug("Serial number: " + pump.serialNumber); + log.debug("Shipping date: " + pump.shippingDate); + log.debug("Shipping country: " + pump.shippingCountry); } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusBasic.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusBasic.java index 5422c4318b..d79dd6aea8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusBasic.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusBasic.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.DanaR.DanaRPump; public class MsgStatusBasic extends MessageBase { @@ -15,41 +16,31 @@ public class MsgStatusBasic extends MessageBase { } public void handleMessage(byte[] bytes) { - boolean pumpSuspended = intFromBuff(bytes, 0, 1) == 1; - boolean calculatorEnabled = intFromBuff(bytes, 1, 1) == 1; - double dailyTotalUnits = intFromBuff(bytes, 2, 3) / 750d; - int maxDailyTotalUnits = intFromBuff(bytes, 5, 2) / 100; - double reservoirRemainingUnits = intFromBuff(bytes, 7, 3) / 750d; - boolean bolusBlocked = intFromBuff(bytes, 10, 1) == 1; - double currentBasal = intFromBuff(bytes, 11, 2) / 100d; - int tempBasalPercent = intFromBuff(bytes, 13, 1); - boolean isExtendedInProgress = intFromBuff(bytes, 14, 1) == 1; - boolean isTempBasalInProgress = intFromBuff(bytes, 15, 1) == 1; - int batteryRemaining = intFromBuff(bytes, 20, 1); + DanaRPump pump = DanaRPlugin.getDanaRPump(); - DanaRPlugin.getDanaRPump().pumpSuspended = pumpSuspended; - DanaRPlugin.getDanaRPump().calculatorEnabled = calculatorEnabled; - DanaRPlugin.getDanaRPump().dailyTotalUnits = dailyTotalUnits; - DanaRPlugin.getDanaRPump().maxDailyTotalUnits = maxDailyTotalUnits; - DanaRPlugin.getDanaRPump().reservoirRemainingUnits = reservoirRemainingUnits; - DanaRPlugin.getDanaRPump().bolusBlocked = bolusBlocked; - DanaRPlugin.getDanaRPump().currentBasal = currentBasal; - DanaRPlugin.getDanaRPump().tempBasalPercent = tempBasalPercent; - DanaRPlugin.getDanaRPump().isExtendedInProgress = isExtendedInProgress; - DanaRPlugin.getDanaRPump().isTempBasalInProgress = isTempBasalInProgress; - DanaRPlugin.getDanaRPump().batteryRemaining = batteryRemaining; + pump.pumpSuspended = intFromBuff(bytes, 0, 1) == 1; + pump.calculatorEnabled = intFromBuff(bytes, 1, 1) == 1; + pump.dailyTotalUnits = intFromBuff(bytes, 2, 3) / 750d; + pump.maxDailyTotalUnits = intFromBuff(bytes, 5, 2) / 100; + pump.reservoirRemainingUnits = intFromBuff(bytes, 7, 3) / 750d; + pump.bolusBlocked = intFromBuff(bytes, 10, 1) == 1; + pump.currentBasal = intFromBuff(bytes, 11, 2) / 100d; + pump.tempBasalPercent = intFromBuff(bytes, 13, 1); + pump.isExtendedInProgress = intFromBuff(bytes, 14, 1) == 1; + pump.isTempBasalInProgress = intFromBuff(bytes, 15, 1) == 1; + pump.batteryRemaining = intFromBuff(bytes, 20, 1); if (Config.logDanaMessageDetail) { - log.debug("Pump suspended: " + pumpSuspended); - log.debug("Calculator enabled: " + calculatorEnabled); - log.debug("Daily total units: " + dailyTotalUnits); - log.debug("Max daily total units: " + maxDailyTotalUnits); - log.debug("Reservoir remaining units: " + reservoirRemainingUnits); - log.debug("Bolus blocked: " + bolusBlocked); - log.debug("Current basal: " + currentBasal); - log.debug("Current temp basal percent: " + tempBasalPercent); - log.debug("Is extended bolus running: " + isExtendedInProgress); - log.debug("Is temp basal running: " + isTempBasalInProgress); + log.debug("Pump suspended: " + pump.pumpSuspended); + log.debug("Calculator enabled: " + pump.calculatorEnabled); + log.debug("Daily total units: " + pump.dailyTotalUnits); + log.debug("Max daily total units: " + pump.maxDailyTotalUnits); + log.debug("Reservoir remaining units: " + pump.reservoirRemainingUnits); + log.debug("Bolus blocked: " + pump.bolusBlocked); + log.debug("Current basal: " + pump.currentBasal); + log.debug("Current temp basal percent: " + pump.tempBasalPercent); + log.debug("Is extended bolus running: " + pump.isExtendedInProgress); + log.debug("Is temp basal running: " + pump.isTempBasalInProgress); } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java new file mode 100644 index 0000000000..77e320c3a9 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java @@ -0,0 +1,234 @@ +package info.nightscout.androidaps.plugins.DanaRKorean; + + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.os.HandlerThread; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.DateFormat; +import java.util.Date; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventPreferenceChange; +import info.nightscout.androidaps.events.EventTempBasalChange; +import info.nightscout.androidaps.interfaces.FragmentBase; +import info.nightscout.androidaps.plugins.DanaR.Dialogs.ProfileViewDialog; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus; +import info.nightscout.androidaps.plugins.DanaRKorean.History.DanaRHistoryActivity; +import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.SetWarnColor; + +public class DanaRKoreanFragment extends Fragment implements FragmentBase { + private static Logger log = LoggerFactory.getLogger(DanaRKoreanFragment.class); + + private static DanaRKoreanPlugin danaRKoreanPlugin = new DanaRKoreanPlugin(); + + public static DanaRKoreanPlugin getPlugin() { + return danaRKoreanPlugin; + } + + private static Handler sHandler; + private static HandlerThread sHandlerThread; + + private Handler loopHandler = new Handler(); + private Runnable refreshLoop = null; + + TextView lastConnectionView; + TextView btConnectionView; + TextView lastBolusView; + TextView dailyUnitsView; + TextView basaBasalRateView; + TextView tempBasalView; + TextView extendedBolusView; + TextView batteryView; + TextView reservoirView; + TextView iobView; + Button viewProfileButton; + Button historyButton; + + public DanaRKoreanFragment() { + if (sHandlerThread == null) { + sHandlerThread = new HandlerThread(DanaRKoreanFragment.class.getSimpleName()); + sHandlerThread.start(); + sHandler = new Handler(sHandlerThread.getLooper()); + } + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (refreshLoop == null) { + refreshLoop = new Runnable() { + @Override + public void run() { + updateGUI(); + loopHandler.postDelayed(refreshLoop, 60 * 1000L); + } + }; + loopHandler.postDelayed(refreshLoop, 60 * 1000L); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.danar_fragment, container, false); + btConnectionView = (TextView) view.findViewById(R.id.danar_btconnection); + lastConnectionView = (TextView) view.findViewById(R.id.danar_lastconnection); + lastBolusView = (TextView) view.findViewById(R.id.danar_lastbolus); + dailyUnitsView = (TextView) view.findViewById(R.id.danar_dailyunits); + basaBasalRateView = (TextView) view.findViewById(R.id.danar_basabasalrate); + tempBasalView = (TextView) view.findViewById(R.id.danar_tempbasal); + extendedBolusView = (TextView) view.findViewById(R.id.danar_extendedbolus); + batteryView = (TextView) view.findViewById(R.id.danar_battery); + reservoirView = (TextView) view.findViewById(R.id.danar_reservoir); + iobView = (TextView) view.findViewById(R.id.danar_iob); + viewProfileButton = (Button) view.findViewById(R.id.danar_viewprofile); + historyButton = (Button) view.findViewById(R.id.danar_history); + + viewProfileButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + FragmentManager manager = getFragmentManager(); + ProfileViewDialog profileViewDialog = new ProfileViewDialog(); + profileViewDialog.show(manager, "ProfileViewDialog"); + } + }); + + historyButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + startActivity(new Intent(getContext(), DanaRHistoryActivity.class)); + } + }); + + btConnectionView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + sHandler.post(new Runnable() { + @Override + public void run() { + DanaRKoreanPlugin.sExecutionService.connect("Connect request from GUI"); + } + } + ); + } + }); + + updateGUI(); + return view; + } + + @Override + public void onPause() { + super.onPause(); + MainApp.bus().unregister(this); + } + + @Override + public void onResume() { + super.onResume(); + MainApp.bus().register(this); + } + + @Subscribe + public void onStatusEvent(final EventDanaRConnectionStatus c) { + Activity activity = getActivity(); + if (activity != null) { + activity.runOnUiThread( + new Runnable() { + @Override + public void run() { + if (c.sStatus == EventDanaRConnectionStatus.CONNECTING) + btConnectionView.setText("{fa-bluetooth-b spin} " + c.sSecondsElapsed + "s"); + else if (c.sStatus == EventDanaRConnectionStatus.CONNECTED) + btConnectionView.setText("{fa-bluetooth}"); + else + btConnectionView.setText("{fa-bluetooth-b}"); + } + } + ); + } + } + + @Subscribe + public void onStatusEvent(final EventDanaRNewStatus s) { + updateGUI(); + } + + @Subscribe + public void onStatusEvent(final EventTempBasalChange s) { + updateGUI(); + } + + @Subscribe + public void onStatusEvent(final EventPreferenceChange s) { + updateGUI(); + } + + // GUI functions + private void updateGUI() { + final DateFormat formatTime = DateFormat.getTimeInstance(DateFormat.SHORT); + + Activity activity = getActivity(); + if (activity != null && basaBasalRateView != null) + activity.runOnUiThread(new Runnable() { + @SuppressLint("SetTextI18n") + @Override + public void run() { + + if (DanaRKoreanPlugin.getDanaRPump().lastConnection.getTime() != 0) { + Long agoMsec = new Date().getTime() - DanaRKoreanPlugin.getDanaRPump().lastConnection.getTime(); + int agoMin = (int) (agoMsec / 60d / 1000d); + lastConnectionView.setText(formatTime.format(DanaRKoreanPlugin.getDanaRPump().lastConnection) + " (" + agoMin + " " + MainApp.sResources.getString(R.string.minago) + ")"); + SetWarnColor.setColor(lastConnectionView, agoMin, 16d, 31d); + } +// if (DanaRKoreanPlugin.getDanaRPump().lastBolusTime.getTime() != 0) { +// Long agoMsec = new Date().getTime() - DanaRKoreanPlugin.getDanaRPump().lastBolusTime.getTime(); +// double agoHours = agoMsec / 60d / 60d / 1000d; +// if (agoHours < 6) // max 6h back +// lastBolusView.setText(formatTime.format(DanaRKoreanPlugin.getDanaRPump().lastBolusTime) + " (" + DecimalFormatter.to1Decimal(agoHours) + " " + getString(R.string.hoursago) + ") " + DecimalFormatter.to2Decimal(danaRKoreanPlugin.getDanaRPump().lastBolusAmount) + " U"); +// else lastBolusView.setText(""); +// } + + dailyUnitsView.setText(DecimalFormatter.to0Decimal(DanaRKoreanPlugin.getDanaRPump().dailyTotalUnits) + " / " + DanaRKoreanPlugin.getDanaRPump().maxDailyTotalUnits + " U"); + SetWarnColor.setColor(dailyUnitsView, DanaRKoreanPlugin.getDanaRPump().dailyTotalUnits, DanaRKoreanPlugin.getDanaRPump().maxDailyTotalUnits * 0.75d, DanaRKoreanPlugin.getDanaRPump().maxDailyTotalUnits * 0.9d); + basaBasalRateView.setText("( " + (DanaRKoreanPlugin.getDanaRPump().activeProfile + 1) + " ) " + DecimalFormatter.to2Decimal(danaRKoreanPlugin.getBaseBasalRate()) + " U/h"); + if (danaRKoreanPlugin.isRealTempBasalInProgress()) { + tempBasalView.setText(danaRKoreanPlugin.getRealTempBasal().toString()); + } else { + tempBasalView.setText(""); + } + if (danaRKoreanPlugin.isExtendedBoluslInProgress()) { + extendedBolusView.setText(danaRKoreanPlugin.getExtendedBolus().toString()); + } else { + extendedBolusView.setText(""); + } + reservoirView.setText(DecimalFormatter.to0Decimal(DanaRKoreanPlugin.getDanaRPump().reservoirRemainingUnits) + " / 300 U"); + SetWarnColor.setColorInverse(reservoirView, DanaRKoreanPlugin.getDanaRPump().reservoirRemainingUnits, 50d, 20d); + batteryView.setText("{fa-battery-" + (DanaRKoreanPlugin.getDanaRPump().batteryRemaining / 25) + "}"); + SetWarnColor.setColorInverse(batteryView, DanaRKoreanPlugin.getDanaRPump().batteryRemaining, 51d, 26d); + iobView.setText(DanaRKoreanPlugin.getDanaRPump().iob + " U"); + } + }); + + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java new file mode 100644 index 0000000000..67ee8d3ad2 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java @@ -0,0 +1,780 @@ +package info.nightscout.androidaps.plugins.DanaRKorean; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.content.SharedPreferences; +import android.os.IBinder; +import android.preference.PreferenceManager; +import android.support.annotation.Nullable; + +import com.squareup.otto.Subscribe; + +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; +import java.util.Objects; + +import info.nightscout.androidaps.BuildConfig; +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.PumpEnactResult; +import info.nightscout.androidaps.db.TempBasal; +import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.events.EventAppExit; +import info.nightscout.androidaps.events.EventPreferenceChange; +import info.nightscout.androidaps.interfaces.ConstraintsInterface; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.ProfileInterface; +import info.nightscout.androidaps.interfaces.PumpDescription; +import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.Services.ExecutionService; +import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.DateUtil; +import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.Round; +import info.nightscout.utils.ToastUtils; + +/** + * Created by mike on 05.08.2016. + */ +public class DanaRKoreanPlugin implements PluginBase, PumpInterface, ConstraintsInterface, ProfileInterface { + private static Logger log = LoggerFactory.getLogger(DanaRKoreanPlugin.class); + + @Override + public String getFragmentClass() { + return DanaRKoreanFragment.class.getName(); + } + + static boolean fragmentPumpEnabled = true; + static boolean fragmentProfileEnabled = true; + static boolean fragmentPumpVisible = true; + + public static ExecutionService sExecutionService; + + + private static DanaRKoreanPump sDanaRKoreanPump = new DanaRKoreanPump(); + private static boolean useExtendedBoluses = false; + + private PumpDescription pumpDescription = new PumpDescription(); + + public static DanaRKoreanPump getDanaRPump() { + return sDanaRKoreanPump; + } + + public DanaRKoreanPlugin() { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false); + + Context context = MainApp.instance().getApplicationContext(); + Intent intent = new Intent(context, ExecutionService.class); + context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); + MainApp.bus().register(this); + + pumpDescription.isBolusCapable = true; // TODO: use description in setTempBasalAbsolute + pumpDescription.bolusStep = 0.1d; + + pumpDescription.isExtendedBolusCapable = true; + pumpDescription.extendedBolusStep = 0.1d; + + pumpDescription.isTempBasalCapable = true; + pumpDescription.lowTempBasalStyle = PumpDescription.PERCENT; + pumpDescription.highTempBasalStyle = useExtendedBoluses ? PumpDescription.EXTENDED : PumpDescription.PERCENT; + pumpDescription.maxHighTempPercent = 200; + pumpDescription.maxHighTempAbsolute = 0; + pumpDescription.lowTempPercentStep = 10; + pumpDescription.lowTempAbsoluteStep = 0; + pumpDescription.lowTempPercentDuration = 60; + pumpDescription.lowTempAbsoluteDuration = 60; + pumpDescription.highTempPercentStep = 10; + pumpDescription.highTempAbsoluteStep = 0.05d; + pumpDescription.highTempPercentDuration = 60; + pumpDescription.highTempAbsoluteDuration = 30; + + pumpDescription.isSetBasalProfileCapable = true; + pumpDescription.basalStep = 0.01d; + pumpDescription.basalMinimumRate = 0.1d; + + pumpDescription.isRefillingCapable = true; + } + + ServiceConnection mConnection = new ServiceConnection() { + + public void onServiceDisconnected(ComponentName name) { + log.debug("Service is disconnected"); + sExecutionService = null; + } + + public void onServiceConnected(ComponentName name, IBinder service) { + log.debug("Service is connected"); + ExecutionService.LocalBinder mLocalBinder = (ExecutionService.LocalBinder) service; + sExecutionService = mLocalBinder.getServiceInstance(); + } + }; + + @SuppressWarnings("UnusedParameters") + @Subscribe + public void onStatusEvent(final EventAppExit e) { + MainApp.instance().getApplicationContext().unbindService(mConnection); + } + + @Subscribe + public void onStatusEvent(final EventPreferenceChange s) { + if (isEnabled(PUMP)) { + boolean previousValue = useExtendedBoluses; + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false); + + pumpDescription.highTempBasalStyle = useExtendedBoluses ? PumpDescription.EXTENDED : PumpDescription.PERCENT; + + if (useExtendedBoluses != previousValue && isExtendedBoluslInProgress()) { + sExecutionService.extendedBolusStop(); + } + } + } + + // Plugin base interface + @Override + public int getType() { + return PluginBase.PUMP; + } + + @Override + public String getName() { + return MainApp.instance().getString(R.string.danarkoreanpump); + } + + @Override + public boolean isEnabled(int type) { + if (type == PluginBase.PROFILE) return fragmentProfileEnabled && fragmentPumpEnabled; + else if (type == PluginBase.PUMP) return fragmentPumpEnabled; + else if (type == PluginBase.CONSTRAINTS) return fragmentPumpEnabled; + return false; + } + + @Override + public boolean isVisibleInTabs(int type) { + if (type == PluginBase.PROFILE || type == PluginBase.CONSTRAINTS) return false; + else if (type == PluginBase.PUMP) return fragmentPumpVisible; + return false; + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + if (type == PluginBase.PROFILE) this.fragmentProfileEnabled = fragmentEnabled; + else if (type == PluginBase.PUMP) this.fragmentPumpEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + if (type == PluginBase.PUMP) + this.fragmentPumpVisible = fragmentVisible; + } + + @Override + public boolean isInitialized() { + return getDanaRPump().lastConnection.getTime() > 0; + } + + // Pump interface + @Override + public boolean isTempBasalInProgress() { + if (getRealTempBasal() != null) return true; + if (getExtendedBolus() != null && useExtendedBoluses) return true; + return false; + } + + public boolean isRealTempBasalInProgress() { + return getRealTempBasal() != null; //TODO: crosscheck here + } + + @Override + public boolean isExtendedBoluslInProgress() { + return getExtendedBolus() != null; //TODO: crosscheck here + } + + @Override + public void setNewBasalProfile(NSProfile profile) { + if (sExecutionService == null) { + log.error("setNewBasalProfile sExecutionService is null"); + return; + } + if (!sExecutionService.updateBasalsInPump(profile)) + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.failedupdatebasalprofile)); + } + + @Override + public double getBaseBasalRate() { + return getDanaRPump().currentBasal; + } + + @Override + public double getTempBasalAbsoluteRate() { + if (isRealTempBasalInProgress()) { + if (getRealTempBasal().isAbsolute) { + return getRealTempBasal().absolute; + } else { + Double baseRate = getBaseBasalRate(); + Double tempRate = baseRate * (getRealTempBasal().percent / 100d); + return tempRate; + } + } + if (isExtendedBoluslInProgress() && useExtendedBoluses) { + return getBaseBasalRate() + getExtendedBolus().absolute; + } + return 0; + } + + @Override + public double getTempBasalRemainingMinutes() { + if (isRealTempBasalInProgress()) + return getRealTempBasal().getPlannedRemainingMinutes(); + if (isExtendedBoluslInProgress() && useExtendedBoluses) + return getExtendedBolus().getPlannedRemainingMinutes(); + return 0; + } + + @Override + public TempBasal getTempBasal() { + if (isRealTempBasalInProgress()) + return getRealTempBasal(); + if (isExtendedBoluslInProgress() && useExtendedBoluses) + return getExtendedBolus(); + return null; + } + + public TempBasal getTempBasal(Date time) { + TempBasal temp = MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(time); + if (temp != null) return temp; + if (useExtendedBoluses) + return MainApp.getConfigBuilder().getActiveTempBasals().getExtendedBolus(time); + return null; + } + + public TempBasal getRealTempBasal() { + return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(new Date()); + } + + @Override + public TempBasal getExtendedBolus() { + return MainApp.getConfigBuilder().getActiveTempBasals().getExtendedBolus(new Date()); + } + + @Override + public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) { + ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); + insulin = configBuilderPlugin.applyBolusConstraints(insulin); + if (insulin > 0 || carbs > 0) { + Treatment t = new Treatment(); + boolean connectionOK = false; + if (insulin > 0 || carbs > 0) connectionOK = sExecutionService.bolus(insulin, carbs, t); + PumpEnactResult result = new PumpEnactResult(); + result.success = connectionOK; + result.bolusDelivered = t.insulin; + result.carbsDelivered = carbs; + result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); + if (Config.logPumpActions) + log.debug("deliverTreatment: OK. Asked: " + insulin + " Delivered: " + result.bolusDelivered); + return result; + } else { + PumpEnactResult result = new PumpEnactResult(); + result.success = false; + result.bolusDelivered = 0d; + result.carbsDelivered = 0; + result.comment = MainApp.instance().getString(R.string.danar_invalidinput); + log.error("deliverTreatment: Invalid input"); + return result; + } + } + + @Override + public void stopBolusDelivering() { + if (sExecutionService == null) { + log.error("stopBolusDelivering sExecutionService is null"); + return; + } + sExecutionService.bolusStop(); + } + + // This is called from APS + @Override + public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) { + // Recheck pump status if older than 30 min + if (getDanaRPump().lastConnection.getTime() + 30 * 60 * 1000L < new Date().getTime()) { + doConnect("setTempBasalAbsolute old data"); + } + + PumpEnactResult result = new PumpEnactResult(); + + ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); + absoluteRate = configBuilderPlugin.applyBasalConstraints(absoluteRate); + + final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d; + final boolean doLowTemp = absoluteRate < getBaseBasalRate(); + final boolean doHighTemp = absoluteRate > getBaseBasalRate() && !useExtendedBoluses; + final boolean doExtendedTemp = absoluteRate > getBaseBasalRate() && useExtendedBoluses; + + if (doTempOff) { + // If extended in progress + if (isExtendedBoluslInProgress() && useExtendedBoluses) { + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: Stopping extended bolus (doTempOff)"); + return cancelExtendedBolus(); + } + // If temp in progress + if (isRealTempBasalInProgress()) { + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: Stopping temp basal (doTempOff)"); + return cancelRealTempBasal(); + } + result.success = true; + result.enacted = false; + result.percent = 100; + result.isPercent = true; + result.isTempCancel = true; + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: doTempOff OK"); + return result; + } + + if (doLowTemp || doHighTemp) { + Integer percentRate = Double.valueOf(absoluteRate / getBaseBasalRate() * 100).intValue(); + if (percentRate < 100) percentRate = Round.ceilTo((double) percentRate, 10d).intValue(); + else percentRate = Round.floorTo((double) percentRate, 10d).intValue(); + if (percentRate > 200) { + percentRate = 200; + } + // If extended in progress + if (isExtendedBoluslInProgress() && useExtendedBoluses) { + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: Stopping extended bolus (doLowTemp || doHighTemp)"); + result = cancelExtendedBolus(); + if (!result.success) { + log.error("setTempBasalAbsolute: Failed to stop previous extended bolus (doLowTemp || doHighTemp)"); + return result; + } + } + // Check if some temp is already in progress + if (isRealTempBasalInProgress()) { + // Correct basal already set ? + if (getRealTempBasal().percent == percentRate) { + result.success = true; + result.percent = percentRate; + result.absolute = getTempBasalAbsoluteRate(); + result.enacted = false; + result.duration = ((Double) getTempBasalRemainingMinutes()).intValue(); + result.isPercent = true; + result.isTempCancel = false; + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: Correct temp basal already set (doLowTemp || doHighTemp)"); + return result; + } else { + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: Stopping temp basal (doLowTemp || doHighTemp)"); + result = cancelRealTempBasal(); + // Check for proper result + if (!result.success) { + log.error("setTempBasalAbsolute: Failed to stop previous temp basal (doLowTemp || doHighTemp)"); + return result; + } + } + } + // Convert duration from minutes to hours + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: Setting temp basal " + percentRate + "% for " + durationInMinutes + " mins (doLowTemp || doHighTemp)"); + return setTempBasalPercent(percentRate, durationInMinutes); + } + if (doExtendedTemp) { + // Check if some temp is already in progress + if (isRealTempBasalInProgress()) { + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: Stopping temp basal (doExtendedTemp)"); + result = cancelRealTempBasal(); + // Check for proper result + if (!result.success) { + log.error("setTempBasalAbsolute: Failed to stop previous temp basal (doExtendedTemp)"); + return result; + } + } + + // Calculate # of halfHours from minutes + Integer durationInHalfHours = Math.max(durationInMinutes / 30, 1); + // We keep current basal running so need to sub current basal + Double extendedRateToSet = absoluteRate - getBaseBasalRate(); + extendedRateToSet = configBuilderPlugin.applyBasalConstraints(extendedRateToSet); + // needs to be rounded to 0.1 + extendedRateToSet = Round.roundTo(extendedRateToSet, 0.1d); + + // What is current rate of extended bolusing in u/h? + if (Config.logPumpActions) { + log.debug("setTempBasalAbsolute: Extended bolus in progress: " + isExtendedBoluslInProgress() + " rate: " + getDanaRPump().extendedBolusAbsoluteRate + "U/h duration remaining: " + getDanaRPump().extendedBolusRemainingMinutes + "min"); + log.debug("setTempBasalAbsolute: Rate to set: " + extendedRateToSet + "U/h"); + } + + // Compare with extended rate in progress + if (Math.abs(getDanaRPump().extendedBolusAbsoluteRate - extendedRateToSet) < 0.02D) { // Allow some rounding diff + // correct extended already set + result.success = true; + result.absolute = getDanaRPump().extendedBolusAbsoluteRate; + result.enacted = false; + result.duration = getDanaRPump().extendedBolusRemainingMinutes; + result.isPercent = false; + result.isTempCancel = false; + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: Correct extended already set"); + return result; + } + + // Now set new extended, no need to to stop previous (if running) because it's replaced + Double extendedAmount = extendedRateToSet / 2 * durationInHalfHours; + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: Setting extended: " + extendedAmount + "U halfhours: " + durationInHalfHours); + result = setExtendedBolus(extendedAmount, durationInMinutes); + if (!result.success) { + log.error("setTempBasalAbsolute: Failed to set extended bolus"); + return result; + } + if (Config.logPumpActions) + log.debug("setTempBasalAbsolute: Extended bolus set ok"); + result.absolute = result.absolute + getBaseBasalRate(); + return result; + } + // We should never end here + log.error("setTempBasalAbsolute: Internal error"); + result.success = false; + result.comment = "Internal error"; + return result; + } + + @Override + public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) { + PumpEnactResult result = new PumpEnactResult(); + ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); + percent = configBuilderPlugin.applyBasalConstraints(percent); + if (percent < 0) { + result.isTempCancel = false; + result.enacted = false; + result.success = false; + result.comment = MainApp.instance().getString(R.string.danar_invalidinput); + log.error("setTempBasalPercent: Invalid input"); + return result; + } + if (percent > 200) percent = 200; + if (getDanaRPump().isTempBasalInProgress && getDanaRPump().tempBasalPercent == percent) { + result.enacted = false; + result.success = true; + result.isTempCancel = false; + result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); + result.duration = getDanaRPump().tempBasalRemainingMin; + result.percent = getDanaRPump().tempBasalPercent; + result.isPercent = true; + if (Config.logPumpActions) + log.debug("setTempBasalPercent: Correct value already set"); + return result; + } + int durationInHours = Math.max(durationInMinutes / 60, 1); + boolean connectionOK = sExecutionService.tempBasal(percent, durationInHours); + if (connectionOK && getDanaRPump().isTempBasalInProgress && getDanaRPump().tempBasalPercent == percent) { + result.enacted = true; + result.success = true; + result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); + result.isTempCancel = false; + result.duration = getDanaRPump().tempBasalRemainingMin; + result.percent = getDanaRPump().tempBasalPercent; + result.isPercent = true; + if (Config.logPumpActions) + log.debug("setTempBasalPercent: OK"); + return result; + } + result.enacted = false; + result.success = false; + result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly); + log.error("setTempBasalPercent: Failed to set temp basal"); + return result; + } + + @Override + public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { + ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); + insulin = configBuilderPlugin.applyBolusConstraints(insulin); + // needs to be rounded to 0.1 + insulin = Round.roundTo(insulin, 0.1d); + + PumpEnactResult result = new PumpEnactResult(); + if (getDanaRPump().isExtendedInProgress && Math.abs(getDanaRPump().extendedBolusAmount - insulin) < 0.1d) { + result.enacted = false; + result.success = true; + result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); + result.duration = getDanaRPump().extendedBolusRemainingMinutes; + result.absolute = getDanaRPump().extendedBolusAbsoluteRate; + result.isPercent = false; + result.isTempCancel = false; + if (Config.logPumpActions) + log.debug("setExtendedBolus: Correct extended bolus already set"); + return result; + } + int durationInHalfHours = Math.max(durationInMinutes / 30, 1); + boolean connectionOK = sExecutionService.extendedBolus(insulin, durationInHalfHours); + if (connectionOK && getDanaRPump().isExtendedInProgress && Math.abs(getDanaRPump().extendedBolusAmount - insulin) < 0.1d) { + result.enacted = true; + result.success = true; + result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); + result.isTempCancel = false; + result.duration = getDanaRPump().extendedBolusRemainingMinutes; + result.absolute = getDanaRPump().extendedBolusAbsoluteRate; + result.bolusDelivered = getDanaRPump().extendedBolusAmount; + result.isPercent = false; + if (Config.logPumpActions) + log.debug("setExtendedBolus: OK"); + return result; + } + result.enacted = false; + result.success = false; + result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly); + log.error("setExtendedBolus: Failed to extended bolus"); + return result; + } + + @Override + public PumpEnactResult cancelTempBasal() { + if (isRealTempBasalInProgress()) + return cancelRealTempBasal(); + if (isExtendedBoluslInProgress()) + return cancelExtendedBolus(); + PumpEnactResult result = new PumpEnactResult(); + result.success = true; + result.enacted = false; + result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); + result.isTempCancel = true; + return result; + } + + public PumpEnactResult cancelRealTempBasal() { + PumpEnactResult result = new PumpEnactResult(); + if (getDanaRPump().isTempBasalInProgress) { + sExecutionService.tempBasalStop(); + result.enacted = true; + result.isTempCancel = true; + } + if (!getDanaRPump().isTempBasalInProgress) { + result.success = true; + result.isTempCancel = true; + result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); + if (Config.logPumpActions) + log.debug("cancelRealTempBasal: OK"); + return result; + } else { + result.success = false; + result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly); + result.isTempCancel = true; + log.error("cancelRealTempBasal: Failed to cancel temp basal"); + return result; + } + } + + @Override + public PumpEnactResult cancelExtendedBolus() { + PumpEnactResult result = new PumpEnactResult(); + if (getDanaRPump().isExtendedInProgress) { + sExecutionService.extendedBolusStop(); + result.enacted = true; + result.isTempCancel = true; + } + if (!getDanaRPump().isExtendedInProgress) { + result.success = true; + result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); + if (Config.logPumpActions) + log.debug("cancelExtendedBolus: OK"); + return result; + } else { + result.success = false; + result.comment = MainApp.instance().getString(R.string.danar_valuenotsetproperly); + log.error("cancelExtendedBolus: Failed to cancel extended bolus"); + return result; + } + } + + public static void doConnect(String from) { + if (sExecutionService != null) sExecutionService.connect(from); + } + + public static boolean isConnected() { + return sExecutionService != null && sExecutionService.isConnected(); + } + + public static boolean isConnecting() { + return sExecutionService != null && sExecutionService.isConnecting(); + } + + public static void doDisconnect(String from) { + if (sExecutionService != null) sExecutionService.disconnect(from); + } + + @Override + public JSONObject getJSONStatus() { + if (getDanaRPump().lastConnection.getTime() + 5 * 60 * 1000L < new Date().getTime()) { + return null; + } + JSONObject pump = new JSONObject(); + JSONObject battery = new JSONObject(); + JSONObject status = new JSONObject(); + JSONObject extended = new JSONObject(); + try { + battery.put("percent", getDanaRPump().batteryRemaining); + status.put("status", "normal"); + status.put("timestamp", DateUtil.toISOString(getDanaRPump().lastConnection)); + extended.put("Version", BuildConfig.VERSION_NAME + "-" + BuildConfig.BUILDVERSION); + extended.put("PumpIOB", getDanaRPump().iob); +// extended.put("LastBolus", getDanaRPump().lastBolusTime.toLocaleString()); +// extended.put("LastBolusAmount", getDanaRPump().lastBolusAmount); + if (isTempBasalInProgress()) { + extended.put("TempBasalAbsoluteRate", getTempBasalAbsoluteRate()); + extended.put("TempBasalStart", getTempBasal().timeStart.toLocaleString()); + extended.put("TempBasalRemaining", getTempBasal().getPlannedRemainingMinutes()); + extended.put("IsExtended", getTempBasal().isExtended); + } + extended.put("BaseBasalRate", getBaseBasalRate()); + try { + extended.put("ActiveProfile", MainApp.getConfigBuilder().getActiveProfile().getProfile().getActiveProfile()); + } catch (Exception e) {} + + pump.put("battery", battery); + pump.put("status", status); + pump.put("extended", extended); + pump.put("reservoir", (int) getDanaRPump().reservoirRemainingUnits); + pump.put("clock", DateUtil.toISOString(new Date())); + } catch (JSONException e) { + e.printStackTrace(); + } + return pump; + } + + @Override + public String deviceID() { + return getDanaRPump().serialNumber; + } + + @Override + public PumpDescription getPumpDescription() { + return pumpDescription; + } + + /** + * Constraint interface + */ + + @Override + public boolean isLoopEnabled() { + return true; + } + + @Override + public boolean isClosedModeEnabled() { + return true; + } + + @Override + public boolean isAutosensModeEnabled() { + return true; + } + + @Override + public boolean isAMAModeEnabled() { + return true; + } + + @SuppressWarnings("PointlessBooleanExpression") + @Override + public Double applyBasalConstraints(Double absoluteRate) { + double origAbsoluteRate = absoluteRate; + if (getDanaRPump() != null) { + if (absoluteRate > getDanaRPump().maxBasal) { + absoluteRate = getDanaRPump().maxBasal; + if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit) + log.debug("Limiting rate " + origAbsoluteRate + "U/h by pump constraint to " + absoluteRate + "U/h"); + } + } + return absoluteRate; + } + + @SuppressWarnings("PointlessBooleanExpression") + @Override + public Integer applyBasalConstraints(Integer percentRate) { + Integer origPercentRate = percentRate; + if (percentRate < 0) percentRate = 0; + if (percentRate > 200) percentRate = 200; + if (!Objects.equals(percentRate, origPercentRate) && Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit)) + log.debug("Limiting percent rate " + origPercentRate + "% to " + percentRate + "%"); + return percentRate; + } + + @SuppressWarnings("PointlessBooleanExpression") + @Override + public Double applyBolusConstraints(Double insulin) { + double origInsulin = insulin; + if (getDanaRPump() != null) { + if (insulin > getDanaRPump().maxBolus) { + insulin = getDanaRPump().maxBolus; + if (Config.logConstraintsChanges && origInsulin != Constants.bolusOnlyForCheckLimit) + log.debug("Limiting bolus " + origInsulin + "U by pump constraint to " + insulin + "U"); + } + } + return insulin; + } + + @Override + public Integer applyCarbsConstraints(Integer carbs) { + return carbs; + } + + @Override + public Double applyMaxIOBConstraints(Double maxIob) { + return maxIob; + } + + @Nullable + @Override + public NSProfile getProfile() { + DanaRKoreanPump pump = getDanaRPump(); + if (pump.lastSettingsRead.getTime() == 0) + return null; // no info now + return pump.createConvertedProfile(); + } + + // Reply for sms communicator + public String shortStatus() { + String ret = ""; + if (getDanaRPump().lastConnection.getTime() != 0) { + Long agoMsec = new Date().getTime() - getDanaRPump().lastConnection.getTime(); + int agoMin = (int) (agoMsec / 60d / 1000d); + ret += "LastConn: " + agoMin + " minago\n"; + } +// if (getDanaRPump().lastBolusTime.getTime() != 0) { +// ret += "LastBolus: " + DecimalFormatter.to2Decimal(getDanaRPump().lastBolusAmount) + "U @" + android.text.format.DateFormat.format("HH:mm", getDanaRPump().lastBolusTime) + "\n"; +// } + if (isRealTempBasalInProgress()) { + ret += "Temp: " + getRealTempBasal().toString() + "\n"; + } + if (isExtendedBoluslInProgress()) { + ret += "Extended: " + getExtendedBolus().toString() + "\n"; + } + ret += "IOB: " + getDanaRPump().iob + "U\n"; + ret += "Reserv: " + DecimalFormatter.to0Decimal(getDanaRPump().reservoirRemainingUnits) + "U\n"; + ret += "Batt: " + getDanaRPump().batteryRemaining + "\n"; + return ret; + } + // TODO: daily total constraint + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java new file mode 100644 index 0000000000..538b225eb5 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java @@ -0,0 +1,167 @@ +package info.nightscout.androidaps.plugins.DanaRKorean; + +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.text.DecimalFormat; +import java.util.Date; + +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.SafeParse; + +/** + * Created by mike on 04.07.2016. + */ +public class DanaRKoreanPump { + public static final int UNITS_MGDL = 0; + public static final int UNITS_MMOL = 1; + + public static final int DELIVERY_PRIME = 0x01; + public static final int DELIVERY_STEP_BOLUS = 0x02; + public static final int DELIVERY_BASAL = 0x04; + public static final int DELIVERY_EXT_BOLUS = 0x08; + + public static final String PROFILE_PREFIX = "DanaR-"; + + public Date lastConnection = new Date(0); + public Date lastSettingsRead = new Date(0); + + // Info + public String serialNumber = ""; + public Date shippingDate = new Date(0); + public String shippingCountry = ""; + public boolean isNewPump = false; + public int password = -1; + public Date pumpTime = new Date(0); + + public static final int DOMESTIC_MODEL = 0x01; + public static final int EXPORT_MODEL = 0x03; + public int model; + public int protocol; + public int productCode; + + // Status + public double dailyTotalUnits; + public int maxDailyTotalUnits; + + public double bolusStep; + + public double iob; + + public double reservoirRemainingUnits; + public int batteryRemaining; + + public double currentBasal; + + public boolean isTempBasalInProgress; + public int tempBasalPercent; + public int tempBasalRemainingMin; + public int tempBasalTotalSec; + public Date tempBasalStart; + + public boolean isExtendedInProgress; + public int extendedBolusMinutes; + public double extendedBolusAmount; + public double extendedBolusAbsoluteRate; + public int extendedBolusSoFarInMinutes; + public Date extendedBolusStart; + public int extendedBolusRemainingMinutes; + + // Profile + public int units; + public int easyBasalMode; + public boolean basal48Enable = false; + public int currentCIR; + public double currentCF; + public double currentAI; + public double currentTarget; + public int currentAIDR; + + public int morningCIR; + public double morningCF; + public int afternoonCIR; + public double afternoonCF; + public int eveningCIR; + public double eveningCF; + public int nightCIR; + public double nightCF; + + + public int activeProfile = 0; + public double[][] pumpProfiles = null; + + //Limits + public double maxBolus; + public double maxBasal; + + public NSProfile createConvertedProfile() { + JSONObject json = new JSONObject(); + JSONObject store = new JSONObject(); + JSONObject profile = new JSONObject(); + +// Morning / 6:00–10:59 +// Afternoon / 11:00–16:59 +// Evening / 17:00–21:59 +// Night / 22:00–5:59 + + SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + double dia = SafeParse.stringToDouble(SP.getString("danarprofile_dia", "3")); + double car = SafeParse.stringToDouble(SP.getString("danarprofile_car", "20")); + + try { + json.put("defaultProfile", PROFILE_PREFIX + (activeProfile + 1)); + json.put("store", store); + profile.put("dia", dia); + + JSONArray carbratios = new JSONArray(); + carbratios.put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", nightCIR)); + carbratios.put(new JSONObject().put("time", "06:00").put("timeAsSeconds", 6 * 3600).put("value", morningCIR)); + carbratios.put(new JSONObject().put("time", "11:00").put("timeAsSeconds", 11 * 3600).put("value", afternoonCIR)); + carbratios.put(new JSONObject().put("time", "14:00").put("timeAsSeconds", 17 * 3600).put("value", eveningCIR)); + carbratios.put(new JSONObject().put("time", "22:00").put("timeAsSeconds", 22 * 3600).put("value", nightCIR)); + profile.put("carbratio", carbratios); + + profile.put("carbs_hr", car); + + JSONArray sens = new JSONArray(); + sens.put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", nightCF)); + sens.put(new JSONObject().put("time", "06:00").put("timeAsSeconds", 6 * 3600).put("value", morningCF)); + sens.put(new JSONObject().put("time", "11:00").put("timeAsSeconds", 11 * 3600).put("value", afternoonCF)); + sens.put(new JSONObject().put("time", "17:00").put("timeAsSeconds", 17 * 3600).put("value", eveningCF)); + sens.put(new JSONObject().put("time", "22:00").put("timeAsSeconds", 22 * 3600).put("value", nightCF)); + profile.put("sens", sens); + + JSONArray basals = new JSONArray(); + int basalValues = basal48Enable ? 48 : 24; + int basalIncrement = basal48Enable ? 30 * 60 : 60 * 60; + for (int h = 0; h < basalValues; h++) { + String time; + DecimalFormat df = new DecimalFormat("00"); + if (basal48Enable) { + time = df.format((long) h / 2) + ":" + df.format(30 * (h % 2)); + } else { + time = df.format(h) + ":00"; + } + basals.put(new JSONObject().put("time", time).put("timeAsSeconds", h * basalIncrement).put("value", pumpProfiles[activeProfile][h])); + } + profile.put("basal", basals); + + profile.put("target_low", new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", currentTarget))); + profile.put("target_high", new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", currentTarget))); + profile.put("units", units == UNITS_MGDL ? Constants.MGDL : Constants.MMOL); + store.put(PROFILE_PREFIX + (activeProfile + 1), profile); + } catch (JSONException e) { + e.printStackTrace(); + } catch (Exception e) { + return null; + } + return new NSProfile(json, PROFILE_PREFIX + (activeProfile + 1)); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/History/DanaRHistoryActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/History/DanaRHistoryActivity.java new file mode 100644 index 0000000000..3346dd94e8 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/History/DanaRHistoryActivity.java @@ -0,0 +1,462 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.History; + +import android.app.Activity; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.Bundle; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.IBinder; +import android.support.v7.widget.CardView; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.Spinner; +import android.widget.TextView; + +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.stmt.PreparedQuery; +import com.j256.ormlite.stmt.QueryBuilder; +import com.j256.ormlite.stmt.Where; +import com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.SQLException; +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.DanaRHistoryRecord; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.DanaR.History.DanaRNSHistorySync; +import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRSyncStatus; +import info.nightscout.androidaps.plugins.DanaRKorean.Services.ExecutionService; +import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.ToastUtils; + +public class DanaRHistoryActivity extends Activity { + private static Logger log = LoggerFactory.getLogger(DanaRHistoryActivity.class); + + private boolean mBounded; + private static ExecutionService mExecutionService; + + private Handler mHandler; + private static HandlerThread mHandlerThread; + + static NSProfile profile = null; + + Spinner historyTypeSpinner; + TextView statusView; + Button reloadButton; + Button syncButton; + RecyclerView recyclerView; + LinearLayoutManager llm; + + static byte showingType = RecordTypes.RECORD_TYPE_ALARM; + List historyList = new ArrayList<>(); + + public static class TypeList { + public byte type; + String name; + + public TypeList(byte type, String name) { + this.type = type; + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + + public DanaRHistoryActivity() { + super(); + mHandlerThread = new HandlerThread(DanaRHistoryActivity.class.getSimpleName()); + mHandlerThread.start(); + this.mHandler = new Handler(mHandlerThread.getLooper()); + } + + + @Override + public void onStart() { + super.onStart(); + Intent intent = new Intent(this, ExecutionService.class); + bindService(intent, mConnection, Context.BIND_AUTO_CREATE); + } + + @Override + protected void onResume() { + super.onResume(); + MainApp.bus().register(this); + } + + @Override + protected void onPause() { + super.onPause(); + MainApp.bus().unregister(this); + } + + @Override + public void onStop() { + super.onStop(); + if (mBounded) { + unbindService(mConnection); + mBounded = false; + } + } + + ServiceConnection mConnection = new ServiceConnection() { + + public void onServiceDisconnected(ComponentName name) { + log.debug("Service is disconnected"); + mBounded = false; + mExecutionService = null; + } + + public void onServiceConnected(ComponentName name, IBinder service) { + log.debug("Service is connected"); + mBounded = true; + ExecutionService.LocalBinder mLocalBinder = (ExecutionService.LocalBinder) service; + mExecutionService = mLocalBinder.getServiceInstance(); + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.danar_historyactivity); + + historyTypeSpinner = (Spinner) findViewById(R.id.danar_historytype); + statusView = (TextView) findViewById(R.id.danar_historystatus); + reloadButton = (Button) findViewById(R.id.danar_historyreload); + syncButton = (Button) findViewById(R.id.danar_historysync); + recyclerView = (RecyclerView) findViewById(R.id.danar_history_recyclerview); + + recyclerView.setHasFixedSize(true); + llm = new LinearLayoutManager(this); + recyclerView.setLayoutManager(llm); + + RecyclerViewAdapter adapter = new RecyclerViewAdapter(historyList); + recyclerView.setAdapter(adapter); + + statusView.setVisibility(View.GONE); + + // Types + + ArrayList typeList = new ArrayList<>(); + typeList.add(new TypeList(RecordTypes.RECORD_TYPE_ALARM, getString(R.string.danar_history_alarm))); + typeList.add(new TypeList(RecordTypes.RECORD_TYPE_BASALHOUR, getString(R.string.danar_history_basalhours))); + typeList.add(new TypeList(RecordTypes.RECORD_TYPE_BOLUS, getString(R.string.danar_history_bolus))); + typeList.add(new TypeList(RecordTypes.RECORD_TYPE_CARBO, getString(R.string.danar_history_carbohydrates))); + typeList.add(new TypeList(RecordTypes.RECORD_TYPE_DAILY, getString(R.string.danar_history_dailyinsulin))); + typeList.add(new TypeList(RecordTypes.RECORD_TYPE_ERROR, getString(R.string.danar_history_errors))); + typeList.add(new TypeList(RecordTypes.RECORD_TYPE_GLUCOSE, getString(R.string.danar_history_glucose))); + typeList.add(new TypeList(RecordTypes.RECORD_TYPE_REFILL, getString(R.string.danar_history_refill))); + typeList.add(new TypeList(RecordTypes.RECORD_TYPE_SUSPEND, getString(R.string.danar_history_syspend))); + ArrayAdapter spinnerAdapter = new ArrayAdapter<>(this, + android.R.layout.simple_spinner_item, typeList); + spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + historyTypeSpinner.setAdapter(spinnerAdapter); + + reloadButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mExecutionService.isConnected() || mExecutionService.isConnecting()) { + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.pumpbusy)); + return; + } + mHandler.post(new Runnable() { + @Override + public void run() { + TypeList selected = (TypeList) historyTypeSpinner.getSelectedItem(); + runOnUiThread(new Runnable() { + @Override + public void run() { + reloadButton.setVisibility(View.GONE); + syncButton.setVisibility(View.GONE); + statusView.setVisibility(View.VISIBLE); + } + }); + clearCardView(); + mExecutionService.loadHistory(selected.type); + loadDataFromDB(selected.type); + runOnUiThread(new Runnable() { + @Override + public void run() { + reloadButton.setVisibility(View.VISIBLE); + syncButton.setVisibility(View.VISIBLE); + statusView.setVisibility(View.GONE); + } + }); + } + }); + } + }); + + syncButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + mHandler.post(new Runnable() { + @Override + public void run() { + runOnUiThread(new Runnable() { + @Override + public void run() { + reloadButton.setVisibility(View.GONE); + syncButton.setVisibility(View.GONE); + statusView.setVisibility(View.VISIBLE); + } + }); + DanaRNSHistorySync sync = new DanaRNSHistorySync(historyList); + sync.sync(DanaRNSHistorySync.SYNC_ALL); + runOnUiThread(new Runnable() { + @Override + public void run() { + reloadButton.setVisibility(View.VISIBLE); + syncButton.setVisibility(View.VISIBLE); + statusView.setVisibility(View.GONE); + } + }); + } + }); + } + }); + + historyTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + TypeList selected = (TypeList) historyTypeSpinner.getSelectedItem(); + loadDataFromDB(selected.type); + showingType = selected.type; + } + + @Override + public void onNothingSelected(AdapterView parent) { + clearCardView(); + } + }); + profile = ConfigBuilderPlugin.getActiveProfile().getProfile(); + if (profile == null) { + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.noprofile)); + finish(); + } + } + + public static class RecyclerViewAdapter extends RecyclerView.Adapter { + + List historyList; + + RecyclerViewAdapter(List historyList) { + this.historyList = historyList; + } + + @Override + public HistoryViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { + View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.danar_history_item, viewGroup, false); + return new HistoryViewHolder(v); + } + + @Override + public void onBindViewHolder(HistoryViewHolder holder, int position) { + DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); + DanaRHistoryRecord record = historyList.get(position); + holder.time.setText(df.format(new Date(record.getRecordDate()))); + holder.value.setText(DecimalFormatter.to2Decimal(record.getRecordValue())); + holder.stringvalue.setText(record.getStringRecordValue()); + holder.bolustype.setText(record.getBolusType()); + holder.duration.setText(DecimalFormatter.to0Decimal(record.getRecordDuration())); + holder.alarm.setText(record.getRecordAlarm()); + switch (showingType) { + case RecordTypes.RECORD_TYPE_ALARM: + holder.time.setVisibility(View.VISIBLE); + holder.value.setVisibility(View.VISIBLE); + holder.stringvalue.setVisibility(View.GONE); + holder.bolustype.setVisibility(View.GONE); + holder.duration.setVisibility(View.GONE); + holder.dailybasal.setVisibility(View.GONE); + holder.dailybolus.setVisibility(View.GONE); + holder.dailytotal.setVisibility(View.GONE); + holder.alarm.setVisibility(View.VISIBLE); + break; + case RecordTypes.RECORD_TYPE_BOLUS: + holder.time.setVisibility(View.VISIBLE); + holder.value.setVisibility(View.VISIBLE); + holder.stringvalue.setVisibility(View.GONE); + holder.bolustype.setVisibility(View.VISIBLE); + holder.duration.setVisibility(View.VISIBLE); + holder.dailybasal.setVisibility(View.GONE); + holder.dailybolus.setVisibility(View.GONE); + holder.dailytotal.setVisibility(View.GONE); + holder.alarm.setVisibility(View.GONE); + break; + case RecordTypes.RECORD_TYPE_DAILY: + df = DateFormat.getDateInstance(DateFormat.SHORT); + holder.dailybasal.setText(DecimalFormatter.to2Decimal(record.getRecordDailyBasal()) + "U"); + holder.dailybolus.setText(DecimalFormatter.to2Decimal(record.getRecordDailyBolus()) + "U"); + holder.dailytotal.setText(DecimalFormatter.to2Decimal(record.getRecordDailyBolus()+ record.getRecordDailyBasal()) + "U"); + holder.time.setText(df.format(new Date(record.getRecordDate()))); + holder.time.setVisibility(View.VISIBLE); + holder.value.setVisibility(View.GONE); + holder.stringvalue.setVisibility(View.GONE); + holder.bolustype.setVisibility(View.GONE); + holder.duration.setVisibility(View.GONE); + holder.dailybasal.setVisibility(View.VISIBLE); + holder.dailybolus.setVisibility(View.VISIBLE); + holder.dailytotal.setVisibility(View.VISIBLE); + holder.alarm.setVisibility(View.GONE); + break; + case RecordTypes.RECORD_TYPE_GLUCOSE: + holder.value.setText(NSProfile.toUnitsString(record.getRecordValue(), record.getRecordValue() * Constants.MGDL_TO_MMOLL, profile.getUnits())); + // rest is the same + case RecordTypes.RECORD_TYPE_CARBO: + case RecordTypes.RECORD_TYPE_BASALHOUR: + case RecordTypes.RECORD_TYPE_ERROR: + case RecordTypes.RECORD_TYPE_PRIME: + case RecordTypes.RECORD_TYPE_REFILL: + case RecordTypes.RECORD_TYPE_TB: + holder.time.setVisibility(View.VISIBLE); + holder.value.setVisibility(View.VISIBLE); + holder.stringvalue.setVisibility(View.GONE); + holder.bolustype.setVisibility(View.GONE); + holder.duration.setVisibility(View.GONE); + holder.dailybasal.setVisibility(View.GONE); + holder.dailybolus.setVisibility(View.GONE); + holder.dailytotal.setVisibility(View.GONE); + holder.alarm.setVisibility(View.GONE); + break; + case RecordTypes.RECORD_TYPE_SUSPEND: + holder.time.setVisibility(View.VISIBLE); + holder.value.setVisibility(View.GONE); + holder.stringvalue.setVisibility(View.VISIBLE); + holder.bolustype.setVisibility(View.GONE); + holder.duration.setVisibility(View.GONE); + holder.dailybasal.setVisibility(View.GONE); + holder.dailybolus.setVisibility(View.GONE); + holder.dailytotal.setVisibility(View.GONE); + holder.alarm.setVisibility(View.GONE); + break; + } + } + + @Override + public int getItemCount() { + return historyList.size(); + } + + @Override + public void onAttachedToRecyclerView(RecyclerView recyclerView) { + super.onAttachedToRecyclerView(recyclerView); + } + + public static class HistoryViewHolder extends RecyclerView.ViewHolder { + CardView cv; + TextView time; + TextView value; + TextView bolustype; + TextView stringvalue; + TextView duration; + TextView dailybasal; + TextView dailybolus; + TextView dailytotal; + TextView alarm; + + HistoryViewHolder(View itemView) { + super(itemView); + cv = (CardView) itemView.findViewById(R.id.danar_history_cardview); + time = (TextView) itemView.findViewById(R.id.danar_history_time); + value = (TextView) itemView.findViewById(R.id.danar_history_value); + bolustype = (TextView) itemView.findViewById(R.id.danar_history_bolustype); + stringvalue = (TextView) itemView.findViewById(R.id.danar_history_stringvalue); + duration = (TextView) itemView.findViewById(R.id.danar_history_duration); + dailybasal = (TextView) itemView.findViewById(R.id.danar_history_dailybasal); + dailybolus = (TextView) itemView.findViewById(R.id.danar_history_dailybolus); + dailytotal = (TextView) itemView.findViewById(R.id.danar_history_dailytotal); + alarm = (TextView) itemView.findViewById(R.id.danar_history_alarm); + } + } + } + + private void loadDataFromDB(byte type) { + try { + Dao dao = MainApp.getDbHelper().getDaoDanaRHistory(); + QueryBuilder queryBuilder = dao.queryBuilder(); + queryBuilder.orderBy("recordDate", false); + Where where = queryBuilder.where(); + where.eq("recordCode", type); + queryBuilder.limit(200L); + PreparedQuery preparedQuery = queryBuilder.prepare(); + historyList = dao.query(preparedQuery); + } catch (SQLException e) { + e.printStackTrace(); + historyList = new ArrayList<>(); + } + runOnUiThread(new Runnable() { + @Override + public void run() { + recyclerView.swapAdapter(new RecyclerViewAdapter(historyList), false); + } + }); + } + + private void clearCardView() { + historyList = new ArrayList<>(); + runOnUiThread(new Runnable() { + @Override + public void run() { + recyclerView.swapAdapter(new RecyclerViewAdapter(historyList), false); + } + }); + } + + @Subscribe + public void onStatusEvent(final EventDanaRSyncStatus s) { + log.debug("EventDanaRSyncStatus: " + s.message); + runOnUiThread( + new Runnable() { + @Override + public void run() { + statusView.setText(s.message); + } + }); + } + + @Subscribe + public void onStatusEvent(final EventDanaRConnectionStatus c) { + runOnUiThread( + new Runnable() { + @Override + public void run() { + if (c.sStatus == EventDanaRConnectionStatus.CONNECTING) { + statusView.setText(String.format(getString(R.string.danar_history_connectingfor), c.sSecondsElapsed)); + log.debug("EventDanaRConnectionStatus: " + "Connecting for " + c.sSecondsElapsed + "s"); + } else if (c.sStatus == EventDanaRConnectionStatus.CONNECTED) { + statusView.setText(MainApp.sResources.getString(R.string.connected)); + log.debug("EventDanaRConnectionStatus: Connected"); + } else { + statusView.setText(MainApp.sResources.getString(R.string.disconnected)); + log.debug("EventDanaRConnectionStatus: Disconnected"); + } + } + } + ); + } + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/SerialIOThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/SerialIOThread.java new file mode 100644 index 0000000000..ed53de2815 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/SerialIOThread.java @@ -0,0 +1,224 @@ +package info.nightscout.androidaps.plugins.DanaRKorean; + +import android.bluetooth.BluetoothSocket; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MessageHashTable; +import info.nightscout.utils.CRC; + +/** + * Created by mike on 17.07.2016. + */ +public class SerialIOThread extends Thread { + private static Logger log = LoggerFactory.getLogger(SerialIOThread.class); + + private InputStream mInputStream = null; + private OutputStream mOutputStream = null; + private BluetoothSocket mRfCommSocket; + + private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor(); + private static ScheduledFuture scheduledDisconnection = null; + + private boolean mKeepRunning = true; + private byte[] mReadBuff = new byte[0]; + + MessageBase processedMessage; + + public SerialIOThread(BluetoothSocket rfcommSocket) { + super(SerialIOThread.class.toString()); + + mRfCommSocket = rfcommSocket; + try { + mOutputStream = mRfCommSocket.getOutputStream(); + mInputStream = mRfCommSocket.getInputStream(); + } catch (IOException e) { + e.printStackTrace(); + } + this.start(); + } + + @Override + public final void run() { + try { + while (mKeepRunning) { + int availableBytes = mInputStream.available(); + // Ask for 1024 byte (or more if available) + byte[] newData = new byte[Math.max(1024, availableBytes)]; + int gotBytes = mInputStream.read(newData); + // When we are here there is some new data available + appendToBuffer(newData, gotBytes); + + // process all messages we already got + while (mReadBuff.length > 3) { // 3rd byte is packet size. continue only if we an determine packet size + byte[] extractedBuff = cutMessageFromBuffer(); + if (extractedBuff == null) break; // message is not complete in buffer (wrong packet calls disconnection) + + int command = (extractedBuff[5] & 0xFF) | ((extractedBuff[4] << 8) & 0xFF00); + + MessageBase message; + if (processedMessage != null && processedMessage.getCommand() == command) { + message = processedMessage; + } else { + // get it from hash table + message = MessageHashTable.findMessage(command); + } + + if (Config.logDanaMessageDetail) + log.debug("<<<<< " + message.getMessageName() + " " + message.toHexString(extractedBuff)); + + // process the message content + message.received = true; + message.handleMessage(extractedBuff); + synchronized (message) { + message.notify(); + } + scheduleDisconnection(); + } + } + } catch (Exception e) { + if (Config.logDanaSerialEngine && e.getMessage().indexOf("bt socket closed") < 0) + log.error("Thread exception: ", e); + mKeepRunning = false; + } + disconnect("EndOfLoop"); + } + + void appendToBuffer(byte[] newData, int gotBytes) { + // add newData to mReadBuff + byte[] newReadBuff = new byte[mReadBuff.length + gotBytes]; + System.arraycopy(mReadBuff, 0, newReadBuff, 0, mReadBuff.length); + System.arraycopy(newData, 0, newReadBuff, mReadBuff.length, gotBytes); + mReadBuff = newReadBuff; + } + + byte[] cutMessageFromBuffer() { + if (mReadBuff[0] == (byte) 0x7E && mReadBuff[1] == (byte) 0x7E) { + int length = (mReadBuff[2] & 0xFF) + 7; + // Check if we have enough data + if (mReadBuff.length < length) { + return null; + } + if (mReadBuff[length - 2] != (byte) 0x2E || mReadBuff[length - 1] != (byte) 0x2E) { + log.error("wrong packet lenght=" + length + " data " + MessageBase.toHexString(mReadBuff)); + disconnect("wrong packet"); + return null; + } + + short crc = CRC.getCrc16(mReadBuff, 3, length - 7); + byte crcByte0 = (byte) (crc >> 8 & 0xFF); + byte crcByte1 = (byte) (crc & 0xFF); + + byte crcByte0received = mReadBuff[length - 4]; + byte crcByte1received = mReadBuff[length - 3]; + + if (crcByte0 != crcByte0received || crcByte1 != crcByte1received) { + log.error("CRC Error" + String.format("%02x ", crcByte0) + String.format("%02x ", crcByte1) + String.format("%02x ", crcByte0received) + String.format("%02x ", crcByte1received)); + disconnect("crc error"); + return null; + } + // Packet is verified here. extract data + byte[] extractedBuff = new byte[length]; + System.arraycopy(mReadBuff, 0, extractedBuff, 0, length); + // remove extracted data from read buffer + byte[] unprocessedData = new byte[mReadBuff.length - length]; + System.arraycopy(mReadBuff, length, unprocessedData, 0, unprocessedData.length); + mReadBuff = unprocessedData; + return extractedBuff; + } else { + log.error("Wrong beginning of packet len=" + mReadBuff.length + " " + MessageBase.toHexString(mReadBuff)); + disconnect("Wrong beginning of packet"); + return null; + } + } + + public synchronized void sendMessage(MessageBase message) { + if (!mRfCommSocket.isConnected()) { + log.error("Socket not connected on sendMessage"); + return; + } + processedMessage = message; + + byte[] messageBytes = message.getRawMessageBytes(); + if (Config.logDanaSerialEngine) + log.debug(">>>>> " + message.getMessageName() + " " + message.toHexString(messageBytes)); + + try { + mOutputStream.write(messageBytes); + } catch (Exception e) { + log.error("sendMessage write exception: ", e); + e.printStackTrace(); + } + + synchronized (message) { + try { + message.wait(5000); + } catch (InterruptedException e) { + log.error("sendMessage InterruptedException", e); + e.printStackTrace(); + } + } + + try { + Thread.sleep(200); + } catch (InterruptedException e) { + } + if (!message.received) { + log.warn("Reply not received " + message.getMessageName()); + } + scheduleDisconnection(); + } + + public void scheduleDisconnection() { + class DisconnectRunnable implements Runnable { + public void run() { + disconnect("scheduleDisconnection"); + scheduledDisconnection = null; + } + } + // prepare task for execution in 5 sec + // cancel waiting task to prevent sending multiple disconnections + if (scheduledDisconnection != null) + scheduledDisconnection.cancel(false); + Runnable task = new DisconnectRunnable(); + final int sec = 5; + scheduledDisconnection = worker.schedule(task, sec, TimeUnit.SECONDS); + } + + public void disconnect(String reason) { + mKeepRunning = false; + try { + mInputStream.close(); + } catch (Exception e) { + if (Config.logDanaSerialEngine) log.debug(e.getMessage()); + } + try { + mOutputStream.close(); + } catch (Exception e) { + if (Config.logDanaSerialEngine) log.debug(e.getMessage()); + } + try { + mRfCommSocket.close(); + } catch (Exception e) { + if (Config.logDanaSerialEngine) log.debug(e.getMessage()); + } + try { + System.runFinalization(); + } catch (Exception e) { + if (Config.logDanaSerialEngine) log.debug(e.getMessage()); + } + if (Config.logDanaSerialEngine) log.debug("Disconnected: " + reason); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java new file mode 100644 index 0000000000..bec87acd5f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java @@ -0,0 +1,488 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.Services; + +import android.app.Service; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothSocket; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.SharedPreferences; +import android.os.Binder; +import android.os.IBinder; +import android.os.PowerManager; +import android.preference.PreferenceManager; + +import com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Calendar; +import java.util.Date; +import java.util.Set; +import java.util.UUID; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.events.EventAppExit; +import info.nightscout.androidaps.events.EventPreferenceChange; +import info.nightscout.androidaps.plugins.DanaR.comm.*; +import info.nightscout.androidaps.plugins.DanaR.comm.MsgSetTime; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRBolusStart; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; +import info.nightscout.androidaps.plugins.DanaRKorean.SerialIOThread; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.*; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgBolusProgress; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgBolusStart; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgBolusStop; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgCheckValue; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgHistoryAlarm; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgHistoryBasalHour; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgHistoryBolus; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgHistoryCarbo; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgHistoryDailyInsulin; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgHistoryDone; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgHistoryError; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgHistoryGlucose; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgHistoryRefill; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgHistorySuspend; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgPCCommStart; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgPCCommStop; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSetCarbsEntry; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSetExtendedBolusStart; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSetExtendedBolusStop; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSetSingleBasalProfile; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSetTempBasalStart; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSetTempBasalStop; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSettingBasal; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSettingGlucose; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSettingMaxValues; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSettingProfileRatios; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSettingPumpTime; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSettingShippingInfo; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgStatusBasic; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgStatusBolusExtended; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgStatusTempBasal; +import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.SafeParse; +import info.nightscout.utils.ToastUtils; + +public class ExecutionService extends Service { + private static Logger log = LoggerFactory.getLogger(ExecutionService.class); + + private SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + private String devName; + + private SerialIOThread mSerialIOThread; + private BluetoothSocket mRfcommSocket; + private BluetoothDevice mBTDevice; + + private PowerManager.WakeLock mWakeLock; + private IBinder mBinder = new LocalBinder(); + + private DanaRKoreanPump danaRKoreanPump; + private Treatment bolusingTreatment = null; + + private static Boolean connectionInProgress = false; + private static final Object connectionLock = new Object(); + + private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); + + private BroadcastReceiver receiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + String action = intent.getAction(); + if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { + log.debug("Device has disconnected " + device.getName());//Device has disconnected + if (mBTDevice != null && mBTDevice.getName().equals(device.getName())) { + if (mSerialIOThread != null) { + mSerialIOThread.disconnect("BT disconnection broadcast"); + } + MainApp.bus().post(new EventDanaRConnectionStatus(EventDanaRConnectionStatus.DISCONNECTED, 0)); + } + } + } + }; + + public ExecutionService() { + registerBus(); + MainApp.instance().getApplicationContext().registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); + danaRKoreanPump = DanaRKoreanPlugin.getDanaRPump(); + + PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE); + mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ExecutionService"); + } + + public class LocalBinder extends Binder { + public ExecutionService getServiceInstance() { + return ExecutionService.this; + } + } + + @Override + public IBinder onBind(Intent intent) { + return mBinder; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + + return START_STICKY; + } + + private void registerBus() { + try { + MainApp.bus().unregister(this); + } catch (RuntimeException x) { + // Ignore + } + MainApp.bus().register(this); + } + + @Subscribe + public void onStatusEvent(EventAppExit event) { + if (Config.logFunctionCalls) + log.debug("EventAppExit received"); + + if (mSerialIOThread != null) + mSerialIOThread.disconnect("Application exit"); + + MainApp.instance().getApplicationContext().unregisterReceiver(receiver); + + stopSelf(); + if (Config.logFunctionCalls) + log.debug("EventAppExit finished"); + } + + public boolean isConnected() { + return mRfcommSocket != null && mRfcommSocket.isConnected(); + } + + public boolean isConnecting() { + return connectionInProgress; + } + + public void disconnect(String from) { + if (mSerialIOThread != null) + mSerialIOThread.disconnect(from); + } + + public void connect(String from) { + if (danaRKoreanPump.password != -1 && danaRKoreanPump.password != SafeParse.stringToInt(SP.getString("danar_password", "-1"))) { + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.wrongpumppassword), R.raw.error); + return; + } + while (isConnected() || isConnecting()) { + if (Config.logDanaBTComm) + log.debug("already connected/connecting from: " + from); + waitMsec(3000); + } + final long maxConnectionTime = 5 * 60 * 1000L; // 5 min + synchronized (connectionLock) { + //log.debug("entering connection while loop"); + connectionInProgress = true; + mWakeLock.acquire(); + getBTSocketForSelectedPump(); + if (mRfcommSocket == null || mBTDevice == null) + return; // Device not found + long startTime = new Date().getTime(); + while (!isConnected() && startTime + maxConnectionTime >= new Date().getTime()) { + long secondsElapsed = (new Date().getTime() - startTime) / 1000L; + MainApp.bus().post(new EventDanaRConnectionStatus(EventDanaRConnectionStatus.CONNECTING, (int) secondsElapsed)); + if (Config.logDanaBTComm) + log.debug("connect waiting " + secondsElapsed + "sec from: " + from); + try { + mRfcommSocket.connect(); + } catch (IOException e) { + //e.printStackTrace(); + if (e.getMessage().contains("socket closed")) { + e.printStackTrace(); + break; + } + } + waitMsec(1000); + + if (isConnected()) { + if (mSerialIOThread != null) { + mSerialIOThread.disconnect("Recreate SerialIOThread"); + } + mSerialIOThread = new SerialIOThread(mRfcommSocket); + MainApp.bus().post(new EventDanaRConnectionStatus(EventDanaRConnectionStatus.CONNECTED, 0)); + if (!getPumpStatus()) { + mSerialIOThread.disconnect("getPumpStatus failed"); + waitMsec(3000); + } + } + } + if (!isConnected()) { + MainApp.bus().post(new EventDanaRConnectionStatus(EventDanaRConnectionStatus.DISCONNECTED, 0)); + log.error("Pump connection timed out"); + } + connectionInProgress = false; + mWakeLock.release(); + } + } + + private void getBTSocketForSelectedPump() { + devName = SP.getString("danar_bt_name", ""); + BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + + if (bluetoothAdapter != null) { + Set bondedDevices = bluetoothAdapter.getBondedDevices(); + + for (BluetoothDevice device : bondedDevices) { + if (devName.equals(device.getName())) { + mBTDevice = device; + try { + mRfcommSocket = mBTDevice.createRfcommSocketToServiceRecord(SPP_UUID); + } catch (IOException e) { + log.error("Error creating socket: ", e); + } + break; + } + } + } else { + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.nobtadapter)); + } + if (mBTDevice == null) { + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.devicenotfound)); + } + } + + @Subscribe + public void onStatusEvent(final EventPreferenceChange pch) { + if (mSerialIOThread != null) + mSerialIOThread.disconnect("EventPreferenceChange"); + } + + private boolean getPumpStatus() { + try { + //MsgStatus statusMsg = new MsgStatus(); + MsgStatusBasic statusBasicMsg = new MsgStatusBasic(); + MsgStatusTempBasal tempStatusMsg = new MsgStatusTempBasal(); + MsgStatusBolusExtended exStatusMsg = new MsgStatusBolusExtended(); + + + mSerialIOThread.sendMessage(new MsgSettingShippingInfo()); // TODO: show it somewhere + mSerialIOThread.sendMessage(tempStatusMsg); // do this before statusBasic because here is temp duration + mSerialIOThread.sendMessage(exStatusMsg); + //mSerialIOThread.sendMessage(statusMsg); + mSerialIOThread.sendMessage(statusBasicMsg); + + mSerialIOThread.sendMessage(new MsgCheckValue()); + +// if (!statusMsg.received) { +// mSerialIOThread.sendMessage(statusMsg); +// } + if (!statusBasicMsg.received) { + mSerialIOThread.sendMessage(statusBasicMsg); + } + if (!tempStatusMsg.received) { + // Load of status of current basal rate failed, give one more try + mSerialIOThread.sendMessage(tempStatusMsg); + } + if (!exStatusMsg.received) { + // Load of status of current extended bolus failed, give one more try + mSerialIOThread.sendMessage(exStatusMsg); + } + + // Check we have really current status of pump + if (/*!statusMsg.received || */!statusBasicMsg.received || !tempStatusMsg.received || !exStatusMsg.received) { + waitMsec(10 * 1000); + log.debug("getPumpStatus failed"); + return false; + } + + Date now = new Date(); + if (danaRKoreanPump.lastSettingsRead.getTime() + 60 * 60 * 1000L < now.getTime()) { + mSerialIOThread.sendMessage(new MsgSettingShippingInfo()); + //0x3203 + mSerialIOThread.sendMessage(new MsgSettingBasal()); + //0x3201 + mSerialIOThread.sendMessage(new MsgSettingMaxValues()); + mSerialIOThread.sendMessage(new MsgSettingGlucose()); + mSerialIOThread.sendMessage(new MsgSettingPumpTime()); + mSerialIOThread.sendMessage(new MsgSettingProfileRatios()); + mSerialIOThread.sendMessage(new MsgSetTime(new Date())); + danaRKoreanPump.lastSettingsRead = now; + } + + danaRKoreanPump.lastConnection = now; + MainApp.bus().post(new EventDanaRNewStatus()); + } catch (Exception e) { + e.printStackTrace(); + } + return true; + } + + public boolean tempBasal(int percent, int durationInHours) { + connect("tempBasal"); + if (!isConnected()) return false; + mSerialIOThread.sendMessage(new MsgSetTempBasalStart(percent, durationInHours)); + mSerialIOThread.sendMessage(new MsgStatusTempBasal()); + return true; + } + + public boolean tempBasalStop() { + connect("tempBasalStop"); + if (!isConnected()) return false; + mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); + mSerialIOThread.sendMessage(new MsgStatusTempBasal()); + return true; + } + + public boolean extendedBolus(double insulin, int durationInHalfHours) { + connect("extendedBolus"); + if (!isConnected()) return false; + mSerialIOThread.sendMessage(new MsgSetExtendedBolusStart(insulin, (byte) (durationInHalfHours & 0xFF))); + mSerialIOThread.sendMessage(new MsgStatusBolusExtended()); + return true; + } + + public boolean extendedBolusStop() { + connect("extendedBolusStop"); + if (!isConnected()) return false; + mSerialIOThread.sendMessage(new MsgSetExtendedBolusStop()); + mSerialIOThread.sendMessage(new MsgStatusBolusExtended()); + return true; + } + + public boolean bolus(Double amount, int carbs, Treatment t) { + bolusingTreatment = t; + MsgBolusStart start = new MsgBolusStart(amount); + MsgBolusProgress progress = new MsgBolusProgress(MainApp.bus(), amount, t); + MsgBolusStop stop = new MsgBolusStop(MainApp.bus(), amount, t); + + connect("bolus"); + if (!isConnected()) return false; + + if (carbs > 0) { + Calendar time = Calendar.getInstance(); + mSerialIOThread.sendMessage(new MsgSetCarbsEntry(time, carbs)); + } + MainApp.bus().post(new EventDanaRBolusStart()); + + if (!stop.stopped) { + mSerialIOThread.sendMessage(start); + } else { + t.insulin = 0d; + return false; + } + while (!stop.stopped && !start.failed) { + waitMsec(100); + } + waitMsec(300); + bolusingTreatment = null; + getPumpStatus(); + return true; + } + + public void bolusStop() { + if (Config.logDanaBTComm) + log.debug("bolusStop >>>>> @ " + (bolusingTreatment == null ? "" : bolusingTreatment.insulin)); + MsgBolusStop stop = new MsgBolusStop(); + stop.forced = true; + if (isConnected()) { + mSerialIOThread.sendMessage(stop); + while (!stop.stopped) { + mSerialIOThread.sendMessage(stop); + waitMsec(200); + } + } else { + stop.stopped = true; + } + } + + public boolean carbsEntry(int amount) { + connect("carbsEntry"); + if (!isConnected()) return false; + Calendar time = Calendar.getInstance(); + MsgSetCarbsEntry msg = new MsgSetCarbsEntry(time, amount); + mSerialIOThread.sendMessage(msg); + return true; + } + + public boolean loadHistory(byte type) { + connect("loadHistory"); + if (!isConnected()) return false; + MessageBase msg = null; + switch (type) { + case RecordTypes.RECORD_TYPE_ALARM: + msg = new MsgHistoryAlarm(); + break; + case RecordTypes.RECORD_TYPE_BASALHOUR: + msg = new MsgHistoryBasalHour(); + break; + case RecordTypes.RECORD_TYPE_BOLUS: + msg = new MsgHistoryBolus(); + break; + case RecordTypes.RECORD_TYPE_CARBO: + msg = new MsgHistoryCarbo(); + break; + case RecordTypes.RECORD_TYPE_DAILY: + msg = new MsgHistoryDailyInsulin(); + break; + case RecordTypes.RECORD_TYPE_ERROR: + msg = new MsgHistoryError(); + break; + case RecordTypes.RECORD_TYPE_GLUCOSE: + msg = new MsgHistoryGlucose(); + break; + case RecordTypes.RECORD_TYPE_REFILL: + msg = new MsgHistoryRefill(); + break; + case RecordTypes.RECORD_TYPE_SUSPEND: + msg = new MsgHistorySuspend(); + break; + } + MsgHistoryDone done = new MsgHistoryDone(); + mSerialIOThread.sendMessage(new MsgPCCommStart()); + waitMsec(400); + mSerialIOThread.sendMessage(msg); + while (!done.received && mRfcommSocket.isConnected()) { + waitMsec(100); + } + waitMsec(200); + mSerialIOThread.sendMessage(new MsgPCCommStop()); + return true; + } + + public boolean updateBasalsInPump(final NSProfile profile) { + connect("updateBasalsInPump"); + if (!isConnected()) return false; + double[] basal = buildDanaRProfileRecord(profile); + MsgSetSingleBasalProfile msgSet = new MsgSetSingleBasalProfile(basal); + mSerialIOThread.sendMessage(msgSet); + danaRKoreanPump.lastSettingsRead = new Date(0); // force read full settings + getPumpStatus(); + return true; + } + + private double[] buildDanaRProfileRecord(NSProfile nsProfile) { + double[] record = new double[24]; + for (Integer hour = 0; hour < 24; hour++) { + double value = nsProfile.getBasal(hour * 60 * 60); + if (Config.logDanaMessageDetail) + log.debug("NS basal value for " + hour + ":00 is " + value); + record[hour] = value; + } + return record; + } + + private void waitMsec(long msecs) { + try { + Thread.sleep(msecs); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MessageHashTable.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MessageHashTable.java new file mode 100644 index 0000000000..6effa0e374 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MessageHashTable.java @@ -0,0 +1,79 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; + +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +/** + * Created by mike on 28.05.2016. + */ +public class MessageHashTable { + private static Logger log = LoggerFactory.getLogger(MessageHashTable.class); + + public static HashMap messages = null; + + static { + if (messages == null) { + messages = new HashMap(); + put(new MsgBolusStop()); // 0x0101 CMD_MEALINS_STOP + put(new MsgBolusStart()); // 0x0102 CMD_MEALINS_START_DATA + put(new MsgBolusProgress()); // 0x0202 CMD_PUMP_THIS_REMAINDER_MEAL_INS + put(new MsgStatusProfile()); // 0x0204 CMD_PUMP_CALCULATION_SETTING + put(new MsgStatusTempBasal()); // 0x0205 CMD_PUMP_EXERCISE_MODE + put(new MsgStatusBolusExtended()); // 0x0207 CMD_PUMP_EXPANS_INS_I + put(new MsgStatusBasic()); // 0x020A CMD_PUMP_INITVIEW_I + put(new MsgStatus()); // 0x020B CMD_PUMP_STATUS + put(new MsgInitConnStatusTime()); // 0x0301 CMD_PUMPINIT_TIME_INFO + put(new MsgInitConnStatusBolus()); // 0x0302 CMD_PUMPINIT_BOLUS_INFO + put(new MsgInitConnStatusBasic()); // 0x0303 CMD_PUMPINIT_INIT_INFO + put(new MsgSetTempBasalStart()); // 0x0401 CMD_PUMPSET_EXERCISE_S + put(new MsgSetCarbsEntry()); // 0x0402 CMD_PUMPSET_HIS_S + put(new MsgSetTempBasalStop()); // 0x0403 CMD_PUMPSET_EXERCISE_STOP + put(new MsgSetExtendedBolusStop()); // 0x0406 CMD_PUMPSET_EXPANS_INS_STOP + put(new MsgSetExtendedBolusStart()); // 0x0407 CMD_PUMPSET_EXPANS_INS_S + put(new MsgError()); // 0x0601 CMD_PUMPOWAY_SYSTEM_STATUS + put(new MsgPCCommStart()); // 0x3001 CMD_CONNECT + put(new MsgPCCommStop()); // 0x3002 CMD_DISCONNECT + put(new MsgHistoryBolus()); // 0x3101 CMD_HISTORY_MEAL_INS + put(new MsgHistoryDailyInsulin()); // 0x3102 CMD_HISTORY_DAY_INS + put(new MsgHistoryGlucose()); // 0x3104 CMD_HISTORY_GLUCOSE + put(new MsgHistoryAlarm()); // 0x3105 CMD_HISTORY_ALARM + put(new MsgHistoryError()); // 0x3106 CMD_HISTORY_ERROR + put(new MsgHistoryCarbo()); // 0x3107 CMD_HISTORY_CARBOHY + put(new MsgHistoryRefill()); // 0x3108 CMD_HISTORY_REFILL + put(new MsgHistorySuspend()); // 0x3109 CMD_HISTORY_SUSPEND + put(new MsgHistoryBasalHour()); // 0x310A CMD_HISTORY_BASAL_HOUR + put(new MsgHistoryDone()); // 0x31F1 CMD_HISTORY_DONT_USED + put(new MsgSettingBasal()); // 0x3202 CMD_SETTING_V_BASAL_INS_I + put(new MsgSettingProfileRatios()); // 0x3204 CMD_SETTING_V_CCC_I + put(new MsgSettingMaxValues()); // 0x3205 CMD_SETTING_V_MAX_VALUE_I + put(new MsgSettingBasalProfileAll()); // 0x3206 CMD_SETTING_V_BASAL_PROFILE_ALL + put(new MsgSettingShippingInfo()); // 0x3207 CMD_SETTING_V_SHIPPING_I + put(new MsgSettingGlucose()); // 0x3209 CMD_SETTING_V_GLUCOSEandEASY + put(new MsgSettingPumpTime()); // 0x320A CMD_SETTING_V_TIME_I + put(new MsgSetSingleBasalProfile()); // 0x3302 CMD_SETTING_BASAL_INS_S + put(new MsgHistoryAll()); // 0x41F2 CMD_HISTORY_ALL + put(new MsgHistoryNewDone()); // 0x42F1 CMD_HISTORY_NEW_DONE + put(new MsgHistoryNew()); // 0x42F2 CMD_HISTORY_NEW + put(new MsgCheckValue()); // 0xF0F1 CMD_PUMP_CHECK_VALUE + } + } + + public static void put(MessageBase message) { + int command = message.getCommand(); + //String name = MessageOriginalNames.getName(command); + messages.put(command, message); + //log.debug(String.format("%04x ", command) + " " + name); + } + + public static MessageBase findMessage(Integer command) { + if (messages.containsKey(command)) { + return messages.get(command); + } else { + return new MessageBase(); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgBolusProgress.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgBolusProgress.java new file mode 100644 index 0000000000..05db56bc26 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgBolusProgress.java @@ -0,0 +1,51 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import com.squareup.otto.Bus; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; + +public class MsgBolusProgress extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgBolusProgress.class); + private static Bus bus = null; + + private static Treatment t; + private static double amount; + + public int progress = -1; + + public MsgBolusProgress() { + SetCommand(0x0202); + } + + public MsgBolusProgress(Bus bus, double amount, Treatment t) { + this(); + this.amount = amount; + this.t = t; + this.bus = bus; + } + + @Override + public void handleMessage(byte[] bytes) { + progress = intFromBuff(bytes, 0, 2); + Double done = (amount * 100 - progress) / 100d; + t.insulin = done; + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); + bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivering), done); + bolusingEvent.t = t; + bolusingEvent.percent = Math.min((int) (done / amount * 100), 100); + + if (Config.logDanaMessageDetail) { + log.debug("Bolus remaining: " + progress + " delivered: " + done); + } + + bus.post(bolusingEvent); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgBolusStart.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgBolusStart.java new file mode 100644 index 0000000000..04e58349e3 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgBolusStart.java @@ -0,0 +1,40 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.BuildConfig; +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +public class MsgBolusStart extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgBolusStart.class); + + public MsgBolusStart() { + SetCommand(0x0102); + } + + public MsgBolusStart(double amount) { + this(); + + // HARDCODED LIMIT + amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); + if (amount < 0) amount = 0d; + if (amount > BuildConfig.MAXBOLUS) amount = BuildConfig.MAXBOLUS; + + AddParamInt((int) (amount * 100)); + } + + @Override + public void handleMessage(byte[] bytes) { + int result = intFromBuff(bytes, 0, 1); + if (result != 2) { + failed = true; + log.debug("Messsage response: " + result + " FAILED!!"); + } else { + if (Config.logDanaMessageDetail) + log.debug("Messsage response: " + result); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgBolusStop.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgBolusStop.java new file mode 100644 index 0000000000..2a414bbf82 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgBolusStop.java @@ -0,0 +1,49 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import com.squareup.otto.Bus; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; + +public class MsgBolusStop extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgBolusStop.class); + private static Treatment t; + private static Double amount; + private static Bus bus = null; + + public static boolean stopped = false; + public static boolean forced = false; + + public MsgBolusStop() { + SetCommand(0x0101); + stopped = false; + } + + public MsgBolusStop(Bus bus, Double amount, Treatment t) { + this(); + this.bus = bus; + this.t = t; + this.amount = amount; + forced = false; + } + + @Override + public void handleMessage(byte[] bytes) { + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); + stopped = true; + if (!forced) { + t.insulin = amount; + bolusingEvent.status = MainApp.sResources.getString(R.string.overview_bolusprogress_delivered); + bolusingEvent.percent = 100; + } else { + bolusingEvent.status = MainApp.sResources.getString(R.string.overview_bolusprogress_stoped); + } + bus.post(bolusingEvent); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgCheckValue.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgCheckValue.java new file mode 100644 index 0000000000..0a9ea6f2aa --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgCheckValue.java @@ -0,0 +1,44 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; +import info.nightscout.utils.ToastUtils; + +/** + * Created by mike on 30.06.2016. + */ +public class MsgCheckValue extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgCheckValue.class); + + public MsgCheckValue() { + SetCommand(0xF0F1); + } + + @Override + public void handleMessage(byte[] bytes) { + DanaRKoreanPump pump = DanaRKoreanPlugin.getDanaRPump(); + + pump.model = intFromBuff(bytes, 0, 1); + pump.protocol = intFromBuff(bytes, 1, 1); + pump.productCode = intFromBuff(bytes, 2, 1); + if (pump.model != DanaRKoreanPump.DOMESTIC_MODEL) { + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(),MainApp.sResources.getString(R.string.wrongpumpdriverselected), R.raw.error); + ((DanaRKoreanPlugin)MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).doDisconnect("Wrong Model"); + log.debug("Wrong model selected"); + } + + if (Config.logDanaMessageDetail) { + log.debug("Model: " + String.format("%02X ", pump.model)); + log.debug("Protocol: " + String.format("%02X ", pump.protocol)); + log.debug("Product Code: " + String.format("%02X ", pump.productCode)); + } + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgError.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgError.java new file mode 100644 index 0000000000..2145a78ef4 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgError.java @@ -0,0 +1,52 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; + +public class MsgError extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgError.class); + + public MsgError() { + SetCommand(0x0601); + } + + @Override + public void handleMessage(byte[] bytes) { + int errorCode = intFromBuff(bytes, 0, 1); + String errorString = ""; + + switch (errorCode) { + case 1: + case 2: + case 3: // Pump error + errorString = MainApp.sResources.getString(R.string.pumperror) + " " + errorCode; + break; + case 4: // Shutdown + errorString = MainApp.sResources.getString(R.string.pumpshutdown); + break; + case 5: // Occlusion + errorString = MainApp.sResources.getString(R.string.occlusion); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); + MsgBolusStop.stopped = true; + bolusingEvent.status = errorString; + MainApp.bus().post(bolusingEvent); + break; + case 7: // Low Battery + errorString = MainApp.sResources.getString(R.string.lowbattery); + break; + case 8: // Battery 0% + errorString = MainApp.sResources.getString(R.string.batterydischarged); + break; + } + if (Config.logDanaMessageDetail) + log.debug("Error detected: " + errorString); + MainApp.getConfigBuilder().uploadError(errorString); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryAlarm.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryAlarm.java new file mode 100644 index 0000000000..a4560c9629 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryAlarm.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryAlarm extends MsgHistoryAll { + private static Logger log = LoggerFactory.getLogger(MsgHistoryAlarm.class); + public MsgHistoryAlarm() { + SetCommand(0x3105); + } + // Handle message taken from MsgHistoryAll +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryAll.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryAll.java new file mode 100644 index 0000000000..4b642c5bc9 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryAll.java @@ -0,0 +1,162 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import com.j256.ormlite.dao.Dao; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.SQLException; +import java.text.DateFormat; +import java.util.Date; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.db.DanaRHistoryRecord; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRSyncStatus; + +public class MsgHistoryAll extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgHistoryAll.class); + + public MsgHistoryAll() { + SetCommand(0x41F2); + } + + @Override + public void handleMessage(byte[] bytes) { + byte recordCode = (byte) intFromBuff(bytes, 0, 1); + Date date = dateFromBuff(bytes, 1); // 3 bytes + Date datetime = dateTimeFromBuff(bytes, 1); // 5 bytes + Date datetimewihtsec = dateTimeSecFromBuff(bytes, 1); // 6 bytes + + double dailyBasal = intFromBuff(bytes, 4, 2) * 0.01d; + double dailyBolus = intFromBuff(bytes, 6, 2) * 0.01d; + byte paramByte5 = (byte) intFromBuff(bytes, 4, 1); + byte paramByte6 = (byte) intFromBuff(bytes, 5, 1); + byte paramByte7 = (byte) intFromBuff(bytes, 6, 1); + byte paramByte8 = (byte) intFromBuff(bytes, 7, 1); + double value = (double) intFromBuff(bytes, 8, 2); + + EventDanaRSyncStatus ev = new EventDanaRSyncStatus(); + + DanaRHistoryRecord danaRHistoryRecord = new DanaRHistoryRecord(); + + danaRHistoryRecord.setRecordCode(recordCode); + danaRHistoryRecord.setBytes(bytes); + + String messageType = ""; + + switch (recordCode) { + case RecordTypes.RECORD_TYPE_BOLUS: + danaRHistoryRecord.setRecordDate(datetime); + switch (0xF0 & paramByte8) { + case 0xA0: + danaRHistoryRecord.setBolusType("DS"); + messageType += "DS bolus"; + break; + case 0xC0: + danaRHistoryRecord.setBolusType("E"); + messageType += "E bolus"; + break; + case 0x80: + danaRHistoryRecord.setBolusType("S"); + messageType += "S bolus"; + break; + case 0x90: + danaRHistoryRecord.setBolusType("DE"); + messageType += "DE bolus"; + break; + default: + danaRHistoryRecord.setBolusType("None"); + break; + } + danaRHistoryRecord.setRecordDuration(((int) paramByte8 & 0x0F) * 60 + (int) paramByte7); + danaRHistoryRecord.setRecordValue(value * 0.01); + break; + case RecordTypes.RECORD_TYPE_DAILY: + messageType += "dailyinsulin"; + danaRHistoryRecord.setRecordDate(date); + danaRHistoryRecord.setRecordDailyBasal(dailyBasal); + danaRHistoryRecord.setRecordDailyBolus(dailyBolus); + break; + case RecordTypes.RECORD_TYPE_PRIME: + messageType += "prime"; + danaRHistoryRecord.setRecordDate(datetimewihtsec); + danaRHistoryRecord.setRecordValue(value * 0.01); + break; + case RecordTypes.RECORD_TYPE_ERROR: + messageType += "error"; + danaRHistoryRecord.setRecordDate(datetimewihtsec); + danaRHistoryRecord.setRecordValue(value * 0.01); + break; + case RecordTypes.RECORD_TYPE_REFILL: + messageType += "refill"; + danaRHistoryRecord.setRecordDate(datetimewihtsec); + danaRHistoryRecord.setRecordValue(value * 0.01); + break; + case RecordTypes.RECORD_TYPE_BASALHOUR: + messageType += "basal hour"; + danaRHistoryRecord.setRecordDate(datetimewihtsec); + danaRHistoryRecord.setRecordValue(value * 0.01); + break; + case RecordTypes.RECORD_TYPE_TB: + messageType += "tb"; + danaRHistoryRecord.setRecordDate(datetimewihtsec); + danaRHistoryRecord.setRecordValue(value * 0.01); + break; + case RecordTypes.RECORD_TYPE_GLUCOSE: + messageType += "glucose"; + danaRHistoryRecord.setRecordDate(datetimewihtsec); + danaRHistoryRecord.setRecordValue(value); + break; + case RecordTypes.RECORD_TYPE_CARBO: + messageType += "carbo"; + danaRHistoryRecord.setRecordDate(datetimewihtsec); + danaRHistoryRecord.setRecordValue(value); + break; + case RecordTypes.RECORD_TYPE_ALARM: + messageType += "alarm"; + danaRHistoryRecord.setRecordDate(datetimewihtsec); + String strAlarm = "None"; + switch ((int) paramByte8) { + case 67: + strAlarm = "Check"; + break; + case 79: + strAlarm = "Occlusion"; + break; + case 66: + strAlarm = "Low Battery"; + break; + case 83: + strAlarm = "Shutdown"; + break; + } + danaRHistoryRecord.setRecordAlarm(strAlarm); + danaRHistoryRecord.setRecordValue(value * 0.01); + break; + case RecordTypes.RECORD_TYPE_SUSPEND: + messageType += "suspend"; + danaRHistoryRecord.setRecordDate(datetimewihtsec); + String strRecordValue = "Off"; + if ((int) paramByte8 == 79) + strRecordValue = "On"; + danaRHistoryRecord.setStringRecordValue(strRecordValue); + break; + } + + try { + Dao daoHistoryRecords = MainApp.getDbHelper().getDaoDanaRHistory(); + daoHistoryRecords.createIfNotExists(danaRHistoryRecord); + } catch (SQLException e) { + log.error(e.getMessage(), e); + } + + DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); + ev.message = df.format(new Date(danaRHistoryRecord.getRecordDate())); + ev.message += " " + messageType; + MainApp.bus().post(ev); + + return; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryBasalHour.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryBasalHour.java new file mode 100644 index 0000000000..63faec34ab --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryBasalHour.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryBasalHour extends MsgHistoryAll { + private static Logger log = LoggerFactory.getLogger(MsgHistoryBasalHour.class); + public MsgHistoryBasalHour() { + SetCommand(0x310A); + } + // Handle message taken from MsgHistoryAll +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryBolus.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryBolus.java new file mode 100644 index 0000000000..884c5b0737 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryBolus.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryBolus extends MsgHistoryAll { + private static Logger log = LoggerFactory.getLogger(MsgHistoryBolus.class); + public MsgHistoryBolus() { + SetCommand(0x3101); + } + // Handle message taken from MsgHistoryAll +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryCarbo.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryCarbo.java new file mode 100644 index 0000000000..a4536f140d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryCarbo.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryCarbo extends MsgHistoryAll { + private static Logger log = LoggerFactory.getLogger(MsgHistoryCarbo.class); + public MsgHistoryCarbo() { + SetCommand(0x3107); + } + // Handle message taken from MsgHistoryAll +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryDailyInsulin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryDailyInsulin.java new file mode 100644 index 0000000000..0557dddefa --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryDailyInsulin.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryDailyInsulin extends MsgHistoryAll { + private static Logger log = LoggerFactory.getLogger(MsgHistoryDailyInsulin.class); + public MsgHistoryDailyInsulin() { + SetCommand(0x3102); + } + // Handle message taken from MsgHistoryAll +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryDone.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryDone.java new file mode 100644 index 0000000000..2d70653f8f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryDone.java @@ -0,0 +1,28 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryDone extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgHistoryDone.class); + public static boolean received = false; + + public MsgHistoryDone() { + SetCommand(0x31F1); + received = false; + } + + @Override + public void handleMessage(byte[] bytes) { + received = true; + if (Config.logDanaMessageDetail) + log.debug("History done received"); + + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryError.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryError.java new file mode 100644 index 0000000000..99333d6ea4 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryError.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryError extends MsgHistoryAll { + private static Logger log = LoggerFactory.getLogger(MsgHistoryError.class); + public MsgHistoryError() { + SetCommand(0x3106); + } + // Handle message taken from MsgHistoryAll +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryGlucose.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryGlucose.java new file mode 100644 index 0000000000..83a13f211a --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryGlucose.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryGlucose extends MsgHistoryAll { + private static Logger log = LoggerFactory.getLogger(MsgHistoryGlucose.class); + public MsgHistoryGlucose() { + SetCommand(0x3104); + } + // Handle message taken from MsgHistoryAll +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryNew.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryNew.java new file mode 100644 index 0000000000..aa71976865 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryNew.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryNew extends MsgHistoryAll { + private static Logger log = LoggerFactory.getLogger(MsgHistoryNew.class); + public MsgHistoryNew() { + SetCommand(0x42F2); + } + // Handle message taken from MsgHistoryAll +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryNewDone.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryNewDone.java new file mode 100644 index 0000000000..daeccf8657 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryNewDone.java @@ -0,0 +1,27 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryNewDone extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgHistoryNewDone.class); + public static boolean received = false; + + public MsgHistoryNewDone() { + SetCommand(0x42F1); + received = false; + } + + @Override + public void handleMessage(byte[] bytes) { + received = true; + if (Config.logDanaMessageDetail) + log.debug("History new done received"); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryRefill.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryRefill.java new file mode 100644 index 0000000000..4294ea7e28 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryRefill.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistoryRefill extends MsgHistoryAll { + private static Logger log = LoggerFactory.getLogger(MsgHistoryRefill.class); + public MsgHistoryRefill() { + SetCommand(0x3108); + } + // Handle message taken from MsgHistoryAll +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistorySuspend.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistorySuspend.java new file mode 100644 index 0000000000..15a639e603 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistorySuspend.java @@ -0,0 +1,15 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by mike on 20.07.2016. + */ +public class MsgHistorySuspend extends MsgHistoryAll { + private static Logger log = LoggerFactory.getLogger(MsgHistorySuspend.class); + public MsgHistorySuspend() { + SetCommand(0x3109); + } + // Handle message taken from MsgHistoryAll +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusBasic.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusBasic.java new file mode 100644 index 0000000000..379a558bea --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusBasic.java @@ -0,0 +1,37 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; + +public class MsgInitConnStatusBasic extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgInitConnStatusBasic.class); + + public MsgInitConnStatusBasic() { + SetCommand(0x0303); + } + + @Override + public void handleMessage(byte[] bytes) { + if (bytes.length - 10 > 6) { + return; + } + DanaRKoreanPump pump = DanaRKoreanPlugin.getDanaRPump(); + int isStatusSuspendOn = intFromBuff(bytes, 0, 1); + int isUtilityEnable = intFromBuff(bytes, 1, 1); + int isEasyUIEnable = intFromBuff(bytes, 2, 1); + int easyUIMode = intFromBuff(bytes, 3, 1); + pump.password = intFromBuff(bytes, 4, 2) ^ 0x3463; + if (Config.logDanaMessageDetail) { + log.debug("isStatusSuspendOn: " + isStatusSuspendOn); + log.debug("isUtilityEnable: " + isUtilityEnable); + log.debug("isEasyUIEnable: " + isEasyUIEnable); + log.debug("easyUIMode: " + easyUIMode); + log.debug("Pump password: " + pump.password); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusBolus.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusBolus.java new file mode 100644 index 0000000000..1af0e1f224 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusBolus.java @@ -0,0 +1,48 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; + +/** + * Created by mike on 28.05.2016. + */ +public class MsgInitConnStatusBolus extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgInitConnStatusBolus.class); + + public MsgInitConnStatusBolus() { + SetCommand(0x0302); + } + + @Override + public void handleMessage(byte[] bytes) { + if (bytes.length - 10 < 13) { + return; + } + DanaRKoreanPump pump = DanaRKoreanPlugin.getDanaRPump(); + int bolusConfig = intFromBuff(bytes, 0, 1); + boolean deliveryPrime = (bolusConfig & DanaRKoreanPump.DELIVERY_PRIME) != 0; + boolean deliveryStepBolus = (bolusConfig & DanaRKoreanPump.DELIVERY_STEP_BOLUS) != 0; + boolean deliveryBasal = (bolusConfig & DanaRKoreanPump.DELIVERY_BASAL) != 0; + boolean deliveryExtBolus = (bolusConfig & DanaRKoreanPump.DELIVERY_EXT_BOLUS) != 0; + + pump.bolusStep = intFromBuff(bytes, 1, 1) / 100d; + pump.maxBolus = intFromBuff(bytes, 2, 2) / 100d; + //int bolusRate = intFromBuff(bytes, 4, 8); + int deliveryStatus = intFromBuff(bytes, 12, 1); + + if (Config.logDanaMessageDetail) { + log.debug("Delivery prime: " + deliveryPrime); + log.debug("Delivery step bolus: " + deliveryStepBolus); + log.debug("Delivery basal: " + deliveryBasal); + log.debug("Delivery ext bolus: " + deliveryExtBolus); + log.debug("Bolus increment: " + pump.bolusStep); + log.debug("Bolus max: " + pump.maxBolus); + log.debug("Delivery status: " + deliveryStatus); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusTime.java new file mode 100644 index 0000000000..ee12e1688e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusTime.java @@ -0,0 +1,55 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.utils.ToastUtils; + +public class MsgInitConnStatusTime extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgInitConnStatusTime.class); + + public MsgInitConnStatusTime() { + SetCommand(0x0301); + } + + @Override + public void handleMessage(byte[] bytes) { + + if (bytes.length - 10 < 10) { + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(),MainApp.sResources.getString(R.string.wrongpumpdriverselected), R.raw.error); + ((DanaRKoreanPlugin)MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).doDisconnect("Wrong Model"); + log.debug("Wrong model selected. Switching to export DanaR"); + ((DanaRKoreanPlugin)MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setFragmentEnabled(PluginBase.PUMP, false); + ((DanaRKoreanPlugin)MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setFragmentVisible(PluginBase.PUMP, false); + ((DanaRPlugin)MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentEnabled(PluginBase.PUMP, true); + ((DanaRPlugin)MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentVisible(PluginBase.PUMP, true); + MainApp.getConfigBuilder().storeSettings(); + MainApp.bus().post(new EventRefreshGui(false)); + return; + } + + Date time = dateTimeSecFromBuff(bytes, 0); + int versionCode1 = intFromBuff(bytes, 6, 1); + int versionCode2 = intFromBuff(bytes, 7, 1); + int versionCode3 = intFromBuff(bytes, 8, 1); + int versionCode4 = intFromBuff(bytes, 9, 1); + + if (Config.logDanaMessageDetail) { + log.debug("Pump time: " + time); + log.debug("Version code1: " + versionCode1); + log.debug("Version code2: " + versionCode2); + log.debug("Version code3: " + versionCode3); + log.debug("Version code4: " + versionCode4); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgPCCommStart.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgPCCommStart.java new file mode 100644 index 0000000000..a97182f0b4 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgPCCommStart.java @@ -0,0 +1,20 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +public class MsgPCCommStart extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgPCCommStart.class); + public MsgPCCommStart() { + SetCommand(0x3001); + } + + @Override + public void handleMessage(byte[] bytes) { + if (Config.logDanaMessageDetail) + log.debug("PC comm start received"); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgPCCommStop.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgPCCommStop.java new file mode 100644 index 0000000000..9ae58288a8 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgPCCommStop.java @@ -0,0 +1,20 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +public class MsgPCCommStop extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgPCCommStop.class); + public MsgPCCommStop() { + SetCommand(0x3002); + } + + @Override + public void handleMessage(byte[] bytes) { + if (Config.logDanaMessageDetail) + log.debug("PC comm stop received"); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetCarbsEntry.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetCarbsEntry.java new file mode 100644 index 0000000000..2b6ad9adf4 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetCarbsEntry.java @@ -0,0 +1,46 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Calendar; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes; + +public class MsgSetCarbsEntry extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSetCarbsEntry.class); + + public MsgSetCarbsEntry() { + SetCommand(0x0402); + } + + public MsgSetCarbsEntry(Calendar time, int amount) { + this(); + + AddParamByte((byte) RecordTypes.RECORD_TYPE_CARBO); + AddParamByte((byte) (time.get(Calendar.YEAR) % 100)); + AddParamByte((byte) (time.get(Calendar.MONTH) + 1)); + AddParamByte((byte) (time.get(Calendar.DAY_OF_MONTH))); + AddParamByte((byte) (time.get(Calendar.HOUR_OF_DAY))); + AddParamByte((byte) (time.get(Calendar.MINUTE))); + AddParamByte((byte) (time.get(Calendar.SECOND))); + AddParamByte((byte) 0x43); //?? + AddParamInt(amount); + if (Config.logDanaMessageDetail) + log.debug("Set carb entry: " + amount + " date " + time.toString()); + } + + @Override + public void handleMessage(byte[] bytes) { + int result = intFromBuff(bytes, 0, 1); + if (result != 1) { + failed = true; + log.debug("Set carb entry result: " + result + " FAILED!!!"); + } else { + if (Config.logDanaMessageDetail) + log.debug("Set carb entry result: " + result); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetExtendedBolusStart.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetExtendedBolusStart.java new file mode 100644 index 0000000000..6bceb9f1d7 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetExtendedBolusStart.java @@ -0,0 +1,44 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +public class MsgSetExtendedBolusStart extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSetExtendedBolusStart.class); + + public MsgSetExtendedBolusStart() { + SetCommand(0x0407); + } + + public MsgSetExtendedBolusStart(double amount, byte halfhours) { + this(); + + // HARDCODED LIMITS + if (halfhours < 1) halfhours = 1; + if (halfhours > 16) halfhours = 16; + amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); + if (amount < 0d) amount = 0d; + if (amount > 10d) amount = 10d; + + AddParamInt((int) (amount * 100)); + AddParamByte(halfhours); + if (Config.logDanaMessageDetail) + log.debug("Set extended bolus start: " + (((int) (amount * 100)) / 100d) + "U halfhours: " + (int) halfhours); + } + + @Override + public void handleMessage(byte[] bytes) { + int result = intFromBuff(bytes, 0, 1); + if (result != 1) { + failed = true; + log.debug("Set extended bolus start result: " + result + " FAILED!!!"); + } else { + if (Config.logDanaMessageDetail) + log.debug("Set extended bolus start result: " + result); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetExtendedBolusStop.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetExtendedBolusStop.java new file mode 100644 index 0000000000..e4d09293c1 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetExtendedBolusStop.java @@ -0,0 +1,31 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +public class MsgSetExtendedBolusStop extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSetExtendedBolusStop.class); + + public MsgSetExtendedBolusStop() { + SetCommand(0x0406); + if (Config.logDanaMessageDetail) + log.debug("Set extended bolus stop"); + } + + @Override + public void handleMessage(byte[] bytes) { + int result = intFromBuff(bytes, 0, 1); + if (result != 1) { + failed = true; + log.debug("Set extended bolus stop result: " + result + " FAILED!!!"); + } else { + if (Config.logDanaMessageDetail) + log.debug("Set extended bolus stop result: " + result); + } + } + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetSingleBasalProfile.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetSingleBasalProfile.java new file mode 100644 index 0000000000..ce046bdccc --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetSingleBasalProfile.java @@ -0,0 +1,47 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.Overview.Notification; +import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; + +public class MsgSetSingleBasalProfile extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSetSingleBasalProfile.class); + + public MsgSetSingleBasalProfile() { + SetCommand(0x3302); + } + + // index 0-3 + public MsgSetSingleBasalProfile(double[] values) { + this(); + for (Integer i = 0; i < 24; i++) { + AddParamInt((int) (values[i] * 100)); + } + if (Config.logDanaMessageDetail) + log.debug("Set basal profile"); + } + + @Override + public void handleMessage(byte[] bytes) { + int result = intFromBuff(bytes, 0, 1); + if (result != 1) { + failed = true; + log.debug("Set basal profile result: " + result + " FAILED!!!"); + Notification reportFail = new Notification(Notification.PROFILE_SET_FAILED, MainApp.sResources.getString(R.string.profile_set_failed), Notification.URGENT); + MainApp.bus().post(new EventNewNotification(reportFail)); + } else { + if (Config.logDanaMessageDetail) + log.debug("Set basal profile result: " + result); + Notification reportOK = new Notification(Notification.PROFILE_SET_OK, MainApp.sResources.getString(R.string.profile_set_ok), Notification.INFO, 60); + MainApp.bus().post(new EventNewNotification(reportOK)); + } + } + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetTempBasalStart.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetTempBasalStart.java new file mode 100644 index 0000000000..cb75b15202 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetTempBasalStart.java @@ -0,0 +1,44 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +public class MsgSetTempBasalStart extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSetTempBasalStart.class); + + public MsgSetTempBasalStart() { + SetCommand(0x0401); + } + + public MsgSetTempBasalStart(int percent, int durationInHours) { + this(); + + //HARDCODED LIMITS + if (percent < 0) percent = 0; + if (percent > 200) percent = 200; + if (durationInHours < 1) durationInHours = 1; + if (durationInHours > 24) durationInHours = 24; + + AddParamByte((byte) (percent & 255)); + AddParamByte((byte) (durationInHours & 255)); + + if (Config.logDanaMessageDetail) + log.debug("Temp basal start percent: " + percent + " duration hours: " + durationInHours); + } + + public void handleMessage(byte[] bytes) { + int result = intFromBuff(bytes, 0, 1); + if (result != 1) { + failed = true; + log.debug("Set temp basal start result: " + result + " FAILED!!!"); + } else { + if (Config.logDanaMessageDetail) + log.debug("Set temp basal start result: " + result); + } + } + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetTempBasalStop.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetTempBasalStop.java new file mode 100644 index 0000000000..b42cc71e88 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetTempBasalStop.java @@ -0,0 +1,30 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +public class MsgSetTempBasalStop extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSetTempBasalStop.class); + + public MsgSetTempBasalStop() { + SetCommand(0x0403); + if (Config.logDanaMessageDetail) + log.debug("Temp basal stop"); + } + + public void handleMessage(byte[] bytes) { + int result = intFromBuff(bytes, 0, 1); + if (result != 1) { + failed = true; + log.debug("Set temp basal stop result: " + result + " FAILED!!!"); + } else { + if (Config.logDanaMessageDetail) + log.debug("Set temp basal stop result: " + result); + } + } + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetTime.java new file mode 100644 index 0000000000..a610ca4613 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetTime.java @@ -0,0 +1,29 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +/** + * Created by mike on 09.12.2016. + */ + +public class MsgSetTime extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSetTime.class); + + public MsgSetTime(Date time) { + SetCommand(0x330a); + AddParamDateTime(time); + } + + public void handleMessage(byte[] bytes) { + int result = intFromBuff(bytes, 0, 1); + + if (Config.logDanaMessageDetail) + log.debug("Result: " + result); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingBasal.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingBasal.java new file mode 100644 index 0000000000..e6ac4b9003 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingBasal.java @@ -0,0 +1,36 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; + +/** + * Created by mike on 05.07.2016. + */ +public class MsgSettingBasal extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSettingBasal.class); + + public MsgSettingBasal() { + SetCommand(0x3202); + } + + public void handleMessage(byte[] bytes) { + DanaRKoreanPump pump = DanaRKoreanPlugin.getDanaRPump(); + if (pump.pumpProfiles == null) pump.pumpProfiles = new double[4][]; + pump.pumpProfiles[pump.activeProfile] = new double[24]; + for (int index = 0; index < 24; index++) { + int basal = intFromBuff(bytes, 2 * index, 2); + if (basal < 10) basal = 0; + pump.pumpProfiles[pump.activeProfile][index] = basal / 100d; + } + + if (Config.logDanaMessageDetail) + for (int index = 0; index < 24; index++) { + log.debug("Basal " + String.format("%02d", index) + "h: " + DanaRKoreanPlugin.getDanaRPump().pumpProfiles[DanaRKoreanPlugin.getDanaRPump().activeProfile][index]); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingBasalProfileAll.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingBasalProfileAll.java new file mode 100644 index 0000000000..9397ed86c6 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingBasalProfileAll.java @@ -0,0 +1,73 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; + + +/** + * Created by mike on 05.07.2016. + *

+ *

+ * THIS IS BROKEN IN PUMP... SENDING ONLY 1 PROFILE + */ +public class MsgSettingBasalProfileAll extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSettingBasalProfileAll.class); + + public MsgSettingBasalProfileAll() { + SetCommand(0x3206); + } + + public void handleMessage(byte[] bytes) { + DanaRKoreanPump pump = DanaRKoreanPlugin.getDanaRPump(); + if (DanaRKoreanPlugin.getDanaRPump().basal48Enable) { + pump.pumpProfiles = new double[4][]; + for (int profile = 0; profile < 4; profile++) { + int position = intFromBuff(bytes, 107 * profile, 1); + pump.pumpProfiles[position] = new double[48]; + for (int index = 0; index < 48; index++) { + int basal = intFromBuff(bytes, 107 * profile + 2 * index + 1, 2); + if (basal < 10) basal = 0; + pump.pumpProfiles[position][index] = basal / 100 / 24d; // in units/day + } + } + } else { + pump.pumpProfiles = new double[4][]; + for (int profile = 0; profile < 4; profile++) { + int position = intFromBuff(bytes, 49 * profile, 1); + log.debug("position " + position); + pump.pumpProfiles[position] = new double[24]; + for (int index = 0; index < 24; index++) { + int basal = intFromBuff(bytes, 59 * profile + 2 * index + 1, 2); + if (basal < 10) basal = 0; + log.debug("position " + position + " index " + index); + pump.pumpProfiles[position][index] = basal / 100 / 24d; // in units/day + } + } + } + + if (Config.logDanaMessageDetail) { + if (DanaRKoreanPlugin.getDanaRPump().basal48Enable) { + for (int profile = 0; profile < 4; profile++) { + for (int index = 0; index < 24; index++) { + log.debug("Basal profile " + profile + ": " + String.format("%02d", index) + "h: " + DanaRKoreanPlugin.getDanaRPump().pumpProfiles[profile][index]); + } + } + } else { + for (int profile = 0; profile < 4; profile++) { + for (int index = 0; index < 48; index++) { + log.debug("Basal profile " + profile + ": " + + String.format("%02d", (index / 2)) + + ":" + String.format("%02d", (index % 2) * 30) + " : " + + DanaRKoreanPlugin.getDanaRPump().pumpProfiles[profile][index]); + } + } + } + } + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingGlucose.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingGlucose.java new file mode 100644 index 0000000000..8ebc972495 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingGlucose.java @@ -0,0 +1,30 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; + +/** + * Created by mike on 05.07.2016. + */ +public class MsgSettingGlucose extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSettingGlucose.class); + + public MsgSettingGlucose() { + SetCommand(0x3209); + } + + public void handleMessage(byte[] bytes) { + DanaRKoreanPlugin.getDanaRPump().units = intFromBuff(bytes, 0, 1); + DanaRKoreanPlugin.getDanaRPump().easyBasalMode = intFromBuff(bytes, 1, 1); + + if (Config.logDanaMessageDetail) { + log.debug("Pump units: " + (DanaRKoreanPlugin.getDanaRPump().units == DanaRKoreanPump.UNITS_MGDL ? "MGDL" : "MMOL")); + log.debug("Easy basal mode: " + DanaRKoreanPlugin.getDanaRPump().easyBasalMode); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingMaxValues.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingMaxValues.java new file mode 100644 index 0000000000..428881e715 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingMaxValues.java @@ -0,0 +1,33 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; + + +/** + * Created by mike on 05.07.2016. + */ +public class MsgSettingMaxValues extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSettingMaxValues.class); + + public MsgSettingMaxValues() { + SetCommand(0x3205); + } + + public void handleMessage(byte[] bytes) { + DanaRKoreanPlugin.getDanaRPump().maxBolus = intFromBuff(bytes, 0, 2) / 100d; + DanaRKoreanPlugin.getDanaRPump().maxBasal = intFromBuff(bytes, 2, 2) / 100d; + DanaRKoreanPlugin.getDanaRPump().maxDailyTotalUnits = intFromBuff(bytes, 4, 2) / 100; + + if (Config.logDanaMessageDetail) { + log.debug("Max bolus: " + DanaRKoreanPlugin.getDanaRPump().maxBolus); + log.debug("Max basal: " + DanaRKoreanPlugin.getDanaRPump().maxBasal); + log.debug("Total daily max units: " + DanaRKoreanPlugin.getDanaRPump().maxDailyTotalUnits); + } + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingProfileRatios.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingProfileRatios.java new file mode 100644 index 0000000000..e023c1bb5b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingProfileRatios.java @@ -0,0 +1,45 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; + +/** + * Created by mike on 05.07.2016. + */ +public class MsgSettingProfileRatios extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSettingProfileRatios.class); + + public MsgSettingProfileRatios() { + SetCommand(0x3204); + } + + public void handleMessage(byte[] bytes) { + if (DanaRKoreanPlugin.getDanaRPump().units == DanaRKoreanPump.UNITS_MGDL) { + DanaRKoreanPlugin.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2); + DanaRKoreanPlugin.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2); + DanaRKoreanPlugin.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d; + DanaRKoreanPlugin.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2); + DanaRKoreanPlugin.getDanaRPump().currentAIDR = intFromBuff(bytes, 8, 1); + } else { + DanaRKoreanPlugin.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2); + DanaRKoreanPlugin.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2) / 100d; + DanaRKoreanPlugin.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d; + DanaRKoreanPlugin.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2) / 100d; + DanaRKoreanPlugin.getDanaRPump().currentAIDR = intFromBuff(bytes, 8, 1); + } + + if (Config.logDanaMessageDetail) { + log.debug("Pump units (saved): " + (DanaRKoreanPlugin.getDanaRPump().units == DanaRKoreanPump.UNITS_MGDL ? "MGDL" : "MMOL")); + log.debug("Current pump CIR: " + DanaRKoreanPlugin.getDanaRPump().currentCIR); + log.debug("Current pump CF: " + DanaRKoreanPlugin.getDanaRPump().currentCF); + log.debug("Current pump AI: " + DanaRKoreanPlugin.getDanaRPump().currentAI); + log.debug("Current pump target: " + DanaRKoreanPlugin.getDanaRPump().currentTarget); + log.debug("Current pump AIDR: " + DanaRKoreanPlugin.getDanaRPump().currentAIDR); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingPumpTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingPumpTime.java new file mode 100644 index 0000000000..359ffb924e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingPumpTime.java @@ -0,0 +1,35 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; + +public class MsgSettingPumpTime extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSettingPumpTime.class); + + public MsgSettingPumpTime() { + SetCommand(0x320A); + } + + public void handleMessage(byte[] bytes) { + Date time = + new Date( + 100 + intFromBuff(bytes, 5, 1), + intFromBuff(bytes, 4, 1) - 1, + intFromBuff(bytes, 3, 1), + intFromBuff(bytes, 2, 1), + intFromBuff(bytes, 1, 1), + intFromBuff(bytes, 0, 1) + ); + + if (Config.logDanaMessageDetail) + log.debug("Pump time: " + time); + + DanaRKoreanPlugin.getDanaRPump().pumpTime = time; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingShippingInfo.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingShippingInfo.java new file mode 100644 index 0000000000..4d10aef389 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingShippingInfo.java @@ -0,0 +1,39 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; + +/** + * Created by mike on 05.07.2016. + */ +public class MsgSettingShippingInfo extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSettingShippingInfo.class); + + public MsgSettingShippingInfo() { + SetCommand(0x3207); + } + + public void handleMessage(byte[] bytes) { + DanaRKoreanPump pump = DanaRKoreanPlugin.getDanaRPump(); + pump.serialNumber = stringFromBuff(bytes, 0, 10); + pump.shippingDate = dateFromBuff(bytes, 10); + pump.shippingCountry = asciiStringFromBuff(bytes, 13, 3); + if (pump.shippingDate.getTime() > new Date(116, 4, 1).getTime()) { + pump.isNewPump = true; + } else + pump.isNewPump = false; + if (Config.logDanaMessageDetail) { + log.debug("Serial number: " + pump.serialNumber); + log.debug("Shipping date: " + pump.shippingDate); + log.debug("Shipping country: " + pump.shippingCountry); + } + } +} + diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatus.java new file mode 100644 index 0000000000..bb4adaa58e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatus.java @@ -0,0 +1,39 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; + +public class MsgStatus extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgStatus.class); + + public MsgStatus() { + SetCommand(0x020B); + } + + public void handleMessage(byte[] bytes) { + DanaRKoreanPlugin.getDanaRPump().dailyTotalUnits = intFromBuff(bytes, 0, 3) / 750d; + DanaRKoreanPlugin.getDanaRPump().isExtendedInProgress = intFromBuff(bytes, 3, 1) == 1; + DanaRKoreanPlugin.getDanaRPump().extendedBolusMinutes = intFromBuff(bytes, 4, 2); + DanaRKoreanPlugin.getDanaRPump().extendedBolusAmount = intFromBuff(bytes, 6, 2) / 100d; + Double lastBolusAmount = intFromBuff(bytes, 13, 2) / 100d; +// if (lastBolusAmount != 0d) { +// DanaRKoreanPlugin.getDanaRPump().lastBolusTime = dateTimeFromBuff(bytes, 8); +// DanaRKoreanPlugin.getDanaRPump().lastBolusAmount = lastBolusAmount; +// } + DanaRKoreanPlugin.getDanaRPump().iob = intFromBuff(bytes, 15, 2) / 100d; + + if (Config.logDanaMessageDetail) { + log.debug("Daily total: " + DanaRKoreanPlugin.getDanaRPump().dailyTotalUnits); + log.debug("Is extended bolus running: " + DanaRKoreanPlugin.getDanaRPump().isExtendedInProgress); + log.debug("Extended bolus min: " + DanaRKoreanPlugin.getDanaRPump().extendedBolusMinutes); + log.debug("Extended bolus amount: " + DanaRKoreanPlugin.getDanaRPump().extendedBolusAmount); +// log.debug("Last bolus time: " + DanaRKoreanPlugin.getDanaRPump().lastBolusTime); +// log.debug("Last bolus amount: " + DanaRKoreanPlugin.getDanaRPump().lastBolusAmount); + log.debug("IOB: " + DanaRKoreanPlugin.getDanaRPump().iob); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusBasic.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusBasic.java new file mode 100644 index 0000000000..46a230b184 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusBasic.java @@ -0,0 +1,38 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; + + +public class MsgStatusBasic extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgStatusBasic.class); + + public MsgStatusBasic() { + SetCommand(0x020A); + } + + public void handleMessage(byte[] bytes) { + double currentBasal = intFromBuff(bytes, 0, 2) / 100d; + int batteryRemaining = intFromBuff(bytes, 2, 1); + double reservoirRemainingUnits = intFromBuff(bytes, 3, 3) / 750d; + double dailyTotalUnits = intFromBuff(bytes, 6, 3) / 750d; + int maxDailyTotalUnits = intFromBuff(bytes, 9, 2) / 100; + + DanaRKoreanPlugin.getDanaRPump().dailyTotalUnits = dailyTotalUnits; + DanaRKoreanPlugin.getDanaRPump().maxDailyTotalUnits = maxDailyTotalUnits; + DanaRKoreanPlugin.getDanaRPump().reservoirRemainingUnits = reservoirRemainingUnits; + DanaRKoreanPlugin.getDanaRPump().currentBasal = currentBasal; + DanaRKoreanPlugin.getDanaRPump().batteryRemaining = batteryRemaining; + + if (Config.logDanaMessageDetail) { + log.debug("Daily total units: " + dailyTotalUnits); + log.debug("Max daily total units: " + maxDailyTotalUnits); + log.debug("Reservoir remaining units: " + reservoirRemainingUnits); + log.debug("Current basal: " + currentBasal); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusBolusExtended.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusBolusExtended.java new file mode 100644 index 0000000000..1c740fd960 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusBolusExtended.java @@ -0,0 +1,114 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import android.support.annotation.NonNull; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.SQLException; +import java.util.Date; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.db.TempBasal; +import info.nightscout.androidaps.events.EventTempBasalChange; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; + +public class MsgStatusBolusExtended extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgStatusBolusExtended.class); + + public MsgStatusBolusExtended() { + SetCommand(0x0207); + } + + public void handleMessage(byte[] bytes) { + boolean isExtendedInProgress = intFromBuff(bytes, 0, 1) == 1; + int extendedBolusHalfHours = intFromBuff(bytes, 1, 1); + int extendedBolusMinutes = extendedBolusHalfHours * 30; + + double extendedBolusAmount = intFromBuff(bytes, 2, 2) / 100d; + int extendedBolusSoFarInSecs = intFromBuff(bytes, 4, 3); + int extendedBolusDeliveryPulse = intFromBuff(bytes, 7, 2); + int isEasyUIUserSleep = intFromBuff(bytes, 9, 1); + + int extendedBolusSoFarInMinutes = extendedBolusSoFarInSecs / 60; + double extendedBolusAbsoluteRate = isExtendedInProgress ? extendedBolusAmount / extendedBolusMinutes * 60 : 0d; + Date extendedBolusStart = isExtendedInProgress ? getDateFromSecAgo(extendedBolusSoFarInSecs) : new Date(0); + int extendedBolusRemainingMinutes = extendedBolusMinutes - extendedBolusSoFarInMinutes; + + DanaRKoreanPlugin.getDanaRPump().isExtendedInProgress = isExtendedInProgress; + DanaRKoreanPlugin.getDanaRPump().extendedBolusMinutes = extendedBolusMinutes; + DanaRKoreanPlugin.getDanaRPump().extendedBolusAmount = extendedBolusAmount; + DanaRKoreanPlugin.getDanaRPump().extendedBolusSoFarInMinutes = extendedBolusSoFarInMinutes; + DanaRKoreanPlugin.getDanaRPump().extendedBolusAbsoluteRate = extendedBolusAbsoluteRate; + DanaRKoreanPlugin.getDanaRPump().extendedBolusStart = extendedBolusStart; + DanaRKoreanPlugin.getDanaRPump().extendedBolusRemainingMinutes = extendedBolusRemainingMinutes; + + updateExtendedBolusInDB(); + + if (Config.logDanaMessageDetail) { + log.debug("Is extended bolus running: " + isExtendedInProgress); + log.debug("Extended bolus min: " + extendedBolusMinutes); + log.debug("Extended bolus amount: " + extendedBolusAmount); + log.debug("Extended bolus so far in minutes: " + extendedBolusSoFarInMinutes); + log.debug("Extended bolus absolute rate: " + extendedBolusAbsoluteRate); + log.debug("Extended bolus start: " + extendedBolusStart); + log.debug("Extended bolus remaining minutes: " + extendedBolusRemainingMinutes); + } + } + + @NonNull + private Date getDateFromSecAgo(int tempBasalAgoSecs) { + return new Date((long) (Math.ceil(new Date().getTime() / 1000d) - tempBasalAgoSecs) * 1000); + } + + public static void updateExtendedBolusInDB() { + DanaRKoreanPlugin DanaRKoreanPlugin = (DanaRKoreanPlugin) MainApp.getSpecificPlugin(DanaRKoreanPlugin.class); + DanaRKoreanPump danaRKoreanPump = DanaRKoreanPlugin.getDanaRPump(); + Date now = new Date(); + + try { + + if (DanaRKoreanPlugin.isExtendedBoluslInProgress()) { + TempBasal extendedBolus = DanaRKoreanPlugin.getExtendedBolus(); + if (danaRKoreanPump.isExtendedInProgress) { + if (extendedBolus.absolute != danaRKoreanPump.extendedBolusAbsoluteRate) { + // Close current extended + extendedBolus.timeEnd = now; + MainApp.getDbHelper().getDaoTempBasals().update(extendedBolus); + // Create new + TempBasal newExtended = new TempBasal(); + newExtended.timeStart = now; + newExtended.absolute = danaRKoreanPump.extendedBolusAbsoluteRate; + newExtended.isAbsolute = true; + newExtended.duration = danaRKoreanPump.extendedBolusMinutes; + newExtended.isExtended = true; + MainApp.getDbHelper().getDaoTempBasals().create(newExtended); + MainApp.bus().post(new EventTempBasalChange()); + } + } else { + // Close curent temp basal + extendedBolus.timeEnd = now; + MainApp.getDbHelper().getDaoTempBasals().update(extendedBolus); + MainApp.bus().post(new EventTempBasalChange()); + } + } else { + if (danaRKoreanPump.isExtendedInProgress) { + // Create new + TempBasal newExtended = new TempBasal(); + newExtended.timeStart = now; + newExtended.absolute = danaRKoreanPump.extendedBolusAbsoluteRate; + newExtended.isAbsolute = true; + newExtended.duration = danaRKoreanPump.extendedBolusMinutes; + newExtended.isExtended = true; + MainApp.getDbHelper().getDaoTempBasals().create(newExtended); + MainApp.bus().post(new EventTempBasalChange()); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusProfile.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusProfile.java new file mode 100644 index 0000000000..7af3260b3a --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusProfile.java @@ -0,0 +1,43 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; + +/** + * Created by mike on 05.07.2016. + */ +public class MsgStatusProfile extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgStatusProfile.class); + + public MsgStatusProfile() { + SetCommand(0x0204); + } + + public void handleMessage(byte[] bytes) { + if (DanaRKoreanPlugin.getDanaRPump().units == DanaRKoreanPump.UNITS_MGDL) { + DanaRKoreanPlugin.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2); + DanaRKoreanPlugin.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2); + DanaRKoreanPlugin.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d; + DanaRKoreanPlugin.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2); + } else { + DanaRKoreanPlugin.getDanaRPump().currentCIR = intFromBuff(bytes, 0, 2); + DanaRKoreanPlugin.getDanaRPump().currentCF = intFromBuff(bytes, 2, 2) / 100d; + DanaRKoreanPlugin.getDanaRPump().currentAI = intFromBuff(bytes, 4, 2) / 100d; + DanaRKoreanPlugin.getDanaRPump().currentTarget = intFromBuff(bytes, 6, 2) / 100d; + } + + if (Config.logDanaMessageDetail) { + log.debug("Pump units (saved): " + (DanaRKoreanPlugin.getDanaRPump().units == DanaRKoreanPump.UNITS_MGDL ? "MGDL" : "MMOL")); + log.debug("Current pump CIR: " + DanaRKoreanPlugin.getDanaRPump().currentCIR); + log.debug("Current pump CF: " + DanaRKoreanPlugin.getDanaRPump().currentCF); + log.debug("Current pump AI: " + DanaRKoreanPlugin.getDanaRPump().currentAI); + log.debug("Current pump target: " + DanaRKoreanPlugin.getDanaRPump().currentTarget); + log.debug("Current pump AIDR: " + DanaRKoreanPlugin.getDanaRPump().currentAIDR); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusTempBasal.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusTempBasal.java new file mode 100644 index 0000000000..4506bb944a --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusTempBasal.java @@ -0,0 +1,103 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import android.support.annotation.NonNull; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.SQLException; +import java.util.Date; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.db.TempBasal; +import info.nightscout.androidaps.events.EventTempBasalChange; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPump; + +public class MsgStatusTempBasal extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgStatusTempBasal.class); + + public MsgStatusTempBasal() { + SetCommand(0x0205); + } + + public void handleMessage(byte[] bytes) { + boolean isTempBasalInProgress = intFromBuff(bytes, 0, 1) == 1; + int tempBasalPercent = intFromBuff(bytes, 1, 1); + int tempBasalTotalSec = intFromBuff(bytes, 2, 1) * 60 * 60; + int tempBasalRunningSeconds = intFromBuff(bytes, 3, 3); + int tempBasalRemainingMin = (tempBasalTotalSec - tempBasalRunningSeconds) / 60; + Date tempBasalStart = isTempBasalInProgress ? getDateFromTempBasalSecAgo(tempBasalRunningSeconds) : new Date(0); + + DanaRKoreanPlugin.getDanaRPump().isTempBasalInProgress = isTempBasalInProgress; + DanaRKoreanPlugin.getDanaRPump().tempBasalPercent = tempBasalPercent; + DanaRKoreanPlugin.getDanaRPump().tempBasalRemainingMin = tempBasalRemainingMin; + DanaRKoreanPlugin.getDanaRPump().tempBasalTotalSec = tempBasalTotalSec; + DanaRKoreanPlugin.getDanaRPump().tempBasalStart = tempBasalStart; + + updateTempBasalInDB(); + + if (Config.logDanaMessageDetail) { + log.debug("Is temp basal running: " + isTempBasalInProgress); + log.debug("Current temp basal percent: " + tempBasalPercent); + log.debug("Current temp basal remaining min: " + tempBasalRemainingMin); + log.debug("Current temp basal total sec: " + tempBasalTotalSec); + log.debug("Current temp basal start: " + tempBasalStart); + } + } + + @NonNull + private Date getDateFromTempBasalSecAgo(int tempBasalAgoSecs) { + return new Date((long) (Math.ceil(new Date().getTime() / 1000d) - tempBasalAgoSecs) * 1000); + } + + public static void updateTempBasalInDB() { + DanaRKoreanPlugin DanaRKoreanPlugin = (DanaRKoreanPlugin) MainApp.getSpecificPlugin(DanaRKoreanPlugin.class); + DanaRKoreanPump danaRKoreanPump = DanaRKoreanPlugin.getDanaRPump(); + Date now = new Date(); + + try { + + if (DanaRKoreanPlugin.isRealTempBasalInProgress()) { + TempBasal tempBasal = DanaRKoreanPlugin.getRealTempBasal(); + if (danaRKoreanPump.isTempBasalInProgress) { + if (tempBasal.percent != danaRKoreanPump.tempBasalPercent) { + // Close current temp basal + tempBasal.timeEnd = now; + MainApp.getDbHelper().getDaoTempBasals().update(tempBasal); + // Create new + TempBasal newTempBasal = new TempBasal(); + newTempBasal.timeStart = now; + newTempBasal.percent = danaRKoreanPump.tempBasalPercent; + newTempBasal.isAbsolute = false; + newTempBasal.duration = danaRKoreanPump.tempBasalTotalSec / 60; + newTempBasal.isExtended = false; + MainApp.getDbHelper().getDaoTempBasals().create(newTempBasal); + MainApp.bus().post(new EventTempBasalChange()); + } + } else { + // Close current temp basal + tempBasal.timeEnd = now; + MainApp.getDbHelper().getDaoTempBasals().update(tempBasal); + MainApp.bus().post(new EventTempBasalChange()); + } + } else { + if (danaRKoreanPump.isTempBasalInProgress) { + // Create new + TempBasal newTempBasal = new TempBasal(); + newTempBasal.timeStart = now; + newTempBasal.percent = danaRKoreanPump.tempBasalPercent; + newTempBasal.isAbsolute = false; + newTempBasal.duration = danaRKoreanPump.tempBasalTotalSec / 60; + newTempBasal.isExtended = false; + MainApp.getDbHelper().getDaoTempBasals().create(newTempBasal); + MainApp.bus().post(new EventTempBasalChange()); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java index 329cc7e4d7..94676f8f3e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java @@ -24,9 +24,12 @@ import info.nightscout.androidaps.plugins.Loop.events.EventLoopUpdateGui; public class LoopFragment extends Fragment implements View.OnClickListener, FragmentBase { private static Logger log = LoggerFactory.getLogger(LoopFragment.class); - private static LoopPlugin loopPlugin = new LoopPlugin(); + private static LoopPlugin loopPlugin; public static LoopPlugin getPlugin() { + if (loopPlugin == null){ + loopPlugin = new LoopPlugin(); + } return loopPlugin; } @@ -73,7 +76,7 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Frag public void onClick(View view) { switch (view.getId()) { case R.id.loop_run: - loopPlugin.invoke(true); + getPlugin().invoke(true); break; } @@ -104,13 +107,13 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Frag activity.runOnUiThread(new Runnable() { @Override public void run() { - if (loopPlugin.lastRun != null) { - requestView.setText(loopPlugin.lastRun.request != null ? loopPlugin.lastRun.request.toSpanned() : ""); - constraintsProcessedView.setText(loopPlugin.lastRun.constraintsProcessed != null ? loopPlugin.lastRun.constraintsProcessed.toSpanned() : ""); - setByPumpView.setText(loopPlugin.lastRun.setByPump != null ? loopPlugin.lastRun.setByPump.toSpanned() : ""); - sourceView.setText(loopPlugin.lastRun.source != null ? loopPlugin.lastRun.source : ""); - lastRunView.setText(loopPlugin.lastRun.lastAPSRun != null && loopPlugin.lastRun.lastAPSRun.getTime() != 0 ? loopPlugin.lastRun.lastAPSRun.toLocaleString() : ""); - lastEnactView.setText(loopPlugin.lastRun.lastEnact != null && loopPlugin.lastRun.lastEnact.getTime() != 0 ? loopPlugin.lastRun.lastEnact.toLocaleString() : ""); + if (getPlugin().lastRun != null) { + requestView.setText(getPlugin().lastRun.request != null ? getPlugin().lastRun.request.toSpanned() : ""); + constraintsProcessedView.setText(getPlugin().lastRun.constraintsProcessed != null ? getPlugin().lastRun.constraintsProcessed.toSpanned() : ""); + setByPumpView.setText(getPlugin().lastRun.setByPump != null ? getPlugin().lastRun.setByPump.toSpanned() : ""); + sourceView.setText(getPlugin().lastRun.source != null ? getPlugin().lastRun.source : ""); + lastRunView.setText(getPlugin().lastRun.lastAPSRun != null && getPlugin().lastRun.lastAPSRun.getTime() != 0 ? getPlugin().lastRun.lastAPSRun.toLocaleString() : ""); + lastEnactView.setText(getPlugin().lastRun.lastEnact != null && getPlugin().lastRun.lastEnact.getTime() != 0 ? getPlugin().lastRun.lastEnact.toLocaleString() : ""); } } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/MM640g/MM640gFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/MM640g/MM640gFragment.java deleted file mode 100644 index a16774853e..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/MM640g/MM640gFragment.java +++ /dev/null @@ -1,43 +0,0 @@ -package info.nightscout.androidaps.plugins.MM640g; - - -import android.content.Context; -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.squareup.otto.Subscribe; - -import org.json.JSONObject; - -import java.util.Date; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.PumpEnactResult; -import info.nightscout.androidaps.db.TempBasal; -import info.nightscout.androidaps.events.EventPreferenceChange; -import info.nightscout.androidaps.interfaces.BgSourceInterface; -import info.nightscout.androidaps.interfaces.FragmentBase; -import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.client.data.NSProfile; - -public class MM640gFragment extends Fragment implements FragmentBase { - - private static MM640gPlugin mm640gPlugin = new MM640gPlugin(); - - public static MM640gPlugin getPlugin() { - return mm640gPlugin; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.mm640g_fragment, container, false); - return view; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/MM640g/MM640gPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/MM640g/MM640gPlugin.java deleted file mode 100644 index 7361054a3e..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/MM640g/MM640gPlugin.java +++ /dev/null @@ -1,192 +0,0 @@ -package info.nightscout.androidaps.plugins.MM640g; - -import android.content.Context; - -import com.squareup.otto.Subscribe; - -import org.json.JSONObject; - -import java.util.Date; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.PumpEnactResult; -import info.nightscout.androidaps.db.TempBasal; -import info.nightscout.androidaps.events.EventPreferenceChange; -import info.nightscout.androidaps.interfaces.BgSourceInterface; -import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.client.data.NSProfile; - -/** - * Created by mike on 05.08.2016. - */ -public class MM640gPlugin implements PluginBase, PumpInterface, BgSourceInterface { - - boolean fragmentPumpEnabled = false; - boolean fragmentProfileEnabled = false; - boolean fragmentBgSourceEnabled = false; - boolean fragmentPumpVisible = true; - - @Override - public String getFragmentClass() { - return MM640gFragment.class.getName(); - } - - /** - * Plugin base interface - */ - - @Override - public int getType() { - return PluginBase.PUMP; - } - - @Override - public String getName() { - return MainApp.instance().getString(R.string.mm640g); - } - - @Override - public boolean isEnabled(int type) { - if (type == PluginBase.PROFILE) return fragmentProfileEnabled; - else if (type == PluginBase.BGSOURCE) return fragmentBgSourceEnabled; - else if (type == PluginBase.PUMP) return fragmentPumpEnabled; - else if (type == PluginBase.CONSTRAINTS) return fragmentPumpEnabled; - return false; - } - - @Override - public boolean isVisibleInTabs(int type) { - if (type == PluginBase.PROFILE || type == PluginBase.CONSTRAINTS) return false; - else if (type == PluginBase.PUMP) return fragmentPumpVisible; - return false; - } - - @Override - public boolean canBeHidden(int type) { - return true; - } - - @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { - if (type == PluginBase.PROFILE) this.fragmentProfileEnabled = fragmentEnabled; - if (type == PluginBase.BGSOURCE) this.fragmentBgSourceEnabled = fragmentEnabled; - else if (type == PluginBase.PUMP) this.fragmentPumpEnabled = fragmentEnabled; - } - - @Override - public void setFragmentVisible(int type, boolean fragmentVisible) { - if (type == PluginBase.PUMP) - this.fragmentPumpVisible = fragmentVisible; - } - - /** - * Plugin communications - */ - - @Subscribe - public void onStatusEvent(final EventPreferenceChange s) { - - } - - /** - * Pump Interface - */ - - @Override - public boolean isInitialized() { - return false; - } - - @Override - public boolean isTempBasalInProgress() { - return false; - } - - @Override - public boolean isExtendedBoluslInProgress() { - return false; - } - - @Override - public void setNewBasalProfile(NSProfile profile) { - - } - - @Override - public double getBaseBasalRate() { - return 0; - } - - @Override - public double getTempBasalAbsoluteRate() { - return 0; - } - - @Override - public double getTempBasalRemainingMinutes() { - return 0; - } - - @Override - public TempBasal getTempBasal(Date time) { - return null; - } - - @Override - public TempBasal getTempBasal() { - return null; - } - - @Override - public TempBasal getExtendedBolus() { - return null; - } - - @Override - public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) { - return new PumpEnactResult(); - } - - @Override - public void stopBolusDelivering() { - - } - - @Override - public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) { - return new PumpEnactResult(); - } - - @Override - public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) { - return new PumpEnactResult(); - } - - @Override - public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { - return new PumpEnactResult(); - } - - @Override - public PumpEnactResult cancelTempBasal() { - return new PumpEnactResult(); - } - - @Override - public PumpEnactResult cancelExtendedBolus() { - return new PumpEnactResult(); - } - - @Override - public JSONObject getJSONStatus() { - return new JSONObject(); - } - - @Override - public String deviceID() { - return "MM640G"; // TODO: probably serial goes here - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java index a6b866fc77..2321d8a955 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java @@ -29,9 +29,12 @@ import info.nightscout.androidaps.interfaces.FragmentBase; public class ObjectivesFragment extends Fragment implements View.OnClickListener, FragmentBase { private static Logger log = LoggerFactory.getLogger(ObjectivesFragment.class); - private static ObjectivesPlugin objectivesPlugin = new ObjectivesPlugin(); + private static ObjectivesPlugin objectivesPlugin; public static ObjectivesPlugin getPlugin() { + if (objectivesPlugin == null) { + objectivesPlugin = new ObjectivesPlugin(); + } return objectivesPlugin; } @@ -67,7 +70,7 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener @Override public void onBindViewHolder(ObjectiveViewHolder holder, int position) { ObjectivesPlugin.Objective o = objectives.get(position); - ObjectivesPlugin.RequirementResult requirementsMet = objectivesPlugin.requirementsMet(position); + ObjectivesPlugin.RequirementResult requirementsMet = getPlugin().requirementsMet(position); Context context = MainApp.instance().getApplicationContext(); holder.position.setText(String.valueOf(position + 1)); holder.objective.setText(o.objective); @@ -91,7 +94,7 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener holder.verifyButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ObjectivesPlugin.Objective o = (ObjectivesPlugin.Objective) v.getTag(); - if (objectivesPlugin.requirementsMet(o.num).done || enableFake.isChecked()) { + if (getPlugin().requirementsMet(o.num).done || enableFake.isChecked()) { o.accomplished = new Date(); updateGUI(); ObjectivesPlugin.saveProgress(); @@ -197,26 +200,26 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener }); reset.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { - objectivesPlugin.initializeData(); - objectivesPlugin.saveProgress(); + getPlugin().initializeData(); + getPlugin().saveProgress(); updateGUI(); } }); // Add correct translations to array after app is initialized - objectivesPlugin.objectives.get(0).objective = MainApp.sResources.getString(R.string.objectives_0_objective); - objectivesPlugin.objectives.get(1).objective = MainApp.sResources.getString(R.string.objectives_1_objective); - objectivesPlugin.objectives.get(2).objective = MainApp.sResources.getString(R.string.objectives_2_objective); - objectivesPlugin.objectives.get(3).objective = MainApp.sResources.getString(R.string.objectives_3_objective); - objectivesPlugin.objectives.get(4).objective = MainApp.sResources.getString(R.string.objectives_4_objective); - objectivesPlugin.objectives.get(5).objective = MainApp.sResources.getString(R.string.objectives_5_objective); - objectivesPlugin.objectives.get(6).objective = MainApp.sResources.getString(R.string.objectives_6_objective); - objectivesPlugin.objectives.get(0).gate = MainApp.sResources.getString(R.string.objectives_0_gate); - objectivesPlugin.objectives.get(1).gate = MainApp.sResources.getString(R.string.objectives_1_gate); - objectivesPlugin.objectives.get(2).gate = MainApp.sResources.getString(R.string.objectives_2_gate); - objectivesPlugin.objectives.get(3).gate = MainApp.sResources.getString(R.string.objectives_3_gate); - objectivesPlugin.objectives.get(4).gate = MainApp.sResources.getString(R.string.objectives_4_gate); - objectivesPlugin.objectives.get(5).gate = MainApp.sResources.getString(R.string.objectives_5_gate); + getPlugin().objectives.get(0).objective = MainApp.sResources.getString(R.string.objectives_0_objective); + getPlugin().objectives.get(1).objective = MainApp.sResources.getString(R.string.objectives_1_objective); + getPlugin().objectives.get(2).objective = MainApp.sResources.getString(R.string.objectives_2_objective); + getPlugin().objectives.get(3).objective = MainApp.sResources.getString(R.string.objectives_3_objective); + getPlugin().objectives.get(4).objective = MainApp.sResources.getString(R.string.objectives_4_objective); + getPlugin().objectives.get(5).objective = MainApp.sResources.getString(R.string.objectives_5_objective); + getPlugin().objectives.get(6).objective = MainApp.sResources.getString(R.string.objectives_6_objective); + getPlugin().objectives.get(0).gate = MainApp.sResources.getString(R.string.objectives_0_gate); + getPlugin().objectives.get(1).gate = MainApp.sResources.getString(R.string.objectives_1_gate); + getPlugin().objectives.get(2).gate = MainApp.sResources.getString(R.string.objectives_2_gate); + getPlugin().objectives.get(3).gate = MainApp.sResources.getString(R.string.objectives_3_gate); + getPlugin().objectives.get(4).gate = MainApp.sResources.getString(R.string.objectives_4_gate); + getPlugin().objectives.get(5).gate = MainApp.sResources.getString(R.string.objectives_5_gate); updateGUI(); return view; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java index f124b81a56..a3838d53a6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java @@ -24,9 +24,12 @@ import info.nightscout.utils.JSONFormatter; public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, FragmentBase { private static Logger log = LoggerFactory.getLogger(OpenAPSMAFragment.class); - private static OpenAPSMAPlugin openAPSMAPlugin = new OpenAPSMAPlugin(); + private static OpenAPSMAPlugin openAPSMAPlugin; public static OpenAPSMAPlugin getPlugin() { + if(openAPSMAPlugin==null){ + openAPSMAPlugin = new OpenAPSMAPlugin(); + } return openAPSMAPlugin; } @@ -64,7 +67,7 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, public void onClick(View view) { switch (view.getId()) { case R.id.openapsma_run: - openAPSMAPlugin.invoke(); + getPlugin().invoke(); break; } @@ -98,15 +101,15 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, activity.runOnUiThread(new Runnable() { @Override public void run() { - if (openAPSMAPlugin.lastAPSResult != null) { - glucoseStatusView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getGlucoseStatusParam())); - currentTempView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getCurrentTempParam())); - iobDataView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getIobDataParam())); - profileView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getProfileParam())); - mealDataView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getMealDataParam())); - resultView.setText(JSONFormatter.format(openAPSMAPlugin.lastAPSResult.json)); - requestView.setText(openAPSMAPlugin.lastAPSResult.toSpanned()); - lastRunView.setText(openAPSMAPlugin.lastAPSRun.toLocaleString()); + if (getPlugin().lastAPSResult != null) { + glucoseStatusView.setText(JSONFormatter.format(getPlugin().lastDetermineBasalAdapterJS.getGlucoseStatusParam())); + currentTempView.setText(JSONFormatter.format(getPlugin().lastDetermineBasalAdapterJS.getCurrentTempParam())); + iobDataView.setText(JSONFormatter.format(getPlugin().lastDetermineBasalAdapterJS.getIobDataParam())); + profileView.setText(JSONFormatter.format(getPlugin().lastDetermineBasalAdapterJS.getProfileParam())); + mealDataView.setText(JSONFormatter.format(getPlugin().lastDetermineBasalAdapterJS.getMealDataParam())); + resultView.setText(JSONFormatter.format(getPlugin().lastAPSResult.json)); + requestView.setText(getPlugin().lastAPSResult.toSpanned()); + lastRunView.setText(getPlugin().lastAPSRun.toLocaleString()); } } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java index abf7858db4..bcbc14a877 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java @@ -12,9 +12,39 @@ public class Notification { public static final int LOW = 2; public static final int INFO = 3; + public static final int PROFILE_SET_FAILED = 0; + public static final int PROFILE_SET_OK = 1; + public int id; - Date date; - String text; - Date validTo = new Date(0); - int level; + public Date date; + public String text; + public int level; + public Date validTo = new Date(0); + + public Notification() { + } + + public Notification(int id, Date date, String text, int level, Date validTo) { + this.id = id; + this.date = date; + this.text = text; + this.level = level; + this.validTo = validTo; + } + + public Notification(int id, String text, int level, int validMinutes) { + this.id = id; + this.date = new Date(); + this.text = text; + this.level = level; + this.validTo = new Date(new Date().getTime() + validMinutes * 60 * 1000L); + } + + public Notification(int id, String text, int level) { + this.id = id; + this.date = new Date(); + this.text = text; + this.level = level; + this.validTo = new Date(0); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/NotificationStore.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/NotificationStore.java index 10c86ad2a2..b4cb02e914 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/NotificationStore.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/NotificationStore.java @@ -1,5 +1,8 @@ package info.nightscout.androidaps.plugins.Overview; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -12,6 +15,7 @@ import java.util.List; */ public class NotificationStore { + private static Logger log = LoggerFactory.getLogger(NotificationStore.class); public List store = new ArrayList(); public NotificationStore() { @@ -29,6 +33,7 @@ public class NotificationStore { } public void add(Notification n) { + log.info("Notification received: " + n.text); for (int i = 0; i < store.size(); i++) { if (get(i).id == n.id) { get(i).date = n.date; @@ -54,6 +59,7 @@ public class NotificationStore { Notification n = get(i); if (n.validTo.getTime() != 0 && n.validTo.getTime() < new Date().getTime()) { store.remove(i); + i--; } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java index 4003fedbdd..1b94ff5fad 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java @@ -64,7 +64,7 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { @Override public boolean isLoopEnabled() { - return true; + return MainApp.getConfigBuilder().getPumpDescription().isTempBasalCapable; } /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java index ef61949519..cf02e50597 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java @@ -8,6 +8,7 @@ import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; @@ -16,6 +17,8 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.FragmentBase; +import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; +import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.utils.SafeParse; public class SimpleProfileFragment extends Fragment implements FragmentBase { @@ -36,6 +39,7 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { EditText basalView; EditText targetlowView; EditText targethighView; + Button profileswitchButton; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -50,6 +54,7 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { basalView = (EditText) layout.findViewById(R.id.simpleprofile_basalrate); targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow); targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh); + profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch); mgdlView.setChecked(simpleProfilePlugin.mgdl); mmolView.setChecked(simpleProfilePlugin.mmol); @@ -80,6 +85,17 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { } }); + profileswitchButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); + final OptionsToShow profileswitch = new OptionsToShow(R.id.careportal_profileswitch, R.string.careportal_profileswitch, true, false, false, false, false, false, false, true, false); + profileswitch.executeProfileSwitch = true; + newDialog.setOptions(profileswitch); + newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); + } + }); + TextWatcher textWatch = new TextWatcher() { @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java index 93383032d7..852562ddda 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java @@ -29,9 +29,13 @@ import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommuni public class SmsCommunicatorFragment extends Fragment { private static Logger log = LoggerFactory.getLogger(SmsCommunicatorFragment.class); - private static SmsCommunicatorPlugin smsCommunicatorPlugin = new SmsCommunicatorPlugin(); + private static SmsCommunicatorPlugin smsCommunicatorPlugin; public static SmsCommunicatorPlugin getPlugin() { + + if(smsCommunicatorPlugin==null){ + smsCommunicatorPlugin = new SmsCommunicatorPlugin(); + } return smsCommunicatorPlugin; } @@ -81,15 +85,15 @@ public class SmsCommunicatorFragment extends Fragment { return (int) (object1.date.getTime() - object2.date.getTime()); } } - Collections.sort(smsCommunicatorPlugin.messages, new CustomComparator()); + Collections.sort(getPlugin().messages, new CustomComparator()); int messagesToShow = 40; - int start = Math.max(0, smsCommunicatorPlugin.messages.size() - messagesToShow); + int start = Math.max(0, getPlugin().messages.size() - messagesToShow); DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT); String logText = ""; - for (int x = start; x < smsCommunicatorPlugin.messages.size(); x++) { - SmsCommunicatorPlugin.Sms sms = smsCommunicatorPlugin.messages.get(x); + for (int x = start; x < getPlugin().messages.size(); x++) { + SmsCommunicatorPlugin.Sms sms = getPlugin().messages.get(x); if (sms.received) { logText += df.format(sms.date) + " <<< " + (sms.processed ? "● " : "○ ") + sms.phoneNumber + " " + sms.text + "
"; } else if (sms.sent) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java index 99508d2c7e..c150748920 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java @@ -17,6 +17,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.TempBasal; import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.VirtualPump.events.EventVirtualPumpUpdateGui; @@ -37,6 +38,36 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { boolean fragmentEnabled = true; boolean fragmentVisible = true; + PumpDescription pumpDescription = new PumpDescription(); + + public VirtualPumpPlugin() { + pumpDescription.isBolusCapable = true; + pumpDescription.bolusStep = 1d; + + pumpDescription.isExtendedBolusCapable = true; + pumpDescription.extendedBolusStep = 0.2d; + + pumpDescription.isTempBasalCapable = true; + pumpDescription.lowTempBasalStyle = PumpDescription.ABSOLUTE | PumpDescription.PERCENT; + pumpDescription.highTempBasalStyle = PumpDescription.ABSOLUTE | PumpDescription.PERCENT; + pumpDescription.maxHighTempPercent = 600; + pumpDescription.maxHighTempAbsolute = 10; + pumpDescription.lowTempPercentStep = 5; + pumpDescription.lowTempAbsoluteStep = 0.1; + pumpDescription.lowTempPercentDuration = 30; + pumpDescription.lowTempAbsoluteDuration = 30; + pumpDescription.highTempPercentStep = 10; + pumpDescription.highTempAbsoluteStep = 0.05d; + pumpDescription.highTempPercentDuration = 30; + pumpDescription.highTempAbsoluteDuration = 30; + + pumpDescription.isSetBasalProfileCapable = true; + pumpDescription.basalStep = 0.01d; + pumpDescription.basalMinimumRate = 0.04d; + + pumpDescription.isRefillingCapable = false; + } + @Override public String getFragmentClass() { return VirtualPumpFragment.class.getName(); @@ -367,4 +398,9 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { return "VirtualPump"; } + @Override + public PumpDescription getPumpDescription() { + return pumpDescription; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java index 1ab627fd33..0431c0e5d6 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java @@ -27,6 +27,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService; +import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; import info.nightscout.utils.ToastUtils; public class KeepAliveReceiver extends BroadcastReceiver { @@ -51,6 +52,18 @@ public class KeepAliveReceiver extends BroadcastReceiver { t.start(); } } + final DanaRKoreanPlugin danaRKoreanPlugin = (DanaRKoreanPlugin) MainApp.getSpecificPlugin(DanaRKoreanPlugin.class); + if (danaRKoreanPlugin != null && Config.DANAR && danaRKoreanPlugin.isEnabled(PluginBase.PUMP)) { + if (danaRKoreanPlugin.getDanaRPump().lastConnection.getTime() + 30 * 60 * 1000L < new Date().getTime() && !danaRKoreanPlugin.isConnected() && !danaRKoreanPlugin.isConnecting()) { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + danaRKoreanPlugin.doConnect("KeepAlive"); + } + }); + t.start(); + } + } wl.release(); } diff --git a/app/src/main/res/layout/simpleprofile_fragment.xml b/app/src/main/res/layout/simpleprofile_fragment.xml index f31a5f02c1..0c96593df1 100644 --- a/app/src/main/res/layout/simpleprofile_fragment.xml +++ b/app/src/main/res/layout/simpleprofile_fragment.xml @@ -16,7 +16,7 @@ @@ -25,14 +25,14 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2" - android:text="mgdl" /> + android:text="@string/mgdl" /> + android:text="@string/mmol" /> @@ -43,7 +43,7 @@ + +