diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index b2f46cddb8..22d99bac67 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -22,6 +22,7 @@ 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; @@ -70,6 +71,7 @@ public class MainApp extends Application { pluginsList.add(OverviewFragment.getPlugin()); pluginsList.add(ActionsFragment.getPlugin()); if (Config.DANAR) pluginsList.add(DanaRFragment.getPlugin()); + if (Config.DANAR) pluginsList.add(DanaRKoreanFragment.getPlugin()); if (Config.MM640G) pluginsList.add(MM640gFragment.getPlugin()); if (Config.CAREPORTALENABLED) pluginsList.add(CareportalFragment.getPlugin()); pluginsList.add(VirtualPumpFragment.getPlugin()); 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..26473eb58c --- /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..cbfcd0b4f9 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java @@ -0,0 +1,737 @@ +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.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; + + 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); + } + + 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) { + boolean previousValue = useExtendedBoluses; + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + useExtendedBoluses = sharedPreferences.getBoolean("danar_useextended", false); + 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(); + } + + @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; + } + + /** + * 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..9f0f20e070 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java @@ -0,0 +1,160 @@ +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 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); + + // Status + public boolean pumpSuspended; + public boolean calculatorEnabled; + public double dailyTotalUnits; + public int maxDailyTotalUnits; + + public double iob; + + public double reservoirRemainingUnits; + public int batteryRemaining; + + public boolean bolusBlocked; + public Date lastBolusTime = new Date(0); + public double lastBolusAmount; + + 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", nightCF)); + carbratios.put(new JSONObject().put("time", "06:00").put("timeAsSeconds", 6 * 3600).put("value", morningCF)); + carbratios.put(new JSONObject().put("time", "11:00").put("timeAsSeconds", 11 * 3600).put("value", afternoonCF)); + carbratios.put(new JSONObject().put("time", "14:00").put("timeAsSeconds", 17 * 3600).put("value", eveningCF)); + carbratios.put(new JSONObject().put("time", "22:00").put("timeAsSeconds", 22 * 3600).put("value", nightCF)); + 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", nightCIR)); + sens.put(new JSONObject().put("time", "06:00").put("timeAsSeconds", 6 * 3600).put("value", morningCIR)); + sens.put(new JSONObject().put("time", "11:00").put("timeAsSeconds", 11 * 3600).put("value", afternoonCIR)); + sens.put(new JSONObject().put("time", "17:00").put("timeAsSeconds", 17 * 3600).put("value", eveningCIR)); + sens.put(new JSONObject().put("time", "22:00").put("timeAsSeconds", 22 * 3600).put("value", nightCIR)); + 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..3c49df5229 --- /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.events.EventDanaRConnectionStatus; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRSyncStatus; +import info.nightscout.androidaps.plugins.DanaRKorean.Services.ExecutionService; +import info.nightscout.androidaps.plugins.DanaRKorean.comm.RecordTypes; +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/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java new file mode 100644 index 0000000000..40e7808271 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java @@ -0,0 +1,453 @@ +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.SerialIOThread; +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; +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.comm.*; +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 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(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 (danaRKoreanPump.isNewPump) { + 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()); + 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); + MsgSetBasalProfile msgSet = new MsgSetBasalProfile((byte) 0, basal); + mSerialIOThread.sendMessage(msgSet); + //MsgSetActivateBasalProfile msgActivate = new MsgSetActivateBasalProfile((byte) 0); + //mSerialIOThread.sendMessage(msgActivate); + 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..048074ab37 --- /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 MsgSetBasalProfile()); // 0x3306 CMD_SETTING_BASAL_PROFILE_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/MessageOriginalNames.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MessageOriginalNames.java new file mode 100644 index 0000000000..513e7dfcb9 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MessageOriginalNames.java @@ -0,0 +1,163 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; + +/** + * Created by mike on 28.05.2016. + */ +public class MessageOriginalNames { + private static Logger log = LoggerFactory.getLogger(MessageOriginalNames.class); + + public static HashMap messageNames; + + static { + messageNames = new HashMap(); + + messageNames.put(0x3001, "CMD_CONNECT"); + messageNames.put(0x3002, "CMD_DISCONNECT"); + + messageNames.put(0x3101, "CMD_HISTORY_MEAL_INS"); + messageNames.put(0x3102, "CMD_HISTORY_DAY_INS"); + messageNames.put(0x3103, "CMD_HISTORY_AIR_SUB"); + messageNames.put(0x3104, "CMD_HISTORY_GLUCOSE"); + messageNames.put(0x3105, "CMD_HISTORY_ALARM"); + messageNames.put(0x3106, "CMD_HISTORY_ERROR"); + messageNames.put(0x3107, "CMD_HISTORY_CARBOHY"); + messageNames.put(0x3108, "CMD_HISTORY_REFILL"); + messageNames.put(0x3109, "CMD_HISTORY_SUSPEND"); + messageNames.put(0x310a, "CMD_HISTORY_BASAL_HOUR"); + messageNames.put(0x310b, "CMD_HISTORY_TB"); + messageNames.put(0x31f1, "CMD_HISTORY_STOP"); + messageNames.put(0x31f2, "CMD_HISTORY_LAST_T_R"); + messageNames.put(0x31f3, "CMD_HISTORY_LAST_T_S"); + + messageNames.put(0x0501, "CMD_HISPAGE_MEAL_INS"); + messageNames.put(0x0502, "CMD_HISPAGE_DAY_INS"); + messageNames.put(0x0503, "CMD_HISPAGE_AIR_SUB"); + messageNames.put(0x0504, "CMD_HISPAGE_GLUCOSE"); + messageNames.put(0x0505, "CMD_HISPAGE_ALARM"); + messageNames.put(0x0506, "CMD_HISPAGE_ERROR"); + messageNames.put(0x0507, "CMD_HISPAGE_CARBOHY"); + messageNames.put(0x0508, "CMD_HISPAGE_REFILL"); + messageNames.put(0x050a, "CMD_HISPAGE_DAILTY_PRE_DATA"); + messageNames.put(0x050b, "CMD_HISPAGE_BOLUS_AVG"); + messageNames.put(0x050c, "CMD_HISPAGE_BASAL_RECORD"); + messageNames.put(0x050d, "CMD_HISPAGE_TB"); + + messageNames.put(0x3201, "CMD_SETTING_V_MEAL_INS_I"); + messageNames.put(0x3202, "CMD_SETTING_V_BASAL_INS_I"); + messageNames.put(0x3203, "CMD_SETTING_V_MEAL_SETTING_I"); + messageNames.put(0x3204, "CMD_SETTING_V_CCC_I"); + messageNames.put(0x3205, "CMD_SETTING_V_MAX_VALUE_I"); + messageNames.put(0x3206, "CMD_SETTING_V_BASAL_PROFILE_ALL"); + messageNames.put(0x3207, "CMD_SETTING_V_SHIPPING_I"); + messageNames.put(0x3208, "CMD_SETTING_V_CLOGGIN_SENS_I"); + messageNames.put(0x3209, "CMD_SETTING_V_GLUCOSEandEASY"); + messageNames.put(0x320a, "CMD_SETTING_V_TIME_I"); + messageNames.put(0x320b, "CMD_SETTING_V_USER_OPTIONS"); + messageNames.put(0x320c, "CMD_SETTING_V_PROFILE_NUMBER"); + 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(0x3303, "CMD_SETTING_MEAL_SETTING_S"); + messageNames.put(0x3304, "CMD_SETTING_CCC_S"); + messageNames.put(0x3305, "CMD_SETTING_MAX_VALUE_S"); + messageNames.put(0x3306, "CMD_SETTING_BASAL_PROFILE_S"); + messageNames.put(0x3307, "CMD_SETTING_SHIPPING_S"); + messageNames.put(0x3308, "CMD_SETTING_CLOGGIN_SENS_S"); + messageNames.put(0x3309, "CMD_SETTING_GLUCOSEandEASY_S"); + messageNames.put(0x330a, "CMD_SETTING_TIME_S"); + messageNames.put(0x330b, "CMD_SETTING_USER_OPTIONS_S"); + messageNames.put(0x330c, "CMD_SETTING_PROFILE_NUMBER_S"); + messageNames.put(0x330d, "CMD_SETTING_CIR_CF_VALUE_S"); + + messageNames.put(0x0101, "CMD_MEALINS_STOP"); + messageNames.put(0x0102, "CMD_MEALINS_START_DATA"); + messageNames.put(0x0103, "CMD_MEALINS_START_NODATA"); + messageNames.put(0x0104, "CMD_MEALINS_START_DATA_SPEED"); + messageNames.put(0x0105, "CMD_MEALINS_START_NODATA_SPEED"); + + messageNames.put(0x0201, "CMD_PUMP_ACT_INS_VALUE"); + messageNames.put(0x0202, "CMD_PUMP_THIS_REMAINDER_MEAL_INS"); + messageNames.put(0x0203, "CMD_PUMP_BASE_SET"); + messageNames.put(0x0204, "CMD_PUMP_CALCULATION_SETTING"); + messageNames.put(0x0205, "CMD_PUMP_EXERCISE_MODE"); + messageNames.put(0x0206, "CMD_PUMP_MEAL_INS_I"); + + messageNames.put(0x0207, "CMD_PUMP_EXPANS_INS_I"); + messageNames.put(0x0208, "CMD_PUMP_EXPANS_INS_RQ"); + + messageNames.put(0x0209, "CMD_PUMP_DUAL_INS_RQ"); + messageNames.put(0x020a, "CMD_PUMP_INITVIEW_I"); + messageNames.put(0x020b, "CMD_PUMP_STATUS"); + messageNames.put(0x020c, "CMD_PUMP_CAR_N_CIR"); + + messageNames.put(0x0301, "CMD_PUMPINIT_TIME_INFO"); + messageNames.put(0x0302, "CMD_PUMPINIT_BOLUS_INFO"); + messageNames.put(0x0303, "CMD_PUMPINIT_INIT_INFO"); + messageNames.put(0x0304, "CMD_PUMPINIT_OPTION"); + + messageNames.put(0x0401, "CMD_PUMPSET_EXERCISE_S"); + messageNames.put(0x0402, "CMD_PUMPSET_HIS_S"); + messageNames.put(0x0403, "CMD_PUMPSET_EXERCISE_STOP"); + + messageNames.put(0x0404, "CMD_PUMPSET_PAUSE"); + messageNames.put(0x0405, "CMD_PUMPSET_PAUSE_STOP"); + + messageNames.put(0x0406, "CMD_PUMPSET_EXPANS_INS_STOP"); + messageNames.put(0x0407, "CMD_PUMPSET_EXPANS_INS_S"); + + messageNames.put(0x0408, "CMD_PUMPSET_DUAL_S"); + messageNames.put(0x0409, "CMD_PUMPSET_EASY_OFF"); + + + messageNames.put(0x0601, "CMD_PUMPOWAY_SYSTEM_STATUS"); + messageNames.put(0x0602, "CMD_PUMPOWAY_GLUCOSE_ALARM"); + messageNames.put(0x0603, "CMD_PUMPOWAY_LOW_INSULIN_ALARM"); + messageNames.put(0x0610, "CMD_PUMP_ALARM_TIEOUT"); + + messageNames.put(0x0701, "CMD_MSGRECEP_TAKE_SUGAR"); + messageNames.put(0x0702, "CMD_MSGRECEP_GO_TO_DOCTOR"); + messageNames.put(0x0703, "CMD_MSGRECEP_CALL_TO_CAREGIVER"); + messageNames.put(0x0704, "CMD_MSGRECEP_CHECK_GLUCOSE_AGAIN"); + messageNames.put(0x0705, "CMD_MSGRECEP_CALL_TO_HOME"); + messageNames.put(0x0706, "CMD_MSGRECEP_DO_DELIVER"); + + messageNames.put(0x0801, "CMD_MSGSEND_YES_I_DO"); + messageNames.put(0x0802, "CMD_MSGSEND_NO_I_CANNOT"); + messageNames.put(0x0803, "CMD_MSGSEND_CALL_TO_ME_MOM"); + messageNames.put(0x0804, "CMD_MSGSEND_DO_NOT_INFUSE"); + + messageNames.put(0x0901, "CMD_FILL_REFILL_COUNT"); + messageNames.put(0x0902, "CMD_FILL_PRIME_CHECK"); + messageNames.put(0x0903, "CMD_FILL_PRIME_END"); + messageNames.put(0x0904, "CMD_FILL_PRIME_STOP"); + messageNames.put(0x0905, "CMD_FILL_PRIME_PAUSE"); + messageNames.put(0x0906, "CMD_FILL_PRIME_RATE"); + + messageNames.put(0x41f2, "CMD_HISTORY_ALL"); + messageNames.put(0x42f2, "CMD_HISTORY_NEW"); + + messageNames.put(0x41f1, "CMD_HISTORY_ALL_DONE"); + messageNames.put(0x42f1, "CMD_HISTORY_NEW_DONE"); + + messageNames.put(0xF0F1, "CMD_PUMP_CHECK_VALUE"); + messageNames.put(0xF0F2, "CMD_PUMP_TIMECHANGE_CHECK"); + messageNames.put(0xF0F3, "CMD_PUMP_TIMECHANGE_CLEAR"); + messageNames.put(0x43F2, "CMD_HISTORY_DATEOVER_ALL"); + messageNames.put(0x4300, "CMD_HISTORY_DATEOVER_DONE"); + } + + public static String getName(Integer command) { + if (messageNames.containsKey(command)) + return messageNames.get(command); + else { + log.debug("Unknown command: " + String.format("%04X", command)); + return "UNKNOWN_COMMAND"; + } + } +} 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..e0fadbf148 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgCheckValue.java @@ -0,0 +1,32 @@ +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 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) { + int a = intFromBuff(bytes, 0, 1); + int b = intFromBuff(bytes, 1, 1); + if (a != 3 || b <= 0) { + // another message will follow + } else { + + } + 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/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..88aff8ff9b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgHistoryAll.java @@ -0,0 +1,161 @@ +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.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..699bdff968 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusBasic.java @@ -0,0 +1,38 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +public class MsgInitConnStatusBasic extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgInitConnStatusBasic.class); + + public MsgInitConnStatusBasic() { + SetCommand(0x0303); + } + + @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; + try { + o = intFromBuff(bytes, 21, 1); + } catch (Exception ex) { + o = 0; + } + } +} 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..5b0e10f577 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusBolus.java @@ -0,0 +1,26 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.plugins.DanaR.comm.MessageBase; + +/** + * 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) { + 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); + } +} 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..be835bbaa8 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgInitConnStatusTime.java @@ -0,0 +1,25 @@ +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; + +public class MsgInitConnStatusTime extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgInitConnStatusTime.class); + + public MsgInitConnStatusTime() { + SetCommand(0x0301); + } + + @Override + public void handleMessage(byte[] bytes) { + Date time = dateTimeSecFromBuff(bytes, 0); + + if (Config.logDanaMessageDetail) + log.debug("Pump time: " + time); + } +} 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/MsgSetBasalProfile.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetBasalProfile.java new file mode 100644 index 0000000000..8d9b13bf85 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetBasalProfile.java @@ -0,0 +1,40 @@ +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 MsgSetBasalProfile extends MessageBase { + private static Logger log = LoggerFactory.getLogger(MsgSetBasalProfile.class); + + public MsgSetBasalProfile() { + SetCommand(0x3306); + } + + // index 0-3 + public MsgSetBasalProfile(byte index, double[] values) { + this(); + AddParamByte(index); + for (Integer i = 0; i < 24; i++) { + AddParamInt((int) (values[i] * 100)); + } + if (Config.logDanaMessageDetail) + log.debug("Set basal profile: " + index); + } + + @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!!!"); + } else { + if (Config.logDanaMessageDetail) + log.debug("Set basal profile result: " + result); + } + } + + +} 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..2adfab7ca6 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSetCarbsEntry.java @@ -0,0 +1,45 @@ +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; + +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/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/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..2856b8dea9 --- /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 / 100d; + } + } + } 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 / 100d; + } + } + } + + 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..53184cd85f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgSettingShippingInfo.java @@ -0,0 +1,37 @@ +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; + +/** + * 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) { + DanaRKoreanPlugin.getDanaRPump().serialNumber = stringFromBuff(bytes, 0, 10); + DanaRKoreanPlugin.getDanaRPump().shippingDate = dateFromBuff(bytes, 10); + DanaRKoreanPlugin.getDanaRPump().shippingCountry = asciiStringFromBuff(bytes, 13, 3); + if (DanaRKoreanPlugin.getDanaRPump().shippingDate.getTime() > new Date(116, 4, 1).getTime()) { + DanaRKoreanPlugin.getDanaRPump().isNewPump = true; + } else + DanaRKoreanPlugin.getDanaRPump().isNewPump = false; + if (Config.logDanaMessageDetail) { + log.debug("Serial number: " + DanaRKoreanPlugin.getDanaRPump().serialNumber); + log.debug("Shipping date: " + DanaRKoreanPlugin.getDanaRPump().shippingDate); + log.debug("Shipping country: " + DanaRKoreanPlugin.getDanaRPump().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..3dcd6a71bd --- /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..7f4745dd18 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusBasic.java @@ -0,0 +1,56 @@ +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) { + 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); + + DanaRKoreanPlugin.getDanaRPump().pumpSuspended = pumpSuspended; + DanaRKoreanPlugin.getDanaRPump().calculatorEnabled = calculatorEnabled; + DanaRKoreanPlugin.getDanaRPump().dailyTotalUnits = dailyTotalUnits; + DanaRKoreanPlugin.getDanaRPump().maxDailyTotalUnits = maxDailyTotalUnits; + DanaRKoreanPlugin.getDanaRPump().reservoirRemainingUnits = reservoirRemainingUnits; + DanaRKoreanPlugin.getDanaRPump().bolusBlocked = bolusBlocked; + DanaRKoreanPlugin.getDanaRPump().currentBasal = currentBasal; + DanaRKoreanPlugin.getDanaRPump().tempBasalPercent = tempBasalPercent; + DanaRKoreanPlugin.getDanaRPump().isExtendedInProgress = isExtendedInProgress; + DanaRKoreanPlugin.getDanaRPump().isTempBasalInProgress = isTempBasalInProgress; + DanaRKoreanPlugin.getDanaRPump().batteryRemaining = batteryRemaining; + + 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); + } + } +} 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..8f14f67457 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/MsgStatusBolusExtended.java @@ -0,0 +1,112 @@ +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 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/DanaRKorean/comm/RecordTypes.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/RecordTypes.java new file mode 100644 index 0000000000..e2574b53cc --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/comm/RecordTypes.java @@ -0,0 +1,19 @@ +package info.nightscout.androidaps.plugins.DanaRKorean.comm; + +/** + * Created by mike on 28.05.2016. + */ +public class RecordTypes { + public static final byte RECORD_TYPE_BOLUS = (byte) 0x01; + public static final byte RECORD_TYPE_DAILY = (byte) 0x02; + public static final byte RECORD_TYPE_PRIME = (byte) 0x03; + public static final byte RECORD_TYPE_ERROR = (byte) 0x04; + public static final byte RECORD_TYPE_ALARM = (byte) 0x05; + public static final byte RECORD_TYPE_GLUCOSE = (byte) 0x06; + public static final byte RECORD_TYPE_CARBO = (byte) 0x08; + public static final byte RECORD_TYPE_REFILL = (byte) 0x09; + public static final byte RECORD_TYPE_SUSPEND = (byte) 0x0B; + public static final byte RECORD_TYPE_BASALHOUR = (byte) 0x0C; + public static final byte RECORD_TYPE_TB = (byte) 0x0D; + public static final byte RECORD_TYPE_TEMP_BASAL = (byte) 0x14; +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1543734b62..02dda92323 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -375,4 +375,5 @@ Low Battery Pump Shutdown Pump Battery Discharged + DanaR Korean diff --git a/wear/wear.iml b/wear/wear.iml index a09c181fa1..8a3092b3f4 100644 --- a/wear/wear.iml +++ b/wear/wear.iml @@ -123,6 +123,8 @@ + + @@ -131,11 +133,13 @@ + +