initialize treatment data first by another message

This commit is contained in:
Milos Kozak 2017-05-29 11:39:12 +02:00
parent fa695b9399
commit 442ff12aa1
7 changed files with 113 additions and 158 deletions

View file

@ -7,15 +7,12 @@ import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Telephony;
import com.j256.ormlite.dao.Dao;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.Date;
import info.nightscout.androidaps.Config;
@ -24,20 +21,19 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.interfaces.InsulinInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.History.DanaRNSHistorySync;
import info.nightscout.androidaps.plugins.InsulinFastacting.InsulinFastactingFragment;
import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.OverviewPlugin;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.History.DanaRNSHistorySync;
import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorPlugin;
import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS;
import info.nightscout.androidaps.plugins.SourceGlimp.SourceGlimpPlugin;
@ -45,8 +41,6 @@ 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.receivers.DataReceiver;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
import info.nightscout.utils.SP;
@ -358,7 +352,7 @@ public class DataService extends IntentService {
try {
if (bundles.containsKey("treatment")) {
String trstring = bundles.getString("treatment");
handleAddedTreatment(trstring);
handleAddChangeDataFromNS(trstring);
}
if (bundles.containsKey("treatments")) {
String trstring = bundles.getString("treatments");
@ -366,7 +360,7 @@ public class DataService extends IntentService {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject trJson = jsonArray.getJSONObject(i);
String trstr = trJson.toString();
handleAddedTreatment(trstr);
handleAddChangeDataFromNS(trstr);
}
}
} catch (Exception e) {
@ -379,7 +373,7 @@ public class DataService extends IntentService {
try {
if (bundles.containsKey("treatment")) {
String trstring = bundles.getString("treatment");
handleChangedTreatment(trstring);
handleAddChangeDataFromNS(trstring);
}
if (bundles.containsKey("treatments")) {
String trstring = bundles.getString("treatments");
@ -387,7 +381,7 @@ public class DataService extends IntentService {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject trJson = jsonArray.getJSONObject(i);
String trstr = trJson.toString();
handleChangedTreatment(trstr);
handleAddChangeDataFromNS(trstr);
}
}
} catch (Exception e) {
@ -401,11 +395,7 @@ public class DataService extends IntentService {
String trstring = bundles.getString("treatment");
JSONObject trJson = new JSONObject(trstring);
String _id = trJson.getString("_id");
MainApp.getDbHelper().deleteTreatmentById(_id);
MainApp.getDbHelper().deleteTempTargetById(trJson.getString("_id"));
MainApp.getDbHelper().deleteTempBasalById(trJson.getString("_id"));
MainApp.getDbHelper().deleteExtendedBolusById(trJson.getString("_id"));
MainApp.getDbHelper().deleteCareportalEventById(trJson.getString("_id"));
handleRemovedRecordFromNS(_id);
}
if (bundles.containsKey("treatments")) {
@ -414,11 +404,7 @@ public class DataService extends IntentService {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject trJson = jsonArray.getJSONObject(i);
String _id = trJson.getString("_id");
MainApp.getDbHelper().deleteTreatmentById(_id);
MainApp.getDbHelper().deleteTempTargetById(trJson.getString("_id"));
MainApp.getDbHelper().deleteTempBasalById(trJson.getString("_id"));
MainApp.getDbHelper().deleteExtendedBolusById(trJson.getString("_id"));
MainApp.getDbHelper().deleteCareportalEventById(trJson.getString("_id"));
handleRemovedRecordFromNS(_id);
}
}
} catch (Exception e) {
@ -470,128 +456,22 @@ 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);
handleAddChangeTempBasalRecord(trJson);
handleAddChangeExtendedBolusRecord(trJson);
handleAddChangeCareportalEventRecord(trJson);
if (!trJson.has("insulin") && !trJson.has("carbs")) {
if (Config.logIncommingData)
log.debug("Ignoring non insulin/carbs record: " + trstring);
return;
}
Treatment stored = null;
String _id = trJson.getString("_id");
if (trJson.has("date")) {
if (Config.logIncommingData)
log.debug("ADD: date found: " + trstring);
stored = MainApp.getDbHelper().findTreatmentByTimeIndex(trJson.getLong("date"));
} else {
stored = MainApp.getDbHelper().findTreatmentById(_id);
}
if (stored != null) {
if (Config.logIncommingData)
log.debug("ADD: Existing treatment: " + trstring);
if (trJson.has("date")) {
stored._id = _id;
int updated = MainApp.getDbHelper().update(stored);
if (Config.logIncommingData)
log.debug("Records updated: " + updated);
}
} else {
if (Config.logIncommingData)
log.debug("ADD: New treatment: " + trstring);
InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin();
if (insulinInterface == null) insulinInterface = InsulinFastactingFragment.getPlugin();
Treatment treatment = new Treatment(insulinInterface);
treatment._id = _id;
treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0;
treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d;
treatment.date = trJson.getLong("mills");
if (trJson.has("eventType")) {
treatment.mealBolus = true;
if (trJson.get("eventType").equals("Correction Bolus"))
treatment.mealBolus = false;
double carbs = treatment.carbs;
if (trJson.has("boluscalc")) {
JSONObject boluscalc = trJson.getJSONObject("boluscalc");
if (boluscalc.has("carbs")) {
carbs = Math.max(boluscalc.getDouble("carbs"), carbs);
}
}
if (carbs <= 0)
treatment.mealBolus = false;
}
MainApp.getDbHelper().createOrUpdate(treatment);
if (Config.logIncommingData)
log.debug("ADD: Stored treatment: " + treatment.log());
}
private void handleRemovedRecordFromNS(String _id) {
MainApp.getDbHelper().deleteTreatmentById(_id);
MainApp.getDbHelper().deleteTempTargetById(_id);
MainApp.getDbHelper().deleteTempBasalById(_id);
MainApp.getDbHelper().deleteExtendedBolusById(_id);
MainApp.getDbHelper().deleteCareportalEventById(_id);
}
private void handleChangedTreatment(String trstring) throws JSONException, SQLException {
private void handleAddChangeDataFromNS(String trstring) throws JSONException {
JSONObject trJson = new JSONObject(trstring);
handleDanaRHistoryRecords(trJson); // update record _id in history
handleAddChangeTempTargetRecord(trJson);
handleAddChangeTempBasalRecord(trJson);
handleAddChangeExtendedBolusRecord(trJson);
handleAddChangeCareportalEventRecord(trJson);
if (!trJson.has("insulin") && !trJson.has("carbs")) {
if (Config.logIncommingData)
log.debug("CHANGE: Uninterested treatment: " + trstring);
return;
}
String _id = trJson.getString("_id");
Treatment stored;
if (trJson.has("date")) {
if (Config.logIncommingData)
log.debug("ADD: date found: " + trstring);
stored = MainApp.getDbHelper().findTreatmentByTimeIndex(trJson.getLong("date"));
} else {
stored = MainApp.getDbHelper().findTreatmentById(_id);
}
if (stored != null) {
if (Config.logIncommingData)
log.debug("CHANGE: Removing old: " + trstring);
MainApp.getDbHelper().deleteTreatmentById(_id);
}
if (Config.logIncommingData)
log.debug("CHANGE: Adding new treatment: " + trstring);
InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin();
if (insulinInterface == null) insulinInterface = InsulinFastactingFragment.getPlugin();
Treatment treatment = new Treatment(insulinInterface);
treatment._id = _id;
treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0;
treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d;
//treatment.created_at = DateUtil.fromISODateString(trJson.getString("created_at"));
treatment.date = trJson.getLong("mills");
if (trJson.has("eventType")) {
treatment.mealBolus = true;
if (trJson.get("eventType").equals("Correction Bolus"))
treatment.mealBolus = false;
double carbs = treatment.carbs;
if (trJson.has("boluscalc")) {
JSONObject boluscalc = trJson.getJSONObject("boluscalc");
if (boluscalc.has("carbs")) {
carbs = Math.max(boluscalc.getDouble("carbs"), carbs);
}
}
if (carbs <= 0)
treatment.mealBolus = false;
}
Dao.CreateOrUpdateStatus status = MainApp.getDbHelper().createOrUpdate(treatment);
if (Config.logIncommingData)
log.debug("Records updated: " + status.getNumLinesChanged());
if (Config.logIncommingData)
log.debug("CHANGE: Stored treatment: " + treatment.log());
handleAddChangeTreatmentRecord(trJson);
}
public void handleDanaRHistoryRecords(JSONObject trJson) {
@ -600,7 +480,16 @@ public class DataService extends IntentService {
}
}
public void handleAddChangeTempTargetRecord(JSONObject trJson) throws JSONException, SQLException {
public void handleAddChangeTreatmentRecord(JSONObject trJson) throws JSONException {
if (trJson.has("insulin") || trJson.has("carbs")) {
if (Config.logIncommingData)
log.debug("Processing Treatment record: " + trJson.toString());
MainApp.getDbHelper().createTreatmentFromJsonIfNotExists(trJson);
return;
}
}
public void handleAddChangeTempTargetRecord(JSONObject trJson) throws JSONException {
if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.TEMPORARYTARGET)) {
if (Config.logIncommingData)
log.debug("Processing TempTarget record: " + trJson.toString());
@ -608,7 +497,7 @@ public class DataService extends IntentService {
}
}
public void handleAddChangeTempBasalRecord(JSONObject trJson) throws JSONException, SQLException {
public void handleAddChangeTempBasalRecord(JSONObject trJson) throws JSONException {
if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.TEMPBASAL)) {
if (Config.logIncommingData)
log.debug("Processing TempBasal record: " + trJson.toString());
@ -616,7 +505,7 @@ public class DataService extends IntentService {
}
}
public void handleAddChangeExtendedBolusRecord(JSONObject trJson) throws JSONException, SQLException {
public void handleAddChangeExtendedBolusRecord(JSONObject trJson) throws JSONException {
if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.COMBOBOLUS)) {
if (Config.logIncommingData)
log.debug("Processing Extended Bolus record: " + trJson.toString());
@ -624,7 +513,7 @@ public class DataService extends IntentService {
}
}
public void handleAddChangeCareportalEventRecord(JSONObject trJson) throws JSONException, SQLException {
public void handleAddChangeCareportalEventRecord(JSONObject trJson) throws JSONException {
if (trJson.has("eventType") && (
trJson.getString("eventType").equals(CareportalEvent.SITECHANGE) ||
trJson.getString("eventType").equals(CareportalEvent.INSULINCHANGE) ||

View file

@ -34,6 +34,8 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.events.EventCareportalEventChange;
import info.nightscout.androidaps.events.EventExtendedBolusChange;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventReloadTempBasalData;
import info.nightscout.androidaps.events.EventReloadTreatmentData;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.events.EventTempTargetChange;
import info.nightscout.androidaps.events.EventTreatmentChange;
@ -562,6 +564,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
static public void scheduleTreatmentChange() {
class PostRunnable implements Runnable {
public void run() {
MainApp.bus().post(new EventReloadTreatmentData());
MainApp.bus().post(new EventTreatmentChange());
if (latestTreatmentChange != null)
MainApp.bus().post(new EventNewHistoryData(latestTreatmentChange));
@ -596,6 +599,56 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return new ArrayList<Treatment>();
}
public void createTreatmentFromJsonIfNotExists(JSONObject trJson) {
try {
QueryBuilder<Treatment, Long> queryBuilder = null;
queryBuilder = getDaoTreatments().queryBuilder();
Where where = queryBuilder.where();
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
List<Treatment> list = getDaoTreatments().query(preparedQuery);
Treatment treatment;
if (list.size() == 0) {
treatment = new Treatment();
treatment.source = Source.NIGHTSCOUT;
if (Config.logIncommingData)
log.debug("Adding Treatment record to database: " + trJson.toString());
// Record does not exists. add
} else if (list.size() == 1) {
treatment = list.get(0);
if (Config.logIncommingData)
log.debug("Updating Treatment record in database: " + trJson.toString());
} else {
log.error("Something went wrong");
return;
}
treatment.date = trJson.getLong("mills");
treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0;
treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d;
treatment._id = trJson.getString("_id");
if (trJson.has("eventType")) {
treatment.mealBolus = true;
if (trJson.get("eventType").equals("Correction Bolus"))
treatment.mealBolus = false;
double carbs = treatment.carbs;
if (trJson.has("boluscalc")) {
JSONObject boluscalc = trJson.getJSONObject("boluscalc");
if (boluscalc.has("carbs")) {
carbs = Math.max(boluscalc.getDouble("carbs"), carbs);
}
}
if (carbs <= 0)
treatment.mealBolus = false;
}
createOrUpdate(treatment);
scheduleTreatmentChange();
} catch (SQLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
// ---------------- TempTargets handling ---------------
public List<TempTarget> getTemptargetsDataFromTime(long mills, boolean ascending) {
@ -835,6 +888,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
static public void scheduleTemporaryBasalChange() {
class PostRunnable implements Runnable {
public void run() {
MainApp.bus().post(new EventReloadTempBasalData());
MainApp.bus().post(new EventTempBasalChange());
scheduledTemBasalsPost = null;
}
@ -1123,6 +1177,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
static public void scheduleExtendedBolusChange() {
class PostRunnable implements Runnable {
public void run() {
MainApp.bus().post(new EventReloadTreatmentData());
MainApp.bus().post(new EventExtendedBolusChange());
scheduledExtendedBolusPost = null;
log.debug("Firing EventExtendedBolusChange");

View file

@ -0,0 +1,8 @@
package info.nightscout.androidaps.events;
/**
* Created by mike on 29.05.2017.
*/
public class EventReloadTempBasalData {
}

View file

@ -0,0 +1,8 @@
package info.nightscout.androidaps.events;
/**
* Created by mike on 29.05.2017.
*/
public class EventReloadTreatmentData {
}

View file

@ -565,7 +565,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
log.debug("setExtendedBolus rate: " + rateAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
NSUpload.uploadExtendedBolus(result.bolusDelivered, result.duration);
MainApp.bus().post(new EventTreatmentChange());
}
return result;
}

View file

@ -57,7 +57,6 @@ import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientS
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.androidaps.plugins.PumpDanaR.Services.ExecutionService;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.SP;
import io.socket.client.IO;

View file

@ -22,9 +22,9 @@ import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventExtendedBolusChange;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.events.EventReloadTempBasalData;
import info.nightscout.androidaps.events.EventReloadTreatmentData;
import info.nightscout.androidaps.events.EventTempTargetChange;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
@ -274,23 +274,20 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
}
@Subscribe
public void onStatusEvent(final EventTreatmentChange ev) {
public void onStatusEvent(final EventReloadTreatmentData ev) {
log.debug("EventReloadTreatmentData");
initializeTreatmentData();
updateTotalIOBTreatments();
}
@Subscribe
public void onStatusEvent(final EventTempBasalChange ev) {
initializeTempBasalData();
updateTotalIOBTempBasals();
}
@Subscribe
public void onStatusEvent(final EventExtendedBolusChange ev) {
initializeExtendedBolusData();
updateTotalIOBTreatments();
}
@Subscribe
public void onStatusEvent(final EventReloadTempBasalData ev) {
log.debug("EventReloadTempBasalData");
initializeTempBasalData();
updateTotalIOBTempBasals();
}
@Override
public IobTotal getLastCalculationTempBasals() {
return lastTempBasalsCalculation;