diff --git a/app/build.gradle b/app/build.gradle index 18cf49f1b8..b0f2c864ec 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -157,5 +157,11 @@ dependencies { androidTestCompile "com.google.dexmaker:dexmaker:1.2" androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2" compile(name:'android-edittext-validator-v1.3.4-mod', ext:'aar') + compile ('io.socket:socket.io-client:0.8.3') { + // excluding org.json which is provided by Android + exclude group: 'org.json', module: 'json' + } + compile 'com.google.code.gson:gson:2.4' + compile 'com.google.guava:guava:18.0' } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8f500c4421..7db20117be 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -29,11 +29,14 @@ + - + @@ -42,6 +45,7 @@ + @@ -68,20 +72,44 @@ - - + + + + + + + + + + + + + + + + + + + - - - - - + + + + + + - + \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index 52cb5d85a1..2873d29611 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -52,4 +52,7 @@ public class Constants { //DanaR public static final double dailyLimitWarning = 0.95d; + + //NSClientInternal + public static final int MAX_LOG_LINES = 100; } diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 415e30c1ce..09014619cb 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -27,6 +27,7 @@ import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanFragment; import info.nightscout.androidaps.plugins.LocalProfile.LocalProfileFragment; import info.nightscout.androidaps.plugins.Loop.LoopFragment; import info.nightscout.androidaps.plugins.MDI.MDIFragment; +import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalFragment; import info.nightscout.androidaps.plugins.NSProfile.NSProfileFragment; import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment; import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAFragment; @@ -103,6 +104,7 @@ public class MainApp extends Application { if (Config.WEAR) pluginsList.add(WearFragment.getPlugin(this)); pluginsList.add(new PersistentNotificationPlugin(this)); + pluginsList.add(NSClientInternalFragment.getPlugin()); pluginsList.add(sConfigBuilder = ConfigBuilderFragment.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java index f0d60cae19..02169ac473 100644 --- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java @@ -18,6 +18,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.DanaR.BluetoothDevicePreference; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugin; import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin; import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpPlugin; import info.nightscout.utils.LocaleHelper; @@ -110,6 +111,10 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre if (virtualPumpPlugin != null && virtualPumpPlugin.isEnabled(PluginBase.PUMP)) { addPreferencesFromResource(R.xml.pref_virtualpump); } + NSClientInternalPlugin nsClientInternalPlugin = (NSClientInternalPlugin) MainApp.getSpecificPlugin(NSClientInternalPlugin.class); + if (nsClientInternalPlugin != null && nsClientInternalPlugin.isEnabled(PluginBase.GENERAL)) { + addPreferencesFromResource(R.xml.pref_nsclientinternal); + } if (Config.SMSCOMMUNICATORENABLED) addPreferencesFromResource(R.xml.pref_smscommunicator); addPreferencesFromResource(R.xml.pref_others); diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index 100a452ede..b27a1a85b4 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -49,8 +49,8 @@ import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin; import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange; import info.nightscout.androidaps.receivers.DataReceiver; -import info.nightscout.client.data.NSProfile; -import info.nightscout.client.data.NSSgv; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv; public class DataService extends IntentService { diff --git a/app/src/main/java/info/nightscout/androidaps/Services/Intents.java b/app/src/main/java/info/nightscout/androidaps/Services/Intents.java index 921b04b936..4787838d4f 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/Intents.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/Intents.java @@ -11,11 +11,13 @@ public interface Intents { String ACTION_NEW_MBG = "info.nightscout.client.NEW_MBG"; String ACTION_NEW_CAL = "info.nightscout.client.NEW_CAL"; String ACTION_NEW_STATUS = "info.nightscout.client.NEW_STATUS"; + String ACTION_QUEUE_STATUS = "info.nightscout.client.QUEUE_STATUS"; // App -> NSClient String ACTION_DATABASE = "info.nightscout.client.DBACCESS"; String ACTION_RESTART = "info.nightscout.client.RESTART"; + String ACTION_RESEND = "info.nightscout.client.RESEND"; // xDrip -> App String RECEIVER_PERMISSION = "com.eveningoutpost.dexdrip.permissions.RECEIVE_BG_ESTIMATE"; diff --git a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java index f2a6ad0df2..ce0a94ef5a 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java +++ b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java @@ -6,9 +6,8 @@ import org.json.JSONObject; import java.util.Date; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.Round; diff --git a/app/src/main/java/info/nightscout/androidaps/data/MealData.java b/app/src/main/java/info/nightscout/androidaps/data/MealData.java index b8e73c3e00..18919c3c8a 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/MealData.java +++ b/app/src/main/java/info/nightscout/androidaps/data/MealData.java @@ -10,7 +10,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.OpenAPSAMA.Autosens; import info.nightscout.androidaps.plugins.OpenAPSAMA.AutosensResult; import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; /** * Created by mike on 04.01.2017. diff --git a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java index 7ce7c14678..a2a5b91870 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java +++ b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java @@ -1,7 +1,5 @@ package info.nightscout.androidaps.data; -import android.os.Parcel; -import android.os.Parcelable; import android.text.Html; import android.text.Spanned; @@ -10,7 +8,7 @@ import org.json.JSONObject; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java index 6dcf282e6c..951d370bf3 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory; import java.util.Date; import info.nightscout.androidaps.Constants; -import info.nightscout.client.data.NSSgv; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv; import info.nightscout.utils.DecimalFormatter; @DatabaseTable(tableName = DatabaseHelper.DATABASE_BGREADINGS) diff --git a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java index 99389777d5..d15ab436da 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java @@ -11,7 +11,7 @@ import java.util.Date; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.data.IobTotal; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; diff --git a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java index f8c2f3b829..a8afacd767 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java +++ b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java @@ -15,7 +15,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java b/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java index 61feaa0897..3e270e8a1b 100644 --- a/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java +++ b/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java @@ -1,6 +1,6 @@ package info.nightscout.androidaps.events; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; /** * Created by mike on 04.06.2016. diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/ProfileInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/ProfileInterface.java index ae28697834..0924723776 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ProfileInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ProfileInterface.java @@ -2,7 +2,7 @@ package info.nightscout.androidaps.interfaces; import android.support.annotation.Nullable; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; /** * Created by mike on 14.06.2016. diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index 550b05103e..18f0c54da6 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -8,8 +8,7 @@ import java.util.Date; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.TempBasal; -import info.nightscout.androidaps.plugins.Loop.APSResult; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; /** * Created by mike on 04.06.2016. diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java index ea0ef114e8..f29d6e18ff 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java @@ -23,7 +23,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.PlusMinusEditText; import info.nightscout.utils.SafeParse; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index 4c93b88102..3fe6865a8e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -54,7 +54,7 @@ import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.CircadianPercentageProfile.CircadianPercentageProfilePlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.PlusMinusEditText; import info.nightscout.utils.SafeParse; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java index 56aa813c29..e56651eee1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java @@ -15,7 +15,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index a0b2a7b3b0..129ba98ff7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -54,8 +54,8 @@ import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressHelperAc import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; -import info.nightscout.client.data.DbLogger; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.DbLogger; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java index 7889471d6b..e116ffffe6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java @@ -40,7 +40,7 @@ import info.nightscout.androidaps.plugins.NSProfile.NSProfilePlugin; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java index 1e9808becd..b5d085b185 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java @@ -12,7 +12,7 @@ import java.util.Date; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.SafeParse; /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Dialogs/ProfileViewDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Dialogs/ProfileViewDialog.java index 3cb21f4221..1cedf012fd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Dialogs/ProfileViewDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Dialogs/ProfileViewDialog.java @@ -15,12 +15,10 @@ import org.slf4j.LoggerFactory; import java.util.Date; -import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DecimalFormatter; /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRHistoryActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRHistoryActivity.java index 4056943e70..ea1adaa908 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRHistoryActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRHistoryActivity.java @@ -32,7 +32,6 @@ import org.slf4j.LoggerFactory; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Date; import java.util.List; import info.nightscout.androidaps.Constants; @@ -44,7 +43,7 @@ import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService; import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRSyncStatus; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.ToastUtils; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRNSHistorySync.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRNSHistorySync.java index 327465eba2..205d83df9c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRNSHistorySync.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRNSHistorySync.java @@ -14,7 +14,7 @@ import info.nightscout.androidaps.db.DanaRHistoryRecord; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRSyncStatus; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.ToastUtils; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java index e7ecb4c742..ef707ef2f1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java @@ -34,7 +34,6 @@ import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.DanaR.DanaRPump; import info.nightscout.androidaps.plugins.DanaR.SerialIOThread; @@ -82,7 +81,7 @@ import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatu import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java index 1384be16e2..59d74c4131 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java @@ -40,7 +40,7 @@ import info.nightscout.androidaps.plugins.NSProfile.NSProfilePlugin; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java index b29d9a4347..faf8318710 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java @@ -12,7 +12,7 @@ import java.util.Date; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.SafeParse; /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/History/DanaRHistoryActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/History/DanaRHistoryActivity.java index d23cff488b..98e31261b5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/History/DanaRHistoryActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/History/DanaRHistoryActivity.java @@ -32,7 +32,6 @@ import org.slf4j.LoggerFactory; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Date; import java.util.List; import info.nightscout.androidaps.Constants; @@ -45,7 +44,7 @@ import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRSyncStatus; import info.nightscout.androidaps.plugins.DanaRKorean.Services.ExecutionService; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.ToastUtils; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java index 0bad19b38e..8b3a3287a1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java @@ -77,7 +77,7 @@ import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgStatusBolusExtende import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgStatusTempBasal; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java index 91c994cdbb..6b917b44da 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java @@ -15,8 +15,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; -import info.nightscout.androidaps.plugins.SimpleProfile.SimpleProfileFragment; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.SafeParse; /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java index 85a9f39b09..77e1d322c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java @@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.Loop; import android.content.Context; import android.content.Intent; -import android.content.pm.ResolveInfo; import android.os.Bundle; import org.json.JSONException; @@ -10,12 +9,9 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; - -import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.Services.Intents; -import info.nightscout.client.data.DbLogger; +import info.nightscout.androidaps.plugins.NSClientInternal.data.DbLogger; /* { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/MDI/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/MDI/MDIPlugin.java index ce48a966de..0528ddf21d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/MDI/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/MDI/MDIPlugin.java @@ -7,7 +7,6 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.sql.SQLException; import java.util.Date; import info.nightscout.androidaps.BuildConfig; @@ -19,10 +18,7 @@ import info.nightscout.androidaps.db.TempBasal; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; -import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment; -import info.nightscout.androidaps.plugins.VirtualPump.events.EventVirtualPumpUpdateGui; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java new file mode 100644 index 0000000000..4a9b5f963c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java @@ -0,0 +1,163 @@ +package info.nightscout.androidaps.plugins.NSClientInternal; + + +import android.app.Activity; +import android.content.SharedPreferences; +import android.graphics.Paint; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.ScrollView; +import android.widget.TextView; + +import com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.FragmentBase; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientRestart; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientUpdateGUI; + +public class NSClientInternalFragment extends Fragment implements FragmentBase, View.OnClickListener, CompoundButton.OnCheckedChangeListener { + private static Logger log = LoggerFactory.getLogger(NSClientInternalFragment.class); + + static NSClientInternalPlugin nsClientInternalPlugin; + + static public NSClientInternalPlugin getPlugin() { + if (nsClientInternalPlugin == null) { + nsClientInternalPlugin = new NSClientInternalPlugin(); + } + return nsClientInternalPlugin; + } + + private TextView logTextView; + private TextView queueTextView; + private TextView urlTextView; + private TextView statusTextView; + private TextView restart; + private TextView delivernow; + private TextView clearqueue; + private TextView showqueue; + private ScrollView logScrollview; + private CheckBox autoscrollCheckbox; + private CheckBox pausedCheckbox; + + String status = ""; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.nsclientinternal_fragment, container, false); + + logScrollview = (ScrollView) view.findViewById(R.id.nsclientinternal_logscrollview); + autoscrollCheckbox = (CheckBox) view.findViewById(R.id.nsclientinternal_autoscroll); + autoscrollCheckbox.setOnCheckedChangeListener(this); + pausedCheckbox = (CheckBox) view.findViewById(R.id.nsclientinternal_paused); + pausedCheckbox.setOnCheckedChangeListener(this); + logTextView = (TextView) view.findViewById(R.id.nsclientinternal_log); + queueTextView = (TextView) view.findViewById(R.id.nsclientinternal_queue); + urlTextView = (TextView) view.findViewById(R.id.nsclientinternal_url); + statusTextView = (TextView) view.findViewById(R.id.nsclientinternal_status); + + restart = (TextView) view.findViewById(R.id.nsclientinternal_restart); + restart.setOnClickListener(this); + restart.setPaintFlags(restart.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + delivernow = (TextView) view.findViewById(R.id.nsclientinternal_delivernow); + delivernow.setOnClickListener(this); + delivernow.setPaintFlags(delivernow.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + clearqueue = (TextView) view.findViewById(R.id.nsclientinternal_clearqueue); + clearqueue.setOnClickListener(this); + clearqueue.setPaintFlags(clearqueue.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + showqueue = (TextView) view.findViewById(R.id.nsclientinternal_showqueue); + showqueue.setOnClickListener(this); + showqueue.setPaintFlags(showqueue.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + + updateGUI(); + return view; + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.nsclientinternal_restart: + MainApp.bus().post(new EventNSClientRestart()); + break; + case R.id.nsclientinternal_delivernow: + getPlugin().resend("GUI"); + break; + case R.id.nsclientinternal_clearqueue: + getPlugin().clearLog(); + break; + case R.id.nsclientinternal_showqueue: + MainApp.bus().post(new EventNSClientNewLog("QUEUE", getPlugin().queue().textList())); + break; + } + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + SharedPreferences.Editor editor = SP.edit(); + switch (buttonView.getId()) { + case R.id.nsclientinternal_paused: + editor.putBoolean("nsclientinternal_paused", isChecked); + editor.apply(); + getPlugin().paused = isChecked; + // TODO + updateGUI(); + break; + case R.id.nsclientinternal_autoscroll: + editor.putBoolean("nsclientinternal_autoscroll", isChecked); + editor.apply(); + updateGUI(); + break; + } + } + + @Override + public void onPause() { + super.onPause(); + MainApp.bus().unregister(this); + } + + @Override + public void onResume() { + super.onResume(); + MainApp.bus().register(this); + updateGUI(); + } + + @Subscribe + public void onStatusEvent(final EventNSClientUpdateGUI ev) { + updateGUI(); + } + + private void updateGUI() { + Activity activity = getActivity(); + if (activity != null) + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + logTextView.setText(getPlugin().textLog); + if (getPlugin().autoscroll) { + logScrollview.fullScroll(ScrollView.FOCUS_DOWN); + } + urlTextView.setText(getPlugin().url); + queueTextView.setText(((Integer)getPlugin().queue().size()).toString()); + statusTextView.setText(getPlugin().status); + } + }); + } + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java new file mode 100644 index 0000000000..ed4e74ec50 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java @@ -0,0 +1,215 @@ +package info.nightscout.androidaps.plugins.NSClientInternal; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.content.SharedPreferences; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.IBinder; +import android.preference.PreferenceManager; +import android.text.Html; +import android.text.Spanned; +import android.text.TextUtils; + +import com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventAppExit; +import info.nightscout.androidaps.events.EventPreferenceChange; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.PumpDescription; +import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientRestart; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientUpdateGUI; +import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService; + +public class NSClientInternalPlugin implements PluginBase { + private static Logger log = LoggerFactory.getLogger(NSClientInternalPlugin.class); + + boolean fragmentEnabled = true; + boolean fragmentVisible = true; + + static public Handler handler; + static private HandlerThread handlerThread; + + public List listLog = new ArrayList(); + public Spanned textLog = Html.fromHtml(""); + + public boolean paused = false; + public boolean autoscroll = true; + public String url = ""; + + public String status = ""; + + public NSClientService nsClientService = null; + + public NSClientInternalPlugin() { + MainApp.bus().register(this); + SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + paused = SP.getBoolean("nsclientinternal_paused", false); + autoscroll = SP.getBoolean("nsclientinternal_autoscroll", true); + url = SP.getString("nsclientinternal_url", ""); + + if (handler == null) { + handlerThread = new HandlerThread(NSClientInternalPlugin.class.getSimpleName() + "Handler"); + handlerThread.start(); + handler = new Handler(handlerThread.getLooper()); + } + + Context context = MainApp.instance().getApplicationContext(); + Intent intent = new Intent(context, NSClientService.class); + context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); + } + + @Override + public int getType() { + return PluginBase.GENERAL; + } + + @Override + public String getFragmentClass() { + return NSClientInternalFragment.class.getName(); + } + + @Override + public String getName() { + return MainApp.sResources.getString(R.string.nsclientinternal); + } + + @Override + public String getNameShort() { + String name = MainApp.sResources.getString(R.string.nsclientinternal_shortname); + if (!name.trim().isEmpty()) { + //only if translation exists + return name; + } + // use long name as fallback + return getName(); + } + + @Override + public boolean isEnabled(int type) { + return type == GENERAL && fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return type == GENERAL && fragmentVisible; + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + if (type == GENERAL) this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + if (type == GENERAL) this.fragmentVisible = fragmentVisible; + } + + ServiceConnection mConnection = new ServiceConnection() { + + public void onServiceDisconnected(ComponentName name) { + log.debug("Service is disconnected"); + nsClientService = null; + } + + public void onServiceConnected(ComponentName name, IBinder service) { + log.debug("Service is connected"); + NSClientService.LocalBinder mLocalBinder = (NSClientService.LocalBinder) service; + nsClientService = mLocalBinder.getServiceInstance(); + } + }; + + @SuppressWarnings("UnusedParameters") + @Subscribe + public void onStatusEvent(final EventAppExit e) { + if (nsClientService != null) + MainApp.instance().getApplicationContext().unbindService(mConnection); + } + + @Subscribe + public void onStatusEvent(final EventPreferenceChange s) { + //TODO + } + + @Subscribe + public void onStatusEvent(final EventNSClientNewLog ev) { + addToLog(ev); + log.debug(ev.action + " " + ev.logText); + } + + @Subscribe + public void onStatusEvent(final EventNSClientRestart ev) { + if (nsClientService != null) + nsClientService.restart(); + } + + @Subscribe + public void onStatusEvent(final EventNSClientStatus ev) { + status = ev.status; + MainApp.bus().post(new EventNSClientUpdateGUI()); + } + + public void clearLog() { + handler.post(new Runnable() { + @Override + public void run() { + listLog = new ArrayList(); + updateLog(); + } + }); + } + + private void addToLog(final EventNSClientNewLog ev) { + handler.post(new Runnable() { + @Override + public void run() { + SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); + listLog.add(ev); + // remove the first line if log is too large + if (listLog.size() >= Constants.MAX_LOG_LINES) { + listLog.remove(0); + } + updateLog(); + } + }); + } + + private void updateLog() { + Spanned newTextLog = Html.fromHtml(""); + for (EventNSClientNewLog log : listLog) { + newTextLog = (Spanned) TextUtils.concat(newTextLog, log.toHtml()); + } + textLog = newTextLog; + MainApp.bus().post(new EventNSClientUpdateGUI()); + } + + public void resend(String reason) { + if (nsClientService != null) + nsClientService.resend(reason); + } + + public UploadQueue queue() { + return NSClientService.uploadQueue; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java new file mode 100644 index 0000000000..f0486b2511 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java @@ -0,0 +1,151 @@ +package info.nightscout.androidaps.plugins.NSClientInternal; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSAddAck; +import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSUpdateAck; +import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastQueueStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.data.DbRequest; +import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService; + +/** + * Created by mike on 21.02.2016. + */ +public class UploadQueue { + private static Logger log = LoggerFactory.getLogger(UploadQueue.class); + + public static HashMap queue = null; + + public UploadQueue() { + loadMap(); + } + + public static String status() { + return "QUEUE: " + queue.size(); + } + + public static int size() { + return queue.size(); + } + + public static void add(final DbRequest dbr) { + NSClientService.handler.post(new Runnable() { + @Override + public void run() { + log.debug("QUEUE adding: " + dbr.data.toString()); + queue.put(dbr.hash(), dbr); + } + }); + } + + public static void put(final String hash, final DbRequest dbr) { + NSClientService.handler.post(new Runnable() { + @Override + public void run() { + queue.put(hash, dbr); + BroadcastQueueStatus bs = new BroadcastQueueStatus(); + bs.handleNewStatus(queue.size(), MainApp.instance().getApplicationContext()); + } + }); + } + + public static void reset() { + NSClientService.handler.post(new Runnable() { + @Override + public void run() { + log.debug("QUEUE Reset"); + queue.clear(); + log.debug(status()); + } + }); + } + + public static void removeID(final JSONObject record) { + NSClientService.handler.post(new Runnable() { + @Override + public void run() { + try { + long id = -1L; + if (record.has("NSCLIENT_ID")) { + id = record.getLong("NSCLIENT_ID"); + } else { + return; + } + Iterator> iter = queue.entrySet().iterator(); + while (iter.hasNext()) { + DbRequest dbr = iter.next().getValue(); + JSONObject data = dbr.data; + long nsclientId = -1; + if (data.has("NSCLIENT_ID")) { + nsclientId = data.getLong("NSCLIENT_ID"); + if (nsclientId == id) { + log.debug("Removing item from UploadQueue"); + iter.remove(); + log.debug(UploadQueue.status()); + return; + } + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + }); + } + + final String KEY = "UploadQueue"; + + private void saveMap() { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + JSONObject jsonObject = new JSONObject(queue); + String jsonString = jsonObject.toString(); + SharedPreferences.Editor editor = sp.edit(); + editor.remove(KEY).commit(); + editor.putString(KEY, jsonString); + editor.commit(); + } + + private void loadMap() { + queue = new HashMap(); + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + try { + String jsonString = sp.getString(KEY, (new JSONObject()).toString()); + JSONObject jsonObject = new JSONObject(jsonString); + Iterator keysItr = jsonObject.keys(); + while (keysItr.hasNext()) { + String key = keysItr.next(); + DbRequest value = (DbRequest) jsonObject.get(key); + queue.put(key, value); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public String textList() { + Iterator> iter = queue.entrySet().iterator(); + String result = ""; + + while (iter.hasNext()) { + DbRequest dbr = iter.next().getValue(); + result += dbr.action.toUpperCase() + " "; + result += dbr.collection + ": "; + result += dbr.data.toString(); + } + return result; + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSAddAck.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSAddAck.java new file mode 100644 index 0000000000..43fc09876e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSAddAck.java @@ -0,0 +1,54 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.acks; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService; +import io.socket.client.Ack; + +/** + * Created by mike on 29.12.2015. + */ +public class NSAddAck implements Ack { + private static Logger log = LoggerFactory.getLogger(NSAddAck.class); + public String _id = null; + public void call(Object...args) { + // Regular response + try { + JSONArray responsearray = (JSONArray) (args[0]); + JSONObject response = null; + if (responsearray.length()>0) { + response = responsearray.getJSONObject(0); + _id = response.getString("_id"); + } + synchronized(this) { + this.notify(); + } + return; + } catch (Exception e) { + } + // Check for not authorized + try { + JSONObject response = (JSONObject) (args[0]); + if (response.has("result")) { + _id = null; + if (response.getString("result").equals("Not authorized")) { + synchronized(this) { + this.notify(); + } + NSClientService.forcerestart = true; + return; + } + log.debug("DBACCESS " + response.getString("result")); + } + synchronized(this) { + this.notify(); + } + return; + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSAuthAck.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSAuthAck.java new file mode 100644 index 0000000000..3c3b173173 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSAuthAck.java @@ -0,0 +1,24 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.acks; + +import org.json.JSONObject; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog; +import io.socket.client.Ack; + +/** + * Created by mike on 02.01.2016. + */ +public class NSAuthAck implements Ack{ + public boolean read = false; + public boolean write = false; + public boolean write_treatment = false; + + public void call(Object...args) { + JSONObject response = (JSONObject)args[0]; + read = response.optBoolean("read"); + write = response.optBoolean("write"); + write_treatment = response.optBoolean("write_treatment"); + MainApp.bus().post(this); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSPingAck.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSPingAck.java new file mode 100644 index 0000000000..2824e922aa --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSPingAck.java @@ -0,0 +1,38 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.acks; + +import org.json.JSONException; +import org.json.JSONObject; + +import io.socket.client.Ack; + +/** + * Created by mike on 29.12.2015. + */ +public class NSPingAck implements Ack { + public long mills = 0; + public boolean received = false; + public boolean auth_received = false; + public boolean read = false; + public boolean write = false; + public boolean write_treatment = false; + + public void call(Object...args) { + JSONObject response = (JSONObject)args[0]; + mills = response.optLong("mills"); + if (response.has("authorization")) { + auth_received = true; + try { + JSONObject authorization = response.getJSONObject("authorization"); + read = authorization.optBoolean("read"); + write = authorization.optBoolean("write"); + write_treatment = authorization.optBoolean("write_treatment"); + } catch (JSONException e) { + e.printStackTrace(); + } + } + received = true; + synchronized(this) { + this.notify(); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSUpdateAck.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSUpdateAck.java new file mode 100644 index 0000000000..46b09f4430 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSUpdateAck.java @@ -0,0 +1,32 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.acks; + +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.socket.client.Ack; + +/** + * Created by mike on 21.02.2016. + */ +public class NSUpdateAck implements Ack { + private static Logger log = LoggerFactory.getLogger(NSUpdateAck.class); + public boolean result = false; + public void call(Object...args) { + JSONObject response = (JSONObject)args[0]; + if (response.has("result")) + try { + if (response.getString("result").equals("success")) + result = true; + else if (response.getString("result").equals("Missing _id")) { + result = true; + log.debug("Internal error: Missing _id returned on dbUpdate ack"); + } + } catch (JSONException e) { + } + synchronized(this) { + this.notify(); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastCals.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastCals.java new file mode 100644 index 0000000000..58a24e825c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastCals.java @@ -0,0 +1,34 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.os.Bundle; + +import org.json.JSONArray; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import info.nightscout.androidaps.Services.Intents; + +/** + * Created by mike on 26.06.2016. + */ +public class BroadcastCals { + private static Logger log = LoggerFactory.getLogger(BroadcastCals.class); + + public void handleNewCal(JSONArray cals, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("cals", cals.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_CAL); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("CAL " + x.size() + " receivers"); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastDeviceStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastDeviceStatus.java new file mode 100644 index 0000000000..64fd72b36a --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastDeviceStatus.java @@ -0,0 +1,45 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.os.Bundle; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import info.nightscout.androidaps.Services.Intents; + + +public class BroadcastDeviceStatus { + private static Logger log = LoggerFactory.getLogger(BroadcastDeviceStatus.class); + + public void handleNewDeviceStatus(JSONObject status, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("devicestatus", status.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_DEVICESTATUS); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("DEVICESTATUS " + x.size() + " receivers"); + } + public void handleNewDeviceStatus(JSONArray statuses, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("devicestatuses", statuses.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_DEVICESTATUS); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("DEVICESTATUS " + x.size() + " receivers"); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastMbgs.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastMbgs.java new file mode 100644 index 0000000000..33669e1852 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastMbgs.java @@ -0,0 +1,34 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.os.Bundle; + +import org.json.JSONArray; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import info.nightscout.androidaps.Services.Intents; + +/** + * Created by mike on 26.06.2016. + */ +public class BroadcastMbgs { + private static Logger log = LoggerFactory.getLogger(BroadcastMbgs.class); + + public void handleNewMbg(JSONArray mbgs, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("mbgs", mbgs.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_MBG); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("MBG " + x.size() + " receivers"); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastProfile.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastProfile.java new file mode 100644 index 0000000000..a1d44d0340 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastProfile.java @@ -0,0 +1,37 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.os.Bundle; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; + + +/** + * Created by mike on 20.02.2016. + */ +public class BroadcastProfile { + private static Logger log = LoggerFactory.getLogger(BroadcastProfile.class); + + public void handleNewTreatment(NSProfile profile, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("profile", profile.getData().toString()); + bundle.putString("activeprofile", profile.getActiveProfile()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_PROFILE); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("PROFILE " + x.size() + " receivers"); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastQueueStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastQueueStatus.java new file mode 100644 index 0000000000..ed2b01a27c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastQueueStatus.java @@ -0,0 +1,30 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.PowerManager; + +import info.nightscout.androidaps.Services.Intents; + +/** + * Created by mike on 28.02.2016. + */ +public class BroadcastQueueStatus { + public void handleNewStatus(int size, Context context) { + PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, + "sendQueue"); + wakeLock.acquire(); + try { + Bundle bundle = new Bundle(); + bundle.putInt("size", size); + Intent intent = new Intent(Intents.ACTION_QUEUE_STATUS); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + } finally { + wakeLock.release(); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastSgvs.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastSgvs.java new file mode 100644 index 0000000000..81a2621a77 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastSgvs.java @@ -0,0 +1,49 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.os.Bundle; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import info.nightscout.androidaps.Services.Intents; + +/** + * Created by mike on 22.02.2016. + */ +public class BroadcastSgvs { + private static Logger log = LoggerFactory.getLogger(BroadcastSgvs.class); + + public void handleNewSgv(JSONObject sgv, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("sgv", sgv.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_SGV); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("SGV " + x.size() + " receivers"); + } + + public void handleNewSgv(JSONArray sgvs, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("sgvs", sgvs.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_SGV); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("SGV " + x.size() + " receivers"); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java new file mode 100644 index 0000000000..b99950c89b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java @@ -0,0 +1,45 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.os.Bundle; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService; + +/** + * Created by mike on 24.02.2016. + */ +public class BroadcastStatus { + private static Logger log = LoggerFactory.getLogger(BroadcastStatus.class); + + public void handleNewStatus(NSStatus status, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + try { + bundle.putString("nsclientversionname", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionName); + bundle.putInt("nsclientversioncode", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode); + } catch (PackageManager.NameNotFoundException e) { + e.printStackTrace(); + }; + bundle.putString("nightscoutversionname", NSClientService.nightscoutVersionName); + bundle.putInt("nightscoutversioncode", NSClientService.nightscoutVersionCode); + bundle.putString("status", status.getData().toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_STATUS); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("STATUS: " + x.size() + " receivers"); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java new file mode 100644 index 0000000000..9d1abe43e3 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java @@ -0,0 +1,107 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.broadcasts; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.os.Bundle; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSTreatment; + +/** + * Created by mike on 20.02.2016. + */ +public class BroadcastTreatment { + private static Logger log = LoggerFactory.getLogger(BroadcastTreatment.class); + + public void handleNewTreatment(NSTreatment treatment, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("treatment", treatment.getData().toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("TREAT_ADD " + treatment.getEventType() + " " + x.size() + " receivers"); + } + + public void handleNewTreatment(JSONArray treatments, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("treatments", treatments.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("TREAT_ADD " + treatments.length() + " " + x.size() + " receivers"); + } + + public void handleChangedTreatment(JSONObject treatment, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("treatment", treatment.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_CHANGED_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + try { + log.debug("TREAT_CHANGE " + treatment.getString("_id") + " " + x.size() + " receivers"); + } catch (JSONException e) {} + } + + public void handleChangedTreatment(JSONArray treatments, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("treatments", treatments.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_CHANGED_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("TREAT_CHANGE " + treatments.length() + " " + x.size() + " receivers"); + } + + public void handleRemovedTreatment(JSONObject treatment, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("treatment", treatment.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_REMOVED_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + try { + log.debug("TREAT_REMOVE " + treatment.getString("_id") + " " + x.size() + " receivers"); + } catch (JSONException e) {} + } + + public void handleRemovedTreatment(JSONArray treatments, Context context, boolean isDelta) { + Bundle bundle = new Bundle(); + bundle.putString("treatments", treatments.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_REMOVED_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List x = context.getPackageManager().queryBroadcastReceivers(intent, 0); + + log.debug("TREAT_REMOVE " + treatments.length() + " treatments " + x.size() + " receivers"); + } + +} diff --git a/app/src/main/java/info/nightscout/client/data/DbLogger.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/DbLogger.java similarity index 96% rename from app/src/main/java/info/nightscout/client/data/DbLogger.java rename to app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/DbLogger.java index 3695d5e735..70d8e9e336 100644 --- a/app/src/main/java/info/nightscout/client/data/DbLogger.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/DbLogger.java @@ -1,4 +1,4 @@ -package info.nightscout.client.data; +package info.nightscout.androidaps.plugins.NSClientInternal.data; import android.content.Intent; import android.content.pm.ResolveInfo; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/DbRequest.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/DbRequest.java new file mode 100644 index 0000000000..36fba1e637 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/DbRequest.java @@ -0,0 +1,46 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.data; + +import com.google.common.base.Charsets; +import com.google.common.hash.Hashing; + +import org.json.JSONObject; + +/** + * Created by mike on 27.02.2016. + * + * Allowed actions "dbAdd" || "dbUpdate" || "dbUpdateUnset" || "dbRemove" + */ +public class DbRequest { + public String action = null; + public String collection = null; + public JSONObject data = null; + public String _id = null; + + // dbAdd + public DbRequest(String action, String collection, JSONObject data) { + this.action = action; + this.collection = collection; + this.data = data; + this._id = ""; + } + + // dbUpdate, dbUpdateUnset + public DbRequest(String action, String collection, String _id, JSONObject data) { + this.action = action; + this.collection = collection; + this.data = data; + this._id = _id; + } + + // dbRemove + public DbRequest(String action, String collection, String _id) { + this.action = action; + this.collection = collection; + this.data = new JSONObject(); + this._id = _id; + } + + public String hash() { + return Hashing.sha1().hashString(action + collection + _id + data.toString(), Charsets.UTF_8).toString(); + } +} diff --git a/app/src/main/java/info/nightscout/client/data/NSCal.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSCal.java similarity index 67% rename from app/src/main/java/info/nightscout/client/data/NSCal.java rename to app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSCal.java index 5a78dbaa02..74b5f688d9 100644 --- a/app/src/main/java/info/nightscout/client/data/NSCal.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSCal.java @@ -1,9 +1,12 @@ -package info.nightscout.client.data; +package info.nightscout.androidaps.plugins.NSClientInternal.data; import org.json.JSONException; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class NSCal { + private static Logger log = LoggerFactory.getLogger(NSCal.class); public long date; public double slope; public double intercept; @@ -17,6 +20,7 @@ public class NSCal { scale = json.getDouble("scale"); } catch (JSONException e) { e.printStackTrace(); + log.debug("Data: " + json.toString()); } } } diff --git a/app/src/main/java/info/nightscout/client/data/NSProfile.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSProfile.java similarity index 99% rename from app/src/main/java/info/nightscout/client/data/NSProfile.java rename to app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSProfile.java index 2e732c8c9f..4d3df3068c 100644 --- a/app/src/main/java/info/nightscout/client/data/NSProfile.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSProfile.java @@ -1,4 +1,4 @@ -package info.nightscout.client.data; +package info.nightscout.androidaps.plugins.NSClientInternal.data; import com.crashlytics.android.Crashlytics; diff --git a/app/src/main/java/info/nightscout/client/data/NSSgv.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSSgv.java similarity index 96% rename from app/src/main/java/info/nightscout/client/data/NSSgv.java rename to app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSSgv.java index 5ce9b35caa..c8666f4306 100644 --- a/app/src/main/java/info/nightscout/client/data/NSSgv.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSSgv.java @@ -1,4 +1,4 @@ -package info.nightscout.client.data; +package info.nightscout.androidaps.plugins.NSClientInternal.data; import org.json.JSONException; import org.json.JSONObject; diff --git a/app/src/main/java/info/nightscout/client/data/NSStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSStatus.java similarity index 97% rename from app/src/main/java/info/nightscout/client/data/NSStatus.java rename to app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSStatus.java index f247907296..fe20e90b1d 100644 --- a/app/src/main/java/info/nightscout/client/data/NSStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSStatus.java @@ -1,4 +1,4 @@ -package info.nightscout.client.data; +package info.nightscout.androidaps.plugins.NSClientInternal.data; import org.json.JSONException; import org.json.JSONObject; diff --git a/app/src/main/java/info/nightscout/client/data/NSTreatment.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSTreatment.java similarity index 97% rename from app/src/main/java/info/nightscout/client/data/NSTreatment.java rename to app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSTreatment.java index f358b4e74d..7430caf600 100644 --- a/app/src/main/java/info/nightscout/client/data/NSTreatment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSTreatment.java @@ -1,4 +1,4 @@ -package info.nightscout.client.data; +package info.nightscout.androidaps.plugins.NSClientInternal.data; import org.json.JSONException; import org.json.JSONObject; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientNewLog.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientNewLog.java new file mode 100644 index 0000000000..7f576fde7e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientNewLog.java @@ -0,0 +1,30 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.events; + +import android.text.Html; +import android.text.Spanned; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by mike on 15.02.2017. + */ + +public class EventNSClientNewLog { + public Date date = new Date(); + public String action; + public String logText; + public EventNSClientNewLog(String action, String logText) { + this.action = action; + this.logText = logText; + } + + public Spanned toHtml() { + SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); + Spanned line = Html.fromHtml(timeFormat.format(date) + " " + action + " " + logText + "
"); + return line; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientRestart.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientRestart.java new file mode 100644 index 0000000000..9bf90be31b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientRestart.java @@ -0,0 +1,8 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.events; + +/** + * Created by mike on 15.02.2017. + */ + +public class EventNSClientRestart { +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientStatus.java new file mode 100644 index 0000000000..6a1c721535 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientStatus.java @@ -0,0 +1,16 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.events; + +/** + * Created by mike on 02.01.2016. + */ +public class EventNSClientStatus { + public String status = ""; + + public EventNSClientStatus(String status) { + this.status = status; + } + + public EventNSClientStatus() { + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientUpdateGUI.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientUpdateGUI.java new file mode 100644 index 0000000000..8f00bd3424 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/events/EventNSClientUpdateGUI.java @@ -0,0 +1,8 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.events; + +/** + * Created by mike on 17.02.2017. + */ + +public class EventNSClientUpdateGUI { +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/AutoStartReceiver.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/AutoStartReceiver.java new file mode 100644 index 0000000000..dd5d3330ab --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/AutoStartReceiver.java @@ -0,0 +1,17 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.receivers; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService; + +public class AutoStartReceiver extends BroadcastReceiver { + public AutoStartReceiver() { + } + + @Override + public void onReceive(Context context, Intent intent) { + context.startService(new Intent(context, NSClientService.class)); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/DBAccessReceiver.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/DBAccessReceiver.java new file mode 100644 index 0000000000..1cf0d27b77 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/DBAccessReceiver.java @@ -0,0 +1,87 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.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 java.util.Date; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugin; +import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue; +import info.nightscout.androidaps.plugins.NSClientInternal.data.DbRequest; + +public class DBAccessReceiver extends BroadcastReceiver { + private static Logger log = LoggerFactory.getLogger(DBAccessReceiver.class); + + + @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, + "sendQueue"); + wakeLock.acquire(); + try { + Bundle bundles = intent.getExtras(); + if (bundles == null) return; + if (!bundles.containsKey("action")) return; + + String collection = null; + String _id = null; + JSONObject data = null; + String action = bundles.getString("action"); + try { collection = bundles.getString("collection"); } catch (Exception e) {} + try { _id = bundles.getString("_id"); } catch (Exception e) {} + try { data = new JSONObject(bundles.getString("data")); } catch (Exception e) {} + + if (data == null && !action.equals("dbRemove") || _id == null && action.equals("dbRemove")) { + log.debug("DBACCESS no data inside record"); + return; + } + + // mark by id + if (action.equals("dbRemove")) { + data = new JSONObject(); + } + try { + data.put("NSCLIENT_ID", (new Date()).getTime()); + } catch (JSONException e) { + e.printStackTrace(); + } + + if (!isAllowedCollection(collection)) { + log.debug("DBACCESS wrong collection specified"); + return; + } + + if (action.equals("dbRemove")) { + DbRequest dbr = new DbRequest(action, collection, _id); + UploadQueue.add(dbr); + } else { + DbRequest dbr = new DbRequest(action, collection, data); + UploadQueue.add(dbr); + } + + } finally { + wakeLock.release(); + } + + } + + 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; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/RestartReceiver.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/RestartReceiver.java new file mode 100644 index 0000000000..a38ca842f9 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/RestartReceiver.java @@ -0,0 +1,25 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.receivers; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.support.v4.content.WakefulBroadcastReceiver; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientRestart; +import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService; + +public class RestartReceiver extends WakefulBroadcastReceiver { + public RestartReceiver() { + } + + @Override + public void onReceive(Context context, Intent intent) { + startWakefulService(context, new Intent(context, NSClientService.class) + .setAction(intent.getAction()) + .putExtras(intent)); + + MainApp.bus().post(new EventNSClientRestart()); + completeWakefulIntent(intent); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java new file mode 100644 index 0000000000..9aadc0d1dc --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java @@ -0,0 +1,760 @@ +package info.nightscout.androidaps.plugins.NSClientInternal.services; + +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Binder; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.IBinder; +import android.os.PowerManager; +import android.preference.PreferenceManager; +import android.widget.Toast; + +import com.google.common.base.Charsets; +import com.google.common.hash.Hashing; +import com.squareup.otto.Subscribe; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URISyntaxException; +import java.util.Date; +import java.util.Iterator; +import java.util.Map; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.events.EventAppExit; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService; +import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugin; +import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue; +import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSAddAck; +import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSAuthAck; +import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSPingAck; +import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSUpdateAck; +import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastCals; +import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastDeviceStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastMbgs; +import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastSgvs; +import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastTreatment; +import info.nightscout.androidaps.plugins.NSClientInternal.data.DbRequest; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSCal; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSTreatment; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog; +import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus; +import info.nightscout.utils.SafeParse; +import io.socket.client.IO; +import io.socket.client.Socket; +import io.socket.emitter.Emitter; + +public class NSClientService extends Service { + private static Logger log = LoggerFactory.getLogger(ExecutionService.class); + + static public PowerManager.WakeLock mWakeLock; + public static boolean forcerestart = false; + private IBinder mBinder = new NSClientService.LocalBinder(); + + static NSProfile nsProfile; + + static public Handler handler; + static private HandlerThread handlerThread; + + public static Socket mSocket; + public static boolean isConnected = false; + private static Integer dataCounter = 0; + + + public static String nightscoutVersionName = ""; + public static Integer nightscoutVersionCode = 0; + + private boolean nsEnabled = false; + private String nsURL = ""; + private String nsAPISecret = ""; + private String nsDevice = ""; + private Integer nsHours = 3; + + private final Integer timeToWaitForResponseInMs = 30000; + private boolean uploading = false; + public Date lastReception = new Date(); + + private String nsAPIhashCode = ""; + + public static UploadQueue uploadQueue = new UploadQueue(); + + public NSClientService() { + registerBus(); + if (handler == null) { + handlerThread = new HandlerThread(NSClientService.class.getSimpleName() + "Handler"); + handlerThread.start(); + handler = new Handler(handlerThread.getLooper()); + } + + PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE); + mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NSClientService"); + initialize(); + } + + public class LocalBinder extends Binder { + public NSClientService getServiceInstance() { + return NSClientService.this; + } + } + + @Override + public IBinder onBind(Intent intent) { + return mBinder; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + + return START_STICKY; + } + + private void registerBus() { + try { + MainApp.bus().unregister(this); + } catch (RuntimeException x) { + // Ignore + } + MainApp.bus().register(this); + } + + @Subscribe + public void onStatusEvent(EventAppExit event) { + if (Config.logFunctionCalls) + log.debug("EventAppExit received"); + + destroy(); + + stopSelf(); + if (Config.logFunctionCalls) + log.debug("EventAppExit finished"); + } + + public static void setNsProfile(NSProfile profile) { + nsProfile = profile; + } + + public static NSProfile getNsProfile() { + return nsProfile; + } + + public void initialize() { + dataCounter = 0; + + NSClientService.mWakeLock.acquire(); + + readPreferences(); + + if (!nsAPISecret.equals("")) + nsAPIhashCode = Hashing.sha1().hashString(nsAPISecret, Charsets.UTF_8).toString(); + + MainApp.bus().post(new EventNSClientStatus("Initializing")); + if (!nsEnabled) { + MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "disabled")); + MainApp.bus().post(new EventNSClientStatus("Disabled")); + } else if (!nsURL.equals("")) { + try { + MainApp.bus().post(new EventNSClientStatus("Connecting ...")); + IO.Options opt = new IO.Options(); + opt.forceNew = true; + opt.reconnection = true; + mSocket = IO.socket(nsURL, opt); + mSocket.on(Socket.EVENT_CONNECT, onConnect); + mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect); + mSocket.on(Socket.EVENT_PING, onPing); + MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "do connect")); + mSocket.connect(); + mSocket.on("dataUpdate", onDataUpdate); + } catch (URISyntaxException | RuntimeException e) { + MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "Wrong URL syntax")); + MainApp.bus().post(new EventNSClientStatus("Wrong URL syntax")); + } + } else { + MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "No NS URL specified")); + MainApp.bus().post(new EventNSClientStatus("Not configured")); + } + NSClientService.mWakeLock.release(); + } + + private Emitter.Listener onConnect = new Emitter.Listener() { + @Override + public void call(Object... args) { + MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "connect event. ID: " + mSocket.id())); + sendAuthMessage(new NSAuthAck()); + } + }; + + private Emitter.Listener onDisconnect = new Emitter.Listener() { + @Override + public void call(Object... args) { + MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "disconnect event")); + } + }; + + public void destroy() { + if (mSocket != null) { + MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "destroy")); + isConnected = false; + mSocket.disconnect(); + mSocket = null; + } + } + + + public void sendAuthMessage(NSAuthAck ack) { + JSONObject authMessage = new JSONObject(); + try { + authMessage.put("client", "Android_" + nsDevice); + authMessage.put("history", nsHours); + authMessage.put("status", true); // receive status + authMessage.put("pingme", true); // send mi pings to keep alive + authMessage.put("secret", nsAPIhashCode); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + MainApp.bus().post(new EventNSClientNewLog("AUTH", "requesting auth")); + mSocket.emit("authorize", authMessage, ack); + } + + @Subscribe + public void onStatusEvent(NSAuthAck ack) { + String connectionStatus = "Authenticated ("; + if (ack.read) connectionStatus += "R"; + if (ack.write) connectionStatus += "W"; + if (ack.write_treatment) connectionStatus += "T"; + connectionStatus += ')'; + isConnected = true; + MainApp.bus().post(new EventNSClientStatus(connectionStatus)); + MainApp.bus().post(new EventNSClientNewLog("AUTH", connectionStatus)); + if (!ack.write) { + MainApp.bus().post(new EventNSClientNewLog("ERROR", "Write permission not granted !!!!")); + } + if (!ack.write_treatment) { + MainApp.bus().post(new EventNSClientNewLog("ERROR", "Write treatment permission not granted !!!!")); + } + lastReception = new Date(); + } + + public void readPreferences() { + SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + nsEnabled = MainApp.getSpecificPlugin(NSClientInternalPlugin.class).isEnabled(PluginBase.GENERAL); + nsURL = SP.getString("nsclientinternal_url", ""); + nsAPISecret = SP.getString("nsclientinternal_api_secret", ""); + nsHours = SafeParse.stringToInt(SP.getString("nsclientinternal_hours", "3")); + nsDevice = SP.getString("nsclientinternal_devicename", ""); + } + + private Emitter.Listener onPing = new Emitter.Listener() { + @Override + public void call(final Object... args) { + if (Config.detailedLog) + MainApp.bus().post(new EventNSClientNewLog("PING", "received")); + // send data if there is something waiting + resend("Ping received"); + } + }; + + private Emitter.Listener onDataUpdate = new Emitter.Listener() { + @Override + public void call(final Object... args) { + lastReception = new Date(); + NSClientService.handler.post(new Runnable() { + @Override + public void run() { + PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE); + PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, + "onDataUpdate"); + wakeLock.acquire(); + try { + SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + + JSONObject data = (JSONObject) args[0]; + NSCal actualCal = new NSCal(); + boolean broadcastProfile = false; + try { + // delta means only increment/changes are comming + boolean isDelta = data.has("delta"); + boolean isFull = !isDelta; + MainApp.bus().post(new EventNSClientNewLog("DATA", "Data packet #" + dataCounter++ + (isDelta ? " delta" : " full"))); + + if (data.has("profiles")) { + JSONArray profiles = (JSONArray) data.getJSONArray("profiles"); + if (profiles.length() > 0) { + JSONObject profile = (JSONObject) profiles.get(profiles.length() - 1); + String activeProfile = NSClientService.getNsProfile() == null ? null : NSClientService.getNsProfile().getActiveProfile(); + NSProfile nsProfile = new NSProfile(profile, activeProfile); + NSClientService.setNsProfile(nsProfile); + broadcastProfile = true; + MainApp.bus().post(new EventNSClientNewLog("PROFILE", "profile received")); + } + } + + if (data.has("status")) { + JSONObject status = data.getJSONObject("status"); + NSStatus nsStatus = new NSStatus(status); + + if (!status.has("versionNum")) { + if (status.getInt("versionNum") < 900) { + MainApp.bus().post(new EventNSClientNewLog("ERROR", "Unsupported Nightscout version !!!!")); + } + } else { + nightscoutVersionName = status.getString("version"); + nightscoutVersionCode = status.getInt("versionNum"); + } + + BroadcastStatus bs = new BroadcastStatus(); + bs.handleNewStatus(nsStatus, MainApp.instance().getApplicationContext(), isDelta); + + if (NSClientService.getNsProfile() != null) { + String oldActiveProfile = NSClientService.getNsProfile().getActiveProfile(); + String receivedActiveProfile = nsStatus.getActiveProfile(); + NSClientService.getNsProfile().setActiveProfile(receivedActiveProfile); + if (receivedActiveProfile != null) { + MainApp.bus().post(new EventNSClientNewLog("PROFILE", "status activeProfile received: " + receivedActiveProfile)); + } + // Change possible nulls to "" + String oldP = oldActiveProfile == null ? "" : oldActiveProfile; + String newP = receivedActiveProfile == null ? "" : receivedActiveProfile; + if (!newP.equals(oldP)) { + broadcastProfile = true; + } + } + /* Other received data to 2016/02/10 + { + status: 'ok' + , name: env.name + , version: env.version + , versionNum: versionNum (for ver 1.2.3 contains 10203) + , serverTime: new Date().toISOString() + , apiEnabled: apiEnabled + , careportalEnabled: apiEnabled && env.settings.enable.indexOf('careportal') > -1 + , boluscalcEnabled: apiEnabled && env.settings.enable.indexOf('boluscalc') > -1 + , head: env.head + , settings: env.settings + , extendedSettings: ctx.plugins && ctx.plugins.extendedClientSettings ? ctx.plugins.extendedClientSettings(env.extendedSettings) : {} + , activeProfile ..... calculated from treatments or missing + } + */ + } else if (!isDelta) { + MainApp.bus().post(new EventNSClientNewLog("ERROR", "Unsupported Nightscout version !!!!")); + } + + // If new profile received or change detected broadcast it + if (broadcastProfile && nsProfile != null) { + BroadcastProfile bp = new BroadcastProfile(); + bp.handleNewTreatment(nsProfile, MainApp.instance().getApplicationContext(), isDelta); + MainApp.bus().post(new EventNSClientNewLog("PROFILE", "broadcasting")); + } + + if (data.has("treatments")) { + JSONArray treatments = (JSONArray) data.getJSONArray("treatments"); + JSONArray removedTreatments = new JSONArray(); + JSONArray updatedTreatments = new JSONArray(); + JSONArray addedTreatments = new JSONArray(); + BroadcastTreatment bt = new BroadcastTreatment(); + if (treatments.length() > 0) + MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + treatments.length() + " treatments")); + for (Integer index = 0; index < treatments.length(); index++) { + JSONObject jsonTreatment = treatments.getJSONObject(index); + NSTreatment treatment = new NSTreatment(jsonTreatment); + + // remove from upload queue if Ack is failing + UploadQueue.removeID(jsonTreatment); + if (treatment.getAction() == null) { + if (!isCurrent(treatment)) continue; + addedTreatments.put(jsonTreatment); + } else if (treatment.getAction().equals("update")) { + if (!isCurrent(treatment)) continue; + updatedTreatments.put(jsonTreatment); + } else if (treatment.getAction().equals("remove")) { + removedTreatments.put(jsonTreatment); + } + } + if (removedTreatments.length() > 0) { + bt.handleRemovedTreatment(removedTreatments, MainApp.instance().getApplicationContext(), isDelta); + } + if (updatedTreatments.length() > 0) { + bt.handleChangedTreatment(updatedTreatments, MainApp.instance().getApplicationContext(), isDelta); + } + if (addedTreatments.length() > 0) { + bt.handleNewTreatment(addedTreatments, MainApp.instance().getApplicationContext(), isDelta); + } + } + if (data.has("devicestatus")) { + BroadcastDeviceStatus bds = new BroadcastDeviceStatus(); + JSONArray devicestatuses = (JSONArray) data.getJSONArray("devicestatus"); + if (devicestatuses.length() > 0) { + MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + devicestatuses.length() + " devicestatuses")); + for (Integer index = 0; index < devicestatuses.length(); index++) { + JSONObject jsonStatus = devicestatuses.getJSONObject(index); + // remove from upload queue if Ack is failing + UploadQueue.removeID(jsonStatus); + } + // send only last record + bds.handleNewDeviceStatus(devicestatuses.getJSONObject(devicestatuses.length() - 1), MainApp.instance().getApplicationContext(), isDelta); + } + } + if (data.has("mbgs")) { + BroadcastMbgs bmbg = new BroadcastMbgs(); + JSONArray mbgs = (JSONArray) data.getJSONArray("mbgs"); + if (mbgs.length() > 0) + MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + mbgs.length() + " mbgs")); + for (Integer index = 0; index < mbgs.length(); index++) { + JSONObject jsonMbg = mbgs.getJSONObject(index); + // remove from upload queue if Ack is failing + UploadQueue.removeID(jsonMbg); + } + bmbg.handleNewMbg(mbgs, MainApp.instance().getApplicationContext(), isDelta); + } + if (data.has("cals")) { + BroadcastCals bc = new BroadcastCals(); + JSONArray cals = (JSONArray) data.getJSONArray("cals"); + if (cals.length() > 0) + MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + cals.length() + " cals")); + // Retreive actual calibration + for (Integer index = 0; index < cals.length(); index++) { + if (index == 0) { + actualCal.set(cals.optJSONObject(index)); + } + // remove from upload queue if Ack is failing + UploadQueue.removeID(cals.optJSONObject(index)); + } + bc.handleNewCal(cals, MainApp.instance().getApplicationContext(), isDelta); + } + if (data.has("sgvs")) { + BroadcastSgvs bs = new BroadcastSgvs(); + String units = nsProfile != null ? nsProfile.getUnits() : "mg/dl"; + JSONArray sgvs = (JSONArray) data.getJSONArray("sgvs"); + if (sgvs.length() > 0) + MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + sgvs.length() + " sgvs")); + for (Integer index = 0; index < sgvs.length(); index++) { + JSONObject jsonSgv = sgvs.getJSONObject(index); + // MainApp.bus().post(new EventNSClientNewLog("DATA", "svg " + sgvs.getJSONObject(index).toString()); + NSSgv sgv = new NSSgv(jsonSgv); + // Handle new sgv here + // remove from upload queue if Ack is failing + UploadQueue.removeID(jsonSgv); + } + bs.handleNewSgv(sgvs, MainApp.instance().getApplicationContext(), isDelta); + } + } catch (JSONException e) { + e.printStackTrace(); + } + //MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "onDataUpdate end"); + } finally { + wakeLock.release(); + } + } + + }); + } + }; + + public void dbUpdate(DbRequest dbr, NSUpdateAck ack) { + try { + if (!isConnected) return; + if (uploading) { + MainApp.bus().post(new EventNSClientNewLog("DBUPDATE", "Busy, adding to queue")); + return; + } + uploading = true; + JSONObject message = new JSONObject(); + message.put("collection", dbr.collection); + message.put("_id", dbr._id); + message.put("data", dbr.data); + mSocket.emit("dbUpdate", message, ack); + synchronized (ack) { + try { + ack.wait(timeToWaitForResponseInMs); + } catch (InterruptedException e) { + } + } + } catch (JSONException e) { + e.printStackTrace(); + return; + } + uploading = false; + } + + public void dbUpdate(DbRequest dbr) { + try { + if (!isConnected) return; + if (uploading) { + MainApp.bus().post(new EventNSClientNewLog("DBUPDATE", "Busy, adding to queue")); + return; + } + uploading = true; + JSONObject message = new JSONObject(); + message.put("collection", dbr.collection); + message.put("_id", dbr._id); + message.put("data", dbr.data); + mSocket.emit("dbUpdate", message); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + uploading = false; + } + + public void dbUpdateUnset(DbRequest dbr, NSUpdateAck ack) { + try { + if (!isConnected) return; + if (uploading) { + MainApp.bus().post(new EventNSClientNewLog("DBUPUNSET", "Busy, adding to queue")); + return; + } + uploading = true; + JSONObject message = new JSONObject(); + message.put("collection", dbr.collection); + message.put("_id", dbr._id); + message.put("data", dbr.data); + mSocket.emit("dbUpdateUnset", message, ack); + synchronized (ack) { + try { + ack.wait(timeToWaitForResponseInMs); + } catch (InterruptedException e) { + } + } + } catch (JSONException e) { + e.printStackTrace(); + return; + } + uploading = false; + } + + public void dbUpdateUnset(DbRequest dbr) { + try { + if (!isConnected) return; + if (uploading) { + MainApp.bus().post(new EventNSClientNewLog("DBUPUNSET", "Busy, adding to queue")); + return; + } + uploading = true; + JSONObject message = new JSONObject(); + message.put("collection", dbr.collection); + message.put("_id", dbr._id); + message.put("data", dbr.data); + mSocket.emit("dbUpdateUnset", message); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + uploading = false; + } + + public void dbRemove(DbRequest dbr, NSUpdateAck ack) { + try { + if (!isConnected) return; + if (uploading) { + MainApp.bus().post(new EventNSClientNewLog("DBREMOVE", "Busy, adding to queue")); + return; + } + uploading = true; + JSONObject message = new JSONObject(); + message.put("collection", dbr.collection); + message.put("_id", dbr._id); + mSocket.emit("dbRemove", message, ack); + synchronized (ack) { + try { + ack.wait(timeToWaitForResponseInMs); + } catch (InterruptedException e) { + } + } + } catch (JSONException e) { + e.printStackTrace(); + return; + } + uploading = false; + } + + public void dbRemove(DbRequest dbr) { + try { + if (!isConnected) return; + if (uploading) { + MainApp.bus().post(new EventNSClientNewLog("DBREMOVE", "Busy, adding to queue")); + return; + } + uploading = true; + JSONObject message = new JSONObject(); + message.put("collection", dbr.collection); + message.put("_id", dbr._id); + mSocket.emit("dbRemove", message); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + uploading = false; + } + + public void dbAdd(DbRequest dbr, NSAddAck ack) { + try { + if (!isConnected) return; + if (uploading) { + MainApp.bus().post(new EventNSClientNewLog("DBADD", "Busy, adding to queue")); + return; + } + uploading = true; + JSONObject message = new JSONObject(); + message.put("collection", dbr.collection); + message.put("data", dbr.data); + mSocket.emit("dbAdd", message, ack); + synchronized (ack) { + try { + ack.wait(timeToWaitForResponseInMs); + } catch (InterruptedException e) { + } + } + } catch (JSONException e) { + e.printStackTrace(); + return; + } + uploading = false; + } + + public void dbAdd(DbRequest dbr) { + try { + if (!isConnected) return; + if (uploading) { + MainApp.bus().post(new EventNSClientNewLog("DBADD", "Busy, adding to queue")); + return; + } + uploading = true; + JSONObject message = new JSONObject(); + message.put("collection", dbr.collection); + message.put("data", dbr.data); + mSocket.emit("dbAdd", message); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + uploading = false; + } + + public void doPing() { + if (!isConnected) return; + MainApp.bus().post(new EventNSClientNewLog("PING", "Sending")); + uploading = true; + JSONObject message = new JSONObject(); + try { + message.put("mills", new Date().getTime()); + } catch (JSONException e) { + e.printStackTrace(); + } + NSPingAck ack = new NSPingAck(); + mSocket.emit("nsping", message, ack); + synchronized (ack) { + try { + ack.wait(timeToWaitForResponseInMs); + } catch (InterruptedException e) { + } + } + if (ack.received) { + String connectionStatus = "Pong received"; + if (ack.auth_received) { + connectionStatus += ": "; + if (ack.read) connectionStatus += "R"; + if (ack.write) connectionStatus += "W"; + if (ack.write_treatment) connectionStatus += "T"; + } + if (!ack.read) sendAuthMessage(new NSAuthAck()); + MainApp.bus().post(new EventNSClientNewLog("AUTH ", connectionStatus)); + } else { + MainApp.bus().post(new EventNSClientNewLog("PING", "Ping lost")); + } + uploading = false; + } + + private boolean isCurrent(NSTreatment treatment) { + long now = (new Date()).getTime(); + long minPast = now - nsHours * 60L * 60 * 1000; + if (treatment.getMills() == null) { + log.debug("treatment.getMills() == null " + treatment.getData().toString()); + return false; + } + if (treatment.getMills() > minPast) return true; + return false; + } + + public void resend(final String reason) { + if (UploadQueue.queue.size() == 0) + return; + + if (!isConnected) return; + + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend started: " + reason)); + + handler.post(new Runnable() { + @Override + public void run() { + Logger log = LoggerFactory.getLogger(UploadQueue.class); + Iterator> iter = UploadQueue.queue.entrySet().iterator(); + + if (mSocket == null || !mSocket.connected()) return; + + while (iter.hasNext()) { + DbRequest dbr = iter.next().getValue(); + if (dbr.action.equals("dbAdd")) { + NSAddAck addAck = new NSAddAck(); + dbAdd(dbr, addAck); + if (addAck._id == null) { + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "No response on dbAdd")); + return; + } + if (Config.detailedLog) + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "dbAdd processed: " + dbr.data.toString())); + else + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "dbAdd processed")); + iter.remove(); + } else if (dbr.action.equals("dbRemove")) { + NSUpdateAck removeAck = new NSUpdateAck(); + dbRemove(dbr, removeAck); + if (!removeAck.result) { + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "No response on dbRemove")); + return; + } + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "dbRemove processed: " + dbr._id)); + iter.remove(); + } else if (dbr.action.equals("dbUpdate")) { + NSUpdateAck updateAck = new NSUpdateAck(); + dbUpdate(dbr, updateAck); + if (!updateAck.result) { + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "No response on dbUpdate")); + return; + } + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "dbUpdate processed: " + dbr._id)); + iter.remove(); + } else if (dbr.action.equals("dbUpdateUnset")) { + NSUpdateAck updateUnsetAck = new NSUpdateAck(); + dbUpdateUnset(dbr, updateUnsetAck); + if (!updateUnsetAck.result) { + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "No response on dbUpdateUnset")); + return; + } + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "dbUpdateUnset processed: " + dbr._id)); + iter.remove(); + } + } + MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend ended: " + reason)); + } + }); + } + + public void restart() { + destroy(); + initialize(); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSProfile/NSProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSProfile/NSProfilePlugin.java index 5f5ed9ca1d..5a4098a26a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSProfile/NSProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSProfile/NSProfilePlugin.java @@ -20,7 +20,7 @@ import info.nightscout.androidaps.events.EventNewBasalProfile; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.plugins.NSProfile.events.EventNSProfileUpdateGUI; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; /** * Created by mike on 05.08.2016. diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java index eb449b96c8..e670ac7138 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java @@ -11,11 +11,10 @@ import java.util.Arrays; import java.util.Date; import java.util.List; -import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.db.BgReading; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.Round; import info.nightscout.utils.SafeParse; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java index d443113fc4..4f33a42284 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java @@ -16,17 +16,14 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.db.TempBasal; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.ScriptReader; import info.nightscout.androidaps.data.IobTotal; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.SafeParse; public class DetermineBasalAdapterAMAJS { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java index 49d5b760b9..7bf0f169e1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java @@ -22,14 +22,13 @@ import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.TempBasalsInterface; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.ScriptReader; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateGui; import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateResultGui; import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.Profiler; import info.nightscout.utils.Round; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java index 75c68cd5cc..75f87021c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterMAJS.java @@ -18,7 +18,7 @@ import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.Loop.ScriptReader; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; public class DetermineBasalAdapterMAJS { private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterMAJS.class); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java index a3d5c3c508..b6032b9dcc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java @@ -28,12 +28,11 @@ import info.nightscout.androidaps.plugins.Loop.ScriptReader; import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateGui; import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateResultGui; import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.Profiler; import info.nightscout.utils.Round; import info.nightscout.utils.SafeParse; -import info.nightscout.utils.ToastUtils; import static info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin.checkOnlyHardLimits; import static info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin.verifyHardLimits; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/CalibrationDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/CalibrationDialog.java index 9e06bbb600..0c2b9068bf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/CalibrationDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/CalibrationDialog.java @@ -21,7 +21,7 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.GlucoseStatus; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.PlusMinusEditText; import info.nightscout.utils.XdripCalibrations; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index 890c393edc..2fc8764a9d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -43,7 +43,7 @@ import info.nightscout.androidaps.interfaces.TempBasalsInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.data.IobTotal; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 61f4279a51..9f0efee0e1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -87,7 +87,7 @@ import info.nightscout.androidaps.plugins.Overview.graphExtensions.TimeAsXAxisLa import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin; import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/QuickWizard.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/QuickWizard.java index ac7dbaa101..4f3e2a255c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/QuickWizard.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/QuickWizard.java @@ -7,12 +7,10 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import java.util.Calendar; import java.util.Date; -import java.util.GregorianCalendar; import info.nightscout.androidaps.MainApp; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java index b01abf3436..ddc0333306 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java @@ -13,7 +13,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.Round; import info.nightscout.utils.SafeParse; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java index 3264696feb..8b3659d8ca 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java @@ -15,7 +15,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.SafeParse; /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 4097efcdf9..c71f036477 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -35,7 +35,7 @@ import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommunicatorUpdateGui; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.SafeParse; import info.nightscout.utils.XdripCalibrations; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangeFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangeFragment.java index 4bfe4ca74e..f9e979ae45 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangeFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangeFragment.java @@ -34,7 +34,7 @@ import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.interfaces.FragmentBase; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.ToastUtils; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java index 38a4904b56..4e1b41906e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java @@ -36,7 +36,7 @@ import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.FragmentBase; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.ToastUtils; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 966a5e7578..e77db84e26 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -3,21 +3,14 @@ package info.nightscout.androidaps.plugins.Treatments; import android.content.SharedPreferences; import android.preference.PreferenceManager; -import com.j256.ormlite.dao.Dao; -import com.j256.ormlite.stmt.PreparedQuery; -import com.j256.ormlite.stmt.QueryBuilder; -import com.j256.ormlite.stmt.Where; import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.sql.SQLException; -import java.util.ArrayList; import java.util.Date; import java.util.List; -import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Iob; @@ -28,7 +21,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.SafeParse; /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java index e90916122f..58068f75a4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java @@ -23,7 +23,7 @@ import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.VirtualPump.events.EventVirtualPumpUpdateGui; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; /** diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index 8b24be7979..c4e348a11c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -1,21 +1,13 @@ package info.nightscout.androidaps.plugins.Wear; -import android.content.DialogInterface; import android.content.SharedPreferences; -import android.graphics.Color; import android.os.Handler; import android.os.HandlerThread; import android.preference.PreferenceManager; -import android.support.annotation.BoolRes; import android.support.annotation.NonNull; -import android.support.v7.app.AlertDialog; -import android.view.View; import com.j256.ormlite.dao.Dao; -import org.json.JSONException; -import org.json.JSONObject; - import java.sql.SQLException; import java.text.DecimalFormat; import java.util.Date; @@ -32,12 +24,10 @@ import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; -import info.nightscout.androidaps.plugins.Overview.QuickWizard; import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin; import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java index fc08941cd3..ee36823878 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java @@ -33,7 +33,7 @@ import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; import info.nightscout.androidaps.plugins.Wear.ActionStringHandler; import info.nightscout.androidaps.plugins.Wear.WearPlugin; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/persistentnotification/PersistentNotificationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/persistentnotification/PersistentNotificationPlugin.java index 532959d926..37b2d5b99d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/persistentnotification/PersistentNotificationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/persistentnotification/PersistentNotificationPlugin.java @@ -28,7 +28,7 @@ import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.data.IobTotal; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DecimalFormatter; /** diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java index a7f3f02f36..006982f4bd 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java @@ -18,14 +18,10 @@ import org.slf4j.LoggerFactory; import java.util.Date; -import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; -import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; public class KeepAliveReceiver extends BroadcastReceiver { private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class); diff --git a/app/src/main/java/info/nightscout/utils/BolusWizard.java b/app/src/main/java/info/nightscout/utils/BolusWizard.java index 80e907bb6f..c3607cc89a 100644 --- a/app/src/main/java/info/nightscout/utils/BolusWizard.java +++ b/app/src/main/java/info/nightscout/utils/BolusWizard.java @@ -6,7 +6,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.interfaces.TempBasalsInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.data.IobTotal; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; /** * Created by mike on 11.10.2016. diff --git a/app/src/main/java/info/nightscout/utils/XdripCalibrations.java b/app/src/main/java/info/nightscout/utils/XdripCalibrations.java index 338304d790..53c3bf45ec 100644 --- a/app/src/main/java/info/nightscout/utils/XdripCalibrations.java +++ b/app/src/main/java/info/nightscout/utils/XdripCalibrations.java @@ -17,7 +17,7 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; -import info.nightscout.client.data.NSProfile; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; /** * Created by mike on 10.02.2017. diff --git a/app/src/main/res/layout/nsclientinternal_fragment.xml b/app/src/main/res/layout/nsclientinternal_fragment.xml new file mode 100644 index 0000000000..27f9267dd1 --- /dev/null +++ b/app/src/main/res/layout/nsclientinternal_fragment.xml @@ -0,0 +1,156 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 21bcb13ff5..7d24240f5d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -507,4 +507,26 @@ Password for settings Unlock settings Approaching insulin daily limit + NSClient internal + NSCI + URL: + Autoscroll + Restart + Internal NSClient + Nightscout URL + Enter Nightscout URL + Hours to fetch + Enter hours to fetch from NS (max 48) + NS API secret + NS API secret + Enter NS API secret (min 12 chars) + Device name + Enter device name + It will be used for enteredBy field + Deliver now + Clear queue + Show queue + Queue: + Status: + Paused diff --git a/app/src/main/res/xml/pref_nsclientinternal.xml b/app/src/main/res/xml/pref_nsclientinternal.xml new file mode 100644 index 0000000000..e52bfa7516 --- /dev/null +++ b/app/src/main/res/xml/pref_nsclientinternal.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + \ No newline at end of file