<commit_before>package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;

import android.util.Pair;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.UUID;

import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
import ru.gelin.android.weather.notification.ParcelableWeather2;

public class AppMessageHandlerMarioTime extends AppMessageHandler {

    public static final int KEY_WEATHER_ICON_ID = 10;
    public static final int KEY_WEATHER_TEMPERATURE = 11;
    public static final int KEY_WEATHER_REQUEST = 12;

    private static final Logger LOG = LoggerFactory.getLogger(AppMessageHandlerMarioTime.class);

    public AppMessageHandlerMarioTime(UUID uuid, PebbleProtocol pebbleProtocol) {
        super(uuid, pebbleProtocol);
    }

    private byte[] encodeWeatherMessage(int temperature, int condition) {
        ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(2);
        pairs.add(new Pair<>(KEY_WEATHER_ICON_ID, (Object) (byte) condition));
        pairs.add(new Pair<>(KEY_WEATHER_TEMPERATURE, (Object) (byte) temperature));
        byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);

        ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length);

        // encode ack and put in front of push message (hack for acknowledging the last message)
        buf.put(weatherMessage);

        return buf.array();
    }

    @Override
    public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
        boolean weatherRequested = false;
        for (Pair<Integer, Object> pair : pairs) {
            switch (pair.first) {
                case KEY_WEATHER_REQUEST:
                    LOG.info("got weather request");
                    weatherRequested = true;
                    break;
                default:
                    LOG.info("unknown key " + pair.first);
            }
        }
        if (!weatherRequested) {
            return new GBDeviceEvent[]{null};
        }
        ParcelableWeather2 weather = Weather.getInstance().getWeather2();

        GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
        sendBytes.encodedBytes = encodeWeatherMessage(weather.currentConditionCode, weather.currentTemp - 273);

        GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes();
        sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);

        return new GBDeviceEvent[]{sendBytesAck, sendBytes};
    }
}<commit_msg>Pebble: fix mario time appmessage handler (weather condition icon hardcoded to sunny for now)<commit_after>package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;

import android.util.Pair;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.UUID;

import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
import ru.gelin.android.weather.notification.ParcelableWeather2;

class AppMessageHandlerMarioTime extends AppMessageHandler {

    private static final int KEY_WEATHER_ICON_ID = 10;
    private static final int KEY_WEATHER_TEMPERATURE = 11;
    private static final int KEY_WEATHER_REQUEST = 12;

    private static final Logger LOG = LoggerFactory.getLogger(AppMessageHandlerMarioTime.class);

    AppMessageHandlerMarioTime(UUID uuid, PebbleProtocol pebbleProtocol) {
        super(uuid, pebbleProtocol);
    }

    private byte[] encodeWeatherMessage(int temperature, int condition) {
        ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(2);
        pairs.add(new Pair<>(KEY_WEATHER_ICON_ID, (Object) (byte) condition));
        pairs.add(new Pair<>(KEY_WEATHER_TEMPERATURE, (Object) (byte) temperature));
        byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);

        ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length);

        buf.put(weatherMessage);

        return buf.array();
    }

    @Override
    public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
        boolean weatherRequested = false;
        for (Pair<Integer, Object> pair : pairs) {
            switch (pair.first) {
                case KEY_WEATHER_REQUEST:
                    LOG.info("got weather request");
                    weatherRequested = true;
                    break;
                default:
                    LOG.info("unknown key " + pair.first);
            }
        }
        if (!weatherRequested) {
            return new GBDeviceEvent[]{null};
        }
        return pushMessage();
    }

    @Override
    public GBDeviceEvent[] pushMessage() {
        ParcelableWeather2 weather = Weather.getInstance().getWeather2();

        if (weather == null) {
            return new GBDeviceEvent[]{null};
        }
        GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
        sendBytes.encodedBytes = encodeWeatherMessage(weather.currentTemp - 273, 1); // FIXME: condition code

        GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes();
        sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);

        return new GBDeviceEvent[]{sendBytesAck, sendBytes};

    }
}
{'fraction_non_alphanumeric': 0.08697170799860286, 'fraction_numerical': 0.005413901501921062, 'mean_word_length': 3.4189814814814814, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3442629', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1621, 'n_words': 331}