This commit is contained in:
Milos Kozak 2019-03-17 18:51:16 +01:00
parent f9904d15d8
commit 18ba5878d2
3 changed files with 23 additions and 13 deletions

View file

@ -464,7 +464,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
} catch (SQLException e) { } catch (SQLException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
return new ArrayList<BgReading>(); return new ArrayList<>();
} }
public List<BgReading> getBgreadingsDataFromTime(long start, long end, boolean ascending) { public List<BgReading> getBgreadingsDataFromTime(long start, long end, boolean ascending) {
@ -481,7 +481,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
} catch (SQLException e) { } catch (SQLException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
return new ArrayList<BgReading>(); return new ArrayList<>();
} }
public List<BgReading> getAllBgreadingsDataFromTime(long mills, boolean ascending) { public List<BgReading> getAllBgreadingsDataFromTime(long mills, boolean ascending) {

View file

@ -65,8 +65,6 @@ public class IobCobCalculatorPlugin extends PluginBase {
private volatile List<BgReading> bgReadings = null; // newest at index 0 private volatile List<BgReading> bgReadings = null; // newest at index 0
private volatile List<BgReading> bucketed_data = null; private volatile List<BgReading> bucketed_data = null;
private double dia = Constants.defaultDIA;
final Object dataLock = new Object(); final Object dataLock = new Object();
boolean stopCalculationTrigger = false; boolean stopCalculationTrigger = false;
@ -118,11 +116,22 @@ public class IobCobCalculatorPlugin extends PluginBase {
return rounded; return rounded;
} }
void loadBgData(long now) { void loadBgData(long to) {
long start = (long) (now - 60 * 60 * 1000L * (24 + dia)); Profile profile = ProfileFunctions.getInstance().getProfile(to);
bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime(start, now, false); double dia = Constants.defaultDIA;
if (L.isEnabled(L.AUTOSENS)) if (profile != null) dia = profile.getDia();
log.debug("BG data loaded. Size: " + bgReadings.size() + " Start date: " + DateUtil.dateAndTimeString(start) + " End date: " + DateUtil.dateAndTimeString(now)); long start = to - T.hours((long) (24 + dia)).msecs();
if (DateUtil.isCloseToNow(to)) {
// if close to now expect there can be some readings with time in close future (caused by wrong time setting)
// so read all records
bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime(start, false);
if (L.isEnabled(L.AUTOSENS))
log.debug("BG data loaded. Size: " + bgReadings.size() + " Start date: " + DateUtil.dateAndTimeString(start));
} else {
bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime(start, to, false);
if (L.isEnabled(L.AUTOSENS))
log.debug("BG data loaded. Size: " + bgReadings.size() + " Start date: " + DateUtil.dateAndTimeString(start) + " End date: " + DateUtil.dateAndTimeString(to));
}
} }
public boolean isAbout5minData() { public boolean isAbout5minData() {
@ -608,10 +617,6 @@ public class IobCobCalculatorPlugin extends PluginBase {
} }
if (ConfigBuilderPlugin.getPlugin() == null) if (ConfigBuilderPlugin.getPlugin() == null)
return; // app still initializing return; // app still initializing
Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile == null)
return; // app still initializing
dia = profile.getDia();
if (ev == null) { // on init no need of reset if (ev == null) { // on init no need of reset
return; return;
} }

View file

@ -182,4 +182,9 @@ public class DateUtil {
public static long roundDateToSec(long date) { public static long roundDateToSec(long date) {
return date - date % 1000; return date - date % 1000;
} }
public static boolean isCloseToNow(long date) {
long diff = Math.abs(date - now());
return diff < T.mins(2).msecs();
}
} }