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
|
|
|
|
2015-10-04 15:53:11 +02:00
|
|
|
public class AppMessageHandlerGBPebble extends AppMessageHandler {
|
2015-07-15 00:32:13 +02:00
|
|
|
|
|
|
|
public static final int KEY_TIMESTAMP = 1;
|
|
|
|
public static final int KEY_SAMPLES = 2;
|
|
|
|
|
2015-10-04 15:53:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AppMessageHandlerGBPebble.class);
|
2015-07-15 00:32:13 +02:00
|
|
|
|
2015-10-04 15:53:11 +02:00
|
|
|
AppMessageHandlerGBPebble(UUID uuid, PebbleProtocol pebbleProtocol) {
|
|
|
|
super(uuid, pebbleProtocol);
|
2015-07-15 00:32:13 +02:00
|
|
|
}
|
|
|
|
|
2015-10-04 15:53:11 +02:00
|
|
|
@Override
|
2015-08-27 15:02:29 +02:00
|
|
|
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
|
2015-07-15 00:32:13 +02:00
|
|
|
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();
|
2016-02-29 20:54:39 +01:00
|
|
|
int type = ((sample & 0xe000) >>> 13);
|
|
|
|
int intensity = ((sample & 0x1f80) >>> 7);
|
|
|
|
int steps = (sample & 0x007f);
|
|
|
|
db.addGBActivitySample(timestamp + offset_seconds, SampleProvider.PROVIDER_PEBBLE_GADGETBRIDGE, intensity, steps, type, 0);
|
2015-08-03 01:17:02 +02:00
|
|
|
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();
|
2015-10-04 15:53:11 +02:00
|
|
|
sendBytes.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
|
2015-08-27 15:02:29 +02:00
|
|
|
return new GBDeviceEvent[]{sendBytes};
|
2015-07-15 00:32:13 +02:00
|
|
|
}
|
|
|
|
}
|