Pebble: some minor code cleanups regarding health

This commit is contained in:
Andreas Shimokawa 2016-02-09 01:24:22 +01:00
parent 93db073538
commit d62946df63
3 changed files with 17 additions and 22 deletions

View File

@ -7,16 +7,16 @@ class DatalogSession {
final byte id; final byte id;
final int tag; final int tag;
final UUID uuid; final UUID uuid;
final byte item_type; final byte itemType;
final short item_size; final short itemSize;
String taginfo = "(unknown)"; 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.id = id;
this.tag = tag; this.tag = tag;
this.uuid = uuid; this.uuid = uuid;
this.item_type = item_type; this.itemType = itemType;
this.item_size = item_size; this.itemSize = itemSize;
} }
boolean handleMessage (ByteBuffer buf, int length) { boolean handleMessage (ByteBuffer buf, int length) {

View File

@ -2,13 +2,11 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Date;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
@ -22,9 +20,6 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class DatalogSessionHealth extends DatalogSession { public class DatalogSessionHealth extends DatalogSession {
private final int preambleLength = 14;
private final int packetLength = 99;
private static final Logger LOG = LoggerFactory.getLogger(DatalogSessionHealth.class); private static final Logger LOG = LoggerFactory.getLogger(DatalogSessionHealth.class);
public DatalogSessionHealth(byte id, UUID uuid, int tag, byte item_type, short item_size) { 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) { public boolean handleMessage(ByteBuffer datalogMessage, int length) {
LOG.info(GB.hexdump(datalogMessage.array(), datalogMessage.position(), length)); LOG.info(GB.hexdump(datalogMessage.array(), datalogMessage.position(), length));
int unknownPacketPreamble, timestamp; int timestamp;
byte unknownC, recordLength, recordNum; byte unknownC, recordLength, recordNum;
short unknownA; short unknownA;
int beginOfPacketPosition, beginOfSamplesPosition; int beginOfPacketPosition, beginOfSamplesPosition;
@ -44,9 +39,10 @@ public class DatalogSessionHealth extends DatalogSession {
byte steps, orientation; //possibly byte steps, orientation; //possibly
short intensity; // possibly short intensity; // possibly
if (0 == (length % packetLength)) { // one datalog message may contain several packets int initialPosition = datalogMessage.position();
for (int packet = 0; packet < (length / packetLength); packet++) { if (0 == (length % itemSize)) { // one datalog message may contain several packets
beginOfPacketPosition = preambleLength + packet*packetLength; for (int packet = 0; packet < (length / itemSize); packet++) {
beginOfPacketPosition = initialPosition + packet * itemSize;
datalogMessage.position(beginOfPacketPosition); datalogMessage.position(beginOfPacketPosition);
unknownA = datalogMessage.getShort(); unknownA = datalogMessage.getShort();
timestamp = datalogMessage.getInt(); timestamp = datalogMessage.getInt();
@ -64,10 +60,10 @@ public class DatalogSessionHealth extends DatalogSession {
SampleProvider sampleProvider = new HealthSampleProvider(); SampleProvider sampleProvider = new HealthSampleProvider();
for (int j = 0; j < recordNum; j++) { for (int j = 0; j < recordNum; j++) {
datalogMessage.position(beginOfSamplesPosition + j*recordLength); datalogMessage.position(beginOfSamplesPosition + j * recordLength);
steps = datalogMessage.get(); steps = datalogMessage.get();
orientation = 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 //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(); intensity = datalogMessage.getShort();
} else { } else {
@ -85,9 +81,9 @@ public class DatalogSessionHealth extends DatalogSession {
dbHandler.addGBActivitySamples(samples); dbHandler.addGBActivitySamples(samples);
} }
} catch (Exception ex) { } catch (Exception ex) {
LOG.debug(ex.getMessage()); LOG.debug(ex.getMessage());
return false;//NACK, so that we get the data again return false;//NACK, so that we get the data again
}finally { } finally {
if (dbHandler != null) { if (dbHandler != null) {
dbHandler.release(); dbHandler.release();
} }

View File

@ -1804,7 +1804,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
DatalogSession datalogSession = mDatalogSessions.get(id); DatalogSession datalogSession = mDatalogSessions.get(id);
LOG.info("DATALOG SENDDATA. id=" + (id & 0xff) + ", items_left=" + items_left + ", total length=" + (length - 10)); LOG.info("DATALOG SENDDATA. id=" + (id & 0xff) + ", items_left=" + items_left + ", total length=" + (length - 10));
if (datalogSession != null) { 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); ack = datalogSession.handleMessage(buf, length - 10);
} }
break; break;
@ -1818,12 +1818,11 @@ public class PebbleProtocol extends GBDeviceProtocol {
int log_tag = buf.getInt(); int log_tag = buf.getInt();
byte item_type = buf.get(); byte item_type = buf.get();
short item_size = 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 (!mDatalogSessions.containsKey(id)) {
if (uuid.equals(UUID_ZERO) && log_tag == 81) { if (uuid.equals(UUID_ZERO) && log_tag == 81) {
mDatalogSessions.put(id, new DatalogSessionHealth(id, uuid, log_tag, item_type, item_size)); 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)); mDatalogSessions.put(id, new DatalogSession(id, uuid, log_tag, item_type, item_size));
} }
} }