use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip by NightscoutFoundation.
the class NightscoutUploader method postHeartRate.
private void postHeartRate(NightscoutService nightscoutService, String apiSecret) throws Exception {
Log.d(TAG, "Processing heartrate for RESTAPI");
if (apiSecret != null) {
final String STORE_COUNTER = "nightscout-rest-heartrate-synced-time";
final long syncedTillTime = Math.max(PersistentStore.getLong(STORE_COUNTER), JoH.tsl() - Constants.DAY_IN_MS * 7);
final List<HeartRate> readings = HeartRate.latestForGraph((MAX_ACTIVITY_RECORDS / Math.min(1, Math.max(activityErrorCount, MAX_ACTIVITY_RECORDS / 10))), syncedTillTime);
final JSONArray data = new JSONArray();
// final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
// format.setTimeZone(TimeZone.getDefault());
long highest_timestamp = 0;
if (readings.size() > 0) {
for (HeartRate reading : readings) {
final JSONObject json = new JSONObject();
json.put("type", "hr-bpm");
json.put("timeStamp", reading.timestamp);
// json.put("dateString", format.format(reading.timestamp));
json.put("created_at", DateUtil.toISOString(reading.timestamp));
json.put("bpm", reading.bpm);
if (reading.accuracy != 1)
json.put("accuracy", reading.accuracy);
data.put(json);
highest_timestamp = Math.max(highest_timestamp, reading.timestamp);
}
// send to nightscout - update counter
final RequestBody body = RequestBody.create(MediaType.parse("application/json"), data.toString());
Response<ResponseBody> r;
r = nightscoutService.uploadActivity(apiSecret, body).execute();
if (!r.isSuccess()) {
activityErrorCount++;
if (JoH.ratelimit("heartrate-unable-upload", 3600)) {
UserError.Log.e(TAG, "Unable to upload heart-rate data to Nightscout - check nightscout version");
}
throw new UploaderException(r.message(), r.code());
} else {
PersistentStore.setLong(STORE_COUNTER, highest_timestamp);
UserError.Log.d(TAG, "Updating heartrate synced record count (success) " + JoH.dateTimeText(highest_timestamp) + " Processed: " + readings.size() + " records");
checkGzipSupport(r);
}
}
} else {
UserError.Log.e(TAG, "Api secret is null");
}
}
use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip by NightscoutFoundation.
the class PebbleWatchSync method init.
protected void init() {
Log.i(TAG, "Initialising...");
Log.i(TAG, "configuring PebbleDataReceiver for: " + currentWatchFaceUUID.toString());
PebbleKit.registerReceivedDataHandler(context, new PebbleKit.PebbleDataReceiver(currentWatchFaceUUID) {
@Override
public void receiveData(final Context context, final int transactionId, final PebbleDictionary data) {
getActivePebbleDisplay().receiveData(transactionId, data);
}
});
PebbleKit.registerReceivedAckHandler(context, new PebbleKit.PebbleAckReceiver(currentWatchFaceUUID) {
@Override
public void receiveAck(Context context, int transactionId) {
getActivePebbleDisplay().receiveAck(transactionId);
}
});
PebbleKit.registerReceivedNackHandler(context, new PebbleKit.PebbleNackReceiver(currentWatchFaceUUID) {
@Override
public void receiveNack(Context context, int transactionId) {
getActivePebbleDisplay().receiveNack(transactionId);
}
});
PebbleKit.registerDataLogReceiver(context, new PebbleKit.PebbleDataLogReceiver(currentWatchFaceUUID) {
@Override
public void receiveData(Context context, UUID logUuid, Long timestamp, Long tag, int data) {
if (d)
Log.d(TAG, "receiveLogData: uuid:" + logUuid + " " + JoH.dateTimeText(timestamp * 1000) + " tag:" + tag + " data: " + data);
}
@Override
public void receiveData(Context context, UUID logUuid, Long timestamp, Long tag, Long data) {
Log.d(TAG, "receiveLogData: uuid:" + logUuid + " started: " + JoH.dateTimeText(timestamp * 1000) + " tag:" + tag + " data: " + data);
if (Pref.getBoolean("use_pebble_health", true)) {
if ((tag != null) && (data != null)) {
// alternator
final int s = ((int) (long) tag) & 0xfffffff7;
switch(s) {
case HEARTRATE_LOG:
if (data > sanity_timestamp) {
if (last_heartrate_timestamp > 0) {
Log.e(TAG, "Out of sequence heartrate timestamp received!");
}
last_heartrate_timestamp = data;
} else {
if (data > 0) {
if (last_heartrate_timestamp > 0) {
final HeartRate hr = new HeartRate();
hr.timestamp = last_heartrate_timestamp * 1000;
hr.bpm = (int) (long) data;
Log.d(TAG, "Saving HeartRate: " + hr.toS());
hr.saveit();
// reset state
last_heartrate_timestamp = 0;
} else {
Log.e(TAG, "Out of sequence heartrate value received!");
}
}
}
break;
case MOVEMENT_LOG:
if (data > sanity_timestamp) {
if (last_movement_timestamp > 0) {
Log.e(TAG, "Out of sequence movement timestamp received!");
}
last_movement_timestamp = data;
} else {
if (data > 0) {
if (last_movement_timestamp > 0) {
final StepCounter pm = StepCounter.createEfficientRecord(last_movement_timestamp * 1000, (int) (long) data);
Log.d(TAG, "Saving Movement: " + pm.toS());
// reset state
last_movement_timestamp = 0;
} else {
Log.e(TAG, "Out of sequence movement value received!");
}
}
}
break;
default:
Log.e(TAG, "Unknown pebble data log type received: " + s);
break;
}
} else {
Log.e(TAG, "Got null Long in receive data");
}
}
}
@Override
public void receiveData(Context context, UUID logUuid, Long timestamp, Long tag, byte[] data) {
if (d)
Log.d(TAG, "receiveLogData: uuid:" + logUuid + " " + JoH.dateTimeText(timestamp * 1000) + " tag:" + tag + " hexdata: " + JoH.bytesToHex(data));
}
@Override
public void onFinishSession(Context context, UUID logUuid, Long timestamp, Long tag) {
if (d)
Log.i(TAG, "Session " + tag + " finished!");
}
});
// control app
PebbleKit.registerReceivedDataHandler(context, new PebbleKit.PebbleDataReceiver(PEBBLE_CONTROL_APP_UUID) {
@Override
public void receiveData(final Context context, final int transactionId, final PebbleDictionary data) {
getActivePebbleDisplay().receiveAppData(transactionId, data);
}
});
}
use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip by NightscoutFoundation.
the class BgSendQueue method getSensorSteps.
public static DataMap getSensorSteps(SharedPreferences prefs) {
Log.d("BgSendQueue", "getSensorSteps");
DataMap dataMap = new DataMap();
final long t = System.currentTimeMillis();
final PebbleMovement pm = PebbleMovement.last();
final boolean show_steps = prefs.getBoolean("showSteps", true);
final boolean show_heart_rate = prefs.getBoolean("showHeartRate", true);
final boolean use_wear_health = prefs.getBoolean("use_wear_health", true);
if (use_wear_health || show_steps) {
boolean sameDay = pm != null ? ListenerService.isSameDay(t, pm.timestamp) : false;
if (!sameDay) {
dataMap.putInt("steps", 0);
dataMap.putLong("steps_timestamp", t);
Log.d("BgSendQueue", "getSensorSteps isSameDay false t=" + JoH.dateTimeText(t));
} else {
dataMap.putInt("steps", pm.metric);
dataMap.putLong("steps_timestamp", pm.timestamp);
Log.d("BgSendQueue", "getSensorSteps isSameDay true pm.timestamp=" + JoH.dateTimeText(pm.timestamp) + " metric=" + pm.metric);
}
}
if (use_wear_health && show_heart_rate) {
final HeartRate lastHeartRateReading = HeartRate.last();
if (lastHeartRateReading != null) {
dataMap.putInt("heart_rate", lastHeartRateReading.bpm);
dataMap.putLong("heart_rate_timestamp", lastHeartRateReading.timestamp);
}
}
return dataMap;
}
use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip by NightscoutFoundation.
the class HeartRateService method getWearHeartSensorData.
public static synchronized DataMap getWearHeartSensorData(int count, long last_send_time, int min_count) {
UserError.Log.d(TAG, "getWearHeartSensorData last_send_time:" + JoH.dateTimeText(last_send_time));
if ((count != 0) || (JoH.ratelimit("heartrate-datamap", 5))) {
HeartRate last_log = HeartRate.last();
if (last_log != null) {
UserError.Log.d(TAG, "getWearHeartSensorData last_log.timestamp:" + JoH.dateTimeText((long) last_log.timestamp));
} else {
UserError.Log.d(TAG, "getWearHeartSensorData HeartRate.last() = null:");
return null;
}
if (last_log != null && last_send_time <= last_log.timestamp) {
// startTime
long last_send_success = last_send_time;
UserError.Log.d(TAG, "getWearHeartSensorData last_send_time < last_bg.timestamp:" + JoH.dateTimeText((long) last_log.timestamp));
List<HeartRate> logs = HeartRate.latestForGraph(count, last_send_time);
if (!logs.isEmpty() && logs.size() > min_count) {
DataMap entries = dataMap(last_log);
final ArrayList<DataMap> dataMaps = new ArrayList<>(logs.size());
for (HeartRate log : logs) {
dataMaps.add(dataMap(log));
last_send_success = (long) log.timestamp;
}
// MOST IMPORTANT LINE FOR TIMESTAMP
entries.putLong("time", JoH.tsl());
entries.putDataMapArrayList("entries", dataMaps);
UserError.Log.i(TAG, "getWearHeartSensorData SYNCED up to " + JoH.dateTimeText(last_send_success) + " count = " + logs.size());
return entries;
} else
UserError.Log.i(TAG, "getWearHeartSensorData SYNCED up to " + JoH.dateTimeText(last_send_success) + " count = 0");
}
return null;
} else {
UserError.Log.d(TAG, "Ratelimitted getWearHeartSensorData");
return null;
}
}
use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip-plus by jamorham.
the class BgSendQueue method getSensorSteps.
public static DataMap getSensorSteps(SharedPreferences prefs) {
Log.d("BgSendQueue", "getSensorSteps");
DataMap dataMap = new DataMap();
final long t = System.currentTimeMillis();
final PebbleMovement pm = PebbleMovement.last();
final boolean show_steps = prefs.getBoolean("showSteps", true);
final boolean show_heart_rate = prefs.getBoolean("showHeartRate", true);
final boolean use_wear_health = prefs.getBoolean("use_wear_health", true);
if (use_wear_health || show_steps) {
boolean sameDay = pm != null ? ListenerService.isSameDay(t, pm.timestamp) : false;
if (!sameDay) {
dataMap.putInt("steps", 0);
dataMap.putLong("steps_timestamp", t);
Log.d("BgSendQueue", "getSensorSteps isSameDay false t=" + JoH.dateTimeText(t));
} else {
dataMap.putInt("steps", pm.metric);
dataMap.putLong("steps_timestamp", pm.timestamp);
Log.d("BgSendQueue", "getSensorSteps isSameDay true pm.timestamp=" + JoH.dateTimeText(pm.timestamp) + " metric=" + pm.metric);
}
}
if (use_wear_health && show_heart_rate) {
final HeartRate lastHeartRateReading = HeartRate.last();
if (lastHeartRateReading != null) {
dataMap.putInt("heart_rate", lastHeartRateReading.bpm);
dataMap.putLong("heart_rate_timestamp", lastHeartRateReading.timestamp);
}
}
return dataMap;
}
Aggregations