tempbasal ns sync
This commit is contained in:
parent
7bed883368
commit
8db1bd5ae5
9 changed files with 204 additions and 50 deletions
|
@ -8,9 +8,6 @@ import android.preference.PreferenceManager;
|
|||
import android.provider.Telephony;
|
||||
|
||||
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 org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
@ -20,16 +17,13 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import info.nightscout.androidaps.Config;
|
||||
import info.nightscout.androidaps.Constants;
|
||||
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.Treatment;
|
||||
import info.nightscout.androidaps.events.EventNewBG;
|
||||
import info.nightscout.androidaps.events.EventNewBasalProfile;
|
||||
import info.nightscout.androidaps.interfaces.InsulinInterface;
|
||||
import info.nightscout.androidaps.interfaces.PluginBase;
|
||||
|
@ -473,6 +467,7 @@ public class DataService extends IntentService {
|
|||
JSONObject trJson = new JSONObject(trstring);
|
||||
handleDanaRHistoryRecords(trJson); // update record _id in history
|
||||
handleAddChangeTempTargetRecord(trJson);
|
||||
handleAddChangeTempBasalRecord(trJson);
|
||||
if (!trJson.has("insulin") && !trJson.has("carbs")) {
|
||||
if (Config.logIncommingData)
|
||||
log.debug("ADD: Uninterested treatment: " + trstring);
|
||||
|
@ -612,7 +607,7 @@ public class DataService extends IntentService {
|
|||
if (trJson.has("eventType") && trJson.getString("eventType").equals("Temporary Target")) {
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Processing TempTarget record: " + trJson.toString());
|
||||
MainApp.getDbHelper().createFromJsonIfNotExists(trJson);
|
||||
MainApp.getDbHelper().createTemptargetFromJsonIfNotExists(trJson);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -626,6 +621,39 @@ public class DataService extends IntentService {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
{
|
||||
"_id": "59232e1ddd032d04218dab00",
|
||||
"eventType": "Temp Basal",
|
||||
"duration": 60,
|
||||
"percent": -50,
|
||||
"created_at": "2017-05-22T18:29:57Z",
|
||||
"enteredBy": "AndroidAPS",
|
||||
"notes": "Basal Temp Start 50% 60.0 min",
|
||||
"NSCLIENT_ID": 1495477797863,
|
||||
"mills": 1495477797000,
|
||||
"mgdl": 194.5,
|
||||
"endmills": 1495481397000
|
||||
}
|
||||
*/
|
||||
public void handleAddChangeTempBasalRecord(JSONObject trJson) throws JSONException, SQLException {
|
||||
if (trJson.has("eventType") && trJson.getString("eventType").equals("Temp Basal")) {
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Processing TempBasal record: " + trJson.toString());
|
||||
MainApp.getDbHelper().createTempBasalFromJsonIfNotExists(trJson);
|
||||
}
|
||||
}
|
||||
|
||||
public void handleRemoveTempBasalRecord(JSONObject trJson) {
|
||||
if (trJson.has("_id")) {
|
||||
try {
|
||||
MainApp.getDbHelper().deleteTempBasalById(trJson.getString("_id"));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleNewSMS(Intent intent) {
|
||||
Bundle bundle = intent.getExtras();
|
||||
if (bundle == null) return;
|
||||
|
|
|
@ -56,6 +56,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
|
||||
private static Long latestTreatmentChange = null;
|
||||
|
||||
private static final ScheduledExecutorService bgWorker = Executors.newSingleThreadScheduledExecutor();
|
||||
private static ScheduledFuture<?> scheduledBgPost = null;
|
||||
|
||||
private static final ScheduledExecutorService treatmentsWorker = Executors.newSingleThreadScheduledExecutor();
|
||||
private static ScheduledFuture<?> scheduledTratmentPost = null;
|
||||
|
||||
|
@ -167,6 +170,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
scheduleBgChange(); // trigger refresh
|
||||
}
|
||||
|
||||
public void resetTreatments() {
|
||||
|
@ -247,7 +251,24 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
MainApp.bus().post(new EventNewBG());
|
||||
scheduleBgChange();
|
||||
}
|
||||
|
||||
static public void scheduleBgChange() {
|
||||
class PostRunnable implements Runnable {
|
||||
public void run() {
|
||||
MainApp.bus().post(new EventNewBG());
|
||||
scheduledBgPost = null;
|
||||
}
|
||||
}
|
||||
// prepare task for execution in 1 sec
|
||||
// cancel waiting task to prevent sending multiple posts
|
||||
if (scheduledBgPost != null)
|
||||
scheduledBgPost.cancel(false);
|
||||
Runnable task = new PostRunnable();
|
||||
final int sec = 1;
|
||||
scheduledBgPost = bgWorker.schedule(task, sec, TimeUnit.SECONDS);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -578,7 +599,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public void createFromJsonIfNotExists(JSONObject trJson) {
|
||||
public void createTemptargetFromJsonIfNotExists(JSONObject trJson) {
|
||||
try {
|
||||
QueryBuilder<TempTarget, Long> queryBuilder = null;
|
||||
queryBuilder = getDaoTempTargets().queryBuilder();
|
||||
|
@ -710,10 +731,10 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
return updated;
|
||||
}
|
||||
|
||||
public void create(TemporaryBasal tempBasal) {
|
||||
public void createIfNotExists(TemporaryBasal tempBasal) {
|
||||
tempBasal.date = tempBasal.date - tempBasal.date % 1000;
|
||||
try {
|
||||
getDaoTemporaryBasal().create(tempBasal);
|
||||
getDaoTemporaryBasal().createIfNotExists(tempBasal);
|
||||
latestTreatmentChange = tempBasal.date;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -764,6 +785,77 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
|
||||
}
|
||||
|
||||
public void createTempBasalFromJsonIfNotExists(JSONObject trJson) {
|
||||
try {
|
||||
QueryBuilder<TemporaryBasal, Long> queryBuilder = null;
|
||||
queryBuilder = getDaoTemporaryBasal().queryBuilder();
|
||||
Where where = queryBuilder.where();
|
||||
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
||||
PreparedQuery<TemporaryBasal> preparedQuery = queryBuilder.prepare();
|
||||
List<TemporaryBasal> list = getDaoTemporaryBasal().query(preparedQuery);
|
||||
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
|
||||
if (profile == null) return; // no profile data, better ignore than do something wrong
|
||||
String units = profile.getUnits();
|
||||
TemporaryBasal tempBasal;
|
||||
if (list.size() == 0) {
|
||||
tempBasal = new TemporaryBasal();
|
||||
tempBasal.source = Source.NIGHTSCOUT;
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Adding TemporaryBasal record to database: " + trJson.toString());
|
||||
// Record does not exists. add
|
||||
} else if (list.size() == 1) {
|
||||
tempBasal = list.get(0);
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Updating TemporaryBasal record in database: " + trJson.toString());
|
||||
} else {
|
||||
log.error("Somthing went wrong");
|
||||
return;
|
||||
}
|
||||
tempBasal.date = trJson.getLong("mills");
|
||||
if (trJson.has("duration")) {
|
||||
tempBasal.durationInMinutes = trJson.getInt("duration");
|
||||
}
|
||||
if (trJson.has("percent")) {
|
||||
tempBasal.percentRate = trJson.getInt("percent") + 100;
|
||||
tempBasal.isAbsolute = false;
|
||||
}
|
||||
if (trJson.has("absolute")) {
|
||||
tempBasal.absoluteRate = trJson.getDouble("absolute");
|
||||
tempBasal.isAbsolute = true;
|
||||
}
|
||||
tempBasal._id = trJson.getString("_id");
|
||||
createIfNotExists(tempBasal);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteTempBasalById(String _id) {
|
||||
try {
|
||||
QueryBuilder<TemporaryBasal, Long> queryBuilder = null;
|
||||
queryBuilder = getDaoTemporaryBasal().queryBuilder();
|
||||
Where where = queryBuilder.where();
|
||||
where.eq("_id", _id);
|
||||
PreparedQuery<TemporaryBasal> preparedQuery = queryBuilder.prepare();
|
||||
List<TemporaryBasal> list = getDaoTemporaryBasal().query(preparedQuery);
|
||||
|
||||
if (list.size() == 1) {
|
||||
TemporaryBasal record = list.get(0);
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Removing TempBasal record from database: " + record.log());
|
||||
getDaoTemporaryBasal().delete(record);
|
||||
MainApp.bus().post(new EventTempTargetRangeChange());
|
||||
} else {
|
||||
if (Config.logIncommingData)
|
||||
log.debug("TempTarget not found database: " + _id);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// ------------ ExtendedBolus handling ---------------
|
||||
|
||||
public int update(ExtendedBolus extendedBolus) {
|
||||
|
|
|
@ -99,6 +99,16 @@ public class ExtendedBolus implements Interval {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInProgress() {
|
||||
return match(new Date().getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEndingEvent() {
|
||||
return durationInMinutes == 0;
|
||||
}
|
||||
|
||||
// -------- Interval interface end ---------
|
||||
|
||||
public String log() {
|
||||
|
@ -166,10 +176,6 @@ public class ExtendedBolus implements Interval {
|
|||
return (remainingMin < 0) ? 0 : Math.round(remainingMin);
|
||||
}
|
||||
|
||||
public boolean isInProgress() {
|
||||
return match(new Date().getTime());
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "E " + DecimalFormatter.to2Decimal(absoluteRate()) + "U/h @" +
|
||||
DateUtil.timeString(date) +
|
||||
|
|
|
@ -88,6 +88,16 @@ public class TempTarget implements Interval {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInProgress() {
|
||||
return match(new Date().getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEndingEvent() {
|
||||
return durationInMinutes == 0;
|
||||
}
|
||||
|
||||
// -------- Interval interface end ---------
|
||||
|
||||
public String lowValueToUnitsToString(String units) {
|
||||
|
@ -100,10 +110,6 @@ public class TempTarget implements Interval {
|
|||
else return DecimalFormatter.to1Decimal(low * Constants.MGDL_TO_MMOLL);
|
||||
}
|
||||
|
||||
public boolean isInProgress() {
|
||||
return match(new Date().getTime());
|
||||
}
|
||||
|
||||
public String log() {
|
||||
return "TemporaryTarget{" +
|
||||
"date=" + date +
|
||||
|
|
|
@ -103,6 +103,16 @@ public class TemporaryBasal implements Interval {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInProgress() {
|
||||
return match(new Date().getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEndingEvent() {
|
||||
return durationInMinutes == 0;
|
||||
}
|
||||
|
||||
// -------- Interval interface end ---------
|
||||
|
||||
public IobTotal iobCalc(long time) {
|
||||
|
@ -175,10 +185,6 @@ public class TemporaryBasal implements Interval {
|
|||
return (remainingMin < 0) ? 0 : Math.round(remainingMin);
|
||||
}
|
||||
|
||||
public boolean isInProgress() {
|
||||
return match(new Date().getTime());
|
||||
}
|
||||
|
||||
public double tempBasalConvertedToAbsolute(long time) {
|
||||
if (isAbsolute) return absoluteRate;
|
||||
else {
|
||||
|
|
|
@ -18,4 +18,7 @@ public interface Interval {
|
|||
boolean match(long time);
|
||||
boolean before(long time);
|
||||
boolean after(long time);
|
||||
|
||||
boolean isInProgress();
|
||||
boolean isEndingEvent();
|
||||
}
|
|
@ -368,7 +368,7 @@ public class TreatmentsFromHistoryPlugin implements PluginBase, TreatmentsInterf
|
|||
|
||||
@Override
|
||||
public void tempBasalStart(TemporaryBasal tempBasal) {
|
||||
MainApp.getDbHelper().create(tempBasal);
|
||||
MainApp.getDbHelper().createIfNotExists(tempBasal);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -376,7 +376,7 @@ public class TreatmentsFromHistoryPlugin implements PluginBase, TreatmentsInterf
|
|||
TemporaryBasal temporaryBasal = new TemporaryBasal();
|
||||
temporaryBasal.date = time;
|
||||
temporaryBasal.durationInMinutes = 0;
|
||||
MainApp.getDbHelper().create(temporaryBasal);
|
||||
MainApp.getDbHelper().createIfNotExists(temporaryBasal);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -63,34 +63,46 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment {
|
|||
@Override
|
||||
public void onBindViewHolder(TempBasalsViewHolder holder, int position) {
|
||||
TemporaryBasal tempBasal = tempBasalList.getReversed(position);
|
||||
if (tempBasal.isInProgress()) {
|
||||
if (tempBasal.isEndingEvent()) {
|
||||
holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date));
|
||||
} else {
|
||||
holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date) + " - " + DateUtil.timeString(tempBasalList.get(position).end()));
|
||||
}
|
||||
holder.duration.setText(DecimalFormatter.to0Decimal(tempBasal.getRealDuration()) + " min");
|
||||
if (tempBasal.isAbsolute) {
|
||||
holder.absolute.setText(DecimalFormatter.to0Decimal(tempBasal.tempBasalConvertedToAbsolute(tempBasal.date)) + " U/h");
|
||||
holder.percent.setText("");
|
||||
} else {
|
||||
holder.duration.setText(MainApp.sResources.getString(R.string.stopevent));
|
||||
holder.absolute.setText("");
|
||||
holder.percent.setText(DecimalFormatter.to0Decimal(tempBasal.percentRate) + "%");
|
||||
holder.percent.setText("");
|
||||
holder.realDuration.setText("");
|
||||
holder.iob.setText("");
|
||||
holder.netInsulin.setText("");
|
||||
holder.netRatio.setText("");
|
||||
holder.extendedFlag.setVisibility(View.GONE);
|
||||
} else {
|
||||
if (tempBasal.isInProgress()) {
|
||||
holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date));
|
||||
} else {
|
||||
holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date) + " - " + DateUtil.timeString(tempBasal.end()));
|
||||
}
|
||||
holder.duration.setText(DecimalFormatter.to0Decimal(tempBasal.durationInMinutes) + " min");
|
||||
if (tempBasal.isAbsolute) {
|
||||
holder.absolute.setText(DecimalFormatter.to0Decimal(tempBasal.tempBasalConvertedToAbsolute(tempBasal.date)) + " U/h");
|
||||
holder.percent.setText("");
|
||||
} else {
|
||||
holder.absolute.setText("");
|
||||
holder.percent.setText(DecimalFormatter.to0Decimal(tempBasal.percentRate) + "%");
|
||||
}
|
||||
holder.realDuration.setText(DecimalFormatter.to0Decimal(tempBasal.getRealDuration()) + " min");
|
||||
IobTotal iob = tempBasal.iobCalc(new Date().getTime());
|
||||
holder.iob.setText(DecimalFormatter.to2Decimal(iob.basaliob) + " U");
|
||||
holder.netInsulin.setText(DecimalFormatter.to2Decimal(iob.netInsulin) + " U");
|
||||
holder.netRatio.setText(DecimalFormatter.to2Decimal(iob.netRatio) + " U/h");
|
||||
//holder.extendedFlag.setVisibility(tempBasal.isExtended ? View.VISIBLE : View.GONE);
|
||||
holder.extendedFlag.setVisibility(View.GONE);
|
||||
if (tempBasal.isInProgress())
|
||||
holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
|
||||
else
|
||||
holder.date.setTextColor(holder.netRatio.getCurrentTextColor());
|
||||
if (tempBasal.iobCalc(new Date().getTime()).basaliob != 0)
|
||||
holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
|
||||
else
|
||||
holder.iob.setTextColor(holder.netRatio.getCurrentTextColor());
|
||||
}
|
||||
holder.realDuration.setText(DecimalFormatter.to0Decimal(tempBasal.getRealDuration()) + " min");
|
||||
IobTotal iob = tempBasal.iobCalc(new Date().getTime());
|
||||
holder.iob.setText(DecimalFormatter.to2Decimal(iob.basaliob) + " U");
|
||||
holder.netInsulin.setText(DecimalFormatter.to2Decimal(iob.netInsulin) + " U");
|
||||
holder.netRatio.setText(DecimalFormatter.to2Decimal(iob.netRatio) + " U/h");
|
||||
//holder.extendedFlag.setVisibility(tempBasal.isExtended ? View.VISIBLE : View.GONE);
|
||||
holder.extendedFlag.setVisibility(View.GONE);
|
||||
if (tempBasal.isInProgress())
|
||||
holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
|
||||
else
|
||||
holder.date.setTextColor(holder.netRatio.getCurrentTextColor());
|
||||
if (tempBasal.iobCalc(new Date().getTime()).basaliob != 0)
|
||||
holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
|
||||
else
|
||||
holder.date.setTextColor(holder.netRatio.getCurrentTextColor());
|
||||
holder.remove.setTag(tempBasal);
|
||||
}
|
||||
|
||||
|
|
|
@ -613,4 +613,5 @@
|
|||
<string name="key_ns_noupload" translatable="false">ns_noupload</string>
|
||||
<string name="basal_step">Basal Step</string>
|
||||
<string name="bolus_step">Bolus Step</string>
|
||||
<string name="stopevent">STOP</string>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue