profileswitch sync & v2 bolus fix

This commit is contained in:
Milos Kozak 2017-06-08 20:52:33 +02:00
parent e09fba93cb
commit a39f61284a
4 changed files with 117 additions and 51 deletions

View file

@ -497,8 +497,6 @@ public class DataService extends IntentService {
trJson.getString("eventType").equals(CareportalEvent.OPENAPSOFFLINE) || trJson.getString("eventType").equals(CareportalEvent.OPENAPSOFFLINE) ||
trJson.getString("eventType").equals(CareportalEvent.PUMPBATTERYCHANGE) trJson.getString("eventType").equals(CareportalEvent.PUMPBATTERYCHANGE)
)) { )) {
if (Config.logIncommingData)
log.debug("Processing CareportalEvent record: " + trJson.toString());
MainApp.getDbHelper().createCareportalEventFromJsonIfNotExists(trJson); MainApp.getDbHelper().createCareportalEventFromJsonIfNotExists(trJson);
} }
@ -514,8 +512,6 @@ public class DataService extends IntentService {
public void handleAddChangeProfileSwitchRecord(JSONObject trJson) throws JSONException { public void handleAddChangeProfileSwitchRecord(JSONObject trJson) throws JSONException {
if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.PROFILESWITCH)) { if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.PROFILESWITCH)) {
if (Config.logIncommingData)
log.debug("Processing ProfileSwitch record: " + trJson.toString());
MainApp.getDbHelper().createProfileSwitchFromJsonIfNotExists(trJson); MainApp.getDbHelper().createProfileSwitchFromJsonIfNotExists(trJson);
} }
} }

View file

@ -1491,14 +1491,58 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return new ArrayList<ProfileSwitch>(); return new ArrayList<ProfileSwitch>();
} }
public void createOrUpdate(ProfileSwitch profileSwitch) { public boolean createOrUpdate(ProfileSwitch profileSwitch) {
profileSwitch.date = profileSwitch.date - profileSwitch.date % 1000;
try { try {
getDaoProfileSwitch().createOrUpdate(profileSwitch); ProfileSwitch old;
scheduleProfileSwitchChange(); 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) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
return false;
} }
public void delete(ProfileSwitch profileSwitch) { public void delete(ProfileSwitch profileSwitch) {
@ -1542,32 +1586,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public void createProfileSwitchFromJsonIfNotExists(JSONObject trJson) { public void createProfileSwitchFromJsonIfNotExists(JSONObject trJson) {
try { try {
QueryBuilder<ProfileSwitch, Long> queryBuilder = null; ProfileSwitch profileSwitch = new ProfileSwitch();
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.date = trJson.getLong("mills"); profileSwitch.date = trJson.getLong("mills");
if (trJson.has("duration")) if (trJson.has("duration"))
profileSwitch.durationInMinutes = trJson.getInt("duration"); profileSwitch.durationInMinutes = trJson.getInt("duration");
profileSwitch._id = trJson.getString("_id"); profileSwitch._id = trJson.getString("_id");
profileSwitch.profileName = trJson.getString("profile"); profileSwitch.profileName = trJson.getString("profile");
profileSwitch.isCPP = trJson.has("CircadianPercentageProfile"); profileSwitch.isCPP = trJson.has("CircadianPercentageProfile");
profileSwitch.source = Source.NIGHTSCOUT;
if (trJson.has("timeshift")) if (trJson.has("timeshift"))
profileSwitch.timeshift = trJson.getInt("timeshift"); profileSwitch.timeshift = trJson.getInt("timeshift");
if (trJson.has("percentage")) if (trJson.has("percentage"))
@ -1577,12 +1603,21 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
if (trJson.has("profilePlugin")) if (trJson.has("profilePlugin"))
profileSwitch.profilePlugin = trJson.getString("profilePlugin"); profileSwitch.profilePlugin = trJson.getString("profilePlugin");
createOrUpdate(profileSwitch); createOrUpdate(profileSwitch);
} catch (SQLException | JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void deleteProfileSwitchById(String _id) { 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 { try {
QueryBuilder<ProfileSwitch, Long> queryBuilder = getDaoProfileSwitch().queryBuilder(); QueryBuilder<ProfileSwitch, Long> queryBuilder = getDaoProfileSwitch().queryBuilder();
Where where = queryBuilder.where(); Where where = queryBuilder.where();
@ -1591,17 +1626,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
List<ProfileSwitch> list = getDaoProfileSwitch().query(preparedQuery); List<ProfileSwitch> list = getDaoProfileSwitch().query(preparedQuery);
if (list.size() == 1) { if (list.size() == 1) {
ProfileSwitch record = list.get(0); return list.get(0);
if (Config.logIncommingData)
log.debug("Removing ProfileSwitch record from database: " + record.log());
delete(record);
} else { } else {
if (Config.logIncommingData) return null;
log.debug("ProfileSwitch not found database: " + _id);
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null;
} }
} }

View file

@ -9,6 +9,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.interfaces.Interval; import info.nightscout.androidaps.interfaces.Interval;
import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface; import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
@ -49,6 +50,41 @@ public class ProfileSwitch implements Interval, DataPointWithLabelInterface {
@DatabaseField @DatabaseField
public int durationInMinutes = 0; 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 --------- // -------- Interval interface ---------
Long cuttedEnd = null; Long cuttedEnd = null;

View file

@ -401,8 +401,9 @@ public class DanaRv2ExecutionService extends Service {
mSerialIOThread.sendMessage(msg); mSerialIOThread.sendMessage(msg);
MsgSetHistoryEntry_v2 msgSetHistoryEntry_v2 = new MsgSetHistoryEntry_v2(DanaRPump.CARBS, carbtime, carbs, 0); MsgSetHistoryEntry_v2 msgSetHistoryEntry_v2 = new MsgSetHistoryEntry_v2(DanaRPump.CARBS, carbtime, carbs, 0);
mSerialIOThread.sendMessage(msgSetHistoryEntry_v2); mSerialIOThread.sendMessage(msgSetHistoryEntry_v2);
lastHistoryFetched = carbtime - 1000; lastHistoryFetched = carbtime - 60000;
} }
if (amount > 0) {
MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables
MainApp.bus().post(new EventDanaRBolusStart()); MainApp.bus().post(new EventDanaRBolusStart());
@ -420,7 +421,8 @@ public class DanaRv2ExecutionService extends Service {
log.debug("Communication stopped"); log.debug("Communication stopped");
} }
} }
waitMsec(1000); }
waitMsec(3000);
bolusingTreatment = null; bolusingTreatment = null;
loadEvents(); loadEvents();
return true; return true;