write offline events directly to DB

This commit is contained in:
Milos Kozak 2020-02-03 22:05:48 +01:00
parent db47cd6fe9
commit aaf66efa64
5 changed files with 29 additions and 13 deletions

View file

@ -14,6 +14,8 @@ import android.os.SystemClock;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -697,7 +699,7 @@ public class LoopPlugin extends PluginBase {
} }
}); });
} }
NSUpload.uploadOpenAPSOffline(durationInMinutes); createOfflineEvent(durationInMinutes);
} }
public void suspendLoop(int 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);
}
} }

View file

@ -28,7 +28,7 @@ public class ActionLoopResume extends Action {
if (LoopPlugin.getPlugin().isSuspended()) { if (LoopPlugin.getPlugin().isSuspended()) {
LoopPlugin.getPlugin().suspendTo(0); LoopPlugin.getPlugin().suspendTo(0);
ConfigBuilderPlugin.getPlugin().storeSettings("ActionLoopResume"); ConfigBuilderPlugin.getPlugin().storeSettings("ActionLoopResume");
NSUpload.uploadOpenAPSOffline(0); LoopPlugin.getPlugin().createOfflineEvent(0);
RxBus.INSTANCE.send(new EventRefreshOverview("ActionLoopResume")); RxBus.INSTANCE.send(new EventRefreshOverview("ActionLoopResume"));
if (callback != null) if (callback != null)
callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run(); callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run();

View file

@ -336,12 +336,10 @@ public class NSUpload {
UploadQueue.add(new DbRequest("dbRemove", "treatments", _id)); UploadQueue.add(new DbRequest("dbRemove", "treatments", _id));
} }
public static void uploadOpenAPSOffline(double durationInMinutes) { public static void uploadOpenAPSOffline(CareportalEvent event) {
try { try {
JSONObject data = new JSONObject(); JSONObject data = new JSONObject(event.json);
data.put("eventType", "OpenAPS Offline"); data.put("created_at", DateUtil.toISOString(event.date));
data.put("duration", durationInMinutes);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name));
UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
} catch (JSONException e) { } catch (JSONException e) {

View file

@ -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; return true;
} else if (item.getTitle().equals(MainApp.gs(R.string.enableloop))) { } else if (item.getTitle().equals(MainApp.gs(R.string.enableloop))) {
loopPlugin.setPluginEnabled(PluginType.LOOP, true); loopPlugin.setPluginEnabled(PluginType.LOOP, true);
loopPlugin.setFragmentVisible(PluginType.LOOP, true); loopPlugin.setFragmentVisible(PluginType.LOOP, true);
ConfigBuilderPlugin.getPlugin().storeSettings("EnablingLoop"); ConfigBuilderPlugin.getPlugin().storeSettings("EnablingLoop");
updateGUI("suspendmenu"); updateGUI("suspendmenu");
NSUpload.uploadOpenAPSOffline(0); LoopPlugin.getPlugin().createOfflineEvent(0);
return true; return true;
} else if (item.getTitle().equals(MainApp.gs(R.string.resume)) || } else if (item.getTitle().equals(MainApp.gs(R.string.resume)) ||
item.getTitle().equals(MainApp.gs(R.string.reconnect))) { 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); SP.putBoolean(R.string.key_objectiveusereconnect, true);
NSUpload.uploadOpenAPSOffline(0); LoopPlugin.getPlugin().createOfflineEvent(0);
return true; return true;
} else if (item.getTitle().equals(MainApp.gs(R.string.suspendloopfor1h))) { } else if (item.getTitle().equals(MainApp.gs(R.string.suspendloopfor1h))) {
LoopPlugin.getPlugin().suspendLoop(60); LoopPlugin.getPlugin().suspendLoop(60);

View file

@ -330,7 +330,7 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
"RESUME" -> { "RESUME" -> {
LoopPlugin.getPlugin().suspendTo(0) LoopPlugin.getPlugin().suspendTo(0)
send(EventRefreshOverview("SMS_LOOP_RESUME")) send(EventRefreshOverview("SMS_LOOP_RESUME"))
NSUpload.uploadOpenAPSOffline(0.0) LoopPlugin.getPlugin().createOfflineEvent(0)
sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopresumed)) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopresumed))
} }
"SUSPEND" -> { "SUSPEND" -> {
@ -352,7 +352,7 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
override fun run() { override fun run() {
if (result.success) { if (result.success) {
LoopPlugin.getPlugin().suspendTo(System.currentTimeMillis() + anInteger() * 60L * 1000) LoopPlugin.getPlugin().suspendTo(System.currentTimeMillis() + anInteger() * 60L * 1000)
NSUpload.uploadOpenAPSOffline(anInteger() * 60.toDouble()) LoopPlugin.getPlugin().createOfflineEvent(anInteger() * 60)
send(EventRefreshOverview("SMS_LOOP_SUSPENDED")) send(EventRefreshOverview("SMS_LOOP_SUSPENDED"))
val replyText = MainApp.gs(R.string.smscommunicator_loopsuspended) + " " + val replyText = MainApp.gs(R.string.smscommunicator_loopsuspended) + " " +
MainApp.gs(if (result.success) R.string.smscommunicator_tempbasalcanceled else R.string.smscommunicator_tempbasalcancelfailed) MainApp.gs(if (result.success) R.string.smscommunicator_tempbasalcanceled else R.string.smscommunicator_tempbasalcancelfailed)