Search in sources :

Example 56 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class LefunDeviceSupport method onReadConfiguration.

@Override
public void onReadConfiguration(String config) {
    // Just going to read all the settings
    try {
        TransactionBuilder builder = performInitialized("Read settings");
        GetGeneralSettingsRequest getGeneralSettingsRequest = new GetGeneralSettingsRequest(this, builder);
        getGeneralSettingsRequest.perform();
        inProgressRequests.add(getGeneralSettingsRequest);
        GetEnabledFeaturesRequest getEnabledFeaturesRequest = new GetEnabledFeaturesRequest(this, builder);
        getEnabledFeaturesRequest.perform();
        inProgressRequests.add(getEnabledFeaturesRequest);
        GetSedentaryReminderIntervalRequest getSedentaryReminderIntervalRequest = new GetSedentaryReminderIntervalRequest(this, builder);
        getSedentaryReminderIntervalRequest.perform();
        inProgressRequests.add(getSedentaryReminderIntervalRequest);
        GetHydrationReminderIntervalRequest getHydrationReminderIntervalRequest = new GetHydrationReminderIntervalRequest(this, builder);
        getHydrationReminderIntervalRequest.perform();
        inProgressRequests.add(getHydrationReminderIntervalRequest);
        performConnected(builder.getTransaction());
    } catch (IOException e) {
        GB.toast(getContext(), "Failed to retrieve settings", Toast.LENGTH_SHORT, GB.ERROR, e);
    }
}
Also used : GetEnabledFeaturesRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetEnabledFeaturesRequest) GetSedentaryReminderIntervalRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetSedentaryReminderIntervalRequest) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) GetHydrationReminderIntervalRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetHydrationReminderIntervalRequest) GetGeneralSettingsRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.GetGeneralSettingsRequest) IOException(java.io.IOException)

Example 57 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class LefunDeviceSupport method sendEnabledFeaturesSetting.

/**
 * Sends enabled features settings to the device
 *
 * @param cmd the features command to send
 */
private void sendEnabledFeaturesSetting(FeaturesCommand cmd) {
    try {
        TransactionBuilder builder = performInitialized(SetEnabledFeaturesRequest.class.getSimpleName());
        SetEnabledFeaturesRequest request = new SetEnabledFeaturesRequest(this, builder);
        request.setCmd(cmd);
        request.perform();
        inProgressRequests.add(request);
        performConnected(builder.getTransaction());
    } catch (IOException e) {
        GB.toast(getContext(), "Failed to set enabled features", Toast.LENGTH_SHORT, GB.ERROR, e);
    }
}
Also used : SetEnabledFeaturesRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.lefun.requests.SetEnabledFeaturesRequest) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 58 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class WatchXPlusDeviceSupport method sendNotification.

/**
 * Format text and send it to watch
 * @param notificationChannel - text or call
 * @param notificationText - text to show
 */
private void sendNotification(int notificationChannel, String notificationText) {
    try {
        TransactionBuilder builder = performInitialized("showNotification");
        byte[] command = WatchXPlusConstants.CMD_NOTIFICATION_TEXT_TASK;
        // byte[] text = notificationText.getBytes(StandardCharsets.UTF_8);
        byte[] text = transliterate(notificationText).getBytes(StandardCharsets.UTF_8);
        byte[] messagePart;
        int messageLength = text.length;
        int parts = messageLength / 9;
        int remainder = messageLength % 9;
        // Increment parts quantity if message length is not multiple of 9
        if (remainder != 0) {
            parts++;
        }
        for (int messageIndex = 0; messageIndex < parts; messageIndex++) {
            if (messageIndex + 1 != parts || remainder == 0) {
                messagePart = new byte[11];
            } else {
                messagePart = new byte[remainder + 2];
            }
            System.arraycopy(text, messageIndex * 9, messagePart, 2, messagePart.length - 2);
            if (messageIndex + 1 == parts) {
                messageIndex = 0xFF;
            }
            messagePart[0] = (byte) notificationChannel;
            messagePart[1] = (byte) messageIndex;
            builder.write(getCharacteristic(WatchXPlusConstants.UUID_CHARACTERISTIC_WRITE), buildCommand(command, WatchXPlusConstants.KEEP_ALIVE, messagePart));
        }
        builder.queue(getQueue());
    } catch (IOException e) {
        LOG.warn(" Unable to send notification ", e);
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 59 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class WatchXPlusDeviceSupport method setPowerMode.

/**
 * set watch power
 * switch watch power mode
 * modes (0- normal, 1- energysaving, 2- only watch)
 */
private void setPowerMode() {
    byte setWatchPowerMode = 0x00;
    String powermodeStr = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()).getString(DeviceSettingsPreferenceConst.PREF_POWER_MODE, "0");
    if (powermodeStr.equals("0")) {
        setWatchPowerMode = 0x00;
    } else if (powermodeStr.equals("1")) {
        setWatchPowerMode = 0x01;
    } else if (powermodeStr.equals("2")) {
        setWatchPowerMode = 0x02;
    }
    byte[] bArr = new byte[1];
    bArr[0] = (byte) setWatchPowerMode;
    LOG.info(" setting power mode to: " + setWatchPowerMode);
    try {
        TransactionBuilder builder = performInitialized("setWatchPowerMode");
        builder.write(getCharacteristic(WatchXPlusConstants.UUID_CHARACTERISTIC_WRITE), buildCommand(WatchXPlusConstants.CMD_POWER_MODE, WatchXPlusConstants.TASK, bArr));
        builder.queue(getQueue());
    } catch (IOException e) {
        LOG.warn(" Unable to set power mode ", e);
    }
// prefs.getPreferences().edit().putInt("PREF_POWER_MODE", 0).apply();
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 60 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder in project Gadgetbridge by Freeyourgadget.

the class WatchXPlusDeviceSupport method onSendWeather.

@Override
public void onSendWeather(WeatherSpec weatherSpec) {
    try {
        TransactionBuilder builder = performInitialized("setWeather");
        int currentTemp;
        int todayMinTemp;
        int todayMaxTemp;
        byte[] command = WatchXPlusConstants.CMD_WEATHER_SET;
        byte[] weatherInfo = new byte[5];
        int currentCondition = weatherSpec.currentConditionCode;
        // set weather icon
        // 0 is sunny
        int currentConditionCode = 0;
        switch(currentCondition) {
            // thunderstorm with light rain:  //11d
            case 200:
            // thunderstorm with rain:  //11d
            case 201:
            case // thunderstorm with heavy rain:  //11d
            202:
                currentConditionCode = 1024;
                break;
            // light thunderstorm::  //11d
            case 210:
            // thunderstorm:  //11d
            case 211:
            // heavy thunderstorm:  //11d
            case 212:
            // ragged thunderstorm:  //11d
            case 221:
            // thunderstorm with light drizzle:  //11d
            case 230:
            // thunderstorm with drizzle:  //11d
            case 231:
            case // thunderstorm with heavy drizzle:  //11d
            232:
                currentConditionCode = 1025;
                break;
            // light intensity drizzle:  //09d
            case 300:
            // drizzle:  //09d
            case 301:
            // heavy intensity drizzle:  //09d
            case 302:
            // light intensity drizzle rain:  //09d
            case 310:
            case // light rain:  //10d
            500:
                currentConditionCode = 256;
                break;
            // drizzle rain:  //09d
            case 311:
            // heavy intensity drizzle rain:  //09d
            case 312:
            // shower rain and drizzle:  //09d
            case 313:
            // heavy shower rain and drizzle:  //09d
            case 314:
            // shower drizzle:  //09d
            case 321:
            case // moderate rain:  //10d
            501:
                currentConditionCode = 1280;
                break;
            // freezing rain:  //13d
            case 511:
            // light intensity shower rain:  //09d
            case 520:
            // shower rain:  //09d
            case 521:
            // heavy intensity rain:  //10d
            case 502:
            // very heavy rain:  //10d
            case 503:
            // extreme rain:  //10d
            case 504:
            // heavy intensity shower rain:  //09d
            case 522:
            case // ragged shower rain:  //09d
            531:
                currentConditionCode = 258;
                break;
            // light snow:
            case 600:
            case // snow:  //[[file:13d.png]]
            601:
                currentConditionCode = 513;
                break;
            case // light shower snow:  //[[file:13d.png]]
            620:
                currentConditionCode = 514;
                break;
            // heavy snow:  //[[file:13d.png]]
            case 602:
            // shower snow:  //[[file:13d.png]]
            case 621:
            case // heavy shower snow:  //[[file:13d.png]]
            622:
                currentConditionCode = 515;
                break;
            // sleet:  //[[file:13d.png]]
            case 611:
            case // shower sleet:  //[[file:13d.png]]
            612:
                currentConditionCode = 1026;
                break;
            // light rain and snow:  //[[file:13d.png]]
            case 615:
            case // rain and snow:  //[[file:13d.png]]
            616:
                currentConditionCode = 4;
                break;
            // fog:  //[[file:50d.png]]
            case 741:
            // mist:  //[[file:50d.png]]
            case 701:
            case // smoke:  //[[file:50d.png]]
            711:
                currentConditionCode = 5;
                break;
            case // haze:  //[[file:50d.png]]
            721:
                currentConditionCode = 3;
                break;
            case // sandcase  dust whirls:  //[[file:50d.png]]
            731:
                currentConditionCode = 771;
                break;
            // sand:  //[[file:50d.png]]
            case 751:
            // dust:  //[[file:50d.png]]
            case 761:
            // volcanic ash:  //[[file:50d.png]]
            case 762:
            case // squalls:  //[[file:50d.png]]
            771:
                currentConditionCode = 769;
                break;
            // tornado:  //[[file:50d.png]]
            case 781:
            case // tornado
            900:
                currentConditionCode = 1283;
                break;
            // Group 800: Clear
            case // clear sky
            800:
                currentConditionCode = 0;
                break;
            // few clouds:  //[[file:02d.png]] [[file:02n.png]]
            case 801:
            // scattered clouds:  //[[file:03d.png]] [[file:03d.png]]
            case 802:
            case // broken clouds:  //[[file:04d.png]] [[file:03d.png]]
            803:
                currentConditionCode = 1;
                break;
            case // overcast clouds:  //[[file:04d.png]] [[file:04d.png]]
            804:
                currentConditionCode = 2;
                break;
            // tropical storm
            case 901:
            // cold
            case 903:
            // hot
            case 904:
            // windy
            case 905:
            case // hail
            906:
                currentConditionCode = 1027;
                break;
            // calm
            case 951:
            // light breeze
            case 952:
            // gentle breeze
            case 953:
            // moderate breeze
            case 954:
            // fresh breeze
            case 955:
            // strong breeze
            case 956:
            // high windcase  near gale
            case 957:
            // gale
            case 958:
            // severe gale
            case 959:
            // storm
            case 960:
            // violent storm
            case 961:
            // hurricane
            case 902:
            case // hurricane
            962:
                currentConditionCode = 261;
                break;
        }
        LOG.info(" Weather cond: " + currentCondition + " icon: " + currentConditionCode);
        // calculate for temps under 0
        currentTemp = (Math.abs(weatherSpec.currentTemp)) - 273;
        if (currentTemp < 0) {
            currentTemp = (Math.abs(currentTemp) ^ 255) + 1;
        }
        todayMinTemp = (Math.abs(weatherSpec.todayMinTemp)) - 273;
        if (todayMinTemp < 0) {
            todayMinTemp = (Math.abs(todayMinTemp) ^ 255) + 1;
        }
        todayMaxTemp = (Math.abs(weatherSpec.todayMaxTemp)) - 273;
        if (todayMaxTemp < 0) {
            todayMaxTemp = (Math.abs(todayMaxTemp) ^ 255) + 1;
        }
        LOG.info(" Set weather min: " + todayMinTemp + " max: " + todayMaxTemp + " current: " + currentTemp + " icon: " + currentCondition);
        // First two bytes are controlling the icon
        weatherInfo[0] = (byte) (currentConditionCode >> 8);
        weatherInfo[1] = (byte) currentConditionCode;
        weatherInfo[2] = (byte) todayMinTemp;
        weatherInfo[3] = (byte) todayMaxTemp;
        weatherInfo[4] = (byte) currentTemp;
        builder.write(getCharacteristic(WatchXPlusConstants.UUID_CHARACTERISTIC_WRITE), buildCommand(command, WatchXPlusConstants.KEEP_ALIVE, weatherInfo));
        builder.queue(getQueue());
    } catch (IOException e) {
        LOG.warn(" Unable to set weather ", e);
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Aggregations

TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)279 IOException (java.io.IOException)218 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)17 Alarm (nodomain.freeyourgadget.gadgetbridge.model.Alarm)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 GregorianCalendar (java.util.GregorianCalendar)11 UUID (java.util.UUID)11 Calendar (java.util.Calendar)10 SetDeviceBusyAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction)10 ByteBuffer (java.nio.ByteBuffer)8 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)8 ServerTransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.ServerTransactionBuilder)7 SetDeviceStateAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)7 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 SetProgressAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction)4 AlertNotificationProfile (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile)4 NewAlert (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert)4 GBPrefs (nodomain.freeyourgadget.gadgetbridge.util.GBPrefs)4 InvalidKeyException (java.security.InvalidKeyException)3