temptarget sync
This commit is contained in:
parent
1153c33200
commit
e8be720138
5 changed files with 109 additions and 77 deletions
|
@ -717,14 +717,58 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
return new ArrayList<TempTarget>();
|
return new ArrayList<TempTarget>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createOrUpdate(TempTarget tempTarget) {
|
public boolean createOrUpdate(TempTarget tempTarget) {
|
||||||
tempTarget.date = tempTarget.date - tempTarget.date % 1000;
|
|
||||||
try {
|
try {
|
||||||
getDaoTempTargets().createOrUpdate(tempTarget);
|
TempTarget old;
|
||||||
scheduleTemporaryTargetChange();
|
tempTarget.date = roundDateToSec(tempTarget.date);
|
||||||
|
|
||||||
|
if (tempTarget.source == Source.NIGHTSCOUT) {
|
||||||
|
old = getDaoTempTargets().queryForId(tempTarget.date);
|
||||||
|
if (old != null) {
|
||||||
|
if (!old.isEqual(tempTarget)) {
|
||||||
|
getDaoTempTargets().delete(old); // need to delete/create because date may change too
|
||||||
|
old.copyFrom(tempTarget);
|
||||||
|
getDaoTempTargets().create(old);
|
||||||
|
log.debug("TEMPTARGET: Updating record by date from: " + Source.getString(tempTarget.source) + " " + old.toString());
|
||||||
|
scheduleTemporaryTargetChange();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// find by NS _id
|
||||||
|
if (tempTarget._id != null) {
|
||||||
|
QueryBuilder<TempTarget, Long> queryBuilder = getDaoTempTargets().queryBuilder();
|
||||||
|
Where where = queryBuilder.where();
|
||||||
|
where.eq("_id", tempTarget._id);
|
||||||
|
PreparedQuery<TempTarget> preparedQuery = queryBuilder.prepare();
|
||||||
|
List<TempTarget> trList = getDaoTempTargets().query(preparedQuery);
|
||||||
|
if (trList.size() > 0) {
|
||||||
|
old = trList.get(0);
|
||||||
|
if (!old.isEqual(tempTarget)) {
|
||||||
|
getDaoTempTargets().delete(old); // need to delete/create because date may change too
|
||||||
|
old.copyFrom(tempTarget);
|
||||||
|
getDaoTempTargets().create(old);
|
||||||
|
log.debug("TEMPTARGET: Updating record by _id from: " + Source.getString(tempTarget.source) + " " + old.toString());
|
||||||
|
scheduleTemporaryTargetChange();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
getDaoTempTargets().create(tempTarget);
|
||||||
|
log.debug("TEMPTARGET: New record from: " + Source.getString(tempTarget.source) + " " + tempTarget.toString());
|
||||||
|
scheduleTemporaryTargetChange();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (tempTarget.source == Source.USER) {
|
||||||
|
getDaoTempTargets().create(tempTarget);
|
||||||
|
log.debug("TEMPTARGET: New record from: " + Source.getString(tempTarget.source) + " " + tempTarget.toString());
|
||||||
|
scheduleTemporaryTargetChange();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(TempTarget tempTarget) {
|
public void delete(TempTarget tempTarget) {
|
||||||
|
@ -771,41 +815,32 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
|
|
||||||
public void createTemptargetFromJsonIfNotExists(JSONObject trJson) {
|
public void createTemptargetFromJsonIfNotExists(JSONObject trJson) {
|
||||||
try {
|
try {
|
||||||
QueryBuilder<TempTarget, Long> queryBuilder = null;
|
|
||||||
queryBuilder = getDaoTempTargets().queryBuilder();
|
|
||||||
Where where = queryBuilder.where();
|
|
||||||
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
|
||||||
PreparedQuery<TempTarget> preparedQuery = queryBuilder.prepare();
|
|
||||||
List<TempTarget> list = getDaoTempTargets().query(preparedQuery);
|
|
||||||
Profile profile = MainApp.getConfigBuilder().getProfile();
|
Profile profile = MainApp.getConfigBuilder().getProfile();
|
||||||
String units = profile.getUnits();
|
String units = profile.getUnits();
|
||||||
TempTarget tempTarget;
|
TempTarget tempTarget = new TempTarget();
|
||||||
if (list.size() == 0) {
|
|
||||||
tempTarget = new TempTarget();
|
|
||||||
if (Config.logIncommingData)
|
|
||||||
log.debug("Adding TempTarget record to database: " + trJson.toString());
|
|
||||||
// Record does not exists. add
|
|
||||||
} else if (list.size() == 1) {
|
|
||||||
tempTarget = list.get(0);
|
|
||||||
if (Config.logIncommingData)
|
|
||||||
log.debug("Updating TempTarget record in database: " + trJson.toString());
|
|
||||||
} else {
|
|
||||||
log.error("Something went wrong");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
tempTarget.date = trJson.getLong("mills");
|
tempTarget.date = trJson.getLong("mills");
|
||||||
tempTarget.durationInMinutes = trJson.getInt("duration");
|
tempTarget.durationInMinutes = trJson.getInt("duration");
|
||||||
tempTarget.low = Profile.toMgdl(trJson.getDouble("targetBottom"), units);
|
tempTarget.low = Profile.toMgdl(trJson.getDouble("targetBottom"), units);
|
||||||
tempTarget.high = Profile.toMgdl(trJson.getDouble("targetTop"), units);
|
tempTarget.high = Profile.toMgdl(trJson.getDouble("targetTop"), units);
|
||||||
tempTarget.reason = trJson.getString("reason");
|
tempTarget.reason = trJson.getString("reason");
|
||||||
tempTarget._id = trJson.getString("_id");
|
tempTarget._id = trJson.getString("_id");
|
||||||
|
tempTarget.source = Source.NIGHTSCOUT;
|
||||||
createOrUpdate(tempTarget);
|
createOrUpdate(tempTarget);
|
||||||
} catch (SQLException | JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteTempTargetById(String _id) {
|
public void deleteTempTargetById(String _id) {
|
||||||
|
TempTarget stored = findTempTargetById(_id);
|
||||||
|
if (stored != null) {
|
||||||
|
log.debug("TEMPTARGET: Removing TempTarget record from database: " + stored.toString());
|
||||||
|
delete(stored);
|
||||||
|
scheduleTemporaryTargetChange();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TempTarget findTempTargetById(String _id) {
|
||||||
try {
|
try {
|
||||||
QueryBuilder<TempTarget, Long> queryBuilder = getDaoTempTargets().queryBuilder();
|
QueryBuilder<TempTarget, Long> queryBuilder = getDaoTempTargets().queryBuilder();
|
||||||
Where where = queryBuilder.where();
|
Where where = queryBuilder.where();
|
||||||
|
@ -814,17 +849,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
List<TempTarget> list = getDaoTempTargets().query(preparedQuery);
|
List<TempTarget> list = getDaoTempTargets().query(preparedQuery);
|
||||||
|
|
||||||
if (list.size() == 1) {
|
if (list.size() == 1) {
|
||||||
TempTarget record = list.get(0);
|
return list.get(0);
|
||||||
if (Config.logIncommingData)
|
|
||||||
log.debug("Removing TempTarget record from database: " + record.log());
|
|
||||||
delete(record);
|
|
||||||
} else {
|
} else {
|
||||||
if (Config.logIncommingData)
|
return null;
|
||||||
log.debug("TempTarget not found database: " + _id);
|
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------- DanaRHistory handling --------------------
|
// ----------------- DanaRHistory handling --------------------
|
||||||
|
@ -988,16 +1020,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
return new ArrayList<TemporaryBasal>();
|
return new ArrayList<TemporaryBasal>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public TemporaryBasal findTempBasalByTime(long date) {
|
|
||||||
try {
|
|
||||||
return getDaoTemporaryBasal().queryForId(date);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void scheduleTemporaryBasalChange() {
|
private static void scheduleTemporaryBasalChange() {
|
||||||
class PostRunnable implements Runnable {
|
class PostRunnable implements Runnable {
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -1217,31 +1239,16 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
return new ArrayList<ExtendedBolus>();
|
return new ArrayList<ExtendedBolus>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public ExtendedBolus findExtendedBolusByTime(Long timeIndex) {
|
|
||||||
try {
|
|
||||||
QueryBuilder<ExtendedBolus, String> qb = null;
|
|
||||||
Dao<ExtendedBolus, Long> daoExtendedBolus = getDaoExtendedBolus();
|
|
||||||
QueryBuilder<ExtendedBolus, Long> queryBuilder = daoExtendedBolus.queryBuilder();
|
|
||||||
Where where = queryBuilder.where();
|
|
||||||
where.eq("date", timeIndex);
|
|
||||||
queryBuilder.limit(10L);
|
|
||||||
PreparedQuery<ExtendedBolus> preparedQuery = queryBuilder.prepare();
|
|
||||||
List<ExtendedBolus> trList = daoExtendedBolus.query(preparedQuery);
|
|
||||||
if (trList.size() != 1) {
|
|
||||||
//log.debug("ExtendedBolus findExtendedBolusByTime query size: " + trList.size());
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
//log.debug("ExtendedBolus findExtendedBolusByTime found: " + trList.get(0).log());
|
|
||||||
return trList.get(0);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteExtendedBolusById(String _id) {
|
public void deleteExtendedBolusById(String _id) {
|
||||||
|
ExtendedBolus stored = findExtendedBolusById(_id);
|
||||||
|
if (stored != null) {
|
||||||
|
log.debug("EXTENDEDBOLUS: Removing ExtendedBolus record from database: " + stored.toString());
|
||||||
|
delete(stored);
|
||||||
|
updateEarliestDataChange(stored.date);
|
||||||
|
scheduleExtendedBolusChange();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public ExtendedBolus findExtendedBolusById(String _id) {
|
||||||
try {
|
try {
|
||||||
QueryBuilder<ExtendedBolus, Long> queryBuilder = null;
|
QueryBuilder<ExtendedBolus, Long> queryBuilder = null;
|
||||||
queryBuilder = getDaoExtendedBolus().queryBuilder();
|
queryBuilder = getDaoExtendedBolus().queryBuilder();
|
||||||
|
@ -1251,17 +1258,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
List<ExtendedBolus> list = getDaoExtendedBolus().query(preparedQuery);
|
List<ExtendedBolus> list = getDaoExtendedBolus().query(preparedQuery);
|
||||||
|
|
||||||
if (list.size() == 1) {
|
if (list.size() == 1) {
|
||||||
ExtendedBolus record = list.get(0);
|
return list.get(0);
|
||||||
if (Config.logIncommingData)
|
|
||||||
log.debug("Removing ExtendedBolus record from database: " + record.log());
|
|
||||||
delete(record);
|
|
||||||
} else {
|
} else {
|
||||||
if (Config.logIncommingData)
|
return null;
|
||||||
log.debug("ExtendedBolus not found database: " + _id);
|
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -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.interfaces.Interval;
|
import info.nightscout.androidaps.interfaces.Interval;
|
||||||
|
@ -40,6 +41,32 @@ public class TempTarget implements Interval {
|
||||||
@DatabaseField
|
@DatabaseField
|
||||||
public int durationInMinutes;
|
public int durationInMinutes;
|
||||||
|
|
||||||
|
public boolean isEqual(TempTarget other) {
|
||||||
|
if (date != other.date) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (durationInMinutes != other.durationInMinutes)
|
||||||
|
return false;
|
||||||
|
if (low != other.low)
|
||||||
|
return false;
|
||||||
|
if (high != other.high)
|
||||||
|
return false;
|
||||||
|
if (reason != other.reason)
|
||||||
|
return false;
|
||||||
|
if (!Objects.equals(_id, other._id))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void copyFrom(TempTarget t) {
|
||||||
|
date = t.date;
|
||||||
|
_id = t._id;
|
||||||
|
durationInMinutes = t.durationInMinutes;
|
||||||
|
low = t.low;
|
||||||
|
high = t.high;
|
||||||
|
reason = t.reason;
|
||||||
|
}
|
||||||
|
|
||||||
// -------- Interval interface ---------
|
// -------- Interval interface ---------
|
||||||
|
|
||||||
Long cuttedEnd = null;
|
Long cuttedEnd = null;
|
||||||
|
@ -108,7 +135,7 @@ public class TempTarget implements Interval {
|
||||||
else return DecimalFormatter.to1Decimal(low * Constants.MGDL_TO_MMOLL);
|
else return DecimalFormatter.to1Decimal(low * Constants.MGDL_TO_MMOLL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String log() {
|
public String toString() {
|
||||||
return "TemporaryTarget{" +
|
return "TemporaryTarget{" +
|
||||||
"date=" + date +
|
"date=" + date +
|
||||||
"date=" + DateUtil.dateAndTimeString(date) +
|
"date=" + DateUtil.dateAndTimeString(date) +
|
||||||
|
|
|
@ -658,6 +658,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
|
||||||
tempTarget.date = eventTime.getTime();
|
tempTarget.date = eventTime.getTime();
|
||||||
tempTarget.durationInMinutes = data.getInt("duration");
|
tempTarget.durationInMinutes = data.getInt("duration");
|
||||||
tempTarget.reason = data.getString("reason");
|
tempTarget.reason = data.getString("reason");
|
||||||
|
tempTarget.source = Source.USER;
|
||||||
if (tempTarget.durationInMinutes != 0) {
|
if (tempTarget.durationInMinutes != 0) {
|
||||||
tempTarget.low = Profile.toMgdl(data.getDouble("targetBottom"), profile.getUnits());
|
tempTarget.low = Profile.toMgdl(data.getDouble("targetBottom"), profile.getUnits());
|
||||||
tempTarget.high = Profile.toMgdl(data.getDouble("targetTop"), profile.getUnits());
|
tempTarget.high = Profile.toMgdl(data.getDouble("targetTop"), profile.getUnits());
|
||||||
|
@ -665,7 +666,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
|
||||||
tempTarget.low = 0;
|
tempTarget.low = 0;
|
||||||
tempTarget.high = 0;
|
tempTarget.high = 0;
|
||||||
}
|
}
|
||||||
log.debug("Creating new TempTarget db record: " + tempTarget.log());
|
log.debug("Creating new TempTarget db record: " + tempTarget.toString());
|
||||||
MainApp.getDbHelper().createOrUpdate(tempTarget);
|
MainApp.getDbHelper().createOrUpdate(tempTarget);
|
||||||
NSUpload.uploadCareportalEntryToNS(data);
|
NSUpload.uploadCareportalEntryToNS(data);
|
||||||
Answers.getInstance().logCustom(new CustomEvent("TempTarget"));
|
Answers.getInstance().logCustom(new CustomEvent("TempTarget"));
|
||||||
|
|
|
@ -949,11 +949,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
|
||||||
tempTargetView.setText(Profile.toUnitsString(tempTarget.low, Profile.fromMgdlToUnits(tempTarget.low, profile.getUnits()), profile.getUnits()));
|
tempTargetView.setText(Profile.toUnitsString(tempTarget.low, Profile.fromMgdlToUnits(tempTarget.low, profile.getUnits()), profile.getUnits()));
|
||||||
else
|
else
|
||||||
tempTargetView.setText(Profile.toUnitsString(tempTarget.low, Profile.fromMgdlToUnits(tempTarget.low, profile.getUnits()), profile.getUnits()) + " - " + Profile.toUnitsString(tempTarget.high, Profile.fromMgdlToUnits(tempTarget.high, profile.getUnits()), profile.getUnits()));
|
tempTargetView.setText(Profile.toUnitsString(tempTarget.low, Profile.fromMgdlToUnits(tempTarget.low, profile.getUnits()), profile.getUnits()) + " - " + Profile.toUnitsString(tempTarget.high, Profile.fromMgdlToUnits(tempTarget.high, profile.getUnits()), profile.getUnits()));
|
||||||
}
|
|
||||||
if (Config.NSCLIENT) {
|
|
||||||
tempTargetView.setVisibility(View.GONE);
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
Double maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL;
|
Double maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL;
|
||||||
Double minBgDefault = Constants.MIN_BG_DEFAULT_MGDL;
|
Double minBgDefault = Constants.MIN_BG_DEFAULT_MGDL;
|
||||||
if (!profile.getUnits().equals(Constants.MGDL)) {
|
if (!profile.getUnits().equals(Constants.MGDL)) {
|
||||||
|
@ -965,6 +961,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
|
||||||
tempTargetView.setText(SP.getDouble("openapsma_min_bg", minBgDefault) + " - " + SP.getDouble("openapsma_max_bg", maxBgDefault));
|
tempTargetView.setText(SP.getDouble("openapsma_min_bg", minBgDefault) + " - " + SP.getDouble("openapsma_max_bg", maxBgDefault));
|
||||||
tempTargetView.setVisibility(View.VISIBLE);
|
tempTargetView.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
|
if (Config.NSCLIENT && tempTarget != null) {
|
||||||
|
tempTargetView.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
// **** Temp button ****
|
// **** Temp button ****
|
||||||
boolean showAcceptButton = !MainApp.getConfigBuilder().isClosedModeEnabled(); // Open mode needed
|
boolean showAcceptButton = !MainApp.getConfigBuilder().isClosedModeEnabled(); // Open mode needed
|
||||||
|
|
|
@ -350,6 +350,7 @@ public class ActionStringHandler {
|
||||||
tempTarget.date = new Date().getTime();
|
tempTarget.date = new Date().getTime();
|
||||||
tempTarget.durationInMinutes = duration;
|
tempTarget.durationInMinutes = duration;
|
||||||
tempTarget.reason = "WearPlugin";
|
tempTarget.reason = "WearPlugin";
|
||||||
|
tempTarget.source = Source.USER;
|
||||||
if (tempTarget.durationInMinutes != 0) {
|
if (tempTarget.durationInMinutes != 0) {
|
||||||
tempTarget.low = low;
|
tempTarget.low = low;
|
||||||
tempTarget.high = high;
|
tempTarget.high = high;
|
||||||
|
|
Loading…
Reference in a new issue