Simplify NSClient
This commit is contained in:
parent
5c9ab721ce
commit
e58b7f8040
13 changed files with 74 additions and 505 deletions
|
@ -123,16 +123,6 @@
|
|||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<!-- NSClient -->
|
||||
<receiver
|
||||
android:name=".plugins.general.nsclient.receivers.DBAccessReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="info.nightscout.client.DBACCESS" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<!-- Network change local receiver -->
|
||||
<receiver android:name=".receivers.NetworkChangeReceiver">
|
||||
<intent-filter>
|
||||
|
|
|
@ -49,13 +49,9 @@ import info.nightscout.androidaps.plugins.general.maintenance.LoggerUtils;
|
|||
import info.nightscout.androidaps.plugins.general.maintenance.MaintenancePlugin;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.receivers.AckAlarmReceiver;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.receivers.DBAccessReceiver;
|
||||
import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin;
|
||||
import info.nightscout.androidaps.plugins.general.persistentNotification.PersistentNotificationPlugin;
|
||||
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin;
|
||||
import info.nightscout.androidaps.plugins.source.RandomBgPlugin;
|
||||
import info.nightscout.androidaps.utils.ActivityMonitor;
|
||||
import info.nightscout.androidaps.plugins.general.wear.WearPlugin;
|
||||
import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin;
|
||||
import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin;
|
||||
|
@ -77,6 +73,7 @@ import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin;
|
|||
import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref0Plugin;
|
||||
import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin;
|
||||
import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin;
|
||||
import info.nightscout.androidaps.plugins.source.RandomBgPlugin;
|
||||
import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin;
|
||||
import info.nightscout.androidaps.plugins.source.SourceEversensePlugin;
|
||||
import info.nightscout.androidaps.plugins.source.SourceGlimpPlugin;
|
||||
|
@ -91,6 +88,7 @@ import info.nightscout.androidaps.receivers.KeepAliveReceiver;
|
|||
import info.nightscout.androidaps.receivers.NSAlarmReceiver;
|
||||
import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver;
|
||||
import info.nightscout.androidaps.services.Intents;
|
||||
import info.nightscout.androidaps.utils.ActivityMonitor;
|
||||
import info.nightscout.androidaps.utils.FabricPrivacy;
|
||||
import info.nightscout.androidaps.utils.LocaleHelper;
|
||||
import info.nightscout.androidaps.utils.SP;
|
||||
|
@ -115,8 +113,6 @@ public class MainApp extends Application {
|
|||
|
||||
private static DataReceiver dataReceiver = new DataReceiver();
|
||||
private static NSAlarmReceiver alarmReciever = new NSAlarmReceiver();
|
||||
private static AckAlarmReceiver ackAlarmReciever = new AckAlarmReceiver();
|
||||
private static DBAccessReceiver dbAccessReciever = new DBAccessReceiver();
|
||||
private LocalBroadcastManager lbm;
|
||||
BroadcastReceiver btReceiver;
|
||||
TimeDateOrTZChangeReceiver timeDateOrTZChangeReceiver;
|
||||
|
@ -297,12 +293,6 @@ public class MainApp extends Application {
|
|||
lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_CLEAR_ALARM));
|
||||
lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_URGENT_ALARM));
|
||||
|
||||
//register ack alarm
|
||||
lbm.registerReceiver(ackAlarmReciever, new IntentFilter(Intents.ACTION_ACK_ALARM));
|
||||
|
||||
//register dbaccess
|
||||
lbm.registerReceiver(dbAccessReciever, new IntentFilter(Intents.ACTION_DATABASE));
|
||||
|
||||
this.timeDateOrTZChangeReceiver = new TimeDateOrTZChangeReceiver();
|
||||
this.timeDateOrTZChangeReceiver.registerBroadcasts(this);
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.utils.DateUtil;
|
||||
|
||||
/**
|
||||
* Created by mike on 27.02.2016.
|
||||
|
@ -40,36 +41,49 @@ public class DbRequest {
|
|||
}
|
||||
|
||||
// dbAdd
|
||||
public DbRequest(String action, String collection, String nsClientID, JSONObject data) {
|
||||
public DbRequest(String action, String collection, JSONObject data) {
|
||||
try {
|
||||
data.put("NSCLIENT_ID", nsClientID);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.action = action;
|
||||
this.collection = collection;
|
||||
this.data = data.toString();
|
||||
this.nsClientID = nsClientID;
|
||||
this.nsClientID = "" + DateUtil.now();
|
||||
this._id = "";
|
||||
}
|
||||
|
||||
// dbUpdate, dbUpdateUnset
|
||||
public DbRequest(String action, String collection, String nsClientID, String _id, JSONObject data) {
|
||||
public DbRequest(String action, String collection, String _id, JSONObject data) {
|
||||
try {
|
||||
data.put("NSCLIENT_ID", nsClientID);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.action = action;
|
||||
this.collection = collection;
|
||||
this.data = data.toString();
|
||||
this.nsClientID = nsClientID;
|
||||
this.nsClientID = "" + DateUtil.now();
|
||||
this._id = _id;
|
||||
}
|
||||
|
||||
// dbRemove
|
||||
public DbRequest(String action, String collection, String nsClientID, String _id) {
|
||||
public DbRequest(String action, String collection,
|
||||
String _id) {
|
||||
JSONObject data = new JSONObject();
|
||||
try {
|
||||
data.put("NSCLIENT_ID", nsClientID);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.action = action;
|
||||
this.collection = collection;
|
||||
this.data = new JSONObject().toString();
|
||||
this.nsClientID = nsClientID;
|
||||
this.data = data.toString();
|
||||
this.nsClientID = "" + DateUtil.now();
|
||||
this._id = _id;
|
||||
}
|
||||
|
||||
public String hash() {
|
||||
return Hashing.sha1().hashString(action + collection + _id + data.toString(), Charsets.UTF_8).toString();
|
||||
}
|
||||
|
||||
public JSONObject toJSON() {
|
||||
JSONObject object = new JSONObject();
|
||||
try {
|
||||
|
|
|
@ -32,6 +32,8 @@ import info.nightscout.androidaps.interfaces.PluginDescription;
|
|||
import info.nightscout.androidaps.interfaces.PluginType;
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.plugins.bus.RxBus;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.data.AlarmAck;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.data.NSAlarm;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientNewLog;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientUpdateGUI;
|
||||
|
@ -250,4 +252,25 @@ public class NSClientPlugin extends PluginBase {
|
|||
public boolean hasWritePermission() {
|
||||
return nsClientService.hasWriteAuth;
|
||||
}
|
||||
|
||||
public void handleClearAlarm(NSAlarm originalAlarm, long silenceTimeInMsec) {
|
||||
|
||||
if (!isEnabled(PluginType.GENERAL)) {
|
||||
return;
|
||||
}
|
||||
if (SP.getBoolean(R.string.key_ns_noupload, false)) {
|
||||
if (L.isEnabled(L.NSCLIENT))
|
||||
log.debug("Upload disabled. Message dropped");
|
||||
return;
|
||||
}
|
||||
|
||||
AlarmAck ack = new AlarmAck();
|
||||
ack.level = originalAlarm.getLevel();
|
||||
ack.group = originalAlarm.getGroup();
|
||||
ack.silenceTime = silenceTimeInMsec;
|
||||
|
||||
if (nsClientService != null)
|
||||
nsClientService.sendAlarmAck(ack);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ import android.content.pm.ResolveInfo;
|
|||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.JSONArray;
|
||||
|
@ -24,22 +24,21 @@ import java.util.Locale;
|
|||
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.R;
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
|
||||
import info.nightscout.androidaps.services.Intents;
|
||||
import info.nightscout.androidaps.data.DetailedBolusInfo;
|
||||
import info.nightscout.androidaps.data.Profile;
|
||||
import info.nightscout.androidaps.db.BgReading;
|
||||
import info.nightscout.androidaps.db.CareportalEvent;
|
||||
import info.nightscout.androidaps.db.DbRequest;
|
||||
import info.nightscout.androidaps.db.ExtendedBolus;
|
||||
import info.nightscout.androidaps.db.ProfileSwitch;
|
||||
import info.nightscout.androidaps.db.TempTarget;
|
||||
import info.nightscout.androidaps.db.TemporaryBasal;
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.plugins.aps.loop.APSResult;
|
||||
import info.nightscout.androidaps.plugins.aps.loop.DeviceStatus;
|
||||
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.data.DbLogger;
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
|
||||
import info.nightscout.androidaps.utils.BatteryLevel;
|
||||
import info.nightscout.androidaps.utils.DateUtil;
|
||||
import info.nightscout.androidaps.utils.SP;
|
||||
|
@ -65,15 +64,7 @@ public class NSUpload {
|
|||
data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name));
|
||||
if (originalExtendedAmount != null)
|
||||
data.put("originalExtendedAmount", originalExtendedAmount); // for back synchronization
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "treatments");
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
|
@ -107,15 +98,7 @@ public class NSUpload {
|
|||
data.put("pumpId", temporaryBasal.pumpId);
|
||||
data.put("created_at", DateUtil.toISOString(temporaryBasal.date));
|
||||
data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name));
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "treatments");
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
|
@ -133,15 +116,7 @@ public class NSUpload {
|
|||
data.put("isFakedTempBasal", isFakedTempBasal);
|
||||
if (pumpId != 0)
|
||||
data.put("pumpId", pumpId);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "treatments");
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
|
@ -161,15 +136,7 @@ public class NSUpload {
|
|||
data.put("pumpId", extendedBolus.pumpId);
|
||||
data.put("created_at", DateUtil.toISOString(extendedBolus.date));
|
||||
data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name));
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "treatments");
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
|
@ -189,15 +156,7 @@ public class NSUpload {
|
|||
data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name));
|
||||
if (pumpId != 0)
|
||||
data.put("pumpId", pumpId);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "treatments");
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
|
@ -258,16 +217,7 @@ public class NSUpload {
|
|||
deviceStatus.uploaderBattery = batteryLevel;
|
||||
|
||||
deviceStatus.created_at = DateUtil.toISOString(new Date());
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "devicestatus");
|
||||
bundle.putString("data", deviceStatus.mongoRecord().toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, deviceStatus.mongoRecord().toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "devicestatus", deviceStatus.mongoRecord().toString()));
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
|
@ -333,17 +283,7 @@ public class NSUpload {
|
|||
try {
|
||||
JSONObject data = getJson(profileSwitch);
|
||||
if (profileSwitch._id != null) {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbUpdate");
|
||||
bundle.putString("collection", "treatments");
|
||||
bundle.putString("data", data.toString());
|
||||
bundle.putString("_id", profileSwitch._id);
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbUpdate", "treatments", profileSwitch._id, data));
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
|
@ -382,16 +322,7 @@ public class NSUpload {
|
|||
prebolus.put("created_at", DateUtil.toISOString(preBolusDate));
|
||||
uploadCareportalEntryToNS(prebolus);
|
||||
}
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "treatments");
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
|
||||
} catch (Exception e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
|
@ -399,21 +330,7 @@ public class NSUpload {
|
|||
}
|
||||
|
||||
public static void removeCareportalEntryFromNS(String _id) {
|
||||
try {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbRemove");
|
||||
bundle.putString("collection", "treatments");
|
||||
bundle.putString("_id", _id);
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbRemove(intent, _id);
|
||||
} catch (Exception e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
|
||||
UploadQueue.add(new DbRequest("dbRemove", "treatments", _id));
|
||||
}
|
||||
|
||||
public static void uploadOpenAPSOffline(double durationInMinutes) {
|
||||
|
@ -424,15 +341,7 @@ public class NSUpload {
|
|||
data.put("duration", durationInMinutes);
|
||||
data.put("created_at", DateUtil.toISOString(new Date()));
|
||||
data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name));
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "treatments");
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
|
@ -443,10 +352,6 @@ public class NSUpload {
|
|||
}
|
||||
|
||||
public static void uploadError(String error, Date date) {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "treatments");
|
||||
JSONObject data = new JSONObject();
|
||||
try {
|
||||
data.put("eventType", "Announcement");
|
||||
|
@ -457,19 +362,10 @@ public class NSUpload {
|
|||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
|
||||
}
|
||||
|
||||
public static void uploadBg(BgReading reading, String source) {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "entries");
|
||||
JSONObject data = new JSONObject();
|
||||
try {
|
||||
data.put("device", source);
|
||||
|
@ -481,20 +377,11 @@ public class NSUpload {
|
|||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "entries", data));
|
||||
}
|
||||
|
||||
public static void uploadAppStart() {
|
||||
if (SP.getBoolean(R.string.key_ns_logappstartedevent, true)) {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "treatments");
|
||||
JSONObject data = new JSONObject();
|
||||
try {
|
||||
data.put("eventType", "Note");
|
||||
|
@ -503,35 +390,17 @@ public class NSUpload {
|
|||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
|
||||
}
|
||||
}
|
||||
|
||||
public static void uploadProfileStore(JSONObject profileStore) {
|
||||
if (SP.getBoolean(R.string.key_ns_uploadlocalprofile, false)) {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "profile");
|
||||
bundle.putString("data", String.valueOf(profileStore));
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, String.valueOf(profileStore));
|
||||
UploadQueue.add(new DbRequest("dbAdd", "profile", String.valueOf(profileStore)));
|
||||
}
|
||||
}
|
||||
|
||||
public static void uploadEvent(String careportalEvent, long time, @Nullable String notes) {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbAdd");
|
||||
bundle.putString("collection", "treatments");
|
||||
JSONObject data = new JSONObject();
|
||||
try {
|
||||
data.put("eventType", careportalEvent);
|
||||
|
@ -543,26 +412,12 @@ public class NSUpload {
|
|||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
bundle.putString("data", data.toString());
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbAdd(intent, data.toString());
|
||||
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
|
||||
}
|
||||
|
||||
public static void removeFoodFromNS(String _id) {
|
||||
try {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("action", "dbRemove");
|
||||
bundle.putString("collection", "food");
|
||||
bundle.putString("_id", _id);
|
||||
Intent intent = new Intent(Intents.ACTION_DATABASE);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
DbLogger.dbRemove(intent, _id);
|
||||
UploadQueue.add(new DbRequest("dbRemove", "food", _id));
|
||||
} catch (Exception e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
package info.nightscout.androidaps.plugins.general.nsclient.broadcasts;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.R;
|
||||
import info.nightscout.androidaps.services.Intents;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.data.NSAlarm;
|
||||
import info.nightscout.androidaps.utils.SP;
|
||||
|
||||
/**
|
||||
* Created by mike on 11.06.2017.
|
||||
*/
|
||||
|
||||
public class BroadcastAckAlarm {
|
||||
|
||||
public static void handleClearAlarm(NSAlarm originalAlarm, Context context, long silenceTimeInMsec) {
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt("level", originalAlarm.getLevel());
|
||||
bundle.putString("group", originalAlarm.getGroup());
|
||||
bundle.putLong("silenceTime", silenceTimeInMsec);
|
||||
Intent intent = new Intent(Intents.ACTION_ACK_ALARM);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
|
||||
|
||||
if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, false)) {
|
||||
bundle = new Bundle();
|
||||
bundle.putInt("level", originalAlarm.getLevel());
|
||||
bundle.putString("group", originalAlarm.getGroup());
|
||||
bundle.putLong("silenceTime", silenceTimeInMsec);
|
||||
intent = new Intent(Intents.ACTION_ACK_ALARM);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -25,12 +25,11 @@ import info.nightscout.androidaps.utils.SP;
|
|||
public class BroadcastTreatment {
|
||||
private static Logger log = LoggerFactory.getLogger(L.NSCLIENT);
|
||||
|
||||
public static void handleNewTreatment(JSONObject treatment, boolean isDelta, boolean isLocalBypass) {
|
||||
public static void handleNewTreatment(JSONObject treatment, boolean isDelta) {
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("treatment", treatment.toString());
|
||||
bundle.putBoolean("delta", isDelta);
|
||||
bundle.putBoolean("islocal", isLocalBypass);
|
||||
Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT);
|
||||
intent.putExtras(bundle);
|
||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
package info.nightscout.androidaps.plugins.general.nsclient.data;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ResolveInfo;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.R;
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.utils.ToastUtils;
|
||||
|
||||
/**
|
||||
* Created by mike on 02.07.2016.
|
||||
*/
|
||||
public class DbLogger {
|
||||
private static Logger log = LoggerFactory.getLogger(L.NSCLIENT);
|
||||
|
||||
public static void dbAdd(Intent intent, String data) {
|
||||
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(intent, 0);
|
||||
if (q.size() < 1) {
|
||||
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.nsclientnotinstalled));
|
||||
log.error("DBADD No receivers");
|
||||
} else if (L.isEnabled(L.NSCLIENT)) {
|
||||
if (L.isEnabled(L.NSCLIENT))
|
||||
log.debug("DBADD dbAdd " + q.size() + " receivers " + data);
|
||||
}
|
||||
}
|
||||
|
||||
public static void dbRemove(Intent intent, String data) {
|
||||
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(intent, 0);
|
||||
if (q.size() < 1) {
|
||||
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.nsclientnotinstalled));
|
||||
log.error("DBREMOVE No receivers");
|
||||
} else if (L.isEnabled(L.NSCLIENT)) {
|
||||
if (L.isEnabled(L.NSCLIENT))
|
||||
log.debug("DBREMOVE dbRemove " + q.size() + " receivers " + data);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
package info.nightscout.androidaps.plugins.general.nsclient.receivers;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.PowerManager;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.R;
|
||||
import info.nightscout.androidaps.interfaces.PluginType;
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.data.AlarmAck;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.services.NSClientService;
|
||||
import info.nightscout.androidaps.utils.SP;
|
||||
|
||||
public class AckAlarmReceiver extends BroadcastReceiver {
|
||||
private static Logger log = LoggerFactory.getLogger(L.NSCLIENT);
|
||||
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
|
||||
AckAlarmReceiver.class.getSimpleName());
|
||||
NSClientPlugin nsClientPlugin = NSClientPlugin.getPlugin();
|
||||
if (!nsClientPlugin.isEnabled(PluginType.GENERAL)) {
|
||||
return;
|
||||
}
|
||||
if (SP.getBoolean(R.string.key_ns_noupload, false)) {
|
||||
if (L.isEnabled(L.NSCLIENT))
|
||||
log.debug("Upload disabled. Message dropped");
|
||||
return;
|
||||
}
|
||||
wakeLock.acquire();
|
||||
try {
|
||||
Bundle bundles = intent.getExtras();
|
||||
if (bundles == null) return;
|
||||
if (!bundles.containsKey("level")) return;
|
||||
if (!bundles.containsKey("group")) return;
|
||||
if (!bundles.containsKey("silenceTime")) return;
|
||||
|
||||
AlarmAck ack = new AlarmAck();
|
||||
ack.level = bundles.getInt("level");
|
||||
ack.group = bundles.getString("group");
|
||||
ack.silenceTime = bundles.getLong("silenceTime");
|
||||
|
||||
NSClientService nsClientService = nsClientPlugin.nsClientService;
|
||||
if (nsClientService != null)
|
||||
nsClientService.sendAlarmAck(ack);
|
||||
|
||||
} finally {
|
||||
wakeLock.release();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,147 +0,0 @@
|
|||
package info.nightscout.androidaps.plugins.general.nsclient.receivers;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.PowerManager;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.R;
|
||||
import info.nightscout.androidaps.db.DbRequest;
|
||||
import info.nightscout.androidaps.interfaces.PluginType;
|
||||
import info.nightscout.androidaps.logging.BundleLogger;
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.broadcasts.BroadcastTreatment;
|
||||
import info.nightscout.androidaps.utils.DateUtil;
|
||||
import info.nightscout.androidaps.utils.SP;
|
||||
|
||||
public class DBAccessReceiver extends BroadcastReceiver {
|
||||
private static Logger log = LoggerFactory.getLogger(L.NSCLIENT);
|
||||
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
|
||||
DBAccessReceiver.class.getSimpleName());
|
||||
wakeLock.acquire();
|
||||
try {
|
||||
Bundle bundles = intent.getExtras();
|
||||
if (bundles == null) return;
|
||||
if (!bundles.containsKey("action")) return;
|
||||
|
||||
if (L.isEnabled(L.NSCLIENT))
|
||||
log.debug(BundleLogger.log(bundles));
|
||||
|
||||
String collection = null;
|
||||
String _id = null;
|
||||
JSONObject data = null;
|
||||
String action = bundles.getString("action");
|
||||
try {
|
||||
collection = bundles.getString("collection");
|
||||
} catch (Exception e) {
|
||||
log.error("Unhandled exception", e);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (!action.equals("dbAdd"))
|
||||
_id = bundles.getString("_id");
|
||||
} catch (Exception e) {
|
||||
log.error("Unhandled exception", e);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (!action.equals("dbRemove"))
|
||||
data = new JSONObject(bundles.getString("data"));
|
||||
} catch (Exception e) {
|
||||
log.error("Unhandled exception", e);
|
||||
return;
|
||||
}
|
||||
|
||||
if (data == null && !action.equals("dbRemove") || _id == null && action.equals("dbRemove")) {
|
||||
log.error("DBACCESS no data inside record");
|
||||
return;
|
||||
}
|
||||
|
||||
if (action.equals("dbRemove")) {
|
||||
data = new JSONObject();
|
||||
}
|
||||
// mark by id
|
||||
Long nsclientid = System.currentTimeMillis();
|
||||
try {
|
||||
data.put("NSCLIENT_ID", nsclientid);
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
|
||||
if (!isAllowedCollection(collection)) {
|
||||
log.error("DBACCESS wrong collection specified");
|
||||
return;
|
||||
}
|
||||
|
||||
if (action.equals("dbRemove")) {
|
||||
if (shouldUpload()) {
|
||||
DbRequest dbr = new DbRequest(action, collection, nsclientid.toString(), _id);
|
||||
UploadQueue.add(dbr);
|
||||
}
|
||||
} else if (action.equals("dbUpdate")) {
|
||||
if (shouldUpload()) {
|
||||
DbRequest dbr = new DbRequest(action, collection, nsclientid.toString(), _id, data);
|
||||
UploadQueue.add(dbr);
|
||||
}
|
||||
} else {
|
||||
DbRequest dbr = new DbRequest(action, collection, nsclientid.toString(), data);
|
||||
// this is not used as mongo _id but only for searching in UploadQueue database
|
||||
// if record has to be removed from queue before upload
|
||||
dbr._id = nsclientid.toString();
|
||||
|
||||
if (shouldUpload()) {
|
||||
UploadQueue.add(dbr);
|
||||
}
|
||||
if (collection.equals("treatments")) {
|
||||
generateTreatmentOfflineBroadcast(dbr);
|
||||
}
|
||||
}
|
||||
|
||||
} finally {
|
||||
wakeLock.release();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean shouldUpload() {
|
||||
NSClientPlugin nsClientPlugin = NSClientPlugin.getPlugin();
|
||||
return nsClientPlugin.isEnabled(PluginType.GENERAL) && !SP.getBoolean(R.string.key_ns_noupload, false);
|
||||
}
|
||||
|
||||
public void generateTreatmentOfflineBroadcast(DbRequest request) {
|
||||
if (request.action.equals("dbAdd")) {
|
||||
try {
|
||||
JSONObject data = new JSONObject(request.data);
|
||||
data.put("mills", DateUtil.fromISODateString(data.getString("created_at")).getTime());
|
||||
data.put("_id", data.get("NSCLIENT_ID")); // this is only fake id
|
||||
BroadcastTreatment.handleNewTreatment(data, false, true);
|
||||
} catch (Exception e) {
|
||||
log.error("Unhadled exception", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAllowedCollection(String collection) {
|
||||
// "treatments" || "entries" || "devicestatus" || "profile" || "food"
|
||||
if (collection.equals("treatments")) return true;
|
||||
if (collection.equals("entries")) return true;
|
||||
if (collection.equals("devicestatus")) return true;
|
||||
if (collection.equals("profile")) return true;
|
||||
if (collection.equals("food")) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@ import info.nightscout.androidaps.MainApp;
|
|||
import info.nightscout.androidaps.R;
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.plugins.bus.RxBus;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.broadcasts.BroadcastAckAlarm;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin;
|
||||
import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin;
|
||||
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification;
|
||||
import info.nightscout.androidaps.utils.DateUtil;
|
||||
|
@ -93,7 +93,7 @@ public class NotificationRecyclerViewAdapter extends RecyclerView.Adapter<Notifi
|
|||
Notification notification = (Notification) v.getTag();
|
||||
RxBus.INSTANCE.send(new EventDismissNotification(notification.id));
|
||||
if (notification.nsAlarm != null) {
|
||||
BroadcastAckAlarm.handleClearAlarm(notification.nsAlarm, MainApp.instance().getApplicationContext(), 60 * 60 * 1000L);
|
||||
NSClientPlugin.getPlugin().handleClearAlarm(notification.nsAlarm, 60 * 60 * 1000L);
|
||||
}
|
||||
// Adding current time to snooze if we got staleData
|
||||
if (L.isEnabled(L.NOTIFICATION))
|
||||
|
|
|
@ -56,10 +56,6 @@ public class DataService extends IntentService {
|
|||
|
||||
boolean acceptNSData = !SP.getBoolean(R.string.key_ns_upload_only, false);
|
||||
Bundle bundles = intent.getExtras();
|
||||
if (bundles != null && bundles.containsKey("islocal")) {
|
||||
acceptNSData = acceptNSData || bundles.getBoolean("islocal");
|
||||
}
|
||||
|
||||
|
||||
final String action = intent.getAction();
|
||||
if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) {
|
||||
|
|
|
@ -20,11 +20,6 @@ public interface Intents {
|
|||
String ACTION_URGENT_ALARM = "info.nightscout.client.URGENT_ALARM";
|
||||
String ACTION_CLEAR_ALARM = "info.nightscout.client.CLEAR_ALARM";
|
||||
|
||||
|
||||
// App -> NSClient
|
||||
String ACTION_DATABASE = "info.nightscout.client.DBACCESS";
|
||||
String ACTION_ACK_ALARM = "info.nightscout.client.ACK_ALARM";
|
||||
|
||||
// xDrip -> App
|
||||
String RECEIVER_PERMISSION = "com.eveningoutpost.dexdrip.permissions.RECEIVE_BG_ESTIMATE";
|
||||
|
||||
|
|
Loading…
Reference in a new issue