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

587 lines
27 KiB
Java
Raw Normal View History

2016-06-07 21:48:17 +02:00
package info.nightscout.androidaps.Services;
import android.app.IntentService;
import android.content.Intent;
import android.content.pm.PackageManager;
2016-06-07 21:48:17 +02:00
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;
2016-07-19 11:31:58 +02:00
import info.nightscout.androidaps.Config;
2016-06-07 21:48:17 +02:00
import info.nightscout.androidaps.MainApp;
2016-06-26 14:56:43 +02:00
import info.nightscout.androidaps.R;
2018-01-23 20:58:48 +01:00
import info.nightscout.androidaps.data.ProfileStore;
2016-06-07 21:48:17 +02:00
import info.nightscout.androidaps.db.BgReading;
2017-05-25 20:18:29 +02:00
import info.nightscout.androidaps.db.CareportalEvent;
2016-06-07 21:48:17 +02:00
import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.events.EventNsFood;
2016-08-05 23:54:03 +02:00
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
2017-04-21 12:15:08 +02:00
import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin;
2018-01-23 20:58:48 +01:00
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus;
2017-06-06 08:21:11 +02:00
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSMbg;
2017-06-26 12:44:03 +02:00
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus;
2018-01-23 20:58:48 +01:00
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
2016-08-10 23:27:26 +02:00
import info.nightscout.androidaps.plugins.Overview.OverviewPlugin;
2017-01-16 23:37:58 +01:00
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
2018-01-23 20:58:48 +01:00
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin;
2018-01-23 20:58:48 +01:00
import info.nightscout.androidaps.plugins.ProfileNS.events.EventNSProfileUpdateGUI;
import info.nightscout.androidaps.plugins.PumpDanaR.activities.DanaRNSHistorySync;
2016-08-10 23:27:26 +02:00
import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS;
2017-11-29 16:07:26 +01:00
import info.nightscout.androidaps.plugins.SourceDexcomG5.SourceDexcomG5Plugin;
2017-03-21 19:11:39 +01:00
import info.nightscout.androidaps.plugins.SourceGlimp.SourceGlimpPlugin;
2016-12-28 14:57:11 +01:00
import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gPlugin;
2016-08-10 23:43:08 +02:00
import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin;
import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin;
import info.nightscout.androidaps.receivers.DataReceiver;
2017-08-14 12:11:53 +02:00
import info.nightscout.utils.BundleLogger;
2017-11-29 16:07:26 +01:00
import info.nightscout.utils.NSUpload;
import info.nightscout.utils.SP;
2016-06-07 21:48:17 +02:00
public class DataService extends IntentService {
private static Logger log = LoggerFactory.getLogger(DataService.class);
2016-07-02 23:58:57 +02:00
boolean xDripEnabled = false;
boolean nsClientEnabled = true;
2016-12-28 14:57:11 +01:00
boolean mm640gEnabled = false;
2017-03-21 19:11:39 +01:00
boolean glimpEnabled = false;
2017-11-29 16:07:26 +01:00
boolean dexcomG5Enabled = false;
2016-06-07 21:48:17 +02:00
public DataService() {
super("DataService");
registerBus();
2016-06-07 21:48:17 +02:00
}
@Override
protected void onHandleIntent(final Intent intent) {
2016-06-07 21:48:17 +02:00
if (Config.logFunctionCalls)
2017-08-14 12:11:53 +02:00
log.debug("onHandleIntent " + BundleLogger.log(intent.getExtras()));
2016-06-07 21:48:17 +02:00
2016-08-11 00:15:50 +02:00
if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceXdripPlugin.class)) {
xDripEnabled = true;
nsClientEnabled = false;
2016-12-28 14:57:11 +01:00
mm640gEnabled = false;
2017-03-21 19:11:39 +01:00
glimpEnabled = false;
2017-11-29 16:07:26 +01:00
dexcomG5Enabled = false;
2016-11-09 17:48:44 +01:00
} else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceNSClientPlugin.class)) {
2016-08-11 00:15:50 +02:00
xDripEnabled = false;
nsClientEnabled = true;
2016-12-28 14:57:11 +01:00
mm640gEnabled = false;
2017-03-21 19:11:39 +01:00
glimpEnabled = false;
2017-11-29 16:07:26 +01:00
dexcomG5Enabled = false;
2016-12-28 14:57:11 +01:00
} else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceMM640gPlugin.class)) {
xDripEnabled = false;
nsClientEnabled = false;
mm640gEnabled = true;
2017-03-21 19:11:39 +01:00
glimpEnabled = false;
2017-11-29 16:07:26 +01:00
dexcomG5Enabled = false;
2017-03-21 19:11:39 +01:00
} else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceGlimpPlugin.class)) {
xDripEnabled = false;
nsClientEnabled = false;
mm640gEnabled = false;
glimpEnabled = true;
2017-11-29 16:07:26 +01:00
dexcomG5Enabled = false;
} else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceDexcomG5Plugin.class)) {
xDripEnabled = false;
nsClientEnabled = false;
mm640gEnabled = false;
glimpEnabled = false;
dexcomG5Enabled = true;
}
2017-06-02 10:25:49 +02:00
boolean isNSProfile = ConfigBuilderPlugin.getActiveProfileInterface().getClass().equals(NSProfilePlugin.class);
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");
}
2016-11-09 17:48:44 +01:00
2016-06-07 21:48:17 +02:00
if (intent != null) {
final String action = intent.getAction();
if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) {
2016-11-09 17:48:44 +01:00
if (xDripEnabled) {
handleNewDataFromXDrip(intent);
2016-11-09 17:48:44 +01:00
}
2016-12-28 14:57:11 +01:00
} else if (Intents.NS_EMULATOR.equals(action)) {
if (mm640gEnabled) {
handleNewDataFromMM640g(intent);
}
2017-03-21 19:11:39 +01:00
} else if (Intents.GLIMP_BG.equals(action)) {
if (glimpEnabled) {
handleNewDataFromGlimp(intent);
}
2017-11-29 16:07:26 +01:00
} else if (Intents.DEXCOMG5_BG.equals(action)) {
if (dexcomG5Enabled) {
handleNewDataFromDexcomG5(intent);
}
2016-11-09 17:48:44 +01:00
} else if (Intents.ACTION_NEW_SGV.equals(action)) {
2018-03-14 20:21:48 +01:00
if (nsClientEnabled || SP.getBoolean(R.string.ns_autobackfill, true))
handleNewDataFromNSClient(intent);
2016-11-09 17:48:44 +01:00
// Objectives 0
ObjectivesPlugin.bgIsAvailableInNS = true;
ObjectivesPlugin.saveProgress();
2017-05-19 14:15:21 +02:00
} else if (isNSProfile && Intents.ACTION_NEW_PROFILE.equals(action) || Intents.ACTION_NEW_DEVICESTATUS.equals(action)) {
// always handle Profile if NSProfile is enabled without looking at nsUploadOnly
handleNewDataFromNSClient(intent);
} else if (acceptNSData &&
(Intents.ACTION_NEW_TREATMENT.equals(action) ||
2016-11-09 17:48:44 +01:00
Intents.ACTION_CHANGED_TREATMENT.equals(action) ||
Intents.ACTION_REMOVED_TREATMENT.equals(action) ||
Intents.ACTION_NEW_STATUS.equals(action) ||
Intents.ACTION_NEW_DEVICESTATUS.equals(action) ||
2017-10-17 20:55:23 +02:00
Intents.ACTION_NEW_FOOD.equals(action) ||
Intents.ACTION_CHANGED_FOOD.equals(action) ||
Intents.ACTION_REMOVED_FOOD.equals(action) ||
2016-11-09 17:48:44 +01:00
Intents.ACTION_NEW_CAL.equals(action) ||
Intents.ACTION_NEW_MBG.equals(action))
2016-06-07 21:48:17 +02:00
) {
handleNewDataFromNSClient(intent);
2016-07-14 23:29:21 +02:00
} else if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(action)) {
handleNewSMS(intent);
2016-06-07 21:48:17 +02:00
}
}
if (Config.logFunctionCalls)
2016-08-07 23:21:08 +02:00
log.debug("onHandleIntent exit " + intent);
DataReceiver.completeWakefulIntent(intent);
2016-06-07 21:48:17 +02:00
}
/*
2016-06-07 21:48:17 +02:00
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (Config.logFunctionCalls)
log.debug("onStartCommand");
return START_STICKY;
}
*/
2016-06-07 21:48:17 +02:00
@Override
public void onDestroy() {
super.onDestroy();
2016-06-07 21:48:17 +02:00
MainApp.bus().unregister(this);
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
}
private void handleNewDataFromXDrip(Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle == null) return;
BgReading bgReading = new BgReading();
bgReading.value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE);
2016-12-27 20:41:28 +01:00
bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME);
2017-05-21 22:05:03 +02:00
bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP);
2016-06-07 21:48:17 +02:00
bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW);
2017-06-06 17:14:17 +02:00
MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP");
2016-06-07 21:48:17 +02:00
}
2017-03-21 19:11:39 +01:00
private void handleNewDataFromGlimp(Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle == null) return;
BgReading bgReading = new BgReading();
bgReading.value = bundle.getDouble("mySGV");
bgReading.direction = bundle.getString("myTrend");
2017-05-21 22:05:03 +02:00
bgReading.date = bundle.getLong("myTimestamp");
2017-03-21 19:11:39 +01:00
bgReading.raw = 0;
2017-06-06 17:14:17 +02:00
MainApp.getDbHelper().createIfNotExists(bgReading, "GLIMP");
2017-03-21 19:11:39 +01:00
}
2017-11-29 16:07:26 +01:00
private void handleNewDataFromDexcomG5(Intent intent) {
// onHandleIntent Bundle{ data => [{"m_time":1511939180,"m_trend":"NotComputable","m_value":335}]; android.support.content.wakelockid => 95; }Bundle
Bundle bundle = intent.getExtras();
if (bundle == null) return;
BgReading bgReading = new BgReading();
String data = bundle.getString("data");
log.debug("Received Dexcom Data", data);
try {
2017-11-29 17:22:45 +01:00
JSONArray jsonArray = new JSONArray(data);
log.debug("Received Dexcom Data size:" + jsonArray.length());
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
bgReading.value = json.getInt("m_value");
bgReading.direction = json.getString("m_trend");
2017-11-29 23:00:41 +01:00
bgReading.date = json.getLong("m_time") * 1000L;
2017-11-29 17:22:45 +01:00
bgReading.raw = 0;
2017-11-29 23:00:41 +01:00
boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "DexcomG5");
if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) {
NSUpload.uploadBg(bgReading);
}
2017-12-14 21:49:11 +01:00
if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) {
NSUpload.sendToXdrip(bgReading);
}
2017-11-29 17:22:45 +01:00
}
2017-11-29 16:07:26 +01:00
} catch (JSONException e) {
e.printStackTrace();
}
}
2016-12-28 14:57:11 +01:00
private void handleNewDataFromMM640g(Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle == null) return;
final String collection = bundle.getString("collection");
if (collection == null) return;
if (collection.equals("entries")) {
final String data = bundle.getString("data");
if ((data != null) && (data.length() > 0)) {
try {
final JSONArray json_array = new JSONArray(data);
for (int i = 0; i < json_array.length(); i++) {
final JSONObject json_object = json_array.getJSONObject(i);
final String type = json_object.getString("type");
switch (type) {
case "sgv":
BgReading bgReading = new BgReading();
bgReading.value = json_object.getDouble("sgv");
bgReading.direction = json_object.getString("direction");
2017-05-21 22:05:03 +02:00
bgReading.date = json_object.getLong("date");
2016-12-28 14:57:11 +01:00
bgReading.raw = json_object.getDouble("sgv");
2017-06-06 17:14:17 +02:00
MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g");
2016-12-28 14:57:11 +01:00
break;
default:
log.debug("Unknown entries type: " + type);
}
}
} catch (JSONException e) {
log.error("Got JSON exception: " + e);
}
}
}
}
2016-06-07 21:48:17 +02:00
private void handleNewDataFromNSClient(Intent intent) {
Bundle bundles = intent.getExtras();
if (bundles == null) return;
if (Config.logIncommingData)
log.debug("Got intent: " + intent.getAction());
2016-06-07 21:48:17 +02:00
2016-06-26 14:56:43 +02:00
if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) {
if (bundles.containsKey("nsclientversioncode")) {
2016-08-11 00:15:50 +02:00
ConfigBuilderPlugin.nightscoutVersionCode = bundles.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203
ConfigBuilderPlugin.nightscoutVersionName = bundles.getString("nightscoutversionname");
ConfigBuilderPlugin.nsClientVersionCode = bundles.getInt("nsclientversioncode"); // for ver 1.17 contains 117
ConfigBuilderPlugin.nsClientVersionName = bundles.getString("nsclientversionname");
log.debug("Got versions: NSClient: " + ConfigBuilderPlugin.nsClientVersionName + " Nightscout: " + ConfigBuilderPlugin.nightscoutVersionName);
try {
if (ConfigBuilderPlugin.nsClientVersionCode < MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode) {
Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.sResources.getString(R.string.unsupportedclientver), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification));
} else {
MainApp.bus().post(new EventDismissNotification(Notification.OLD_NSCLIENT));
}
} catch (PackageManager.NameNotFoundException e) {
log.error("Unhandled exception", e);
}
if (ConfigBuilderPlugin.nightscoutVersionCode < Config.SUPPORTEDNSVERSION) {
2017-12-20 14:34:30 +01:00
Notification notification = new Notification(Notification.OLD_NS, MainApp.sResources.getString(R.string.unsupportednsversion), Notification.NORMAL);
2017-01-16 23:37:58 +01:00
MainApp.bus().post(new EventNewNotification(notification));
} else {
MainApp.bus().post(new EventDismissNotification(Notification.OLD_NS));
2017-01-16 23:37:58 +01:00
}
2016-06-26 14:56:43 +02:00
} else {
2017-01-16 23:37:58 +01:00
Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.sResources.getString(R.string.unsupportedclientver), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification));
2016-06-26 14:56:43 +02:00
}
if (bundles.containsKey("status")) {
try {
JSONObject statusJson = new JSONObject(bundles.getString("status"));
2017-06-26 12:44:03 +02:00
NSSettingsStatus.getInstance().setData(statusJson);
2017-06-12 11:15:03 +02:00
if (Config.logIncommingData)
log.debug("Received status: " + statusJson.toString());
2017-06-26 12:44:03 +02:00
Double targetHigh = NSSettingsStatus.getInstance().getThreshold("bgTargetTop");
Double targetlow = NSSettingsStatus.getInstance().getThreshold("bgTargetBottom");
2017-06-21 07:28:04 +02:00
if (targetHigh != null)
OverviewPlugin.bgTargetHigh = targetHigh;
if (targetlow != null)
OverviewPlugin.bgTargetLow = targetlow;
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
}
2016-06-26 14:56:43 +02:00
}
2016-06-27 18:48:48 +02:00
if (intent.getAction().equals(Intents.ACTION_NEW_DEVICESTATUS)) {
2016-10-16 21:11:16 +02:00
try {
if (bundles.containsKey("devicestatus")) {
JSONObject devicestatusJson = new JSONObject(bundles.getString("devicestatus"));
2017-06-26 12:44:03 +02:00
NSDeviceStatus.getInstance().setData(devicestatusJson);
2016-10-16 21:11:16 +02:00
if (devicestatusJson.has("pump")) {
// Objectives 0
ObjectivesPlugin.pumpStatusIsAvailableInNS = true;
ObjectivesPlugin.saveProgress();
}
}
if (bundles.containsKey("devicestatuses")) {
String devicestatusesstring = bundles.getString("devicestatuses");
JSONArray jsonArray = new JSONArray(devicestatusesstring);
2017-06-26 12:44:03 +02:00
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject devicestatusJson = jsonArray.getJSONObject(i);
NSDeviceStatus.getInstance().setData(devicestatusJson);
if (devicestatusJson.has("pump")) {
// Objectives 0
2016-08-10 23:43:08 +02:00
ObjectivesPlugin.pumpStatusIsAvailableInNS = true;
ObjectivesPlugin.saveProgress();
}
}
2016-06-27 18:48:48 +02:00
}
2016-10-16 21:11:16 +02:00
} catch (Exception e) {
log.error("Unhandled exception", e);
2016-06-27 18:48:48 +02:00
}
}
2016-06-07 21:48:17 +02:00
// Handle profile
if (intent.getAction().equals(Intents.ACTION_NEW_PROFILE)) {
try {
String activeProfile = bundles.getString("activeprofile");
String profile = bundles.getString("profile");
2017-06-02 10:25:49 +02:00
ProfileStore profileStore = new ProfileStore(new JSONObject(profile));
NSProfilePlugin.storeNewProfile(profileStore);
2018-01-23 20:58:48 +01:00
MainApp.bus().post(new EventNSProfileUpdateGUI());
// if there are no profile switches this should lead to profile update
if (MainApp.getConfigBuilder().getProfileSwitchesFromHistory().size() == 0)
MainApp.bus().post(new EventNewBasalProfile());
2016-06-07 21:48:17 +02:00
if (Config.logIncommingData)
2017-06-02 10:25:49 +02:00
log.debug("Received profileStore: " + activeProfile + " " + profile);
2016-06-07 21:48:17 +02:00
} catch (JSONException e) {
log.error("Unhandled exception", e);
2016-06-07 21:48:17 +02:00
}
}
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")) {
String trstring = bundles.getString("treatment");
handleAddChangeDataFromNS(trstring);
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++) {
JSONObject trJson = jsonArray.getJSONObject(i);
String trstr = trJson.toString();
handleAddChangeDataFromNS(trstr);
2016-06-07 21:48:17 +02:00
}
}
2016-06-27 18:48:48 +02:00
} catch (Exception 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");
JSONObject trJson = new JSONObject(trstring);
String _id = trJson.getString("_id");
handleRemovedRecordFromNS(_id);
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++) {
JSONObject trJson = jsonArray.getJSONObject(i);
String _id = trJson.getString("_id");
handleRemovedRecordFromNS(_id);
2016-06-07 21:48:17 +02:00
}
}
2016-06-27 18:48:48 +02:00
} catch (Exception e) {
log.error("Unhandled exception", e);
2016-06-07 21:48:17 +02:00
}
}
if (intent.getAction().equals(Intents.ACTION_NEW_SGV)) {
2016-11-09 17:48:44 +01:00
try {
if (bundles.containsKey("sgv")) {
String sgvstring = bundles.getString("sgv");
JSONObject sgvJson = new JSONObject(sgvstring);
NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
2017-06-06 17:14:17 +02:00
MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
2016-11-09 17:48:44 +01:00
}
if (bundles.containsKey("sgvs")) {
String sgvstring = bundles.getString("sgvs");
JSONArray jsonArray = new JSONArray(sgvstring);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject sgvJson = jsonArray.getJSONObject(i);
2016-06-07 21:48:17 +02:00
NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
2017-06-06 17:14:17 +02:00
MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
2016-06-21 00:34:36 +02:00
}
}
2016-11-09 17:48:44 +01:00
} catch (Exception 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");
JSONObject mbgJson = new JSONObject(mbgstring);
NSMbg nsMbg = new NSMbg(mbgJson);
CareportalEvent careportalEvent = new CareportalEvent(nsMbg);
MainApp.getDbHelper().createOrUpdate(careportalEvent);
if (Config.logIncommingData)
log.debug("Adding/Updating new MBG: " + careportalEvent.log());
}
if (bundles.containsKey("mbgs")) {
String sgvstring = bundles.getString("mbgs");
JSONArray jsonArray = new JSONArray(sgvstring);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject mbgJson = jsonArray.getJSONObject(i);
NSMbg nsMbg = new NSMbg(mbgJson);
CareportalEvent careportalEvent = new CareportalEvent(nsMbg);
MainApp.getDbHelper().createOrUpdate(careportalEvent);
if (Config.logIncommingData)
log.debug("Adding/Updating new MBG: " + careportalEvent.log());
}
}
} 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-01-07 20:24:42 +01:00
if (intent.getAction().equals(Intents.ACTION_NEW_FOOD)
|| intent.getAction().equals(Intents.ACTION_CHANGED_FOOD)) {
int mode = Intents.ACTION_NEW_FOOD.equals(intent.getAction()) ? EventNsFood.ADD : EventNsFood.UPDATE;
EventNsFood evt = new EventNsFood(mode, bundles);
2018-01-07 20:24:42 +01:00
MainApp.bus().post(evt);
2017-10-17 20:55:23 +02:00
}
if (intent.getAction().equals(Intents.ACTION_REMOVED_FOOD)) {
EventNsFood evt = new EventNsFood(EventNsFood.REMOVE, bundles);
2018-01-07 20:24:42 +01:00
MainApp.bus().post(evt);
2017-10-17 20:55:23 +02:00
}
}
private void handleRemovedRecordFromNS(String _id) {
MainApp.getDbHelper().deleteTreatmentById(_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);
}
private void handleAddChangeDataFromNS(String trstring) throws JSONException {
JSONObject trJson = new JSONObject(trstring);
2016-07-27 17:49:56 +02:00
handleDanaRHistoryRecords(trJson); // update record _id in history
2017-01-14 00:37:35 +01:00
handleAddChangeTempTargetRecord(trJson);
2017-05-25 20:18:29 +02:00
handleAddChangeTempBasalRecord(trJson);
handleAddChangeExtendedBolusRecord(trJson);
handleAddChangeCareportalEventRecord(trJson);
handleAddChangeTreatmentRecord(trJson);
2017-06-02 10:25:49 +02:00
handleAddChangeProfileSwitchRecord(trJson);
2016-06-07 21:48:17 +02:00
}
2017-05-22 12:52:19 +02:00
public void handleDanaRHistoryRecords(JSONObject trJson) {
2016-07-27 17:49:56 +02:00
if (trJson.has(DanaRNSHistorySync.DANARSIGNATURE)) {
2017-05-22 12:52:19 +02:00
MainApp.getDbHelper().updateDanaRHistoryRecordId(trJson);
2016-07-27 17:49:56 +02:00
}
}
public void handleAddChangeTreatmentRecord(JSONObject trJson) throws JSONException {
if (trJson.has("insulin") || trJson.has("carbs")) {
MainApp.getDbHelper().createTreatmentFromJsonIfNotExists(trJson);
return;
}
}
public void handleAddChangeTempTargetRecord(JSONObject trJson) throws JSONException {
2017-05-25 20:18:29 +02:00
if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.TEMPORARYTARGET)) {
2017-05-23 20:15:14 +02:00
MainApp.getDbHelper().createTemptargetFromJsonIfNotExists(trJson);
2017-01-14 00:37:35 +01:00
}
}
public void handleAddChangeTempBasalRecord(JSONObject trJson) throws JSONException {
2017-05-25 20:18:29 +02:00
if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.TEMPBASAL)) {
2017-05-23 20:15:14 +02:00
MainApp.getDbHelper().createTempBasalFromJsonIfNotExists(trJson);
}
}
public void handleAddChangeExtendedBolusRecord(JSONObject trJson) throws JSONException {
2017-05-25 20:18:29 +02:00
if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.COMBOBOLUS)) {
MainApp.getDbHelper().createExtendedBolusFromJsonIfNotExists(trJson);
2017-05-23 20:15:14 +02:00
}
}
public void handleAddChangeCareportalEventRecord(JSONObject trJson) throws JSONException {
2017-06-06 08:21:11 +02:00
if (trJson.has("insulin") && trJson.getDouble("insulin") > 0)
return;
if (trJson.has("carbs") && trJson.getDouble("carbs") > 0)
return;
2017-05-25 20:18:29 +02:00
if (trJson.has("eventType") && (
trJson.getString("eventType").equals(CareportalEvent.SITECHANGE) ||
2017-06-02 10:25:49 +02:00
trJson.getString("eventType").equals(CareportalEvent.INSULINCHANGE) ||
2017-06-03 22:43:35 +02:00
trJson.getString("eventType").equals(CareportalEvent.SENSORCHANGE) ||
2017-06-06 08:21:11 +02:00
trJson.getString("eventType").equals(CareportalEvent.BGCHECK) ||
trJson.getString("eventType").equals(CareportalEvent.NOTE) ||
trJson.getString("eventType").equals(CareportalEvent.NONE) ||
trJson.getString("eventType").equals(CareportalEvent.ANNOUNCEMENT) ||
trJson.getString("eventType").equals(CareportalEvent.QUESTION) ||
trJson.getString("eventType").equals(CareportalEvent.EXERCISE) ||
trJson.getString("eventType").equals(CareportalEvent.OPENAPSOFFLINE) ||
2017-06-03 22:43:35 +02:00
trJson.getString("eventType").equals(CareportalEvent.PUMPBATTERYCHANGE)
2017-05-25 20:18:29 +02:00
)) {
MainApp.getDbHelper().createCareportalEventFromJsonIfNotExists(trJson);
}
2017-06-06 08:21:11 +02:00
2017-07-25 19:18:51 +02:00
if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.ANNOUNCEMENT)) {
2017-06-06 08:21:11 +02:00
long date = trJson.getLong("mills");
2017-06-15 23:12:12 +02:00
long now = System.currentTimeMillis();
if (date > now - 15 * 60 * 1000L && trJson.has("notes")
&& !(trJson.has("enteredBy") && trJson.getString("enteredBy").equals(SP.getString("careportal_enteredby", "AndroidAPS")))) {
2017-06-11 17:22:54 +02:00
Notification announcement = new Notification(Notification.NSANNOUNCEMENT, trJson.getString("notes"), Notification.ANNOUNCEMENT, 60);
2017-06-06 08:21:11 +02:00
MainApp.bus().post(new EventNewNotification(announcement));
}
}
2017-05-25 20:18:29 +02:00
}
2017-06-02 10:25:49 +02:00
public void handleAddChangeProfileSwitchRecord(JSONObject trJson) throws JSONException {
if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.PROFILESWITCH)) {
MainApp.getDbHelper().createProfileSwitchFromJsonIfNotExists(trJson);
}
}
2016-07-14 23:29:21 +02:00
private void handleNewSMS(Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle == null) return;
MainApp.bus().post(new EventNewSMS(bundle));
}
2016-06-07 21:48:17 +02:00
}