use of com.eveningoutpost.dexdrip.Models.BgReading in project xDrip by NightscoutFoundation.
the class UploaderTask method doInBackground.
public Void doInBackground(String... urls) {
try {
final List<Long> circuits = new ArrayList<>();
final List<String> types = new ArrayList<>();
types.add(BgReading.class.getSimpleName());
types.add(Calibration.class.getSimpleName());
types.add(BloodTest.class.getSimpleName());
types.add(Treatments.class.getSimpleName());
if (Pref.getBooleanDefaultFalse("wear_sync")) {
circuits.add(UploaderQueue.WATCH_WEARAPI);
}
if (Pref.getBooleanDefaultFalse("cloud_storage_mongodb_enable")) {
circuits.add(UploaderQueue.MONGO_DIRECT);
}
if (Pref.getBooleanDefaultFalse("cloud_storage_api_enable")) {
if ((Pref.getBoolean("cloud_storage_api_use_mobile", true) || (JoH.isLANConnected()))) {
circuits.add(UploaderQueue.NIGHTSCOUT_RESTAPI);
} else {
Log.e(TAG, "Skipping Nightscout upload due to mobile data only");
}
}
if (Pref.getBooleanDefaultFalse("cloud_storage_influxdb_enable")) {
circuits.add(UploaderQueue.INFLUXDB_RESTAPI);
}
for (long THIS_QUEUE : circuits) {
final List<BgReading> bgReadings = new ArrayList<>();
final List<Calibration> calibrations = new ArrayList<>();
final List<BloodTest> bloodtests = new ArrayList<>();
final List<Treatments> treatmentsAdd = new ArrayList<>();
final List<String> treatmentsDel = new ArrayList<>();
final List<UploaderQueue> items = new ArrayList<>();
for (String type : types) {
final List<UploaderQueue> bgups = UploaderQueue.getPendingbyType(type, THIS_QUEUE);
if (bgups != null) {
for (UploaderQueue up : bgups) {
switch(up.action) {
case "insert":
case "update":
case "create":
items.add(up);
if (type.equals(BgReading.class.getSimpleName())) {
final BgReading this_bg = BgReading.byid(up.reference_id);
if (this_bg != null) {
bgReadings.add(this_bg);
} else {
Log.wtf(TAG, "BgReading with ID: " + up.reference_id + " appears to have been deleted");
}
} else if (type.equals(Calibration.class.getSimpleName())) {
final Calibration this_cal = Calibration.byid(up.reference_id);
if ((this_cal != null) && (this_cal.isValid())) {
calibrations.add(this_cal);
} else {
Log.wtf(TAG, "Calibration with ID: " + up.reference_id + " appears to have been deleted");
}
} else if (type.equals(BloodTest.class.getSimpleName())) {
final BloodTest this_bt = BloodTest.byid(up.reference_id);
if (this_bt != null) {
bloodtests.add(this_bt);
} else {
Log.wtf(TAG, "Bloodtest with ID: " + up.reference_id + " appears to have been deleted");
}
} else if (type.equals(Treatments.class.getSimpleName())) {
final Treatments this_treat = Treatments.byid(up.reference_id);
if (this_treat != null) {
treatmentsAdd.add(this_treat);
} else {
Log.wtf(TAG, "Treatments with ID: " + up.reference_id + " appears to have been deleted");
}
}
break;
case "delete":
if ((THIS_QUEUE == UploaderQueue.WATCH_WEARAPI || THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) && type.equals(Treatments.class.getSimpleName())) {
items.add(up);
Log.wtf(TAG, "Delete Treatments with ID: " + up.reference_uuid);
treatmentsDel.add(up.reference_uuid);
} else if (up.reference_uuid != null) {
Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " delete not yet implemented: " + up.reference_uuid);
// mark as completed so as not to tie up the queue for now
up.completed(THIS_QUEUE);
}
break;
default:
Log.e(TAG, "Unsupported operation type for " + type + " " + up.action);
break;
}
}
}
}
if ((bgReadings.size() > 0) || (calibrations.size() > 0) || (bloodtests.size() > 0) || (treatmentsAdd.size() > 0 || treatmentsDel.size() > 0) || (UploaderQueue.getPendingbyType(Treatments.class.getSimpleName(), THIS_QUEUE, 1).size() > 0)) {
Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Processing: " + bgReadings.size() + " BgReadings and " + calibrations.size() + " Calibrations " + bloodtests.size() + " bloodtests " + treatmentsAdd.size() + " treatmentsAdd " + treatmentsDel.size() + " treatmentsDel");
boolean uploadStatus = false;
if (THIS_QUEUE == UploaderQueue.MONGO_DIRECT) {
final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
uploadStatus = uploader.uploadMongo(bgReadings, calibrations, calibrations);
} else if (THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) {
final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
uploadStatus = uploader.uploadRest(bgReadings, bloodtests, calibrations);
} else if (THIS_QUEUE == UploaderQueue.INFLUXDB_RESTAPI) {
final InfluxDBUploader influxDBUploader = new InfluxDBUploader(xdrip.getAppContext());
uploadStatus = influxDBUploader.upload(bgReadings, calibrations, calibrations);
} else if (THIS_QUEUE == UploaderQueue.WATCH_WEARAPI) {
uploadStatus = WatchUpdaterService.sendWearUpload(bgReadings, calibrations, bloodtests, treatmentsAdd, treatmentsDel);
}
// TODO some kind of fail counter?
if (uploadStatus) {
for (UploaderQueue up : items) {
// approve all types for this queue
up.completed(THIS_QUEUE);
}
Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Marking: " + items.size() + " Items as successful");
if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
Log.d(TAG, "Scheduling boosted repeat query");
SyncService.startSyncService(2000);
}
}
} else {
Log.d(TAG, "Nothing to upload for: " + UploaderQueue.getCircuitName(THIS_QUEUE));
if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
PersistentStore.setBoolean(BACKFILLING_BOOSTER, false);
Log.d(TAG, "Switched off backfilling booster");
}
}
}
} catch (Exception e) {
Log.e(TAG, "caught exception", e);
exception = e;
return null;
}
return null;
}
use of com.eveningoutpost.dexdrip.Models.BgReading in project xDrip-plus by jamorham.
the class NightscoutBackfillActivity method backfillRun.
public synchronized void backfillRun(View v) {
locked = JoH.tsl();
doitButton.setVisibility(View.INVISIBLE);
JoH.static_toast_long("Please wait..");
new Thread(new Runnable() {
@Override
public void run() {
final PowerManager.WakeLock wl = JoH.getWakeLock("nightscout-backfill", 600000);
try {
final List<BgReading> the_readings = BgReading.latestForGraphAsc(500000, calendar.getTimeInMillis(), JoH.tsl());
if ((the_readings != null) && (the_readings.size() > 0)) {
PersistentStore.setBoolean(UploaderTask.BACKFILLING_BOOSTER, true);
long bgcount = the_readings.size();
long trcount = 0;
for (BgReading bg : the_readings) {
UploaderQueue.newEntry("update", bg);
}
final List<Treatments> the_treatments = Treatments.latestForGraph(50000, calendar.getTimeInMillis(), JoH.tsl());
if ((the_treatments != null) && (the_treatments.size() > 0)) {
trcount = the_treatments.size();
for (Treatments tr : the_treatments) {
UploaderQueue.newEntry("update", tr);
}
}
// TODO Calibrations? Blood tests?
JoH.static_toast_long("Queued " + bgcount + " glucose readings and " + trcount + " treatments!");
SyncService.startSyncService(500);
// clear lock
locked = 0;
} else {
JoH.static_toast_long("Didn't find any glucose readings in that time period");
}
} finally {
JoH.releaseWakeLock(wl);
}
}
}).start();
finish();
}
use of com.eveningoutpost.dexdrip.Models.BgReading in project xDrip-plus by jamorham.
the class InfluxDBUploader method upload.
public boolean upload(List<BgReading> glucoseDataSets, List<Calibration> meterRecords, List<Calibration> calRecords) {
try {
BatchPoints batchPoints = BatchPoints.database(dbName).retentionPolicy("autogen").consistency(InfluxDB.ConsistencyLevel.ALL).build();
for (BgReading record : glucoseDataSets) {
if (record == null) {
Log.e(TAG, "InfluxDB glucose record is null");
continue;
}
batchPoints.point(createGlucosePoint(record));
}
for (Calibration record : meterRecords) {
if (record == null) {
Log.e(TAG, "InfluxDB meter record is null");
continue;
}
batchPoints.point(createMeterPoint(record));
}
for (Calibration record : calRecords) {
if (record == null) {
Log.e(TAG, "InfluxDB calibration record is null");
continue;
}
if (record.slope == 0d)
continue;
batchPoints.point(createCalibrationPoint(record));
}
try {
Log.d(TAG, "Influx url: " + dbUri);
InfluxDBFactory.connect(dbUri, dbUser, dbPassword, client).enableGzip().write(batchPoints);
last_error = null;
return true;
} catch (java.lang.ExceptionInInitializerError e) {
Log.e(TAG, "InfluxDB failed: " + e.getCause());
return false;
} catch (java.lang.NoClassDefFoundError e) {
Log.e(TAG, "InfluxDB failed more: " + e);
return false;
} catch (IllegalArgumentException e) {
Log.wtf(TAG, "InfluxDB problem: " + e);
return false;
} catch (Exception e) {
Log.e(TAG, "Write to InfluxDB failed: " + e);
last_error = e.getMessage();
return false;
}
} catch (Exception e) {
Log.wtf(TAG, "Exception during initialization: ", e);
return false;
}
}
use of com.eveningoutpost.dexdrip.Models.BgReading in project xDrip-plus by jamorham.
the class WixelReader method setSerialDataToTransmitterRawData.
private void setSerialDataToTransmitterRawData(int raw_data, int filtered_data, int sensor_battery_leve, Long CaptureTime) {
final TransmitterData transmitterData = TransmitterData.create(raw_data, filtered_data, sensor_battery_leve, CaptureTime);
if (transmitterData != null) {
final Sensor sensor = Sensor.currentSensor();
if (sensor != null) {
BgReading bgReading = BgReading.create(transmitterData.raw_data, filtered_data, null, CaptureTime);
// sensor.latest_battery_level = (sensor.latest_battery_level!=0)?Math.min(sensor.latest_battery_level, transmitterData.sensor_battery_level):transmitterData.sensor_battery_level;
// don't lock it only going downwards
sensor.latest_battery_level = transmitterData.sensor_battery_level;
sensor.save();
} else {
Log.d(TAG, "No Active Sensor, Data only stored in Transmitter Data");
}
}
}
use of com.eveningoutpost.dexdrip.Models.BgReading in project xDrip-plus by jamorham.
the class WatchUpdaterService method resendData.
private void resendData() {
Log.d(TAG, "resendData ENTER");
forceGoogleApiConnect();
Log.d(TAG, "resendData googleApiClient connected ENTER");
long startTime = new Date().getTime() - (60000 * 60 * 24);
BgReading last_bg = BgReading.last();
if (last_bg != null) {
List<BgReading> graph_bgs = BgReading.latestForGraph(60, startTime);
BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(getApplicationContext());
if (!graph_bgs.isEmpty()) {
final int battery = PowerStateReceiver.getBatteryLevel(getApplicationContext());
DataMap entries = dataMap(last_bg, mPrefs, bgGraphBuilder, battery);
final ArrayList<DataMap> dataMaps = new ArrayList<>(graph_bgs.size());
for (BgReading bg : graph_bgs) {
dataMaps.add(dataMap(bg, mPrefs, bgGraphBuilder, battery));
}
// MOST IMPORTANT LINE FOR TIMESTAMP
entries.putLong("time", new Date().getTime());
entries.putDataMapArrayList("entries", dataMaps);
if (mPrefs.getBoolean("extra_status_line", false)) {
entries.putString("extra_status_line", Home.extraStatusLine());
}
new SendToDataLayerThread(WEARABLE_DATA_PATH, googleApiClient).executeOnExecutor(xdrip.executor, entries);
}
}
}
Aggregations