use of com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder in project xDrip by NightscoutFoundation.
the class PebbleDisplayTrend method sendTrendToPebble.
public void sendTrendToPebble() {
// create a sparkline bitmap to send to the pebble
Log.i(TAG, "sendTrendToPebble called: sendStep= " + sendStep + ", messageInTransit= " + messageInTransit + ", transactionFailed= " + transactionFailed + ", sendStep= " + sendStep);
if (!done && (sendStep == 1 && ((!messageInTransit && !transactionOk && !transactionFailed) || (messageInTransit && !transactionOk && transactionFailed)))) {
if (!messageInTransit && !transactionOk && !transactionFailed) {
if (!PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("pebble_display_trend", false)) {
sendStep = 5;
transactionFailed = false;
transactionOk = false;
done = true;
current_size = 0;
buff = null;
}
boolean highLine = PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("pebble_high_line", false);
boolean lowLine = PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("pebble_low_line", false);
String trendPeriodString = PreferenceManager.getDefaultSharedPreferences(mContext).getString("pebble_trend_period", "3");
Integer trendPeriod = Integer.parseInt(trendPeriodString);
if ((trendPeriod != lastTrendPeriod) || (JoH.ratelimit("pebble-bggraphbuilder", 60))) {
long end = System.currentTimeMillis() + (60000 * 5);
long start = end - (60000 * 60 * trendPeriod) - (60000 * 10);
this.bgGraphBuilder = new BgGraphBuilder(context, start, end, NUM_VALUES, true);
lastTrendPeriod = trendPeriod;
}
Log.d(TAG, "sendTrendToPebble: highLine is " + highLine + ", lowLine is " + lowLine + ",trendPeriod is " + trendPeriod);
// encode the trend bitmap as a PNG
int depth = 16;
Bitmap bgTrend;
if (pebble_platform == 0) {
Log.d(TAG, "sendTrendToPebble: Encoding trend as Monochrome.");
depth = 2;
bgTrend = new BgSparklineBuilder(mContext).setBgGraphBuilder(bgGraphBuilder).setStart(System.currentTimeMillis() - 60000 * 60 * trendPeriod).setEnd(System.currentTimeMillis()).setHeightPx(63).setWidthPx(84).showHighLine(highLine).showLowLine(lowLine).setTinyDots(true).setSmallDots(false).build();
} else {
bgTrend = new BgSparklineBuilder(mContext).setBgGraphBuilder(bgGraphBuilder).setStart(System.currentTimeMillis() - 60000 * 60 * trendPeriod).setEnd(System.currentTimeMillis()).setHeightPx(84).setWidthPx(144).showHighLine(highLine).showLowLine(lowLine).setTinyDots().setSmallDots().build();
}
byte[] img = SimpleImageEncoder.encodeBitmapAsPNG(bgTrend, true, depth, true);
image_size = img.length;
buff = ByteBuffer.wrap(img);
bgTrend.recycle();
// Prepare the TREND_BEGIN_KEY dictionary. We expect the length of the image to always be less than 65535 bytes.
if (buff != null) {
if (dictionary == null) {
dictionary = new PebbleDictionary();
}
dictionary.addInt16(TREND_BEGIN_KEY, (short) image_size);
Log.d(TAG, "sendTrendToPebble: Sending TREND_BEGIN_KEY to pebble, image size is " + image_size);
} else {
Log.d(TAG, "sendTrendToPebble: Error converting stream to ByteBuffer, buff is null.");
sendStep = 4;
return;
}
}
transactionFailed = false;
transactionOk = false;
messageInTransit = true;
sendDataToPebble(this.dictionary);
}
if (sendStep == 1 && !done && !messageInTransit && transactionOk && !transactionFailed) {
Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
dictionary.remove(TREND_BEGIN_KEY);
current_size = 0;
sendStep = 2;
transactionOk = false;
}
if (!done && ((sendStep == 2 && !messageInTransit) || sendStep == 3 && transactionFailed)) {
if (!transactionFailed && !messageInTransit) {
// send image chunks to Pebble.
Log.d(TAG, "sendTrendToPebble: current_size is " + current_size + ", image_size is " + image_size);
if (current_size < image_size) {
dictionary.remove(TREND_DATA_KEY);
if ((image_size <= (current_size + CHUNK_SIZE))) {
chunk = new byte[image_size - current_size];
Log.d(TAG, "sendTrendToPebble: sending chunk of size " + (image_size - current_size));
buff.get(chunk, 0, image_size - current_size);
sendStep = 3;
} else {
chunk = new byte[CHUNK_SIZE];
Log.d(TAG, "sendTrendToPebble: sending chunk of size " + CHUNK_SIZE);
buff.get(chunk, 0, CHUNK_SIZE);
current_size += CHUNK_SIZE;
}
dictionary.addBytes(TREND_DATA_KEY, chunk);
}
}
Log.d(TAG, "sendTrendToPebble: Sending TREND_DATA_KEY to pebble, current_size is " + current_size);
transactionFailed = false;
transactionOk = false;
messageInTransit = true;
sendDataToPebble(this.dictionary);
}
if (sendStep == 3 && !done && !messageInTransit && transactionOk && !transactionFailed) {
Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
dictionary.remove(TREND_DATA_KEY);
sendStep = 4;
transactionOk = false;
buff = null;
stream = null;
}
if (!done && (sendStep == 4 && ((!messageInTransit && !transactionOk && !transactionFailed) || (messageInTransit && !transactionOk && transactionFailed)))) {
if (!transactionFailed) {
// prepare the TREND_END_KEY dictionary and send it.
dictionary.addUint8(TREND_END_KEY, (byte) 0);
Log.d(TAG, "sendTrendToPebble: Sending TREND_END_KEY to pebble.");
}
transactionFailed = false;
transactionOk = false;
messageInTransit = true;
sendDataToPebble(this.dictionary);
}
if (sendStep == 4 && !done && transactionOk && !messageInTransit && !transactionFailed) {
Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
dictionary.remove(TREND_END_KEY);
sendStep = 5;
transactionFailed = false;
transactionOk = false;
done = true;
current_size = 0;
buff = null;
}
}
use of com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder 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);
}
}
}
use of com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder in project xDrip-plus by jamorham.
the class ExampleChartPreferenceView method refreshView.
protected void refreshView(View view) {
LineChartView chart = (LineChartView) view.findViewById(R.id.example_linechart);
BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(getContext());
chart.setLineChartData(bgGraphBuilder.lineData());
Viewport viewport = chart.getMaximumViewport();
chart.setViewportCalculationEnabled(false);
chart.setInteractive(false);
chart.setCurrentViewport(viewport);
chart.setPadding(0, 0, 0, 0);
chart.setLeft(0);
chart.setTop(0);
Log.d(TAG, "onBindExampleChart: refreshview " + chart.getHeight());
}
use of com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder in project xDrip-plus by jamorham.
the class BGHistory method setupCharts.
private void setupCharts() {
dateButton1.setText(dateFormatter.format(date1.getTime()));
Calendar endDate = (GregorianCalendar) date1.clone();
endDate.add(Calendar.DATE, noDays);
// LimiTTer sample rate 1 per 2 minutes
int numValues = noDays * (60 / 2) * 24;
BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(this, date1.getTimeInMillis(), endDate.getTimeInMillis(), numValues, false);
chart = (LineChartView) findViewById(R.id.chart);
chart.setZoomType(ZoomType.HORIZONTAL);
previewChart = (PreviewLineChartView) findViewById(R.id.chart_preview);
previewChart.setZoomType(ZoomType.HORIZONTAL);
chart.setLineChartData(bgGraphBuilder.lineData());
chart.setOnValueTouchListener(bgGraphBuilder.getOnValueSelectTooltipListener(this));
previewChart.setLineChartData(bgGraphBuilder.previewLineData(chart.getLineChartData()));
previewChart.setViewportCalculationEnabled(true);
chart.setViewportCalculationEnabled(true);
previewChart.setViewportChangeListener(new ViewportListener());
chart.setViewportChangeListener(new ChartViewPortListener());
setupStatistics(date1.getTimeInMillis(), endDate.getTimeInMillis());
}
use of com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder in project xDrip-plus by jamorham.
the class PebbleDisplayTrendOld method sendTrendToPebble.
private synchronized void sendTrendToPebble(boolean clearTrend) {
// create a sparkline bitmap to send to the pebble
final Bitmap blankTrend;
if (clearTrend) {
blankTrend = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
Log.d(TAG, "Attempting to blank trend");
} else {
blankTrend = null;
didTrend = true;
}
Log.i(TAG, //
"sendTrendToPebble called: sendStep= " + sendStep + ", messageInTransit= " + messageInTransit + ", transactionFailed= " + transactionFailed + ", sendStep= " + sendStep);
if (!done && (sendStep == 1 && (//
(!messageInTransit && !transactionOk && !transactionFailed) || (messageInTransit && !transactionOk && transactionFailed)))) {
if (!messageInTransit && !transactionOk && !transactionFailed) {
if (!clearTrend && (!doWeDisplayTrendData())) {
sendStep = 5;
transactionFailed = false;
transactionOk = false;
done = true;
current_size = 0;
buff = null;
}
boolean highLine = getBooleanValue("pebble_high_line");
boolean lowLine = getBooleanValue("pebble_low_line");
String trendPeriodString = PreferenceManager.getDefaultSharedPreferences(this.context).getString("pebble_trend_period", "3");
Integer trendPeriod = Integer.parseInt(trendPeriodString);
if ((trendPeriod != lastTrendPeriod) || (JoH.ratelimit("pebble-bggraphbuilder", 60))) {
long end = System.currentTimeMillis() + (60000 * 5);
long start = end - (60000 * 60 * trendPeriod) - (60000 * 10);
this.bgGraphBuilder = new BgGraphBuilder(context, start, end, NUM_VALUES, true);
lastTrendPeriod = trendPeriod;
}
Log.d(TAG, "sendTrendToPebble: highLine is " + highLine + ", lowLine is " + lowLine + ",trendPeriod is " + trendPeriod);
Bitmap bgTrend = new BgSparklineBuilder(this.context).setBgGraphBuilder(this.bgGraphBuilder).setStart(System.currentTimeMillis() - 60000 * 60 * trendPeriod).setEnd(System.currentTimeMillis()).setHeightPx(// 84
PebbleUtil.pebbleDisplayType == PebbleDisplayType.TrendClassic ? 63 : 84).setWidthPx(// 144
PebbleUtil.pebbleDisplayType == PebbleDisplayType.TrendClassic ? 84 : 144).showHighLine(highLine).showLowLine(lowLine).setTinyDots(Pref.getBooleanDefaultFalse("pebble_tiny_dots")).setShowFiltered(Pref.getBooleanDefaultFalse("pebble_filtered_line")).build();
// encode the trend bitmap as a PNG
final byte[] img = SimpleImageEncoder.encodeBitmapAsPNG(clearTrend ? blankTrend : bgTrend, true, PebbleUtil.pebbleDisplayType == PebbleDisplayType.TrendClassic ? 2 : 16, true);
if (debugPNG) {
try {
// save debug image output
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/sdcard/download/xdrip-trend-debug.png"));
bos.write(img);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
// also save full colour
final byte[] img2 = SimpleImageEncoder.encodeBitmapAsPNG(bgTrend, true, 16, true);
try {
// save debug image output
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/sdcard/download/xdrip-trend-debug-colour.png"));
bos.write(img2);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
image_size = img.length;
buff = ByteBuffer.wrap(img);
bgTrend.recycle();
// Prepare the TREND_BEGIN_KEY dictionary. We expect the length of the image to always be less than 65535 bytes.
if (buff != null) {
// if (this.dictionary == null) {
// this.dictionary = new PebbleDictionary();
// }
this.dictionary.addInt16(TREND_BEGIN_KEY, (short) image_size);
Log.d(TAG, "sendTrendToPebble: Sending TREND_BEGIN_KEY to pebble, image size is " + image_size);
} else {
Log.d(TAG, "sendTrendToPebble: Error converting stream to ByteBuffer, buff is null.");
sendStep = 4;
return;
}
}
transactionFailed = false;
transactionOk = false;
messageInTransit = true;
sendDataToPebble(this.dictionary);
}
if (sendStep == 1 && !done && !messageInTransit && transactionOk && !transactionFailed) {
Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
this.dictionary.remove(TREND_BEGIN_KEY);
current_size = 0;
sendStep = 2;
transactionOk = false;
}
if (!done && ((sendStep == 2 && !messageInTransit) || sendStep == 3 && transactionFailed)) {
if (!transactionFailed && !messageInTransit) {
// send image chunks to Pebble.
if (d)
Log.d(TAG, "sendTrendToPebble: current_size is " + current_size + ", image_size is " + image_size);
if (current_size < image_size) {
this.dictionary.remove(TREND_DATA_KEY);
if ((image_size <= (current_size + CHUNK_SIZE))) {
chunk = new byte[image_size - current_size];
if (d)
Log.d(TAG, "sendTrendToPebble: sending chunk of size " + (image_size - current_size));
buff.get(chunk, 0, image_size - current_size);
sendStep = 3;
} else {
chunk = new byte[CHUNK_SIZE];
if (d)
Log.d(TAG, "sendTrendToPebble: sending chunk of size " + CHUNK_SIZE);
buff.get(chunk, 0, CHUNK_SIZE);
current_size += CHUNK_SIZE;
}
this.dictionary.addBytes(TREND_DATA_KEY, chunk);
}
}
Log.d(TAG, "sendTrendToPebble: Sending TREND_DATA_KEY to pebble, current_size is " + current_size);
transactionFailed = false;
transactionOk = false;
messageInTransit = true;
sendDataToPebble(this.dictionary);
}
if (sendStep == 3 && !done && !messageInTransit && transactionOk && !transactionFailed) {
Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
this.dictionary.remove(TREND_DATA_KEY);
sendStep = 4;
transactionOk = false;
buff = null;
// stream = null;
}
if (!done && (sendStep == 4 && (//
(!messageInTransit && !transactionOk && !transactionFailed) || (messageInTransit && !transactionOk && transactionFailed)))) {
if (!transactionFailed) {
// prepare the TREND_END_KEY dictionary and send it.
this.dictionary.addUint8(TREND_END_KEY, (byte) 0);
Log.d(TAG, "sendTrendToPebble: Sending TREND_END_KEY to pebble.");
}
transactionFailed = false;
transactionOk = false;
messageInTransit = true;
sendDataToPebble(this.dictionary);
}
if (sendStep == 4 && !done && transactionOk && !messageInTransit && !transactionFailed) {
Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
this.dictionary.remove(TREND_END_KEY);
sendStep = 5;
transactionFailed = false;
transactionOk = false;
done = true;
current_size = 0;
buff = null;
// cleared
if (clearTrend)
didTrend = false;
}
}
Aggregations