use of com.eveningoutpost.dexdrip.Models.LibreBlock in project xDrip by NightscoutFoundation.
the class LibreWifiReader method readData.
private void readData() {
// Start very simply by getting only one object and using it.
Log.e(TAG, "readData called");
Long LastReportedTime = 0L;
// TODO change that to work based on readings as well ???
// Allow some packets from the future.
LibreBlock libreBlock = LibreBlock.getLatestForTrend(0L, JoH.tsl() + 5 * 60000);
if (libreBlock != null) {
LastReportedTime = libreBlock.timestamp;
Log.e(TAG, "LastReportedTime = " + JoH.dateTimeText(LastReportedTime));
// jamorham fix to avoid going twice to network when we just got a packet
if ((new Date().getTime() - LastReportedTime) < Constants.MINUTE_IN_MS - 2000) {
Log.d(TAG, "Already have a recent packet - returning");
if (JoH.ratelimit("deferred-msg", 60)) {
statusLog(" Deferred", "Already have recent reading");
}
return;
} else {
statusLog(" Deferred", "");
}
}
String recieversIpAddresses;
// This is the same ip location as for the wixelreader.
if (!WixelReader.IsConfigured()) {
Log.e(TAG, "LibreWifiReader not configured");
return;
}
if ((DexCollectionType.getDexCollectionType() == DexCollectionType.Mock) && Home.get_engineering_mode()) {
recieversIpAddresses = "fake://FAKE_DATA";
} else {
recieversIpAddresses = Pref.getString("wifi_recievers_addresses", "");
}
int packetsToRead = isLibre2() ? 5 : 1;
Log.d(TAG, "reading " + packetsToRead + " packets");
LibreWifiData[] LibreWifiDataArr = Read(recieversIpAddresses, packetsToRead);
Log.d(TAG, "After reading ..." + LibreWifiDataArr);
if (LibreWifiDataArr == null || LibreWifiDataArr.length == 0) {
return;
}
Log.d(TAG, "After verification ... size = " + LibreWifiDataArr.length);
// Last in the array is the most updated reading we have.
for (LibreWifiData LastReading : LibreWifiDataArr) {
// Last in the array is the most updated reading we have.
// TransmitterRawData LastReading = LastReadingArr[LastReadingArr.length -1];
// if (LastReading.CaptureDateTime > LastReportedReading + 5000) {
// Make sure we do not report packets from the far future...
Log.d(TAG, "checking packet from " + JoH.dateTimeText(LastReading.CaptureDateTime));
if ((LastReading.CaptureDateTime > LastReportedTime + Constants.MINUTE_IN_MS - 5000) && LastReading.CaptureDateTime < new Date().getTime() + 2 * Constants.MINUTE_IN_MS) {
// We have a real new reading...
Log.d(TAG, "will call with packet from " + JoH.dateTimeText(LastReading.CaptureDateTime));
byte[] data = Base64.decode(LastReading.BlockBytes, Base64.DEFAULT);
byte[] patchUid = null;
if (LastReading.patchUid != null && (!LastReading.patchUid.isEmpty())) {
patchUid = Base64.decode(LastReading.patchUid, Base64.DEFAULT);
}
byte[] patchInfo = null;
if (LastReading.patchInfo != null && (!LastReading.patchInfo.isEmpty())) {
patchInfo = Base64.decode(LastReading.patchInfo, Base64.DEFAULT);
}
if (data.length == 46) {
// This is the libre 2 case
LibreBluetooth.SendData(data, LastReading.CaptureDateTime);
LastReportedTime = LastReading.CaptureDateTime;
} else {
// This is the libre 1 case
boolean checksum_ok = NFCReaderX.HandleGoodReading(LastReading.SensorId, data, LastReading.CaptureDateTime, false, patchUid, patchInfo);
if (checksum_ok) {
Log.d(TAG, "checksum ok updating LastReportedTime to " + JoH.dateTimeText(LastReading.CaptureDateTime));
// TODO use battery, and other interesting data.
LastReportedTime = LastReading.CaptureDateTime;
PersistentStore.setString("Tomatobattery", Integer.toString(LastReading.TomatoBatteryLife));
Pref.setInt("bridge_battery", LastReading.TomatoBatteryLife);
PersistentStore.setString("TomatoHArdware", LastReading.HwVersion);
PersistentStore.setString("TomatoFirmware", LastReading.FwVersion);
Log.i(TAG, "LastReading.SensorId " + LastReading.SensorId);
PersistentStore.setString("LibreSN", LastReading.SensorId);
if (SensorSanity.checkLibreSensorChangeIfEnabled(LastReading.SensorId)) {
Log.e(TAG, "Problem with Libre Serial Number - not processing");
return;
}
} else {
Log.e(TAG, "Recieved a pacjet with bad checksum");
}
}
}
}
}
use of com.eveningoutpost.dexdrip.Models.LibreBlock in project xDrip by NightscoutFoundation.
the class LibreWifiReader method timeForNextRead.
static Long timeForNextRead() {
// Allow some packets from the future.
LibreBlock libreBlock = LibreBlock.getLatestForTrend(0L, JoH.tsl() + 5 * 60000);
if (libreBlock == null) {
// We did not receive a packet, well someone hopefully is looking at data, return relatively fast
Log.e(TAG, "libreBlock == null returning 60000");
return 60 * 1000L;
}
Long gapTime = new Date().getTime() - libreBlock.timestamp;
Log.d(TAG, "gapTime = " + gapTime);
if (gapTime < 0) {
// There is some confusion here (clock was readjusted?)
Log.e(TAG, "gapTime <= 0 returning 60000");
return 60 * 1000L;
}
long sensor_period = isLibre2() ? LIBRE_2_PERIOD : LIBRE_1_PERIOD;
if (gapTime < sensor_period) {
// 300000 - gaptime is when we expect to have the next packet.
return (sensor_period - gapTime) + 2000;
}
gapTime = gapTime % sensor_period;
Log.d(TAG, "modulus gapTime = " + gapTime);
if (gapTime < 10000) {
// A new packet should arrive any second now
return 10000L;
}
if (gapTime < 60000) {
// A new packet should arrive but chance is we have missed it...
return 30000L;
}
return (sensor_period - gapTime) + 2000;
}
use of com.eveningoutpost.dexdrip.Models.LibreBlock in project xDrip by NightscoutFoundation.
the class GcmListenerSvc method HandleLibreBlock.
private void HandleLibreBlock(String payload) {
LibreBlock lb = LibreBlock.createFromExtendedJson(payload);
if (lb == null) {
return;
}
if (LibreBlock.getForTimestamp(lb.timestamp) != null) {
// We already seen this one.
return;
}
LibreBlock.Save(lb);
PersistentStore.setString("LibreSN", lb.reference);
if (Home.get_master()) {
if (SensorSanity.checkLibreSensorChangeIfEnabled(lb.reference)) {
Log.e(TAG, "Problem with Libre Serial Number - not processing");
}
NFCReaderX.HandleGoodReading(lb.reference, lb.blockbytes, lb.timestamp, false, lb.patchUid, lb.patchInfo);
}
}
use of com.eveningoutpost.dexdrip.Models.LibreBlock in project xDrip by NightscoutFoundation.
the class UploaderTask method doInBackground.
public Void doInBackground(String... urls) {
try {
Log.d(TAG, "UploaderTask doInBackground called");
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());
types.add(TransmitterData.class.getSimpleName());
types.add(LibreBlock.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<TransmitterData> transmittersData = new ArrayList<>();
final List<LibreBlock> libreBlock = 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) {
if (this_cal.isValid()) {
calibrations.add(this_cal);
} else {
Log.d(TAG, "Calibration with ID: " + up.reference_id + " is marked invalid");
}
} 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");
}
} else if (type.equals(TransmitterData.class.getSimpleName())) {
final TransmitterData this_transmitterData = TransmitterData.byid(up.reference_id);
if (this_transmitterData != null) {
transmittersData.add(this_transmitterData);
} else {
Log.wtf(TAG, "TransmitterData with ID: " + up.reference_id + " appears to have been deleted");
}
} else if (type.equals(LibreBlock.class.getSimpleName())) {
final LibreBlock this_LibreBlock = LibreBlock.byid(up.reference_id);
if (this_LibreBlock != null) {
libreBlock.add(this_LibreBlock);
} else {
Log.wtf(TAG, "LibreBlock 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) || (transmittersData.size() > 0) || (libreBlock.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, transmittersData, libreBlock);
} 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);
}
if (retry_timer) {
// standard retry timer
SyncService.startSyncService(Constants.MINUTE_IN_MS * 6);
}
// 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.LibreBlock in project xDrip by NightscoutFoundation.
the class NightscoutUploader method doMongoUpload.
private boolean doMongoUpload(SharedPreferences prefs, List<BgReading> glucoseDataSets, List<Calibration> meterRecords, List<Calibration> calRecords, List<TransmitterData> transmittersData, List<LibreBlock> libreBlock) {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
format.setTimeZone(TimeZone.getDefault());
final String dbURI = prefs.getString("cloud_storage_mongodb_uri", null);
if (dbURI != null) {
try {
final URI uri = new URI(dbURI.trim());
if ((uri.getHost().startsWith("192.168.")) && prefs.getBoolean("skip_lan_uploads_when_no_lan", true) && (!JoH.isLANConnected())) {
Log.d(TAG, "Skipping mongo upload to: " + dbURI + " due to no LAN connection");
return false;
}
} catch (URISyntaxException e) {
UserError.Log.e(TAG, "Invalid mongo URI: " + e);
}
}
final String collectionName = prefs.getString("cloud_storage_mongodb_collection", null);
final String dsCollectionName = prefs.getString("cloud_storage_mongodb_device_status_collection", "devicestatus");
if (dbURI != null && collectionName != null) {
try {
// connect to db
MongoClientURI uri = new MongoClientURI(dbURI.trim() + "?socketTimeoutMS=180000");
MongoClient client = new MongoClient(uri);
// get db
DB db = client.getDB(uri.getDatabase());
// get collection
DBCollection dexcomData = db.getCollection(collectionName.trim());
try {
Log.i(TAG, "The number of EGV records being sent to MongoDB is " + glucoseDataSets.size());
for (BgReading record : glucoseDataSets) {
// make db object
BasicDBObject testData = new BasicDBObject();
testData.put("device", "xDrip-" + prefs.getString("dex_collection_method", "BluetoothWixel"));
if (record != null) {
// KS
testData.put("date", record.timestamp);
testData.put("dateString", format.format(record.timestamp));
testData.put("sgv", Math.round(record.calculated_value));
testData.put("direction", record.slopeName());
testData.put("type", "sgv");
testData.put("filtered", record.ageAdjustedFiltered() * 1000);
testData.put("unfiltered", record.usedRaw() * 1000);
testData.put("rssi", 100);
testData.put("noise", record.noiseValue());
dexcomData.insert(testData, WriteConcern.UNACKNOWLEDGED);
} else
Log.e(TAG, "MongoDB BG record is null.");
}
Log.i(TAG, "REST - The number of MBG records being sent to MongoDB is " + meterRecords.size());
for (Calibration meterRecord : meterRecords) {
// make db object
BasicDBObject testData = new BasicDBObject();
testData.put("device", "xDrip-" + prefs.getString("dex_collection_method", "BluetoothWixel"));
testData.put("type", "mbg");
testData.put("date", meterRecord.timestamp);
testData.put("dateString", format.format(meterRecord.timestamp));
testData.put("mbg", meterRecord.bg);
dexcomData.insert(testData, WriteConcern.UNACKNOWLEDGED);
}
Log.i(TAG, "REST - Finshed upload of mbg");
for (Calibration calRecord : calRecords) {
// do not upload undefined slopes
if (calRecord.slope == 0d)
break;
// make db object
BasicDBObject testData = new BasicDBObject();
testData.put("device", "xDrip-" + prefs.getString("dex_collection_method", "BluetoothWixel"));
testData.put("date", calRecord.timestamp);
testData.put("dateString", format.format(calRecord.timestamp));
if (calRecord.check_in) {
testData.put("slope", (calRecord.first_slope));
testData.put("intercept", ((calRecord.first_intercept)));
testData.put("scale", calRecord.first_scale);
} else {
testData.put("slope", (1000 / calRecord.slope));
testData.put("intercept", ((calRecord.intercept * -1000) / (calRecord.slope)));
testData.put("scale", 1);
}
testData.put("type", "cal");
dexcomData.insert(testData, WriteConcern.UNACKNOWLEDGED);
}
DBCollection libreCollection = db.getCollection("libre");
for (LibreBlock libreBlockEntry : libreBlock) {
Log.d(TAG, "uploading new item to mongo");
// Checksum might be wrong, for libre 2 or libre us 14 days.
boolean ChecksumOk = LibreUtils.verify(libreBlockEntry.blockbytes);
// make db object
BasicDBObject testData = new BasicDBObject();
testData.put("SensorId", PersistentStore.getString("LibreSN"));
testData.put("CaptureDateTime", libreBlockEntry.timestamp);
testData.put("BlockBytes", Base64.encodeToString(libreBlockEntry.blockbytes, Base64.NO_WRAP));
if (libreBlockEntry.patchUid != null && libreBlockEntry.patchUid.length != 0) {
testData.put("patchUid", Base64.encodeToString(libreBlockEntry.patchUid, Base64.NO_WRAP));
}
if (libreBlockEntry.patchInfo != null && libreBlockEntry.patchInfo.length != 0) {
testData.put("patchInfo", Base64.encodeToString(libreBlockEntry.patchInfo, Base64.NO_WRAP));
}
testData.put("ChecksumOk", ChecksumOk ? 1 : 0);
testData.put("Uploaded", 1);
testData.put("UploaderBatteryLife", getBatteryLevel());
testData.put("DebugInfo", android.os.Build.MODEL + " " + new Date(libreBlockEntry.timestamp).toLocaleString());
try {
testData.put("TomatoBatteryLife", Integer.parseInt(PersistentStore.getString("Tomatobattery")));
} catch (NumberFormatException e) {
Log.e(TAG, "Error reading battery daya" + PersistentStore.getString("Tomatobattery"));
}
testData.put("FwVersion", PersistentStore.getString("TomatoFirmware"));
testData.put("HwVersion", PersistentStore.getString("TomatoHArdware"));
WriteResult wr = libreCollection.insert(testData, WriteConcern.ACKNOWLEDGED);
Log.d(TAG, "uploaded libreblock data with " + new Date(libreBlockEntry.timestamp).toLocaleString() + " wr = " + wr);
}
// TODO: quick port from original code, revisit before release
DBCollection dsCollection = db.getCollection(dsCollectionName);
BasicDBObject devicestatus = new BasicDBObject();
devicestatus.put("uploaderBattery", getBatteryLevel());
devicestatus.put("created_at", format.format(System.currentTimeMillis()));
dsCollection.insert(devicestatus, WriteConcern.UNACKNOWLEDGED);
// treatments mongo sync using unified queue
Log.d(TAG, "Starting treatments mongo direct");
final long THIS_QUEUE = UploaderQueue.MONGO_DIRECT;
final DBCollection treatmentDb = db.getCollection("treatments");
final List<UploaderQueue> tups = UploaderQueue.getPendingbyType(Treatments.class.getSimpleName(), THIS_QUEUE);
if (tups != null) {
for (UploaderQueue up : tups) {
if ((up.action.equals("insert") || (up.action.equals("update")))) {
Treatments treatment = Treatments.byid(up.reference_id);
if (treatment != null) {
BasicDBObject record = new BasicDBObject();
record.put("timestamp", treatment.timestamp);
record.put("eventType", treatment.eventType);
record.put("enteredBy", treatment.enteredBy);
if (treatment.notes != null)
record.put("notes", treatment.notes);
record.put("uuid", treatment.uuid);
record.put("carbs", treatment.carbs);
record.put("insulin", treatment.insulin);
if (treatment.insulinJSON != null) {
record.put("insulinInjections", treatment.insulinJSON);
}
record.put("created_at", treatment.created_at);
final BasicDBObject searchQuery = new BasicDBObject().append("uuid", treatment.uuid);
// treatmentDb.insert(record, WriteConcern.UNACKNOWLEDGED);
Log.d(TAG, "Sending upsert for: " + treatment.toJSON());
treatmentDb.update(searchQuery, record, true, false);
} else {
Log.d(TAG, "Got null for treatment id: " + up.reference_id);
}
up.completed(THIS_QUEUE);
} else if (up.action.equals("delete")) {
if (up.reference_uuid != null) {
Log.d(TAG, "Processing treatment delete mongo sync for: " + up.reference_uuid);
final BasicDBObject searchQuery = new BasicDBObject().append("uuid", up.reference_uuid);
Log.d(TAG, treatmentDb.remove(searchQuery, WriteConcern.UNACKNOWLEDGED).toString());
}
up.completed(THIS_QUEUE);
} else {
Log.e(TAG, "Unsupported operation type for treatment: " + up.action);
}
}
Log.d(TAG, "Processed " + tups.size() + " Treatment mongo direct upload records");
}
client.close();
failurecount = 0;
return true;
} catch (Exception e) {
Log.e(TAG, "Unable to upload data to mongo " + e.getMessage());
failurecount++;
if (failurecount > 4) {
Home.toaststaticnext("Mongo " + failurecount + " up fails: " + e.getMessage().substring(0, 51));
}
} finally {
if (client != null) {
client.close();
}
}
} catch (Exception e) {
Log.e(TAG, "Unable to upload data to mongo " + e.getMessage());
}
}
return false;
}
Aggregations