AndroidAPS/app/src/main/java/info/nightscout/androidaps/services/DataService.java

261 lines
12 KiB
Java
Raw Normal View History

2018-07-29 00:00:48 +02:00
package info.nightscout.androidaps.services;
2016-06-07 21:48:17 +02:00
import android.app.IntentService;
2016-06-07 21:48:17 +02:00
import android.content.Intent;
import android.os.Bundle;
2016-07-14 23:29:21 +02:00
import android.provider.Telephony;
2016-06-07 21:48:17 +02:00
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
2016-06-26 14:56:43 +02:00
import info.nightscout.androidaps.R;
2017-05-25 20:18:29 +02:00
import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.events.EventNsFood;
2018-04-05 09:39:18 +02:00
import info.nightscout.androidaps.events.EventNsTreatment;
import info.nightscout.androidaps.logging.BundleLogger;
2018-07-29 14:39:56 +02:00
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus;
2019-02-28 23:16:50 +01:00
import info.nightscout.androidaps.plugins.general.nsclient.data.NSDeviceStatus;
import info.nightscout.androidaps.plugins.general.nsclient.data.NSMbg;
import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin;
2019-02-28 23:16:50 +01:00
import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin;
2019-02-26 20:38:27 +01:00
import info.nightscout.androidaps.plugins.pump.danaR.activities.DanaRNSHistorySync;
2019-06-19 01:38:55 +02:00
import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin;
2019-02-28 23:16:50 +01:00
import info.nightscout.androidaps.plugins.source.SourceEversensePlugin;
import info.nightscout.androidaps.plugins.source.SourceGlimpPlugin;
import info.nightscout.androidaps.plugins.source.SourceMM640gPlugin;
import info.nightscout.androidaps.plugins.source.SourceNSClientPlugin;
import info.nightscout.androidaps.plugins.source.SourcePoctechPlugin;
import info.nightscout.androidaps.plugins.source.SourceTomatoPlugin;
import info.nightscout.androidaps.plugins.source.SourceXdripPlugin;
import info.nightscout.androidaps.receivers.DataReceiver;
2019-02-26 20:38:27 +01:00
import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.SP;
2016-06-07 21:48:17 +02:00
public class DataService extends IntentService {
2018-07-29 14:39:56 +02:00
private Logger log = LoggerFactory.getLogger(L.DATASERVICE);
2016-06-07 21:48:17 +02:00
public DataService() {
super("DataService");
2016-06-07 21:48:17 +02:00
}
@Override
protected void onHandleIntent(final Intent intent) {
2018-07-29 14:39:56 +02:00
if (L.isEnabled(L.DATASERVICE)) {
2018-07-28 16:26:36 +02:00
log.debug("onHandleIntent " + intent);
2017-08-14 12:11:53 +02:00
log.debug("onHandleIntent " + BundleLogger.log(intent.getExtras()));
2018-07-28 16:26:36 +02:00
}
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");
}
2018-07-27 13:41:25 +02:00
final String action = intent.getAction();
if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) {
SourceXdripPlugin.getPlugin().handleNewData(intent);
} else if (Intents.NS_EMULATOR.equals(action)) {
SourceMM640gPlugin.getPlugin().handleNewData(intent);
} else if (Intents.GLIMP_BG.equals(action)) {
SourceGlimpPlugin.getPlugin().handleNewData(intent);
2019-06-19 01:38:55 +02:00
} else if (Intents.DEXCOM_BG.equals(action)) {
SourceDexcomPlugin.INSTANCE.handleNewData(intent);
2018-07-27 13:41:25 +02:00
} else if (Intents.POCTECH_BG.equals(action)) {
SourcePoctechPlugin.getPlugin().handleNewData(intent);
2019-02-19 16:26:16 +01:00
} else if (Intents.TOMATO_BG.equals(action)) {
SourceTomatoPlugin.getPlugin().handleNewData(intent);
2018-12-11 21:56:57 +01:00
} else if (Intents.EVERSENSE_BG.equals(action)) {
SourceEversensePlugin.getPlugin().handleNewData(intent);
2018-07-27 13:41:25 +02:00
} else if (Intents.ACTION_NEW_SGV.equals(action)) {
SourceNSClientPlugin.getPlugin().handleNewData(intent);
} else if (Intents.ACTION_NEW_PROFILE.equals(action)) {
// always handle Profile if NSProfile is enabled without looking at nsUploadOnly
NSProfilePlugin.getPlugin().handleNewData(intent);
2018-07-28 16:26:36 +02:00
} else if (Intents.ACTION_NEW_DEVICESTATUS.equals(action)) {
NSDeviceStatus.getInstance().handleNewData(intent);
} else if (Intents.ACTION_NEW_STATUS.equals(action)) {
NSSettingsStatus.getInstance().handleNewData(intent);
} else if (Intents.ACTION_NEW_FOOD.equals(action)) {
2019-10-14 23:35:24 +02:00
EventNsFood evt = new EventNsFood(EventNsFood.Companion.getADD(), bundles);
RxBus.INSTANCE.send(evt);
2018-07-28 16:26:36 +02:00
} else if (Intents.ACTION_CHANGED_FOOD.equals(action)) {
2019-10-14 23:35:24 +02:00
EventNsFood evt = new EventNsFood(EventNsFood.Companion.getUPDATE(), bundles);
RxBus.INSTANCE.send(evt);
2018-07-28 16:26:36 +02:00
} else if (Intents.ACTION_REMOVED_FOOD.equals(action)) {
2019-10-14 23:35:24 +02:00
EventNsFood evt = new EventNsFood(EventNsFood.Companion.getREMOVE(), bundles);
RxBus.INSTANCE.send(evt);
2018-07-27 13:41:25 +02:00
} else if (acceptNSData &&
(Intents.ACTION_NEW_TREATMENT.equals(action) ||
Intents.ACTION_CHANGED_TREATMENT.equals(action) ||
Intents.ACTION_REMOVED_TREATMENT.equals(action) ||
Intents.ACTION_NEW_CAL.equals(action) ||
Intents.ACTION_NEW_MBG.equals(action))
) {
2018-07-27 13:41:25 +02:00
handleNewDataFromNSClient(intent);
} else if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(action)) {
2019-11-22 00:33:13 +01:00
SmsCommunicatorPlugin.INSTANCE.handleNewData(intent);
2016-06-07 21:48:17 +02:00
}
2018-07-27 13:41:25 +02:00
2018-07-29 14:39:56 +02:00
if (L.isEnabled(L.DATASERVICE))
2016-08-07 23:21:08 +02:00
log.debug("onHandleIntent exit " + intent);
DataReceiver.completeWakefulIntent(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void handleNewDataFromNSClient(Intent intent) {
Bundle bundles = intent.getExtras();
if (bundles == null) return;
2018-07-29 14:39:56 +02:00
if (L.isEnabled(L.DATASERVICE))
log.debug("Got intent: " + intent.getAction());
2017-09-24 22:47:18 +02:00
if (intent.getAction().equals(Intents.ACTION_NEW_TREATMENT) || intent.getAction().equals(Intents.ACTION_CHANGED_TREATMENT)) {
2016-06-07 21:48:17 +02:00
try {
if (bundles.containsKey("treatment")) {
JSONObject json = new JSONObject(bundles.getString("treatment"));
2018-04-05 09:39:18 +02:00
handleTreatmentFromNS(json, intent);
2016-06-07 21:48:17 +02:00
}
if (bundles.containsKey("treatments")) {
String trstring = bundles.getString("treatments");
JSONArray jsonArray = new JSONArray(trstring);
for (int i = 0; i < jsonArray.length(); i++) {
2018-04-05 09:39:18 +02:00
JSONObject json = jsonArray.getJSONObject(i);
handleTreatmentFromNS(json, intent);
2016-06-07 21:48:17 +02:00
}
}
2018-04-05 09:39:18 +02:00
} catch (JSONException e) {
log.error("Unhandled exception", e);
2016-06-07 21:48:17 +02:00
}
}
if (intent.getAction().equals(Intents.ACTION_REMOVED_TREATMENT)) {
try {
if (bundles.containsKey("treatment")) {
String trstring = bundles.getString("treatment");
2018-04-05 09:39:18 +02:00
JSONObject json = new JSONObject(trstring);
2018-07-28 16:26:36 +02:00
handleRemovedTreatmentFromNS(json);
2016-06-07 21:48:17 +02:00
}
if (bundles.containsKey("treatments")) {
String trstring = bundles.getString("treatments");
2016-06-07 21:48:17 +02:00
JSONArray jsonArray = new JSONArray(trstring);
for (int i = 0; i < jsonArray.length(); i++) {
2018-04-05 09:39:18 +02:00
JSONObject json = jsonArray.getJSONObject(i);
2018-07-28 16:26:36 +02:00
handleRemovedTreatmentFromNS(json);
2016-06-07 21:48:17 +02:00
}
}
2018-04-05 09:39:18 +02:00
} catch (JSONException e) {
log.error("Unhandled exception", e);
2016-06-07 21:48:17 +02:00
}
}
if (intent.getAction().equals(Intents.ACTION_NEW_MBG)) {
2017-06-06 08:21:11 +02:00
try {
if (bundles.containsKey("mbg")) {
String mbgstring = bundles.getString("mbg");
2017-06-06 08:21:11 +02:00
JSONObject mbgJson = new JSONObject(mbgstring);
2018-05-29 20:57:55 +02:00
storeMbg(mbgJson);
2017-06-06 08:21:11 +02:00
}
if (bundles.containsKey("mbgs")) {
String sgvstring = bundles.getString("mbgs");
2017-06-06 08:21:11 +02:00
JSONArray jsonArray = new JSONArray(sgvstring);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject mbgJson = jsonArray.getJSONObject(i);
2018-05-29 20:57:55 +02:00
storeMbg(mbgJson);
2017-06-06 08:21:11 +02:00
}
}
} catch (Exception e) {
log.error("Unhandled exception", e);
2017-06-06 08:21:11 +02:00
}
}
2017-10-17 20:55:23 +02:00
}
2018-07-28 16:26:36 +02:00
private void handleRemovedTreatmentFromNS(JSONObject json) {
2018-04-05 09:39:18 +02:00
// new DB model
2019-10-14 23:35:24 +02:00
EventNsTreatment evtTreatment = new EventNsTreatment(EventNsTreatment.Companion.getREMOVE(), json);
RxBus.INSTANCE.send(evtTreatment);
2018-04-05 09:39:18 +02:00
// old DB model
String _id = JsonHelper.safeGetString(json, "_id");
MainApp.getDbHelper().deleteTempTargetById(_id);
MainApp.getDbHelper().deleteTempBasalById(_id);
MainApp.getDbHelper().deleteExtendedBolusById(_id);
MainApp.getDbHelper().deleteCareportalEventById(_id);
2017-06-02 10:25:49 +02:00
MainApp.getDbHelper().deleteProfileSwitchById(_id);
}
2018-08-22 22:04:47 +02:00
private void handleTreatmentFromNS(JSONObject json, Intent intent) {
2018-04-05 09:39:18 +02:00
// new DB model
2019-10-14 23:35:24 +02:00
int mode = Intents.ACTION_NEW_TREATMENT.equals(intent.getAction()) ? EventNsTreatment.Companion.getADD() : EventNsTreatment.Companion.getUPDATE();
2018-04-05 09:39:18 +02:00
double insulin = JsonHelper.safeGetDouble(json, "insulin");
double carbs = JsonHelper.safeGetDouble(json, "carbs");
String eventType = JsonHelper.safeGetString(json, "eventType");
2018-09-04 22:26:59 +02:00
if (eventType == null) {
log.debug("Wrong treatment. Ignoring : " + json.toString());
return;
}
2018-04-05 09:39:18 +02:00
if (insulin > 0 || carbs > 0) {
EventNsTreatment evtTreatment = new EventNsTreatment(mode, json);
2019-10-14 23:35:24 +02:00
RxBus.INSTANCE.send(evtTreatment);
2018-04-05 09:39:18 +02:00
} else if (json.has(DanaRNSHistorySync.DANARSIGNATURE)) {
// old DB model
MainApp.getDbHelper().updateDanaRHistoryRecordId(json);
} else if (eventType.equals(CareportalEvent.TEMPORARYTARGET)) {
MainApp.getDbHelper().createTemptargetFromJsonIfNotExists(json);
} else if (eventType.equals(CareportalEvent.TEMPBASAL)) {
MainApp.getDbHelper().createTempBasalFromJsonIfNotExists(json);
} else if (eventType.equals(CareportalEvent.COMBOBOLUS)) {
MainApp.getDbHelper().createExtendedBolusFromJsonIfNotExists(json);
} else if (eventType.equals(CareportalEvent.PROFILESWITCH)) {
MainApp.getDbHelper().createProfileSwitchFromJsonIfNotExists(json);
} else if (eventType.equals(CareportalEvent.SITECHANGE) ||
eventType.equals(CareportalEvent.INSULINCHANGE) ||
eventType.equals(CareportalEvent.SENSORCHANGE) ||
eventType.equals(CareportalEvent.BGCHECK) ||
eventType.equals(CareportalEvent.NOTE) ||
eventType.equals(CareportalEvent.NONE) ||
eventType.equals(CareportalEvent.ANNOUNCEMENT) ||
eventType.equals(CareportalEvent.QUESTION) ||
eventType.equals(CareportalEvent.EXERCISE) ||
eventType.equals(CareportalEvent.OPENAPSOFFLINE) ||
eventType.equals(CareportalEvent.PUMPBATTERYCHANGE)) {
MainApp.getDbHelper().createCareportalEventFromJsonIfNotExists(json);
2016-07-27 17:49:56 +02:00
}
2018-04-05 09:39:18 +02:00
if (eventType.equals(CareportalEvent.ANNOUNCEMENT)) {
2018-07-27 13:41:25 +02:00
long date = JsonHelper.safeGetLong(json, "mills");
2017-06-15 23:12:12 +02:00
long now = System.currentTimeMillis();
2018-04-05 09:39:18 +02:00
String enteredBy = JsonHelper.safeGetString(json, "enteredBy", "");
String notes = JsonHelper.safeGetString(json, "notes", "");
if (date > now - 15 * 60 * 1000L && !notes.isEmpty()
&& !enteredBy.equals(SP.getString("careportal_enteredby", "AndroidAPS"))) {
Notification announcement = new Notification(Notification.NSANNOUNCEMENT, notes, Notification.ANNOUNCEMENT, 60);
RxBus.INSTANCE.send(new EventNewNotification(announcement));
2017-06-06 08:21:11 +02:00
}
}
2017-05-25 20:18:29 +02:00
}
2018-05-29 20:57:55 +02:00
private void storeMbg(JSONObject mbgJson) {
NSMbg nsMbg = new NSMbg(mbgJson);
CareportalEvent careportalEvent = new CareportalEvent(nsMbg);
MainApp.getDbHelper().createOrUpdate(careportalEvent);
2018-07-29 14:39:56 +02:00
if (L.isEnabled(L.DATASERVICE))
2018-08-24 10:36:01 +02:00
log.debug("Adding/Updating new MBG: " + careportalEvent.toString());
2018-05-29 20:57:55 +02:00
}
2016-06-07 21:48:17 +02:00
}