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>();
|
||||
}
|
||||
|
||||
public void createOrUpdate(TempTarget tempTarget) {
|
||||
tempTarget.date = tempTarget.date - tempTarget.date % 1000;
|
||||
public boolean createOrUpdate(TempTarget tempTarget) {
|
||||
try {
|
||||
getDaoTempTargets().createOrUpdate(tempTarget);
|
||||
scheduleTemporaryTargetChange();
|
||||
TempTarget old;
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void delete(TempTarget tempTarget) {
|
||||
|
@ -771,41 +815,32 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
|
||||
public void createTemptargetFromJsonIfNotExists(JSONObject trJson) {
|
||||
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();
|
||||
String units = profile.getUnits();
|
||||
TempTarget 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 tempTarget = new TempTarget();
|
||||
tempTarget.date = trJson.getLong("mills");
|
||||
tempTarget.durationInMinutes = trJson.getInt("duration");
|
||||
tempTarget.low = Profile.toMgdl(trJson.getDouble("targetBottom"), units);
|
||||
tempTarget.high = Profile.toMgdl(trJson.getDouble("targetTop"), units);
|
||||
tempTarget.reason = trJson.getString("reason");
|
||||
tempTarget._id = trJson.getString("_id");
|
||||
tempTarget.source = Source.NIGHTSCOUT;
|
||||
createOrUpdate(tempTarget);
|
||||
} catch (SQLException | JSONException e) {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
QueryBuilder<TempTarget, Long> queryBuilder = getDaoTempTargets().queryBuilder();
|
||||
Where where = queryBuilder.where();
|
||||
|
@ -814,17 +849,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
List<TempTarget> list = getDaoTempTargets().query(preparedQuery);
|
||||
|
||||
if (list.size() == 1) {
|
||||
TempTarget record = list.get(0);
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Removing TempTarget record from database: " + record.log());
|
||||
delete(record);
|
||||
return list.get(0);
|
||||
} else {
|
||||
if (Config.logIncommingData)
|
||||
log.debug("TempTarget not found database: " + _id);
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ----------------- DanaRHistory handling --------------------
|
||||
|
@ -988,16 +1020,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
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() {
|
||||
class PostRunnable implements Runnable {
|
||||
public void run() {
|
||||
|
@ -1217,31 +1239,16 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
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) {
|
||||
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 {
|
||||
QueryBuilder<ExtendedBolus, Long> queryBuilder = null;
|
||||
queryBuilder = getDaoExtendedBolus().queryBuilder();
|
||||
|
@ -1251,17 +1258,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
List<ExtendedBolus> list = getDaoExtendedBolus().query(preparedQuery);
|
||||
|
||||
if (list.size() == 1) {
|
||||
ExtendedBolus record = list.get(0);
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Removing ExtendedBolus record from database: " + record.log());
|
||||
delete(record);
|
||||
return list.get(0);
|
||||
} else {
|
||||
if (Config.logIncommingData)
|
||||
log.debug("ExtendedBolus not found database: " + _id);
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -7,6 +7,7 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
import info.nightscout.androidaps.Constants;
|
||||
import info.nightscout.androidaps.interfaces.Interval;
|
||||
|
@ -40,6 +41,32 @@ public class TempTarget implements Interval {
|
|||
@DatabaseField
|
||||
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 ---------
|
||||
|
||||
Long cuttedEnd = null;
|
||||
|
@ -108,7 +135,7 @@ public class TempTarget implements Interval {
|
|||
else return DecimalFormatter.to1Decimal(low * Constants.MGDL_TO_MMOLL);
|
||||
}
|
||||
|
||||
public String log() {
|
||||
public String toString() {
|
||||
return "TemporaryTarget{" +
|
||||
"date=" + date +
|
||||
"date=" + DateUtil.dateAndTimeString(date) +
|
||||
|
|
|
@ -658,6 +658,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
|
|||
tempTarget.date = eventTime.getTime();
|
||||
tempTarget.durationInMinutes = data.getInt("duration");
|
||||
tempTarget.reason = data.getString("reason");
|
||||
tempTarget.source = Source.USER;
|
||||
if (tempTarget.durationInMinutes != 0) {
|
||||
tempTarget.low = Profile.toMgdl(data.getDouble("targetBottom"), 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.high = 0;
|
||||
}
|
||||
log.debug("Creating new TempTarget db record: " + tempTarget.log());
|
||||
log.debug("Creating new TempTarget db record: " + tempTarget.toString());
|
||||
MainApp.getDbHelper().createOrUpdate(tempTarget);
|
||||
NSUpload.uploadCareportalEntryToNS(data);
|
||||
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()));
|
||||
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()));
|
||||
}
|
||||
if (Config.NSCLIENT) {
|
||||
tempTargetView.setVisibility(View.GONE);
|
||||
} else {
|
||||
|
||||
Double maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL;
|
||||
Double minBgDefault = Constants.MIN_BG_DEFAULT_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.setVisibility(View.VISIBLE);
|
||||
}
|
||||
if (Config.NSCLIENT && tempTarget != null) {
|
||||
tempTargetView.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
// **** Temp button ****
|
||||
boolean showAcceptButton = !MainApp.getConfigBuilder().isClosedModeEnabled(); // Open mode needed
|
||||
|
|
|
@ -350,6 +350,7 @@ public class ActionStringHandler {
|
|||
tempTarget.date = new Date().getTime();
|
||||
tempTarget.durationInMinutes = duration;
|
||||
tempTarget.reason = "WearPlugin";
|
||||
tempTarget.source = Source.USER;
|
||||
if (tempTarget.durationInMinutes != 0) {
|
||||
tempTarget.low = low;
|
||||
tempTarget.high = high;
|
||||
|
|
Loading…
Reference in a new issue