Search in sources :

Example 1 with WeatherSpec

use of nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec in project Gadgetbridge by Freeyourgadget.

the class FossilHRWatchAdapter method handleBackgroundCharacteristic.

@Override
protected void handleBackgroundCharacteristic(BluetoothGattCharacteristic characteristic) {
    super.handleBackgroundCharacteristic(characteristic);
    byte[] value = characteristic.getValue();
    byte requestType = value[1];
    if (requestType == (byte) 0x04) {
        if (value[7] == 0x00 || value[7] == 0x01) {
            handleCallRequest(value);
        } else if (value[7] == 0x02) {
            handleDeleteNotification(value);
        } else if (value[7] == 0x03) {
            handleQuickReplyRequest(value);
        }
    } else if (requestType == (byte) 0x05) {
        handleMusicRequest(value);
    } else if (requestType == (byte) 0x01) {
        int eventId = value[2];
        LOG.info("got event id " + eventId);
        try {
            String jsonString = new String(value, 3, value.length - 3);
            // logger.info(jsonString);
            JSONObject requestJson = new JSONObject(jsonString);
            JSONObject request = requestJson.getJSONObject("req");
            int requestId = request.getInt("id");
            if (request.has("ringMyPhone")) {
                String action = request.getJSONObject("ringMyPhone").getString("action");
                LOG.info("got ringMyPhone request; " + action);
                GBDeviceEventFindPhone findPhoneEvent = new GBDeviceEventFindPhone();
                JSONObject responseObject = new JSONObject().put("res", new JSONObject().put("id", requestId).put("set", new JSONObject().put("ringMyPhone", new JSONObject())));
                if ("on".equals(action)) {
                    findPhoneEvent.event = GBDeviceEventFindPhone.Event.START;
                    getDeviceSupport().evaluateGBDeviceEvent(findPhoneEvent);
                    responseObject.getJSONObject("res").getJSONObject("set").getJSONObject("ringMyPhone").put("result", "on");
                    queueWrite(new JsonPutRequest(responseObject, this));
                } else if ("off".equals(action)) {
                    findPhoneEvent.event = GBDeviceEventFindPhone.Event.STOP;
                    getDeviceSupport().evaluateGBDeviceEvent(findPhoneEvent);
                    responseObject.getJSONObject("res").getJSONObject("set").getJSONObject("ringMyPhone").put("result", "off");
                    queueWrite(new JsonPutRequest(responseObject, this));
                }
            } else if (request.has("weatherInfo") || request.has("weatherApp._.config.locations")) {
                LOG.info("Got weatherInfo request");
                WeatherSpec weatherSpec = Weather.getInstance().getWeatherSpec();
                if (weatherSpec != null) {
                    onSendWeather(weatherSpec);
                } else {
                    LOG.info("no weather data available  - ignoring request");
                }
            } else if (request.has("commuteApp._.config.commute_info")) {
                String action = request.getJSONObject("commuteApp._.config.commute_info").getString("dest");
                String startStop = request.getJSONObject("commuteApp._.config.commute_info").getString("action");
                if (startStop.equals("stop")) {
                    // overwriteButtons(null);
                    return;
                }
                queueWrite(new SetCommuteMenuMessage(getContext().getString(R.string.fossil_hr_commute_processing), false, this));
                Intent menuIntent = new Intent(QHybridSupport.QHYBRID_EVENT_COMMUTE_MENU);
                menuIntent.putExtra("EXTRA_ACTION", action);
                getContext().sendBroadcast(menuIntent);
            } else if (request.has("master._.config.app_status")) {
                queueWrite(new ConfirmAppStatusRequest(requestId, this));
            } else {
                LOG.warn("Unhandled request from watch: " + requestJson.toString());
            }
        } catch (JSONException e) {
            LOG.error("Error while handling received characteristic", e);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JsonPutRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.json.JsonPutRequest) GBDeviceEventFindPhone(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone) WeatherSpec(nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec) JSONException(org.json.JSONException) Intent(android.content.Intent) ConfirmAppStatusRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.async.ConfirmAppStatusRequest) SetCommuteMenuMessage(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.menu.SetCommuteMenuMessage) Paint(android.graphics.Paint)

Example 2 with WeatherSpec

use of nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec in project Gadgetbridge by Freeyourgadget.

the class FossilHRWatchAdapter method onSendWeather.

@Override
public void onSendWeather(WeatherSpec weatherSpec) {
    long ts = System.currentTimeMillis();
    ts /= 1000;
    try {
        JSONObject responseObject = new JSONObject().put("res", new JSONObject().put("id", // seems the id does not matter?
        0).put("set", new JSONObject().put("weatherInfo", new JSONObject().put("alive", ts + 60 * 60).put("unit", // FIXME: do not hardcode
        "c").put("temp", weatherSpec.currentTemp - 273).put("cond_id", // FIXME do not assume daylight
        getIconForConditionCode(weatherSpec.currentConditionCode, false)))));
        queueWrite(new JsonPutRequest(responseObject, this));
        JSONArray forecastWeekArray = new JSONArray();
        final String[] weekdays = { "", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(weatherSpec.timestamp * 1000L);
        int i = 0;
        for (WeatherSpec.Forecast forecast : weatherSpec.forecasts) {
            cal.add(Calendar.DATE, 1);
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
            forecastWeekArray.put(new JSONObject().put("day", weekdays[dayOfWeek]).put("cond_id", // FIXME do not assume daylight
            getIconForConditionCode(forecast.conditionCode, false)).put("high", forecast.maxTemp - 273).put("low", forecast.minTemp - 273));
            // max 3
            if (++i == 3)
                break;
        }
        JSONArray forecastDayArray = new JSONArray();
        final int[] hours = { 0, 0, 0 };
        for (int hour : hours) {
            forecastDayArray.put(new JSONObject().put("hour", hour).put("cond_id", 0).put("temp", 0));
        }
        JSONObject forecastResponseObject = new JSONObject().put("res", new JSONObject().put("id", 0).put("set", new JSONObject().put("weatherApp._.config.locations", new JSONArray().put(new JSONObject().put("alive", ts + 60 * 60).put("city", weatherSpec.location).put("unit", // FIXME: do not hardcode
        "c").put("temp", weatherSpec.currentTemp - 273).put("high", weatherSpec.todayMaxTemp - 273).put("low", weatherSpec.todayMinTemp - 273).put("rain", 0).put("cond_id", // FIXME do not assume daylight
        getIconForConditionCode(weatherSpec.currentConditionCode, false)).put("forecast_day", forecastDayArray).put("forecast_week", forecastWeekArray)))));
        queueWrite(new JsonPutRequest(forecastResponseObject, this));
    } catch (JSONException e) {
        LOG.error("JSON exception: ", e);
    }
}
Also used : JSONObject(org.json.JSONObject) JsonPutRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.json.JsonPutRequest) Calendar(java.util.Calendar) WeatherSpec(nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Paint(android.graphics.Paint)

Example 3 with WeatherSpec

use of nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec in project Gadgetbridge by Freeyourgadget.

the class PebbleProtocol method encodeWeatherForecast.

private byte[] encodeWeatherForecast(WeatherSpec weatherSpec) {
    short currentTemp = (short) (weatherSpec.currentTemp - 273);
    short todayMax = (short) (weatherSpec.todayMaxTemp - 273);
    short todayMin = (short) (weatherSpec.todayMinTemp - 273);
    short tomorrowMax = 0;
    short tomorrowMin = 0;
    int tomorrowConditionCode = 0;
    if (weatherSpec.forecasts.size() > 0) {
        WeatherSpec.Forecast tomorrow = weatherSpec.forecasts.get(0);
        tomorrowMax = (short) (tomorrow.maxTemp - 273);
        tomorrowMin = (short) (tomorrow.minTemp - 273);
        tomorrowConditionCode = tomorrow.conditionCode;
    }
    String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
    if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) {
        currentTemp = (short) (currentTemp * 1.8f + 32);
        todayMax = (short) (todayMax * 1.8f + 32);
        todayMin = (short) (todayMin * 1.8f + 32);
        tomorrowMax = (short) (tomorrowMax * 1.8f + 32);
        tomorrowMin = (short) (tomorrowMin * 1.8f + 32);
    }
    final short WEATHER_FORECAST_LENGTH = 20;
    String[] parts = { weatherSpec.location, weatherSpec.currentCondition };
    // Calculate length first
    short attributes_length = 0;
    for (String s : parts) {
        if (s == null || s.equals("")) {
            continue;
        }
        attributes_length += (short) (2 + s.getBytes().length);
    }
    short pin_length = (short) (WEATHER_FORECAST_LENGTH + attributes_length);
    ByteBuffer buf = ByteBuffer.allocate(pin_length);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    // unknown, always 3?
    buf.put((byte) 3);
    buf.putShort(currentTemp);
    buf.put(Weather.mapToPebbleCondition(weatherSpec.currentConditionCode));
    buf.putShort(todayMax);
    buf.putShort(todayMin);
    buf.put(Weather.mapToPebbleCondition(tomorrowConditionCode));
    buf.putShort(tomorrowMax);
    buf.putShort(tomorrowMin);
    buf.putInt(weatherSpec.timestamp);
    // automatic location 0=manual 1=auto
    buf.put((byte) 0);
    buf.putShort(attributes_length);
    // Encode Pascal-Style Strings
    for (String s : parts) {
        if (s == null || s.equals("")) {
            continue;
        }
        int partlength = s.getBytes().length;
        if (partlength > 512)
            partlength = 512;
        buf.putShort((short) partlength);
        buf.put(s.getBytes(), 0, partlength);
    }
    return encodeBlobdb(UUID_LOCATION, BLOBDB_INSERT, BLOBDB_WEATHER, buf.array());
}
Also used : WeatherSpec(nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec) ByteBuffer(java.nio.ByteBuffer)

Example 4 with WeatherSpec

use of nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec in project Gadgetbridge by Freeyourgadget.

the class AppMessageHandlerSimplyLight method onAppStart.

@Override
public GBDeviceEvent[] onAppStart() {
    WeatherSpec weatherSpec = Weather.getInstance().getWeatherSpec();
    if (weatherSpec == null) {
        return new GBDeviceEvent[] { null };
    }
    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
    sendBytes.encodedBytes = encodeSimplyLightWeatherMessage(weatherSpec);
    return new GBDeviceEvent[] { sendBytes };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) WeatherSpec(nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)

Example 5 with WeatherSpec

use of nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec in project Gadgetbridge by Freeyourgadget.

the class AppMessageHandlerTimeStylePebble method onAppStart.

@Override
public GBDeviceEvent[] onAppStart() {
    WeatherSpec weatherSpec = Weather.getInstance().getWeatherSpec();
    if (weatherSpec == null) {
        return new GBDeviceEvent[] { null };
    }
    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
    sendBytes.encodedBytes = encodeTimeStylePebbleWeather(weatherSpec);
    return new GBDeviceEvent[] { sendBytes };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) WeatherSpec(nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)

Aggregations

WeatherSpec (nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec)24 GBDeviceEvent (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent)11 GBDeviceEventSendBytes (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)11 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)3 JSONObject (org.json.JSONObject)3 Paint (android.graphics.Paint)2 Pair (android.util.Pair)2 IOException (java.io.IOException)2 TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)2 JsonPutRequest (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.json.JsonPutRequest)2 JSONException (org.json.JSONException)2 SuppressLint (android.annotation.SuppressLint)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 WeatherInfo (cyanogenmod.weather.WeatherInfo)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1