profileswitch sync & v2 bolus fix
This commit is contained in:
parent
e09fba93cb
commit
a39f61284a
4 changed files with 117 additions and 51 deletions
|
@ -497,8 +497,6 @@ public class DataService extends IntentService {
|
|||
trJson.getString("eventType").equals(CareportalEvent.OPENAPSOFFLINE) ||
|
||||
trJson.getString("eventType").equals(CareportalEvent.PUMPBATTERYCHANGE)
|
||||
)) {
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Processing CareportalEvent record: " + trJson.toString());
|
||||
MainApp.getDbHelper().createCareportalEventFromJsonIfNotExists(trJson);
|
||||
}
|
||||
|
||||
|
@ -514,8 +512,6 @@ public class DataService extends IntentService {
|
|||
|
||||
public void handleAddChangeProfileSwitchRecord(JSONObject trJson) throws JSONException {
|
||||
if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.PROFILESWITCH)) {
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Processing ProfileSwitch record: " + trJson.toString());
|
||||
MainApp.getDbHelper().createProfileSwitchFromJsonIfNotExists(trJson);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1491,14 +1491,58 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
return new ArrayList<ProfileSwitch>();
|
||||
}
|
||||
|
||||
public void createOrUpdate(ProfileSwitch profileSwitch) {
|
||||
profileSwitch.date = profileSwitch.date - profileSwitch.date % 1000;
|
||||
public boolean createOrUpdate(ProfileSwitch profileSwitch) {
|
||||
try {
|
||||
getDaoProfileSwitch().createOrUpdate(profileSwitch);
|
||||
scheduleProfileSwitchChange();
|
||||
ProfileSwitch old;
|
||||
profileSwitch.date = roundDateToSec(profileSwitch.date);
|
||||
|
||||
if (profileSwitch.source == Source.NIGHTSCOUT) {
|
||||
old = getDaoProfileSwitch().queryForId(profileSwitch.date);
|
||||
if (old != null) {
|
||||
if (!old.isEqual(profileSwitch)) {
|
||||
getDaoProfileSwitch().delete(old); // need to delete/create because date may change too
|
||||
old.copyFrom(profileSwitch);
|
||||
getDaoProfileSwitch().create(old);
|
||||
log.debug("PROFILESWITCH: Updating record by date from: " + Source.getString(profileSwitch.source) + " " + old.toString());
|
||||
scheduleTemporaryTargetChange();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// find by NS _id
|
||||
if (profileSwitch._id != null) {
|
||||
QueryBuilder<ProfileSwitch, Long> queryBuilder = getDaoProfileSwitch().queryBuilder();
|
||||
Where where = queryBuilder.where();
|
||||
where.eq("_id", profileSwitch._id);
|
||||
PreparedQuery<ProfileSwitch> preparedQuery = queryBuilder.prepare();
|
||||
List<ProfileSwitch> trList = getDaoProfileSwitch().query(preparedQuery);
|
||||
if (trList.size() > 0) {
|
||||
old = trList.get(0);
|
||||
if (!old.isEqual(profileSwitch)) {
|
||||
getDaoProfileSwitch().delete(old); // need to delete/create because date may change too
|
||||
old.copyFrom(profileSwitch);
|
||||
getDaoProfileSwitch().create(old);
|
||||
log.debug("PROFILESWITCH: Updating record by _id from: " + Source.getString(profileSwitch.source) + " " + old.toString());
|
||||
scheduleTemporaryTargetChange();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
getDaoProfileSwitch().create(profileSwitch);
|
||||
log.debug("PROFILESWITCH: New record from: " + Source.getString(profileSwitch.source) + " " + profileSwitch.toString());
|
||||
scheduleTemporaryTargetChange();
|
||||
return true;
|
||||
}
|
||||
if (profileSwitch.source == Source.USER) {
|
||||
getDaoProfileSwitch().create(profileSwitch);
|
||||
log.debug("PROFILESWITCH: New record from: " + Source.getString(profileSwitch.source) + " " + profileSwitch.toString());
|
||||
scheduleTemporaryTargetChange();
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void delete(ProfileSwitch profileSwitch) {
|
||||
|
@ -1542,32 +1586,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
|
||||
public void createProfileSwitchFromJsonIfNotExists(JSONObject trJson) {
|
||||
try {
|
||||
QueryBuilder<ProfileSwitch, Long> queryBuilder = null;
|
||||
queryBuilder = getDaoProfileSwitch().queryBuilder();
|
||||
Where where = queryBuilder.where();
|
||||
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
||||
PreparedQuery<ProfileSwitch> preparedQuery = queryBuilder.prepare();
|
||||
List<ProfileSwitch> list = getDaoProfileSwitch().query(preparedQuery);
|
||||
ProfileSwitch profileSwitch;
|
||||
if (list.size() == 0) {
|
||||
profileSwitch = new ProfileSwitch();
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Adding ProfileSwitch record to database: " + trJson.toString());
|
||||
// Record does not exists. add
|
||||
} else if (list.size() == 1) {
|
||||
profileSwitch = list.get(0);
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Updating ProfileSwitch record in database: " + trJson.toString());
|
||||
} else {
|
||||
log.error("Something went wrong");
|
||||
return;
|
||||
}
|
||||
ProfileSwitch profileSwitch = new ProfileSwitch();
|
||||
profileSwitch.date = trJson.getLong("mills");
|
||||
if (trJson.has("duration"))
|
||||
profileSwitch.durationInMinutes = trJson.getInt("duration");
|
||||
profileSwitch._id = trJson.getString("_id");
|
||||
profileSwitch.profileName = trJson.getString("profile");
|
||||
profileSwitch.isCPP = trJson.has("CircadianPercentageProfile");
|
||||
profileSwitch.source = Source.NIGHTSCOUT;
|
||||
if (trJson.has("timeshift"))
|
||||
profileSwitch.timeshift = trJson.getInt("timeshift");
|
||||
if (trJson.has("percentage"))
|
||||
|
@ -1577,12 +1603,21 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
if (trJson.has("profilePlugin"))
|
||||
profileSwitch.profilePlugin = trJson.getString("profilePlugin");
|
||||
createOrUpdate(profileSwitch);
|
||||
} catch (SQLException | JSONException e) {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteProfileSwitchById(String _id) {
|
||||
ProfileSwitch stored = findProfileSwitchById(_id);
|
||||
if (stored != null) {
|
||||
log.debug("PROFILESWITCH: Removing ProfileSwitch record from database: " + stored.toString());
|
||||
delete(stored);
|
||||
scheduleTemporaryTargetChange();
|
||||
}
|
||||
}
|
||||
|
||||
public ProfileSwitch findProfileSwitchById(String _id) {
|
||||
try {
|
||||
QueryBuilder<ProfileSwitch, Long> queryBuilder = getDaoProfileSwitch().queryBuilder();
|
||||
Where where = queryBuilder.where();
|
||||
|
@ -1591,17 +1626,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
List<ProfileSwitch> list = getDaoProfileSwitch().query(preparedQuery);
|
||||
|
||||
if (list.size() == 1) {
|
||||
ProfileSwitch record = list.get(0);
|
||||
if (Config.logIncommingData)
|
||||
log.debug("Removing ProfileSwitch record from database: " + record.log());
|
||||
delete(record);
|
||||
return list.get(0);
|
||||
} else {
|
||||
if (Config.logIncommingData)
|
||||
log.debug("ProfileSwitch not found database: " + _id);
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -9,6 +9,7 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
import info.nightscout.androidaps.interfaces.Interval;
|
||||
import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
|
||||
|
@ -49,6 +50,41 @@ public class ProfileSwitch implements Interval, DataPointWithLabelInterface {
|
|||
@DatabaseField
|
||||
public int durationInMinutes = 0;
|
||||
|
||||
public boolean isEqual(ProfileSwitch other) {
|
||||
if (date != other.date) {
|
||||
return false;
|
||||
}
|
||||
if (durationInMinutes != other.durationInMinutes)
|
||||
return false;
|
||||
if (percentage != other.percentage)
|
||||
return false;
|
||||
if (timeshift != other.timeshift)
|
||||
return false;
|
||||
if (isCPP != other.isCPP)
|
||||
return false;
|
||||
if (!Objects.equals(_id, other._id))
|
||||
return false;
|
||||
if (!Objects.equals(profilePlugin, other.profilePlugin))
|
||||
return false;
|
||||
if (!Objects.equals(profileJson, other.profileJson))
|
||||
return false;
|
||||
if (!Objects.equals(profileName, other.profileName))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void copyFrom(ProfileSwitch t) {
|
||||
date = t.date;
|
||||
_id = t._id;
|
||||
durationInMinutes = t.durationInMinutes;
|
||||
percentage = t.percentage;
|
||||
timeshift = t.timeshift;
|
||||
isCPP = t.isCPP;
|
||||
profilePlugin = t.profilePlugin;
|
||||
profileJson = t.profileJson;
|
||||
profileName = t.profileName;
|
||||
}
|
||||
|
||||
// -------- Interval interface ---------
|
||||
|
||||
Long cuttedEnd = null;
|
||||
|
|
|
@ -401,8 +401,9 @@ public class DanaRv2ExecutionService extends Service {
|
|||
mSerialIOThread.sendMessage(msg);
|
||||
MsgSetHistoryEntry_v2 msgSetHistoryEntry_v2 = new MsgSetHistoryEntry_v2(DanaRPump.CARBS, carbtime, carbs, 0);
|
||||
mSerialIOThread.sendMessage(msgSetHistoryEntry_v2);
|
||||
lastHistoryFetched = carbtime - 1000;
|
||||
lastHistoryFetched = carbtime - 60000;
|
||||
}
|
||||
if (amount > 0) {
|
||||
MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables
|
||||
MainApp.bus().post(new EventDanaRBolusStart());
|
||||
|
||||
|
@ -420,7 +421,8 @@ public class DanaRv2ExecutionService extends Service {
|
|||
log.debug("Communication stopped");
|
||||
}
|
||||
}
|
||||
waitMsec(1000);
|
||||
}
|
||||
waitMsec(3000);
|
||||
bolusingTreatment = null;
|
||||
loadEvents();
|
||||
return true;
|
||||
|
|
Loading…
Reference in a new issue