From aaf66efa640d6ab1ea49f8667d455baab1364acd Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 3 Feb 2020 22:05:48 +0100 Subject: [PATCH] write offline events directly to DB --- .../plugins/aps/loop/LoopPlugin.java | 22 +++++++++++++++++-- .../automation/actions/ActionLoopResume.java | 2 +- .../plugins/general/nsclient/NSUpload.java | 8 +++---- .../general/overview/OverviewFragment.java | 6 ++--- .../smsCommunicator/SmsCommunicatorPlugin.kt | 4 ++-- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java index b0d6f89065..4710cf9b64 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java @@ -14,6 +14,8 @@ import android.os.SystemClock; import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; +import org.json.JSONException; +import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -697,7 +699,7 @@ public class LoopPlugin extends PluginBase { } }); } - NSUpload.uploadOpenAPSOffline(durationInMinutes); + createOfflineEvent(durationInMinutes); } public void suspendLoop(int durationInMinutes) { @@ -715,7 +717,23 @@ public class LoopPlugin extends PluginBase { } } }); - NSUpload.uploadOpenAPSOffline(durationInMinutes); + createOfflineEvent(durationInMinutes); } + public void createOfflineEvent(int durationInMinutes) { + JSONObject data = new JSONObject(); + try { + data.put("eventType", CareportalEvent.OPENAPSOFFLINE); + data.put("duration", durationInMinutes); + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + CareportalEvent event = new CareportalEvent(); + event.date = DateUtil.now(); + event.source = Source.USER; + event.eventType = CareportalEvent.OPENAPSOFFLINE; + event.json = data.toString(); + MainApp.getDbHelper().createOrUpdate(event); + NSUpload.uploadOpenAPSOffline(event); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopResume.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopResume.java index 38e5dee33a..16b478bbc2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopResume.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopResume.java @@ -28,7 +28,7 @@ public class ActionLoopResume extends Action { if (LoopPlugin.getPlugin().isSuspended()) { LoopPlugin.getPlugin().suspendTo(0); ConfigBuilderPlugin.getPlugin().storeSettings("ActionLoopResume"); - NSUpload.uploadOpenAPSOffline(0); + LoopPlugin.getPlugin().createOfflineEvent(0); RxBus.INSTANCE.send(new EventRefreshOverview("ActionLoopResume")); if (callback != null) callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java index 7fd6694481..0bc15d1d45 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java @@ -336,12 +336,10 @@ public class NSUpload { UploadQueue.add(new DbRequest("dbRemove", "treatments", _id)); } - public static void uploadOpenAPSOffline(double durationInMinutes) { + public static void uploadOpenAPSOffline(CareportalEvent event) { try { - JSONObject data = new JSONObject(); - data.put("eventType", "OpenAPS Offline"); - data.put("duration", durationInMinutes); - data.put("created_at", DateUtil.toISOString(new Date())); + JSONObject data = new JSONObject(event.json); + data.put("created_at", DateUtil.toISOString(event.date)); data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } catch (JSONException e) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java index be963d7a9a..931cca544b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java @@ -679,14 +679,14 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } } }); - NSUpload.uploadOpenAPSOffline(24 * 60); // upload 24h, we don't know real duration + LoopPlugin.getPlugin().createOfflineEvent(24 * 60); // upload 24h, we don't know real duration return true; } else if (item.getTitle().equals(MainApp.gs(R.string.enableloop))) { loopPlugin.setPluginEnabled(PluginType.LOOP, true); loopPlugin.setFragmentVisible(PluginType.LOOP, true); ConfigBuilderPlugin.getPlugin().storeSettings("EnablingLoop"); updateGUI("suspendmenu"); - NSUpload.uploadOpenAPSOffline(0); + LoopPlugin.getPlugin().createOfflineEvent(0); return true; } else if (item.getTitle().equals(MainApp.gs(R.string.resume)) || item.getTitle().equals(MainApp.gs(R.string.reconnect))) { @@ -701,7 +701,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } }); SP.putBoolean(R.string.key_objectiveusereconnect, true); - NSUpload.uploadOpenAPSOffline(0); + LoopPlugin.getPlugin().createOfflineEvent(0); return true; } else if (item.getTitle().equals(MainApp.gs(R.string.suspendloopfor1h))) { LoopPlugin.getPlugin().suspendLoop(60); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index c180dffe83..0021574812 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -330,7 +330,7 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription() "RESUME" -> { LoopPlugin.getPlugin().suspendTo(0) send(EventRefreshOverview("SMS_LOOP_RESUME")) - NSUpload.uploadOpenAPSOffline(0.0) + LoopPlugin.getPlugin().createOfflineEvent(0) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopresumed)) } "SUSPEND" -> { @@ -352,7 +352,7 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription() override fun run() { if (result.success) { LoopPlugin.getPlugin().suspendTo(System.currentTimeMillis() + anInteger() * 60L * 1000) - NSUpload.uploadOpenAPSOffline(anInteger() * 60.toDouble()) + LoopPlugin.getPlugin().createOfflineEvent(anInteger() * 60) send(EventRefreshOverview("SMS_LOOP_SUSPENDED")) val replyText = MainApp.gs(R.string.smscommunicator_loopsuspended) + " " + MainApp.gs(if (result.success) R.string.smscommunicator_tempbasalcanceled else R.string.smscommunicator_tempbasalcancelfailed)