2015-08-03 23:51:53 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
|
2015-07-15 00:32:13 +02:00
|
|
|
|
|
|
|
import android.util.Pair;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
2015-07-27 23:21:06 +02:00
|
|
|
import java.nio.ByteOrder;
|
2015-07-15 00:32:13 +02:00
|
|
|
import java.util.ArrayList;
|
2015-07-27 23:21:06 +02:00
|
|
|
import java.util.SimpleTimeZone;
|
|
|
|
import java.util.TimeZone;
|
2015-07-15 00:32:13 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2015-07-27 23:21:06 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-08-03 01:17:02 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
2015-07-15 00:32:13 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
|
2015-07-15 00:32:13 +02:00
|
|
|
|
|
|
|
public class GadgetbridgePblSupport {
|
|
|
|
|
|
|
|
public static final int KEY_TIMESTAMP = 1;
|
|
|
|
public static final int KEY_SAMPLES = 2;
|
|
|
|
|
|
|
|
public static final UUID uuid = UUID.fromString("61476764-7465-7262-6469-656775527a6c");
|
|
|
|
private final PebbleProtocol mPebbleProtocol;
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(GadgetbridgePblSupport.class);
|
|
|
|
|
|
|
|
public GadgetbridgePblSupport(PebbleProtocol pebbleProtocol) {
|
|
|
|
mPebbleProtocol = pebbleProtocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GBDeviceEvent handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
|
|
|
|
int timestamp = 0;
|
|
|
|
for (Pair<Integer, Object> pair : pairs) {
|
|
|
|
switch (pair.first) {
|
|
|
|
case KEY_TIMESTAMP:
|
2015-07-27 23:21:06 +02:00
|
|
|
TimeZone tz = SimpleTimeZone.getDefault();
|
|
|
|
timestamp = (int) pair.second - (tz.getOffset(System.currentTimeMillis())) / 1000;
|
2015-07-15 00:32:13 +02:00
|
|
|
LOG.info("got timestamp " + timestamp);
|
|
|
|
break;
|
|
|
|
case KEY_SAMPLES:
|
|
|
|
byte[] samples = (byte[]) pair.second;
|
|
|
|
ByteBuffer samplesBuffer = ByteBuffer.wrap(samples);
|
2015-07-27 23:21:06 +02:00
|
|
|
samplesBuffer.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
|
int samples_remaining = samples.length / 2;
|
|
|
|
LOG.info("got " + samples_remaining + " samples");
|
|
|
|
int offset_seconds = 0;
|
2015-08-03 01:17:02 +02:00
|
|
|
DBHandler db = null;
|
|
|
|
try {
|
|
|
|
db = GBApplication.acquireDB();
|
|
|
|
while (samples_remaining-- > 0) {
|
|
|
|
short sample = samplesBuffer.getShort();
|
|
|
|
byte type = (byte) ((sample & 0xe000) >>> 13);
|
|
|
|
byte intensity = (byte) ((sample & 0x1f80) >>> 7);
|
|
|
|
byte steps = (byte) (sample & 0x007f);
|
|
|
|
db.addGBActivitySample(timestamp + offset_seconds, SampleProvider.PROVIDER_PEBBLE_GADGETBRIDGE, intensity, steps, type);
|
|
|
|
offset_seconds += 60;
|
|
|
|
}
|
|
|
|
} catch (GBException e) {
|
|
|
|
LOG.error("Error acquiring database", e);
|
|
|
|
} finally {
|
|
|
|
if (db != null) {
|
|
|
|
db.release();
|
|
|
|
}
|
2015-07-27 23:21:06 +02:00
|
|
|
}
|
2015-07-15 00:32:13 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG.info("unhandled key: " + pair.first);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
|
|
|
|
sendBytes.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(uuid, mPebbleProtocol.last_id);
|
|
|
|
return sendBytes;
|
|
|
|
}
|
|
|
|
}
|