From d62946df63f98cfe22fa2a35081fa08440ec883e Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Tue, 9 Feb 2016 01:24:22 +0100 Subject: [PATCH] Pebble: some minor code cleanups regarding health --- .../devices/pebble/DatalogSession.java | 10 ++++----- .../devices/pebble/DatalogSessionHealth.java | 22 ++++++++----------- .../devices/pebble/PebbleProtocol.java | 7 +++--- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSession.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSession.java index b17e7259..00f67721 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSession.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSession.java @@ -7,16 +7,16 @@ class DatalogSession { final byte id; final int tag; final UUID uuid; - final byte item_type; - final short item_size; + final byte itemType; + final short itemSize; String taginfo = "(unknown)"; - DatalogSession(byte id, UUID uuid, int tag, byte item_type, short item_size) { + DatalogSession(byte id, UUID uuid, int tag, byte itemType, short itemSize) { this.id = id; this.tag = tag; this.uuid = uuid; - this.item_type = item_type; - this.item_size = item_size; + this.itemType = itemType; + this.itemSize = itemSize; } boolean handleMessage (ByteBuffer buf, int length) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealth.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealth.java index 97bf7375..12d036a8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealth.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealth.java @@ -2,13 +2,11 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; import android.database.sqlite.SQLiteDatabase; -import android.widget.Toast; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.nio.ByteBuffer; -import java.util.Date; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.GBApplication; @@ -22,9 +20,6 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB; public class DatalogSessionHealth extends DatalogSession { - private final int preambleLength = 14; - private final int packetLength = 99; - private static final Logger LOG = LoggerFactory.getLogger(DatalogSessionHealth.class); public DatalogSessionHealth(byte id, UUID uuid, int tag, byte item_type, short item_size) { @@ -36,7 +31,7 @@ public class DatalogSessionHealth extends DatalogSession { public boolean handleMessage(ByteBuffer datalogMessage, int length) { LOG.info(GB.hexdump(datalogMessage.array(), datalogMessage.position(), length)); - int unknownPacketPreamble, timestamp; + int timestamp; byte unknownC, recordLength, recordNum; short unknownA; int beginOfPacketPosition, beginOfSamplesPosition; @@ -44,9 +39,10 @@ public class DatalogSessionHealth extends DatalogSession { byte steps, orientation; //possibly short intensity; // possibly - if (0 == (length % packetLength)) { // one datalog message may contain several packets - for (int packet = 0; packet < (length / packetLength); packet++) { - beginOfPacketPosition = preambleLength + packet*packetLength; + int initialPosition = datalogMessage.position(); + if (0 == (length % itemSize)) { // one datalog message may contain several packets + for (int packet = 0; packet < (length / itemSize); packet++) { + beginOfPacketPosition = initialPosition + packet * itemSize; datalogMessage.position(beginOfPacketPosition); unknownA = datalogMessage.getShort(); timestamp = datalogMessage.getInt(); @@ -64,10 +60,10 @@ public class DatalogSessionHealth extends DatalogSession { SampleProvider sampleProvider = new HealthSampleProvider(); for (int j = 0; j < recordNum; j++) { - datalogMessage.position(beginOfSamplesPosition + j*recordLength); + datalogMessage.position(beginOfSamplesPosition + j * recordLength); steps = datalogMessage.get(); orientation = datalogMessage.get(); - if (j<(recordNum-1)) { + if (j < (recordNum - 1)) { //TODO:apparently last minute data do not contain intensity. I guess we are reading it wrong but this approach is our best bet ATM intensity = datalogMessage.getShort(); } else { @@ -85,9 +81,9 @@ public class DatalogSessionHealth extends DatalogSession { dbHandler.addGBActivitySamples(samples); } } catch (Exception ex) { - LOG.debug(ex.getMessage()); + LOG.debug(ex.getMessage()); return false;//NACK, so that we get the data again - }finally { + } finally { if (dbHandler != null) { dbHandler.release(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java index 41cfe311..bb898856 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java @@ -1804,7 +1804,7 @@ public class PebbleProtocol extends GBDeviceProtocol { DatalogSession datalogSession = mDatalogSessions.get(id); LOG.info("DATALOG SENDDATA. id=" + (id & 0xff) + ", items_left=" + items_left + ", total length=" + (length - 10)); if (datalogSession != null) { - LOG.info("DATALOG UUID=" + datalogSession.uuid + ", tag=" + datalogSession.tag + datalogSession.getTaginfo() + ", item_size=" + datalogSession.item_size + ", item_type=" + datalogSession.item_type); + LOG.info("DATALOG UUID=" + datalogSession.uuid + ", tag=" + datalogSession.tag + datalogSession.getTaginfo() + ", itemSize=" + datalogSession.itemSize + ", itemType=" + datalogSession.itemType); ack = datalogSession.handleMessage(buf, length - 10); } break; @@ -1818,12 +1818,11 @@ public class PebbleProtocol extends GBDeviceProtocol { int log_tag = buf.getInt(); byte item_type = buf.get(); short item_size = buf.get(); - LOG.info("DATALOG OPENSESSION. id=" + (id & 0xff) + ", App UUID=" + uuid.toString() + ", log_tag=" + log_tag + ", item_type=" + item_type + ", item_size=" + item_size); + LOG.info("DATALOG OPENSESSION. id=" + (id & 0xff) + ", App UUID=" + uuid.toString() + ", log_tag=" + log_tag + ", item_type=" + item_type + ", itemSize=" + item_size); if (!mDatalogSessions.containsKey(id)) { if (uuid.equals(UUID_ZERO) && log_tag == 81) { mDatalogSessions.put(id, new DatalogSessionHealth(id, uuid, log_tag, item_type, item_size)); - } - else { + } else { mDatalogSessions.put(id, new DatalogSession(id, uuid, log_tag, item_type, item_size)); } }