Search in sources :

Example 11 with PebbleDictionary

use of com.getpebble.android.kit.util.PebbleDictionary in project xDrip-plus by jamorham.

the class PebbleDisplayStandard method buildDictionary.

private PebbleDictionary buildDictionary() {
    PebbleDictionary dictionary = new PebbleDictionary();
    TimeZone tz = TimeZone.getDefault();
    Date now = new Date();
    int offsetFromUTC = tz.getOffset(now.getTime());
    final String bgDelta = getBgDelta();
    final String bgReadingS = getBgReading();
    final String slopeOrdinal = getSlopeOrdinal();
    if (use_best_glucose) {
        Log.v(TAG, // 
        "buildDictionary: slopeOrdinal-" + slopeOrdinal + " bgReading-" + bgReadingS + " now-" + (int) now.getTime() / 1000 + " bgTime-" + // 
        (int) (dg.timestamp / 1000) + " phoneTime-" + (int) (new Date().getTime() / 1000) + " getBgDelta-" + getBgDelta());
    // no_signal = (dg.mssince > Home.stale_data_millis());
    } else {
        Log.v(TAG, // 
        "buildDictionary: slopeOrdinal-" + slopeOrdinal + " bgReading-" + bgReadingS + " now-" + (int) now.getTime() / 1000 + " bgTime-" + // 
        (int) (this.bgReading.timestamp / 1000) + " phoneTime-" + (int) (new Date().getTime() / 1000) + " getBgDelta-" + getBgDelta());
    // no_signal = ((new Date().getTime()) - Home.stale_data_millis() - this.bgReading.timestamp > 0);
    }
    dictionary.addString(ICON_KEY, slopeOrdinal);
    dictionary.addString(BG_KEY, bgReadingS);
    if (use_best_glucose) {
        dictionary.addUint32(RECORD_TIME_KEY, (int) (((dg.timestamp + offsetFromUTC) / 1000)));
    } else {
        dictionary.addUint32(RECORD_TIME_KEY, (int) (((this.bgReading.timestamp + offsetFromUTC) / 1000)));
    }
    dictionary.addUint32(PHONE_TIME_KEY, (int) ((new Date().getTime() + offsetFromUTC) / 1000));
    dictionary.addString(BG_DELTA_KEY, bgDelta);
    addBatteryStatusToDictionary(dictionary);
    return dictionary;
}
Also used : TimeZone(java.util.TimeZone) PebbleDictionary(com.getpebble.android.kit.util.PebbleDictionary) Date(java.util.Date)

Example 12 with PebbleDictionary

use of com.getpebble.android.kit.util.PebbleDictionary in project xDrip-plus by jamorham.

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;
    }
}
Also used : Bitmap(android.graphics.Bitmap) BgSparklineBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder) PebbleDictionary(com.getpebble.android.kit.util.PebbleDictionary) BgGraphBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)

Example 13 with PebbleDictionary

use of com.getpebble.android.kit.util.PebbleDictionary 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);
        }
    });
}
Also used : Context(android.content.Context) HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) StepCounter(com.eveningoutpost.dexdrip.Models.StepCounter) UUID(java.util.UUID) PebbleDictionary(com.getpebble.android.kit.util.PebbleDictionary) PebbleKit(com.getpebble.android.kit.PebbleKit)

Aggregations

PebbleDictionary (com.getpebble.android.kit.util.PebbleDictionary)13 Date (java.util.Date)4 TimeZone (java.util.TimeZone)4 Context (android.content.Context)2 Bitmap (android.graphics.Bitmap)2 HeartRate (com.eveningoutpost.dexdrip.Models.HeartRate)2 StepCounter (com.eveningoutpost.dexdrip.Models.StepCounter)2 BgGraphBuilder (com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)2 BgSparklineBuilder (com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder)2 PebbleKit (com.getpebble.android.kit.PebbleKit)2 UUID (java.util.UUID)2 GlucoseReading (com.nightscout.core.utils.GlucoseReading)1 DateTime (org.joda.time.DateTime)1