Search in sources :

Example 1 with PebbleDictionary

use of com.getpebble.android.kit.util.PebbleDictionary in project android-uploader by nightscout.

the class Pebble method sendDownload.

public void sendDownload(GlucoseReading reading, TrendArrow trend, long recordTime, Context cntx, boolean resend) {
    GlucoseReading delta = new GlucoseReading(0, GlucoseUnit.MGDL);
    if (currentReading != null) {
        delta = reading.subtract(lastReading);
    }
    if (resend) {
        delta = lastDelta;
    }
    String deltaStr = "";
    if (delta.asMgdl() > 0) {
        deltaStr += "+";
    }
    deltaStr += delta.asStr(units);
    String bgStr = reading.asStr(units);
    lastRecordTime = recordTime;
    lastReading = reading;
    lastTrend = trend;
    lastDelta = delta;
    recordTime = DateTimeZone.getDefault().convertUTCToLocal(recordTime);
    int batLevel = AndroidUploaderDevice.getUploaderDevice(cntx).getBatteryLevel();
    PebbleDictionary dictionary = buildDictionary(trend, bgStr, (int) (recordTime / 1000), (int) (DateTimeZone.getDefault().convertUTCToLocal(new DateTime().getMillis()) / 1000), deltaStr, String.valueOf(batLevel), pwdName);
    currentReading = dictionary;
    sendDownload(dictionary);
}
Also used : GlucoseReading(com.nightscout.core.utils.GlucoseReading) PebbleDictionary(com.getpebble.android.kit.util.PebbleDictionary) DateTime(org.joda.time.DateTime)

Example 2 with PebbleDictionary

use of com.getpebble.android.kit.util.PebbleDictionary in project android-uploader by nightscout.

the class Pebble method buildDictionary.

public PebbleDictionary buildDictionary(TrendArrow trend, String bgValue, int recordTime, int uploaderTimeSec, String delta, String uploaderBattery, String name) {
    PebbleDictionary dictionary = new PebbleDictionary();
    dictionary.addString(ICON_KEY, String.valueOf(trend.ordinal()));
    dictionary.addString(BG_KEY, bgValue);
    dictionary.addUint32(RECORD_TIME_KEY, recordTime);
    dictionary.addUint32(PHONE_TIME_KEY, uploaderTimeSec);
    dictionary.addString(BG_DELTA_KEY, delta);
    dictionary.addString(UPLOADER_BATTERY_KEY, uploaderBattery);
    // TODO does this need to be set every time?
    dictionary.addString(NAME_KEY, name);
    return dictionary;
}
Also used : PebbleDictionary(com.getpebble.android.kit.util.PebbleDictionary)

Example 3 with PebbleDictionary

use of com.getpebble.android.kit.util.PebbleDictionary in project xDrip by NightscoutFoundation.

the class PebbleDisplayStandard method sendDownload.

public void sendDownload() {
    if (PebbleKit.isWatchConnected(this.context)) {
        PebbleDictionary dictionary = buildDictionary();
        if (dictionary != null && this.context != null) {
            Log.d(TAG, "sendDownload: Sending data to pebble");
            sendDataToPebble(dictionary);
            last_time_seen = JoH.ts();
        }
    } else {
        watchdog();
    }
}
Also used : PebbleDictionary(com.getpebble.android.kit.util.PebbleDictionary)

Example 4 with PebbleDictionary

use of com.getpebble.android.kit.util.PebbleDictionary 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;
    }
}
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 5 with PebbleDictionary

use of com.getpebble.android.kit.util.PebbleDictionary in project xDrip by NightscoutFoundation.

the class PebbleDisplayTrend method buildDictionary.

public void buildDictionary() {
    TimeZone tz = TimeZone.getDefault();
    Date now = new Date();
    int offsetFromUTC = tz.getOffset(now.getTime());
    if (dictionary == null) {
        dictionary = new PebbleDictionary();
    }
    // check for alerts
    boolean alerting = ActiveBgAlert.currentlyAlerting();
    alerting = alerting && PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("pebble_vibe_alerts", true);
    if (alerting) {
        dictionary.addInt8(VIBE_KEY, (byte) 0x03);
    } else {
        dictionary.addInt8(VIBE_KEY, (byte) 0x00);
    }
    if (mBgReading != null) {
        Log.v(TAG, "buildDictionary: slopeOrdinal-" + slopeOrdinal() + " bgReading-" + bgReading() + " now-" + (int) now.getTime() / 1000 + " bgTime-" + (int) (mBgReading.timestamp / 1000) + " phoneTime-" + (int) (new Date().getTime() / 1000) + " bgDelta-" + bgDelta());
        no_signal = ((new Date().getTime()) - (60000 * 16) - mBgReading.timestamp > 0);
        if (!PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("pebble_show_arrows", true)) {
            dictionary.addString(ICON_KEY, "0");
        } else {
            dictionary.addString(ICON_KEY, slopeOrdinal());
        }
        if (no_signal) {
            dictionary.addString(BG_KEY, "?RF");
            if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("pebble_vibrate_no_signal", true)) {
                if (!alerting)
                    dictionary.addInt8(VIBE_KEY, (byte) 0x01);
            }
        } else {
            dictionary.addString(BG_KEY, bgReading());
            if (!alerting)
                dictionary.addInt8(VIBE_KEY, (byte) 0x00);
        }
        dictionary.addUint32(RECORD_TIME_KEY, (int) (((mBgReading.timestamp + offsetFromUTC) / 1000)));
        if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("pebble_show_delta", true)) {
            if (no_signal) {
                dictionary.addString(BG_DELTA_KEY, "No Signal");
            } else {
                dictionary.addString(BG_DELTA_KEY, bgDelta());
            }
        } else {
            dictionary.addString(BG_DELTA_KEY, "");
        }
        String msg = PreferenceManager.getDefaultSharedPreferences(mContext).getString("pebble_special_value", "");
        if (bgReading().compareTo(msg) == 0) {
            dictionary.addString(MESSAGE_KEY, PreferenceManager.getDefaultSharedPreferences(mContext).getString("pebble_special_text", "BAZINGA!"));
        } else {
            dictionary.addString(MESSAGE_KEY, "");
        }
    } else {
        Log.v(TAG, "buildDictionary: latest mBgReading is null, so sending default values");
        dictionary.addString(ICON_KEY, slopeOrdinal());
        dictionary.addString(BG_KEY, "?SN");
        // dictionary.addString(BG_KEY, bgReading());
        dictionary.addUint32(RECORD_TIME_KEY, (int) ((new Date().getTime() + offsetFromUTC / 1000)));
        dictionary.addString(BG_DELTA_KEY, "No Sensor");
        dictionary.addString(MESSAGE_KEY, "");
    }
    dictionary.addUint32(PHONE_TIME_KEY, (int) ((new Date().getTime() + offsetFromUTC) / 1000));
    if (PreferenceManager.getDefaultSharedPreferences(mContext).getString("dex_collection_method", "DexbridgeWixel").compareTo("DexbridgeWixel") == 0 && PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("display_bridge_battery", true)) {
        dictionary.addString(UPLOADER_BATTERY_KEY, bridgeBatteryString());
        dictionary.addString(NAME_KEY, "Bridge");
    } else {
        dictionary.addString(UPLOADER_BATTERY_KEY, phoneBattery());
        dictionary.addString(NAME_KEY, "Phone");
    }
}
Also used : TimeZone(java.util.TimeZone) PebbleDictionary(com.getpebble.android.kit.util.PebbleDictionary) Date(java.util.Date)

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