From a96bc52e01f8c3fa32938ea3fe8bda32e8bb39cf Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 14 Jan 2017 00:37:35 +0100 Subject: [PATCH] handle temp targets data from NS --- .../androidaps/Services/DataService.java | 78 +++++++++++++++++++ .../androidaps/db/DatabaseHelper.java | 5 +- .../nightscout/androidaps/db/TempTarget.java | 4 +- .../TempTargetRangeFragment.java | 1 + 4 files changed, 85 insertions(+), 3 deletions(-) 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 7ba381f130..cdfe4344c3 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -29,6 +29,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.DanaRHistoryRecord; +import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventNewBasalProfile; @@ -45,6 +46,7 @@ import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS; import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gPlugin; import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin; import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; +import info.nightscout.androidaps.plugins.TempTargetRange.events.EventNewTempTargetRange; import info.nightscout.androidaps.receivers.DataReceiver; import info.nightscout.client.data.NSProfile; import info.nightscout.client.data.NSSgv; @@ -379,6 +381,7 @@ public class DataService extends IntentService { JSONObject trJson = new JSONObject(trstring); String _id = trJson.getString("_id"); removeTreatmentFromDb(_id); + handleRemoveTempTargetRecord(trJson); } if (bundles.containsKey("treatments")) { @@ -388,6 +391,7 @@ public class DataService extends IntentService { JSONObject trJson = jsonArray.getJSONObject(i); String _id = trJson.getString("_id"); removeTreatmentFromDb(_id); + handleRemoveTempTargetRecord(trJson); } } } catch (Exception e) { @@ -443,6 +447,7 @@ public class DataService extends IntentService { private void handleAddedTreatment(String trstring) throws JSONException, SQLException { JSONObject trJson = new JSONObject(trstring); handleDanaRHistoryRecords(trJson); // update record _id in history + handleAddChangeTempTargetRecord(trJson); if (!trJson.has("insulin") && !trJson.has("carbs")) { if (Config.logIncommingData) log.debug("ADD: Uninterested treatment: " + trstring); @@ -499,6 +504,7 @@ public class DataService extends IntentService { private void handleChangedTreatment(String trstring) throws JSONException, SQLException { JSONObject trJson = new JSONObject(trstring); handleDanaRHistoryRecords(trJson); // update record _id in history + handleAddChangeTempTargetRecord(trJson); if (!trJson.has("insulin") && !trJson.has("carbs")) { if (Config.logIncommingData) log.debug("CHANGE: Uninterested treatment: " + trstring); @@ -573,6 +579,78 @@ public class DataService extends IntentService { } } + /* + { + "_id": "58795998aa86647ba4d68ce7", + "enteredBy": "", + "eventType": "Temporary Target", + "reason": "Eating Soon", + "targetTop": 80, + "targetBottom": 80, + "duration": 120, + "created_at": "2017-01-13T22:50:00.782Z", + "carbs": null, + "insulin": null +} + */ + + public void handleAddChangeTempTargetRecord(JSONObject trJson) throws JSONException, SQLException { + if (trJson.getString("eventType").equals("Temporary Target")) { + Dao daoTempTargets = MainApp.getDbHelper().getDaoTempTargets(); + QueryBuilder queryBuilder = daoTempTargets.queryBuilder(); + Where where = queryBuilder.where(); + where.eq("_id", trJson.getString("_id")).or().eq("timeIndex", trJson.getLong("mills")); + PreparedQuery preparedQuery = queryBuilder.prepare(); + List list = daoTempTargets.query(preparedQuery); + if (list.size() == 0) { + // Record does not exists. add + TempTarget newRecord = new TempTarget(); + newRecord.timeStart = new Date(trJson.getLong("mills")); + newRecord.duration = trJson.getInt("duration"); + newRecord.low = trJson.getDouble("targetBottom"); + newRecord.high = trJson.getDouble("targetTop"); + newRecord.reason = trJson.getString("reason"); + daoTempTargets.createIfNotExists(newRecord); + if (Config.logIncommingData) + log.debug("Adding TempTarget record to database: " + newRecord.toString()); + MainApp.bus().post(new EventNewTempTargetRange()); + } else if (list.size() == 1) { + if (Config.logIncommingData) + log.debug("Updating TempTarget record in database: " + trJson.getString("_id")); + TempTarget record = list.get(0); + record.timeStart = new Date(trJson.getLong("mills")); + record.duration = trJson.getInt("duration"); + record.low = trJson.getDouble("targetBottom"); + record.high = trJson.getDouble("targetTop"); + record.reason = trJson.getString("reason"); + daoTempTargets.update(record); + MainApp.bus().post(new EventNewTempTargetRange()); + } + } + } + + public void handleRemoveTempTargetRecord(JSONObject trJson) throws JSONException, SQLException { + if (trJson.getString("eventType").equals("Temporary Target")) { + Dao daoTempTargets = MainApp.getDbHelper().getDaoTempTargets(); + QueryBuilder queryBuilder = daoTempTargets.queryBuilder(); + Where where = queryBuilder.where(); + where.eq("_id", trJson.getString("_id")).or().eq("timeIndex", trJson.getLong("mills")); + PreparedQuery preparedQuery = queryBuilder.prepare(); + List list = daoTempTargets.query(preparedQuery); + + if (list.size() == 1) { + TempTarget record = list.get(0); + if (Config.logIncommingData) + log.debug("Removing TempTarget record from database: " + record.toString()); + daoTempTargets.delete(record); + MainApp.bus().post(new EventNewTempTargetRange()); + } else { + if (Config.logIncommingData) + log.debug("TempTarget not found database: " + trJson.toString()); + } + } + } + @Nullable public static Treatment findById(String _id) { try { diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 4eda34fff0..8455579d4c 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -45,6 +45,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); + onCreate(getWritableDatabase(), getConnectionSource()); } @@ -58,7 +59,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { TableUtils.createTableIfNotExists(connectionSource, BgReading.class); TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class); } catch (SQLException e) { - log.error(DatabaseHelper.class.getName(), "Can't create database", e); + log.error("Can't create database", e); throw new RuntimeException(e); } } @@ -74,7 +75,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true); onCreate(database, connectionSource); } catch (SQLException e) { - log.error(DatabaseHelper.class.getName(), "Can't drop databases", e); + log.error("Can't drop databases", e); throw new RuntimeException(e); } } diff --git a/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java b/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java index 95637cfda4..fe218f6708 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java @@ -10,7 +10,6 @@ import java.util.Date; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.plugins.TempBasals.TempBasalsPlugin; import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin; import info.nightscout.utils.DecimalFormatter; @@ -44,6 +43,9 @@ public class TempTarget { @DatabaseField public int duration; // in minutes + @DatabaseField + public String _id; // NS _id + public Date getPlannedTimeEnd() { return new Date(timeStart.getTime() + 60 * 1_000 * duration); } 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 0ca904afc5..36916d9037 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 @@ -144,6 +144,7 @@ public class TempTargetRangeFragment extends Fragment implements FragmentBase { activity.runOnUiThread(new Runnable() { @Override public void run() { + recyclerView.swapAdapter(new RecyclerViewAdapter(tempTargetRangePlugin.getList()), false); } }); }