treatments synchronization improvement
This commit is contained in:
parent
5931abc860
commit
8fab76ee9b
15 changed files with 204 additions and 146 deletions
|
@ -456,8 +456,6 @@ public class DataService extends IntentService {
|
||||||
|
|
||||||
public void handleAddChangeTreatmentRecord(JSONObject trJson) throws JSONException {
|
public void handleAddChangeTreatmentRecord(JSONObject trJson) throws JSONException {
|
||||||
if (trJson.has("insulin") || trJson.has("carbs")) {
|
if (trJson.has("insulin") || trJson.has("carbs")) {
|
||||||
if (Config.logIncommingData)
|
|
||||||
log.debug("Processing Treatment record: " + trJson.toString());
|
|
||||||
MainApp.getDbHelper().createTreatmentFromJsonIfNotExists(trJson);
|
MainApp.getDbHelper().createTreatmentFromJsonIfNotExists(trJson);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,4 +28,6 @@ public class DetailedBolusInfo {
|
||||||
public JSONObject boluscalc = null; // additional bolus wizard info
|
public JSONObject boluscalc = null; // additional bolus wizard info
|
||||||
public Context context = null; // context for progress dialog
|
public Context context = null; // context for progress dialog
|
||||||
public boolean addToTreatments = true;
|
public boolean addToTreatments = true;
|
||||||
|
public boolean recordFromHistory = false; // true if record is comming from pump history (not a newly created treatment)
|
||||||
|
public long pumpId = 0; // id of record if comming from pump history (not a newly created treatment)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import info.nightscout.androidaps.Constants;
|
import info.nightscout.androidaps.Constants;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
|
@ -133,13 +134,7 @@ public class BgReading implements DataPointWithLabelInterface {
|
||||||
return false;
|
return false;
|
||||||
if (!direction.equals(other.direction))
|
if (!direction.equals(other.direction))
|
||||||
return false;
|
return false;
|
||||||
if (_id == null && other._id != null)
|
if (!Objects.equals(_id, other._id))
|
||||||
return false;
|
|
||||||
else if (_id != null && other._id == null)
|
|
||||||
return false;
|
|
||||||
else if (_id == null && other._id == null)
|
|
||||||
;
|
|
||||||
else if (!_id.equals(other._id))
|
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -492,28 +492,84 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
|
|
||||||
// -------------------- TREATMENT HANDLING -------------------
|
// -------------------- TREATMENT HANDLING -------------------
|
||||||
|
|
||||||
private boolean changeAffectingIobCob(Treatment t) {
|
// return true if new record is created
|
||||||
Treatment existing = findTreatmentByTime(t.date);
|
public boolean createOrUpdate(Treatment treatment) {
|
||||||
if (existing == null)
|
|
||||||
return true;
|
|
||||||
if (existing.insulin == t.insulin && existing.carbs == t.carbs)
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Dao.CreateOrUpdateStatus createOrUpdate(Treatment treatment) {
|
|
||||||
treatment.date = treatment.date - treatment.date % 1000;
|
|
||||||
Dao.CreateOrUpdateStatus status = null;
|
|
||||||
try {
|
try {
|
||||||
boolean historyChange = changeAffectingIobCob(treatment);
|
Treatment old;
|
||||||
status = getDaoTreatments().createOrUpdate(treatment);
|
treatment.date = roundDateToSec(treatment.date);
|
||||||
if (historyChange)
|
|
||||||
|
if (treatment.source == Source.PUMP) {
|
||||||
|
// check for changed from pump change in NS
|
||||||
|
QueryBuilder<Treatment, Long> queryBuilder = getDaoTreatments().queryBuilder();
|
||||||
|
Where where = queryBuilder.where();
|
||||||
|
where.eq("pumpId", treatment.pumpId);
|
||||||
|
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
|
||||||
|
List<Treatment> trList = getDaoTreatments().query(preparedQuery);
|
||||||
|
if (trList.size() > 0) {
|
||||||
|
// do nothing, pump history record cannot be changed
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
getDaoTreatments().create(treatment);
|
||||||
|
log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString());
|
||||||
updateEarliestDataChange(treatment.date);
|
updateEarliestDataChange(treatment.date);
|
||||||
|
scheduleTreatmentChange();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (treatment.source == Source.NIGHTSCOUT) {
|
||||||
|
old = getDaoTreatments().queryForId(treatment.date);
|
||||||
|
if (old != null) {
|
||||||
|
if (!old.isEqual(treatment)) {
|
||||||
|
boolean historyChange = old.isDataChanging(treatment);
|
||||||
|
long oldDate = old.date;
|
||||||
|
getDaoTreatments().delete(old); // need to delete/create because date may change too
|
||||||
|
old.copyFrom(treatment);
|
||||||
|
getDaoTreatments().create(old);
|
||||||
|
log.debug("TREATMENT: Updating record by date from: " + Source.getString(treatment.source) + " " + old.toString());
|
||||||
|
if (historyChange) {
|
||||||
|
updateEarliestDataChange(oldDate);
|
||||||
|
updateEarliestDataChange(old.date);
|
||||||
|
scheduleTreatmentChange();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// find by NS _id
|
||||||
|
if (treatment._id != null) {
|
||||||
|
QueryBuilder<Treatment, Long> queryBuilder = getDaoTreatments().queryBuilder();
|
||||||
|
Where where = queryBuilder.where();
|
||||||
|
where.eq("_id", treatment._id);
|
||||||
|
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
|
||||||
|
List<Treatment> trList = getDaoTreatments().query(preparedQuery);
|
||||||
|
if (trList.size() > 0) {
|
||||||
|
old = trList.get(0);
|
||||||
|
if (!old.isEqual(treatment)) {
|
||||||
|
boolean historyChange = old.isDataChanging(treatment);
|
||||||
|
long oldDate = old.date;
|
||||||
|
getDaoTreatments().delete(old); // need to delete/create because date may change too
|
||||||
|
old.copyFrom(treatment);
|
||||||
|
getDaoTreatments().create(old);
|
||||||
|
log.debug("TREATMENT: Updating record by _id from: " + Source.getString(treatment.source) + " " + old.toString());
|
||||||
|
if (historyChange) {
|
||||||
|
updateEarliestDataChange(oldDate);
|
||||||
|
updateEarliestDataChange(old.date);
|
||||||
|
scheduleTreatmentChange();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (treatment.source == Source.USER) {
|
||||||
|
getDaoTreatments().create(treatment);
|
||||||
|
updateEarliestDataChange(treatment.date);
|
||||||
|
scheduleTreatmentChange();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
scheduleTreatmentChange();
|
return false;
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(Treatment treatment) {
|
public void delete(Treatment treatment) {
|
||||||
|
@ -529,10 +585,10 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
public void deleteTreatmentById(String _id) {
|
public void deleteTreatmentById(String _id) {
|
||||||
Treatment stored = findTreatmentById(_id);
|
Treatment stored = findTreatmentById(_id);
|
||||||
if (stored != null) {
|
if (stored != null) {
|
||||||
log.debug("Removing TempTarget record from database: " + stored.log());
|
log.debug("TREATMENT: Removing Treatment record from database: " + stored.toString());
|
||||||
delete(stored);
|
delete(stored);
|
||||||
} else {
|
updateEarliestDataChange(stored.date);
|
||||||
log.debug("Treatment not found database: " + _id);
|
scheduleTreatmentChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -559,30 +615,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public Treatment findTreatmentByTime(Long timeIndex) {
|
|
||||||
try {
|
|
||||||
QueryBuilder<Treatment, String> qb = null;
|
|
||||||
Dao<Treatment, Long> daoTreatments = getDaoTreatments();
|
|
||||||
QueryBuilder<Treatment, Long> queryBuilder = daoTreatments.queryBuilder();
|
|
||||||
Where where = queryBuilder.where();
|
|
||||||
where.eq("date", timeIndex);
|
|
||||||
queryBuilder.limit(10L);
|
|
||||||
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
|
|
||||||
List<Treatment> trList = daoTreatments.query(preparedQuery);
|
|
||||||
if (trList.size() != 1) {
|
|
||||||
//log.debug("Treatment findTreatmentByTime query size: " + trList.size());
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
//log.debug("Treatment findTreatmentByTime found: " + trList.get(0).log());
|
|
||||||
return trList.get(0);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateEarliestDataChange(long newDate) {
|
private void updateEarliestDataChange(long newDate) {
|
||||||
if (earliestDataChange == null) {
|
if (earliestDataChange == null) {
|
||||||
earliestDataChange = newDate;
|
earliestDataChange = newDate;
|
||||||
|
@ -634,30 +666,13 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
|
|
||||||
public void createTreatmentFromJsonIfNotExists(JSONObject trJson) {
|
public void createTreatmentFromJsonIfNotExists(JSONObject trJson) {
|
||||||
try {
|
try {
|
||||||
QueryBuilder<Treatment, Long> queryBuilder = null;
|
Treatment treatment = new Treatment();
|
||||||
queryBuilder = getDaoTreatments().queryBuilder();
|
;
|
||||||
Where where = queryBuilder.where();
|
treatment.source = Source.NIGHTSCOUT;
|
||||||
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
treatment.date = roundDateToSec(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.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0;
|
||||||
treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d;
|
treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d;
|
||||||
|
treatment.pumpId = trJson.has("pumpId") ? trJson.getLong("pumpId") : 0;
|
||||||
treatment._id = trJson.getString("_id");
|
treatment._id = trJson.getString("_id");
|
||||||
if (trJson.has("eventType")) {
|
if (trJson.has("eventType")) {
|
||||||
treatment.mealBolus = !trJson.get("eventType").equals("Correction Bolus");
|
treatment.mealBolus = !trJson.get("eventType").equals("Correction Bolus");
|
||||||
|
@ -672,7 +687,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
treatment.mealBolus = false;
|
treatment.mealBolus = false;
|
||||||
}
|
}
|
||||||
createOrUpdate(treatment);
|
createOrUpdate(treatment);
|
||||||
} catch (SQLException | JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,19 @@ package info.nightscout.androidaps.db;
|
||||||
|
|
||||||
public class Source {
|
public class Source {
|
||||||
public final static int NONE = 0;
|
public final static int NONE = 0;
|
||||||
public final static int PUMP = 1;
|
public final static int PUMP = 1; // Pump history
|
||||||
public final static int NIGHTSCOUT = 2;
|
public final static int NIGHTSCOUT = 2; // created in NS
|
||||||
|
public final static int USER = 3; // created by user or driver not using history
|
||||||
|
|
||||||
|
public static String getString(int source) {
|
||||||
|
switch (source) {
|
||||||
|
case PUMP:
|
||||||
|
return "PUMP";
|
||||||
|
case NIGHTSCOUT:
|
||||||
|
return "NIGHTSCOUT";
|
||||||
|
case USER:
|
||||||
|
return "USER";
|
||||||
|
}
|
||||||
|
return "NONE";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import info.nightscout.androidaps.Constants;
|
import info.nightscout.androidaps.Constants;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
|
@ -32,15 +33,18 @@ public class Treatment implements DataPointWithLabelInterface {
|
||||||
@DatabaseField
|
@DatabaseField
|
||||||
public boolean isValid = true;
|
public boolean isValid = true;
|
||||||
|
|
||||||
|
@DatabaseField(index = true)
|
||||||
|
public long pumpId = 0;
|
||||||
|
|
||||||
@DatabaseField
|
@DatabaseField
|
||||||
public int source = Source.NONE;
|
public int source = Source.NONE;
|
||||||
@DatabaseField
|
@DatabaseField
|
||||||
public String _id;
|
public String _id;
|
||||||
|
|
||||||
@DatabaseField
|
@DatabaseField
|
||||||
public Double insulin = 0d;
|
public double insulin = 0d;
|
||||||
@DatabaseField
|
@DatabaseField
|
||||||
public Double carbs = 0d;
|
public double carbs = 0d;
|
||||||
@DatabaseField
|
@DatabaseField
|
||||||
public boolean mealBolus = true; // true for meal bolus , false for correction bolus
|
public boolean mealBolus = true; // true for meal bolus , false for correction bolus
|
||||||
|
|
||||||
|
@ -57,32 +61,60 @@ public class Treatment implements DataPointWithLabelInterface {
|
||||||
dia = insulin.getDia();
|
dia = insulin.getDia();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void copyFrom(Treatment t) {
|
|
||||||
this.date = t.date;
|
|
||||||
this.isValid = t.isValid;
|
|
||||||
this.source = t.source;
|
|
||||||
this._id = t._id;
|
|
||||||
this.insulin = t.insulin;
|
|
||||||
this.carbs = t.carbs;
|
|
||||||
this.mealBolus = t.mealBolus;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getMillisecondsFromStart() {
|
public long getMillisecondsFromStart() {
|
||||||
return new Date().getTime() - date;
|
return new Date().getTime() - date;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String log() {
|
public String toString() {
|
||||||
return "Treatment{" +
|
return "Treatment{" +
|
||||||
"date= " + date +
|
"date= " + date +
|
||||||
", date= " + DateUtil.dateAndTimeString(date) +
|
", date= " + DateUtil.dateAndTimeString(date) +
|
||||||
", isValid= " + isValid +
|
", isValid= " + isValid +
|
||||||
", _id= " + _id +
|
", _id= " + _id +
|
||||||
|
", pumpId= " + pumpId +
|
||||||
", insulin= " + insulin +
|
", insulin= " + insulin +
|
||||||
", carbs= " + carbs +
|
", carbs= " + carbs +
|
||||||
", mealBolus= " + mealBolus +
|
", mealBolus= " + mealBolus +
|
||||||
"}";
|
"}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isDataChanging(Treatment other) {
|
||||||
|
if (date != other.date) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (insulin != other.insulin)
|
||||||
|
return true;
|
||||||
|
if (carbs != other.carbs)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEqual(Treatment other) {
|
||||||
|
if (date != other.date) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (insulin != other.insulin)
|
||||||
|
return false;
|
||||||
|
if (carbs != other.carbs)
|
||||||
|
return false;
|
||||||
|
if (mealBolus != other.mealBolus)
|
||||||
|
return false;
|
||||||
|
if (pumpId != other.pumpId)
|
||||||
|
return false;
|
||||||
|
if (!Objects.equals(_id, other._id))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void copyFrom(Treatment t) {
|
||||||
|
date = t.date;
|
||||||
|
_id = t._id;
|
||||||
|
insulin = t.insulin;
|
||||||
|
carbs = t.carbs;
|
||||||
|
mealBolus = t.mealBolus;
|
||||||
|
pumpId = t.pumpId;
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------- DataPointInterface --------------------
|
// ----------------- DataPointInterface --------------------
|
||||||
@Override
|
@Override
|
||||||
public double getX() {
|
public double getX() {
|
||||||
|
@ -102,7 +134,7 @@ public class Treatment implements DataPointWithLabelInterface {
|
||||||
String label = "";
|
String label = "";
|
||||||
if (insulin > 0) label += DecimalFormatter.to2Decimal(insulin) + "U";
|
if (insulin > 0) label += DecimalFormatter.to2Decimal(insulin) + "U";
|
||||||
if (carbs > 0)
|
if (carbs > 0)
|
||||||
label += (label.equals("") ? "" : " ") + DecimalFormatter.to0Decimal(carbs) + "g";
|
label += "~" + DecimalFormatter.to0Decimal(carbs) + "g";
|
||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ public interface TreatmentsInterface {
|
||||||
void addToHistoryExtendedBolusStop(long time);
|
void addToHistoryExtendedBolusStop(long time);
|
||||||
OverlappingIntervals<ExtendedBolus> getExtendedBolusesFromHistory();
|
OverlappingIntervals<ExtendedBolus> getExtendedBolusesFromHistory();
|
||||||
|
|
||||||
void addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo);
|
boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo);
|
||||||
|
|
||||||
TempTarget getTempTargetFromHistory(long time);
|
TempTarget getTempTargetFromHistory(long time);
|
||||||
OverlappingIntervals<TempTarget> getTempTargetsFromHistory();
|
OverlappingIntervals<TempTarget> getTempTargetsFromHistory();
|
||||||
|
|
|
@ -934,11 +934,14 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo) {
|
// return true if new record is created
|
||||||
|
public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo) {
|
||||||
if (!detailedBolusInfo.addToTreatments)
|
if (!detailedBolusInfo.addToTreatments)
|
||||||
return;
|
return false;
|
||||||
activeTreatments.addToHistoryTreatment(detailedBolusInfo);
|
boolean newRecordCreated = activeTreatments.addToHistoryTreatment(detailedBolusInfo);
|
||||||
NSUpload.uploadBolusWizardRecord(detailedBolusInfo);
|
if (newRecordCreated)
|
||||||
|
NSUpload.uploadBolusWizardRecord(detailedBolusInfo);
|
||||||
|
return newRecordCreated;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -394,6 +394,7 @@ public class NSClientService extends Service {
|
||||||
} else if (treatment.getAction().equals("update")) {
|
} else if (treatment.getAction().equals("update")) {
|
||||||
updatedTreatments.put(jsonTreatment);
|
updatedTreatments.put(jsonTreatment);
|
||||||
} else if (treatment.getAction().equals("remove")) {
|
} else if (treatment.getAction().equals("remove")) {
|
||||||
|
if (treatment.getMills() != null && treatment.getMills() > new Date().getTime() - 24 * 60 * 60 * 1000L) // handle 1 day old deletions only
|
||||||
removedTreatments.put(jsonTreatment);
|
removedTreatments.put(jsonTreatment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -350,14 +350,28 @@ public class PointsWithLabelGraphSeries<E extends DataPointWithLabelInterface> e
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawLabel45(float endX, float endY, E value, Canvas canvas) {
|
void drawLabel45(float endX, float endY, E value, Canvas canvas) {
|
||||||
float px = endX;
|
if (value.getLabel().startsWith("~")) {
|
||||||
float py = endY - value.getSize();
|
float px = endX;
|
||||||
canvas.save();
|
float py = endY + value.getSize();
|
||||||
canvas.rotate(-45, px, py);
|
canvas.save();
|
||||||
mPaint.setTextSize((int) (value.getSize() * 2.5));
|
canvas.rotate(-45, px, py);
|
||||||
mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
|
mPaint.setTextSize((int) (value.getSize() * 2.5));
|
||||||
mPaint.setFakeBoldText(true);
|
mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
|
||||||
canvas.drawText(value.getLabel(), px + value.getSize(), py, mPaint);
|
mPaint.setFakeBoldText(true);
|
||||||
canvas.restore();
|
mPaint.setTextAlign(Paint.Align.RIGHT);
|
||||||
|
canvas.drawText(value.getLabel().substring(1), px - value.getSize(), py, mPaint);
|
||||||
|
mPaint.setTextAlign(Paint.Align.LEFT);
|
||||||
|
canvas.restore();
|
||||||
|
} else {
|
||||||
|
float px = endX;
|
||||||
|
float py = endY - value.getSize();
|
||||||
|
canvas.save();
|
||||||
|
canvas.rotate(-45, px, py);
|
||||||
|
mPaint.setTextSize((int) (value.getSize() * 2.5));
|
||||||
|
mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
|
||||||
|
mPaint.setFakeBoldText(true);
|
||||||
|
canvas.drawText(value.getLabel(), px + value.getSize(), py, mPaint);
|
||||||
|
canvas.restore();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -288,7 +288,7 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, ConstraintsInte
|
||||||
Treatment t = new Treatment(detailedBolusInfo.insulinInterface);
|
Treatment t = new Treatment(detailedBolusInfo.insulinInterface);
|
||||||
boolean connectionOK = false;
|
boolean connectionOK = false;
|
||||||
if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0)
|
if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0)
|
||||||
connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, new Date().getTime() + detailedBolusInfo.carbTime * 60 * 1000, t);
|
connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, new Date().getTime() + detailedBolusInfo.carbTime * 60 * 1000 + 1000, t); // +1000 to make the record different
|
||||||
PumpEnactResult result = new PumpEnactResult();
|
PumpEnactResult result = new PumpEnactResult();
|
||||||
result.success = connectionOK;
|
result.success = connectionOK;
|
||||||
result.bolusDelivered = t.insulin;
|
result.bolusDelivered = t.insulin;
|
||||||
|
|
|
@ -9,6 +9,7 @@ import java.util.GregorianCalendar;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.data.DetailedBolusInfo;
|
import info.nightscout.androidaps.data.DetailedBolusInfo;
|
||||||
import info.nightscout.androidaps.db.ExtendedBolus;
|
import info.nightscout.androidaps.db.ExtendedBolus;
|
||||||
|
import info.nightscout.androidaps.db.Source;
|
||||||
import info.nightscout.androidaps.db.TemporaryBasal;
|
import info.nightscout.androidaps.db.TemporaryBasal;
|
||||||
import info.nightscout.androidaps.db.Treatment;
|
import info.nightscout.androidaps.db.Treatment;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
|
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
|
||||||
|
@ -72,15 +73,9 @@ public class MsgHistoryEvents_v2 extends MessageBase {
|
||||||
}
|
}
|
||||||
extendedBolus = new ExtendedBolus();
|
extendedBolus = new ExtendedBolus();
|
||||||
|
|
||||||
Treatment treatment = MainApp.getDbHelper().findTreatmentByTime(datetime.getTime());
|
|
||||||
if (treatment != null) {
|
|
||||||
log.debug("EVENT (" + recordCode + ") " + datetime.toLocaleString() + " Param1: " + param1 + " Param2: " + param2);
|
|
||||||
log.debug("Existing treatment found. Skipping ...");
|
|
||||||
if (datetime.getTime() > lastEventTimeLoaded)
|
|
||||||
lastEventTimeLoaded = datetime.getTime();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
|
DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
|
||||||
|
detailedBolusInfo.recordFromHistory = true;
|
||||||
|
detailedBolusInfo.pumpId = datetime.getTime();
|
||||||
|
|
||||||
switch (recordCode) {
|
switch (recordCode) {
|
||||||
case DanaRPump.TEMPSTART:
|
case DanaRPump.TEMPSTART:
|
||||||
|
@ -92,9 +87,7 @@ public class MsgHistoryEvents_v2 extends MessageBase {
|
||||||
break;
|
break;
|
||||||
case DanaRPump.TEMPSTOP:
|
case DanaRPump.TEMPSTOP:
|
||||||
log.debug("EVENT TEMPSTOP (" + recordCode + ") " + datetime.toLocaleString());
|
log.debug("EVENT TEMPSTOP (" + recordCode + ") " + datetime.toLocaleString());
|
||||||
temporaryBasal.date = datetime.getTime();
|
MainApp.getConfigBuilder().addToHistoryTempBasalStop(datetime.getTime());
|
||||||
temporaryBasal.durationInMinutes = 0;
|
|
||||||
MainApp.getConfigBuilder().addToHistoryTempBasalStart(temporaryBasal);
|
|
||||||
break;
|
break;
|
||||||
case DanaRPump.EXTENDEDSTART:
|
case DanaRPump.EXTENDEDSTART:
|
||||||
log.debug("EVENT EXTENDEDSTART (" + recordCode + ") " + datetime.toLocaleString() + " Amount: " + (param1 / 100d) + "U Duration: " + param2 + "min");
|
log.debug("EVENT EXTENDEDSTART (" + recordCode + ") " + datetime.toLocaleString() + " Amount: " + (param1 / 100d) + "U Duration: " + param2 + "min");
|
||||||
|
@ -105,20 +98,20 @@ public class MsgHistoryEvents_v2 extends MessageBase {
|
||||||
break;
|
break;
|
||||||
case DanaRPump.EXTENDEDSTOP:
|
case DanaRPump.EXTENDEDSTOP:
|
||||||
log.debug("EVENT EXTENDEDSTOP (" + recordCode + ") " + datetime.toLocaleString() + " Delivered: " + (param1 / 100d) + "U RealDuration: " + param2 + "min");
|
log.debug("EVENT EXTENDEDSTOP (" + recordCode + ") " + datetime.toLocaleString() + " Delivered: " + (param1 / 100d) + "U RealDuration: " + param2 + "min");
|
||||||
extendedBolus.date = datetime.getTime();
|
MainApp.getConfigBuilder().addToHistoryExtendedBolusStop(datetime.getTime());
|
||||||
extendedBolus.durationInMinutes = 0;
|
|
||||||
MainApp.getConfigBuilder().addToHistoryExtendedBolusStart(extendedBolus);
|
|
||||||
break;
|
break;
|
||||||
case DanaRPump.BOLUS:
|
case DanaRPump.BOLUS:
|
||||||
log.debug("EVENT BOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min");
|
log.debug("EVENT BOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min");
|
||||||
detailedBolusInfo.date = datetime.getTime();
|
detailedBolusInfo.date = datetime.getTime();
|
||||||
detailedBolusInfo.insulin = param1 / 100d;
|
detailedBolusInfo.insulin = param1 / 100d;
|
||||||
|
detailedBolusInfo.source = Source.PUMP;
|
||||||
MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
|
MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
|
||||||
break;
|
break;
|
||||||
case DanaRPump.DUALBOLUS:
|
case DanaRPump.DUALBOLUS:
|
||||||
log.debug("EVENT DUALBOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min");
|
log.debug("EVENT DUALBOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min");
|
||||||
detailedBolusInfo.date = datetime.getTime();
|
detailedBolusInfo.date = datetime.getTime();
|
||||||
detailedBolusInfo.insulin = param1 / 100d;
|
detailedBolusInfo.insulin = param1 / 100d;
|
||||||
|
detailedBolusInfo.source = Source.PUMP;
|
||||||
MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
|
MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
|
||||||
break;
|
break;
|
||||||
case DanaRPump.DUALEXTENDEDSTART:
|
case DanaRPump.DUALEXTENDEDSTART:
|
||||||
|
@ -130,9 +123,7 @@ public class MsgHistoryEvents_v2 extends MessageBase {
|
||||||
break;
|
break;
|
||||||
case DanaRPump.DUALEXTENDEDSTOP:
|
case DanaRPump.DUALEXTENDEDSTOP:
|
||||||
log.debug("EVENT DUALEXTENDEDSTOP (" + recordCode + ") " + datetime.toLocaleString() + " Delivered: " + (param1 / 100d) + "U RealDuration: " + param2 + "min");
|
log.debug("EVENT DUALEXTENDEDSTOP (" + recordCode + ") " + datetime.toLocaleString() + " Delivered: " + (param1 / 100d) + "U RealDuration: " + param2 + "min");
|
||||||
extendedBolus.date = datetime.getTime();
|
MainApp.getConfigBuilder().addToHistoryExtendedBolusStop(datetime.getTime());
|
||||||
extendedBolus.durationInMinutes = 0;
|
|
||||||
MainApp.getConfigBuilder().addToHistoryExtendedBolusStart(extendedBolus);
|
|
||||||
break;
|
break;
|
||||||
case DanaRPump.SUSPENDON:
|
case DanaRPump.SUSPENDON:
|
||||||
log.debug("EVENT SUSPENDON (" + recordCode + ") " + datetime.toLocaleString());
|
log.debug("EVENT SUSPENDON (" + recordCode + ") " + datetime.toLocaleString());
|
||||||
|
@ -153,6 +144,7 @@ public class MsgHistoryEvents_v2 extends MessageBase {
|
||||||
log.debug("EVENT CARBS (" + recordCode + ") " + datetime.toLocaleString() + " Carbs: " + param1 + "g");
|
log.debug("EVENT CARBS (" + recordCode + ") " + datetime.toLocaleString() + " Carbs: " + param1 + "g");
|
||||||
detailedBolusInfo.date = datetime.getTime();
|
detailedBolusInfo.date = datetime.getTime();
|
||||||
detailedBolusInfo.carbs = param1;
|
detailedBolusInfo.carbs = param1;
|
||||||
|
detailedBolusInfo.source = Source.PUMP;
|
||||||
MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
|
MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -420,7 +420,7 @@ public class DanaRv2ExecutionService extends Service {
|
||||||
log.debug("Communication stopped");
|
log.debug("Communication stopped");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
waitMsec(300);
|
waitMsec(1000);
|
||||||
bolusingTreatment = null;
|
bolusingTreatment = null;
|
||||||
loadEvents();
|
loadEvents();
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -24,6 +24,7 @@ import info.nightscout.androidaps.data.Profile;
|
||||||
import info.nightscout.androidaps.data.ProfileIntervals;
|
import info.nightscout.androidaps.data.ProfileIntervals;
|
||||||
import info.nightscout.androidaps.db.ExtendedBolus;
|
import info.nightscout.androidaps.db.ExtendedBolus;
|
||||||
import info.nightscout.androidaps.db.ProfileSwitch;
|
import info.nightscout.androidaps.db.ProfileSwitch;
|
||||||
|
import info.nightscout.androidaps.db.Source;
|
||||||
import info.nightscout.androidaps.db.TempTarget;
|
import info.nightscout.androidaps.db.TempTarget;
|
||||||
import info.nightscout.androidaps.db.TemporaryBasal;
|
import info.nightscout.androidaps.db.TemporaryBasal;
|
||||||
import info.nightscout.androidaps.db.Treatment;
|
import info.nightscout.androidaps.db.Treatment;
|
||||||
|
@ -405,24 +406,31 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo) {
|
public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo) {
|
||||||
Treatment treatment = new Treatment(detailedBolusInfo.insulinInterface);
|
Treatment treatment = new Treatment(detailedBolusInfo.insulinInterface);
|
||||||
treatment.date = detailedBolusInfo.date;
|
treatment.date = detailedBolusInfo.date;
|
||||||
|
treatment.source = detailedBolusInfo.recordFromHistory ? Source.PUMP : Source.USER;
|
||||||
|
if (detailedBolusInfo.recordFromHistory)
|
||||||
|
treatment.pumpId = treatment.date;
|
||||||
treatment.insulin = detailedBolusInfo.insulin;
|
treatment.insulin = detailedBolusInfo.insulin;
|
||||||
if (detailedBolusInfo.carbTime == 0)
|
if (detailedBolusInfo.carbTime == 0)
|
||||||
treatment.carbs = detailedBolusInfo.carbs;
|
treatment.carbs = detailedBolusInfo.carbs;
|
||||||
treatment.source = detailedBolusInfo.source;
|
treatment.source = detailedBolusInfo.source;
|
||||||
treatment.mealBolus = treatment.carbs > 0;
|
treatment.mealBolus = treatment.carbs > 0;
|
||||||
MainApp.getDbHelper().createOrUpdate(treatment);
|
boolean newRecordCreated = MainApp.getDbHelper().createOrUpdate(treatment);
|
||||||
log.debug("Adding new Treatment record" + treatment.log());
|
log.debug("Adding new Treatment record" + treatment.toString());
|
||||||
if (detailedBolusInfo.carbTime != 0) {
|
if (detailedBolusInfo.carbTime != 0) {
|
||||||
Treatment carbsTreatment = new Treatment(detailedBolusInfo.insulinInterface);
|
Treatment carbsTreatment = new Treatment(detailedBolusInfo.insulinInterface);
|
||||||
carbsTreatment.date = detailedBolusInfo.date + detailedBolusInfo.carbTime * 60 * 1000L;
|
carbsTreatment.source = detailedBolusInfo.recordFromHistory ? Source.PUMP : Source.USER;
|
||||||
|
if (detailedBolusInfo.recordFromHistory)
|
||||||
|
carbsTreatment.pumpId = treatment.date;
|
||||||
|
carbsTreatment.date = detailedBolusInfo.date + detailedBolusInfo.carbTime * 60 * 1000L + 1000L; // add 1 sec to make them different records
|
||||||
carbsTreatment.carbs = detailedBolusInfo.carbs;
|
carbsTreatment.carbs = detailedBolusInfo.carbs;
|
||||||
carbsTreatment.source = detailedBolusInfo.source;
|
carbsTreatment.source = detailedBolusInfo.source;
|
||||||
MainApp.getDbHelper().createOrUpdate(carbsTreatment);
|
MainApp.getDbHelper().createOrUpdate(carbsTreatment);
|
||||||
log.debug("Adding new Treatment record" + carbsTreatment);
|
log.debug("Adding new Treatment record" + carbsTreatment);
|
||||||
}
|
}
|
||||||
|
return newRecordCreated;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -175,23 +175,6 @@ public class NSUpload {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void uploadTreatment(Treatment treatment) {
|
|
||||||
JSONObject data = new JSONObject();
|
|
||||||
try {
|
|
||||||
if (treatment.mealBolus)
|
|
||||||
data.put("eventType", "Meal Bolus");
|
|
||||||
else
|
|
||||||
data.put("eventType", "Correction Bolus");
|
|
||||||
if (treatment.insulin != 0d) data.put("insulin", treatment.insulin);
|
|
||||||
if (treatment.carbs != 0d) data.put("carbs", treatment.carbs.intValue());
|
|
||||||
data.put("created_at", DateUtil.toISOString(treatment.date));
|
|
||||||
data.put("timeIndex", treatment.date);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
NSUpload.uploadCareportalEntryToNS(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void uploadDeviceStatus() {
|
public static void uploadDeviceStatus() {
|
||||||
DeviceStatus deviceStatus = new DeviceStatus();
|
DeviceStatus deviceStatus = new DeviceStatus();
|
||||||
try {
|
try {
|
||||||
|
@ -261,6 +244,8 @@ public class NSUpload {
|
||||||
if (detailedBolusInfo.carbs != 0d) data.put("carbs", (int) detailedBolusInfo.carbs);
|
if (detailedBolusInfo.carbs != 0d) data.put("carbs", (int) detailedBolusInfo.carbs);
|
||||||
data.put("created_at", DateUtil.toISOString(detailedBolusInfo.date));
|
data.put("created_at", DateUtil.toISOString(detailedBolusInfo.date));
|
||||||
data.put("date", detailedBolusInfo.date);
|
data.put("date", detailedBolusInfo.date);
|
||||||
|
if (detailedBolusInfo.pumpId != 0)
|
||||||
|
data.put("pumpId", detailedBolusInfo.pumpId);
|
||||||
if (detailedBolusInfo.glucose != 0d)
|
if (detailedBolusInfo.glucose != 0d)
|
||||||
data.put("glucose", detailedBolusInfo.glucose);
|
data.put("glucose", detailedBolusInfo.glucose);
|
||||||
if (!detailedBolusInfo.glucoseType.equals(""))
|
if (!detailedBolusInfo.glucoseType.equals(""))
|
||||||
|
|
Loading…
Reference in a new issue