1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-27 12:26:48 +01:00

Support for HRM and steps activity recording in Bangle.js

This commit is contained in:
Gordon Williams 2020-12-04 15:14:00 +00:00
parent d0c5ffec28
commit c1195ba05d
4 changed files with 164 additions and 14 deletions

View File

@ -78,6 +78,7 @@ public class GBDaoGenerator {
addLefunBiometricSample(schema,user,device); addLefunBiometricSample(schema,user,device);
addLefunSleepSample(schema, user, device); addLefunSleepSample(schema, user, device);
addSonySWR12Sample(schema, user, device); addSonySWR12Sample(schema, user, device);
addBangleJSActivitySample(schema, user, device);
addHybridHRActivitySample(schema, user, device); addHybridHRActivitySample(schema, user, device);
addCalendarSyncState(schema, device); addCalendarSyncState(schema, device);
@ -461,6 +462,16 @@ public class GBDaoGenerator {
return sleepSample; return sleepSample;
} }
private static Entity addBangleJSActivitySample(Schema schema, Entity user, Entity device) {
Entity activitySample = addEntity(schema, "BangleJSActivitySample");
activitySample.implementsSerializable();
addCommonActivitySampleProperties("AbstractActivitySample", activitySample, user, device);
activitySample.addIntProperty(SAMPLE_STEPS).notNull().codeBeforeGetterAndSetter(OVERRIDE);
activitySample.addIntProperty(SAMPLE_RAW_KIND).notNull().codeBeforeGetterAndSetter(OVERRIDE);
addHeartRateProperties(activitySample);
return activitySample;
}
private static void addCommonActivitySampleProperties(String superClass, Entity activitySample, Entity user, Entity device) { private static void addCommonActivitySampleProperties(String superClass, Entity activitySample, Entity user, Entity device) {
activitySample.setSuperclass(superClass); activitySample.setSuperclass(superClass);
activitySample.addImport(MAIN_PACKAGE + ".devices.SampleProvider"); activitySample.addImport(MAIN_PACKAGE + ".devices.SampleProvider");

View File

@ -93,6 +93,8 @@ public class BangleJSCoordinator extends AbstractDeviceCoordinator {
@Override @Override
public boolean supportsRealtimeData() { public boolean supportsRealtimeData() {
// We could support this easily but I can't figure out how to push the
// act event into real-time data :(
return false; return false;
} }
@ -113,7 +115,7 @@ public class BangleJSCoordinator extends AbstractDeviceCoordinator {
@Override @Override
public boolean supportsActivityTracking() { public boolean supportsActivityTracking() {
return false; return true;
} }
@Override @Override
@ -128,7 +130,7 @@ public class BangleJSCoordinator extends AbstractDeviceCoordinator {
@Override @Override
public boolean supportsHeartRateMeasurement(GBDevice device) { public boolean supportsHeartRateMeasurement(GBDevice device) {
return false; return true;
} }
@Override @Override
@ -158,7 +160,7 @@ public class BangleJSCoordinator extends AbstractDeviceCoordinator {
@Override @Override
public SampleProvider<? extends ActivitySample> getSampleProvider(GBDevice device, DaoSession session) { public SampleProvider<? extends ActivitySample> getSampleProvider(GBDevice device, DaoSession session) {
return null;//new BangleJSSampleProvider(device, session); return new BangleJSSampleProvider(device, session);
} }
@Override @Override

View File

@ -0,0 +1,79 @@
/* Copyright (C) 2018-2020 Daniele Gobbetti, Vadim Kaushan
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.banglejs;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.BangleJSActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.BangleJSActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.ID115ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.ID115ActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public class BangleJSSampleProvider extends AbstractSampleProvider<BangleJSActivitySample> {
public BangleJSSampleProvider(GBDevice device, DaoSession session) {
super(device, session);
}
@Override
public AbstractDao<BangleJSActivitySample, ?> getSampleDao() {
return getSession().getBangleJSActivitySampleDao();
}
@Nullable
@Override
protected Property getRawKindSampleProperty() {
return BangleJSActivitySampleDao.Properties.RawKind;
}
@NonNull
@Override
protected Property getTimestampSampleProperty() {
return BangleJSActivitySampleDao.Properties.Timestamp;
}
@NonNull
@Override
protected Property getDeviceIdentifierSampleProperty() {
return BangleJSActivitySampleDao.Properties.DeviceId;
}
@Override
public int normalizeType(int rawType) {
return rawType;
}
@Override
public int toRawActivityKind(int activityKind) {
return activityKind;
}
@Override
public float normalizeIntensity(int rawIntensity) {
return rawIntensity;
}
@Override
public BangleJSActivitySample createActivitySample() {
return new BangleJSActivitySample();
}
}

View File

@ -32,18 +32,24 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone; import java.util.SimpleTimeZone;
import java.util.UUID; import java.util.UUID;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificationControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificationControl;
import nodomain.freeyourgadget.gadgetbridge.devices.banglejs.BangleJSConstants; import nodomain.freeyourgadget.gadgetbridge.devices.banglejs.BangleJSConstants;
import nodomain.freeyourgadget.gadgetbridge.devices.banglejs.BangleJSSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.BangleJSActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm; import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState; import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
@ -59,12 +65,17 @@ import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.database.DBHelper.*;
public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
private static final Logger LOG = LoggerFactory.getLogger(BangleJSDeviceSupport.class); private static final Logger LOG = LoggerFactory.getLogger(BangleJSDeviceSupport.class);
private BluetoothGattCharacteristic rxCharacteristic = null; private BluetoothGattCharacteristic rxCharacteristic = null;
private BluetoothGattCharacteristic txCharacteristic = null; private BluetoothGattCharacteristic txCharacteristic = null;
private String receivedLine = ""; private String receivedLine = "";
private boolean realtimeHRM = false;
private boolean realtimeStep = false;
private int realtimeHRMInterval = 30*60;
public BangleJSDeviceSupport() { public BangleJSDeviceSupport() {
super(LOG); super(LOG);
@ -87,7 +98,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
Prefs prefs = GBApplication.getPrefs(); Prefs prefs = GBApplication.getPrefs();
if (prefs.getBoolean("datetime_synconconnect", true)) if (prefs.getBoolean("datetime_synconconnect", true))
setTime(builder); transmitTime(builder);
//sendSettings(builder); //sendSettings(builder);
// get version // get version
@ -95,6 +106,9 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
gbDevice.setState(GBDevice.State.INITIALIZED); gbDevice.setState(GBDevice.State.INITIALIZED);
gbDevice.sendDeviceUpdateIntent(getContext()); gbDevice.sendDeviceUpdateIntent(getContext());
getDevice().setFirmwareVersion("N/A");
getDevice().setFirmwareVersion2("N/A");
LOG.info("Initialization Done"); LOG.info("Initialization Done");
return builder; return builder;
@ -152,6 +166,12 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
case "error": case "error":
GB.toast(getContext(), "Bangle.js: " + json.getString("msg"), Toast.LENGTH_LONG, GB.ERROR); GB.toast(getContext(), "Bangle.js: " + json.getString("msg"), Toast.LENGTH_LONG, GB.ERROR);
break; break;
case "ver": {
if (json.has("fw1"))
getDevice().setFirmwareVersion(json.getString("fw1"));
if (json.has("fw2"))
getDevice().setFirmwareVersion2(json.getString("fw2"));
} break;
case "status": { case "status": {
Context context = getContext(); Context context = getContext();
if (json.has("bat")) { if (json.has("bat")) {
@ -199,21 +219,41 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
deviceEvtNotificationControl.reply = json.getString("msg"); deviceEvtNotificationControl.reply = json.getString("msg");
evaluateGBDeviceEvent(deviceEvtNotificationControl); evaluateGBDeviceEvent(deviceEvtNotificationControl);
} break; } break;
/*case "activity": { case "act": {
BangleJSActivitySample sample = new BangleJSActivitySample(); BangleJSActivitySample sample = new BangleJSActivitySample();
sample.setTimestamp((int) (GregorianCalendar.getInstance().getTimeInMillis() / 1000L)); sample.setTimestamp((int) (GregorianCalendar.getInstance().getTimeInMillis() / 1000L));
sample.setHeartRate(json.getInteger("hrm")); int hrm = 0;
int steps = 0;
if (json.has("hrm")) hrm = json.getInt("hrm");
if (json.has("stp")) steps = json.getInt("stp");
int activity = ActivityKind.TYPE_UNKNOWN;
if (json.has("act")) {
String actName = "TYPE_" + json.getString("act").toUpperCase();
try {
Field f = ActivityKind.class.getField(actName);
try {
activity = f.getInt(null);
} catch (IllegalAccessException e) {
LOG.info("JSON activity '"+actName+"' not readable");
}
} catch (NoSuchFieldException e) {
LOG.info("JSON activity '"+actName+"' not found");
}
}
sample.setRawKind(activity);
sample.setHeartRate(hrm);
sample.setSteps(steps);
try (DBHandler dbHandler = GBApplication.acquireDB()) { try (DBHandler dbHandler = GBApplication.acquireDB()) {
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId(); Long userId = getUser(dbHandler.getDaoSession()).getId();
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId(); Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
BangleJSSampleProvider provider = new BangleJSSampleProvider(getDevice(), dbHandler.getDaoSession()); BangleJSSampleProvider provider = new BangleJSSampleProvider(getDevice(), dbHandler.getDaoSession());
sample.setDeviceId(deviceId); sample.setDeviceId(deviceId);
sample.setUserId(userId); sample.setUserId(userId);
provider.addGBActivitySample(sample); provider.addGBActivitySample(sample);
} catch (Exception ex) { } catch (Exception ex) {
LOG.warn("Error saving current heart rate: " + ex.getLocalizedMessage()); LOG.warn("Error saving activity: " + ex.getLocalizedMessage());
} }
} break;*/ } break;
} }
} }
@ -239,7 +279,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
} }
void setTime(TransactionBuilder builder) { void transmitTime(TransactionBuilder builder) {
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
float tz = SimpleTimeZone.getDefault().getOffset(ts) / (1000 * 60 * 60.0f); float tz = SimpleTimeZone.getDefault().getOffset(ts) / (1000 * 60 * 60.0f);
// set time // set time
@ -290,7 +330,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
public void onSetTime() { public void onSetTime() {
try { try {
TransactionBuilder builder = performInitialized("setTime"); TransactionBuilder builder = performInitialized("setTime");
setTime(builder); transmitTime(builder);
builder.queue(getQueue()); builder.queue(getQueue());
} catch (Exception e) { } catch (Exception e) {
GB.toast(getContext(), "Error setting time: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); GB.toast(getContext(), "Error setting time: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
@ -380,9 +420,24 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
} }
} }
private void transmitActivityStatus() {
try {
JSONObject o = new JSONObject();
o.put("t", "act");
o.put("hrm", realtimeHRM);
o.put("stp", realtimeStep);
o.put("int", realtimeHRMInterval);
uartTxJSON("onEnableRealtimeSteps", o);
} catch (JSONException e) {
LOG.info("JSONException: " + e.getLocalizedMessage());
}
}
@Override @Override
public void onEnableRealtimeSteps(boolean enable) { public void onEnableRealtimeSteps(boolean enable) {
if (enable == realtimeHRM) return;
realtimeStep = enable;
transmitActivityStatus();
} }
@Override @Override
@ -432,7 +487,9 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onEnableRealtimeHeartRateMeasurement(boolean enable) { public void onEnableRealtimeHeartRateMeasurement(boolean enable) {
if (enable == realtimeHRM) return;
realtimeHRM = enable;
transmitActivityStatus();
} }
@Override @Override
@ -471,7 +528,8 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onSetHeartRateMeasurementInterval(int seconds) { public void onSetHeartRateMeasurementInterval(int seconds) {
realtimeHRMInterval = seconds;
transmitActivityStatus();
} }
@Override @Override