use of com.eveningoutpost.dexdrip.Models.StepCounter in project xDrip-plus by jamorham.
the class BgGraphBuilder method stepsLines.
// line illustrating result from step counter
private List<Line> stepsLines() {
final List<Line> stepsLines = new ArrayList<>();
if ((prefs.getBoolean("use_pebble_health", true) && prefs.getBoolean("show_pebble_movement_line", true))) {
final List<StepCounter> pmlist = StepCounter.deltaListFromMovementList(StepCounter.latestForGraph(2000, loaded_start, loaded_end));
PointValue last_point = null;
final boolean d = false;
if (d)
Log.d(TAG, "Delta: pmlist size: " + pmlist.size());
final float yscale = doMgdl ? (float) Constants.MMOLL_TO_MGDL : 1f;
// TODO Configurable
final float ypos = 6 * yscale;
// final long last_timestamp = pmlist.get(pmlist.size() - 1).timestamp;
final float MAX_SIZE = 50;
int flipper = 0;
int accumulator = 0;
for (StepCounter pm : pmlist) {
if (last_point == null) {
last_point = new PointValue((float) pm.timestamp / FUZZER, ypos);
} else {
final PointValue this_point = new PointValue((float) pm.timestamp / FUZZER, ypos);
final float time_delta = this_point.getX() - last_point.getX();
if (time_delta > 1) {
final List<PointValue> new_points = new ArrayList<>();
new_points.add(last_point);
new_points.add(this_point);
// update pointer
last_point = this_point;
final Line this_line = new Line(new_points);
flipper ^= 1;
this_line.setColor((flipper == 0) ? getCol(X.color_step_counter1) : getCol(X.color_step_counter2));
float stroke_size = Math.min(MAX_SIZE, (float) Math.log1p(((double) (pm.metric + accumulator)) / time_delta) * 4);
if (d)
Log.d(TAG, "Delta stroke: " + stroke_size);
this_line.setStrokeWidth((int) stroke_size);
if (d)
Log.d(TAG, "Delta-Line: " + JoH.dateTimeText(pm.timestamp) + " time delta: " + time_delta + " total: " + (pm.metric + accumulator) + " lsize: " + stroke_size + " / " + (int) stroke_size);
accumulator = 0;
if (this_line.getStrokeWidth() > 0) {
stepsLines.add(this_line);
this_line.setHasPoints(false);
this_line.setHasLines(true);
} else {
if (d)
Log.d(TAG, "Delta skip: " + JoH.dateTimeText(pm.timestamp));
}
if (d)
Log.d(TAG, "Delta-List: " + JoH.dateTimeText(pm.timestamp) + " time delta: " + time_delta + " val: " + pm.metric);
} else {
accumulator += pm.metric;
if (d)
Log.d(TAG, "Delta: added: " + JoH.dateTimeText(pm.timestamp) + " metric: " + pm.metric + " to accumulator: " + accumulator);
}
}
}
if (d)
Log.d(TAG, "Delta returning stepsLines: " + stepsLines.size() + " final accumulator remaining: " + accumulator);
}
return stepsLines;
}
use of com.eveningoutpost.dexdrip.Models.StepCounter in project xDrip by NightscoutFoundation.
the class WebServiceSteps method request.
// process the request and produce a response object
public WebResponse request(String query) {
query = stripFirstComponent(query);
List<String> components = getUrlComponents(query);
if (components.size() > 0) {
UserError.Log.d(TAG, "Processing " + query);
switch(components.get(0)) {
case "set":
if (components.size() == 2) {
// sets pebble movement data for steps NOW, must be current step counter reading only
try {
int data = Integer.parseInt(components.get(1));
final StepCounter pm = StepCounter.createEfficientRecord(JoH.tsl(), data);
return webOk("Updated step counter: " + data);
} catch (NumberFormatException e) {
return webError("Couldn't parse Set value: " + components.get(1));
}
} else {
return webError("Incorrect parameters for Set", 400);
}
// break;
default:
return webError("Unknown steps command: " + components.get(0));
}
} else {
return webError("No steps command specified", 404);
}
}
use of com.eveningoutpost.dexdrip.Models.StepCounter in project xDrip by NightscoutFoundation.
the class NightscoutUploader method postStepsCount.
private void postStepsCount(NightscoutService nightscoutService, String apiSecret) throws Exception {
Log.d(TAG, "Processing steps for RESTAPI");
final String STORE_COUNTER = "nightscout-rest-steps-synced-time";
if (apiSecret != null) {
final long syncedTillTime = Math.max(PersistentStore.getLong(STORE_COUNTER), JoH.tsl() - Constants.DAY_IN_MS * 7);
final List<StepCounter> readings = StepCounter.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 (StepCounter reading : readings) {
final JSONObject json = new JSONObject();
json.put("type", "steps-total");
json.put("timeStamp", reading.timestamp);
// json.put("dateString", format.format(reading.timestamp));
json.put("created_at", DateUtil.toISOString(reading.timestamp));
json.put("steps", reading.metric);
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++;
UserError.Log.e(TAG, "Unable to upload steps data to Nightscout - check nightscout version");
throw new UploaderException(r.message(), r.code());
} else {
PersistentStore.setLong(STORE_COUNTER, highest_timestamp);
UserError.Log.e(TAG, "Updating steps 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.StepCounter 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.StepCounter in project xDrip-plus by jamorham.
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);
}
});
}
Aggregations