Skip to content

Commit

Permalink
Add SpO2 reading for compatible watches
Browse files Browse the repository at this point in the history
  • Loading branch information
Artaud committed Aug 26, 2020
1 parent f000d09 commit b11c355
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class Constants {
public final static String STARTED_ON_WATCH_NAME = "com.urbandroid.sleep.watch.STARTED_ON_WATCH";
public final static String STOP_SLEEP_TRACK_ACTION = "com.urbandroid.sleep.alarmclock.STOP_SLEEP_TRACK";
// private final static String WATCH_TYPE_EXTRA = "com.urbandroid.sleep.watch.WATCH_TIME_EXTRA";
public final static String DATA_WITH_EXTRA = "com.urbandroid.sleep.watch.ACTION_EXTRA_DATA_UPDATE";
public final static String SPO2_DATA_EXTRA = "com.urbandroid.sleep.EXTRA_DATA_SPO2"; // supposed to be float

// From sleep to plugin
final static String START_WATCH_APP = "com.urbandroid.sleep.watch.START_TRACKING";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;

import static com.urbandroid.sleep.garmin.Constants.CHECK_CONNECTED;
import static com.urbandroid.sleep.garmin.Constants.DATA_WITH_EXTRA;
import static com.urbandroid.sleep.garmin.Constants.DISMISS_ACTION_NAME;
import static com.urbandroid.sleep.garmin.Constants.DO_HR_MONITORING;
import static com.urbandroid.sleep.garmin.Constants.HINT;
Expand All @@ -23,6 +24,7 @@
import static com.urbandroid.sleep.garmin.Constants.SET_BATCH_SIZE;
import static com.urbandroid.sleep.garmin.Constants.SET_PAUSE;
import static com.urbandroid.sleep.garmin.Constants.SNOOZE_ACTION_NAME;
import static com.urbandroid.sleep.garmin.Constants.SPO2_DATA_EXTRA;
import static com.urbandroid.sleep.garmin.Constants.STARTED_ON_WATCH_NAME;
import static com.urbandroid.sleep.garmin.Constants.START_ALARM;
import static com.urbandroid.sleep.garmin.Constants.START_WATCH_APP;
Expand Down Expand Up @@ -135,6 +137,12 @@ public void handleMessageFromWatch(List<Object> message, ConnectIQ.IQMessageStat
queueToWatch.emptyQueue();
queueToWatch.enqueue(TO_WATCH_STOP);
break;
case "SPO2":
float[] spo2Data = new float[]{Float.parseFloat(msgArray[1])};
Logger.logInfo(TAG + ": received SpO2 data from watch " + spo2Data[0]);
Intent spo2Intent = new Intent(DATA_WITH_EXTRA);
spo2Intent.putExtra(SPO2_DATA_EXTRA, spo2Data);
sendExplicitBroadcastToSleep(spo2Intent, context);
}

if (maxRawFloatValues != null) {
Expand Down
3 changes: 3 additions & 0 deletions SleepGarmin-watch2/source/BusinessManager.mc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class BusinessManager {
DebugManager.log("sendHrData " + hr);
self.ctx.commManager.enqueue([CommManager.MSG_HR, hr]);
}
function sendOxyData(oxygenSaturation) {
self.ctx.commManager.enqueue([CommManager.MSG_OXY, oxygenSaturation]);
}

function sendPause() {
self.ctx.commManager.enqueue(CommManager.MSG_PAUSE_TRACKING);
Expand Down
1 change: 1 addition & 0 deletions SleepGarmin-watch2/source/CommManager.mc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CommManager {
static const MSG_RESUME_TRACKING = "RESUME";
static const MSG_DATA = "DATA_NEW";
static const MSG_HR = "HR";
static const MSG_OXY = "SPO2";

const MAX_DELIVERY_ERROR = 3;
const MAX_DELIVERY_PAUSE = 3;
Expand Down
24 changes: 22 additions & 2 deletions SleepGarmin-watch2/source/SensorManager.mc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ using Toybox.Sensor;

class SensorManager {

const SENSOR_PERIOD_SEC = 4;
const SENSOR_PERIOD_SEC = 4;
const OXI_READING_PERIOD_SEC = 60;

var ctx;

Expand All @@ -12,6 +13,8 @@ class SensorManager {
var accBatch = [];

var hrBuf = [];

var lastOximeterReadingSec = 0;

function initialize(ctx) {
DebugManager.log("SensorManager initialized");
Expand Down Expand Up @@ -45,8 +48,19 @@ class SensorManager {

if (sensorData has :heartRateData && sensorData.heartRateData != null) {
onHRData(sensorData.heartRateData.heartBeatIntervals);
}
}

if (lastOximeterReadingSec > OXI_READING_PERIOD_SEC) {
lastOximeterReadingSec = 0;
var sensorInfo = Sensor.getInfo();
if (sensorInfo has :oxygenSaturation && sensorInfo.oxygenSaturation != null) {
onOxyData(oxygenSaturation);
}
}
lastOximeterReadingSec = lastOximeterReadingSec + SENSOR_PERIOD_SEC;
}



self.ctx.businessManager.onDataHook();
}
Expand Down Expand Up @@ -101,5 +115,11 @@ class SensorManager {
hrBuf = [];
}
}

function onOxyData(oxygenSaturation) {
DebugManager.log("onOxyData");

self.ctx.businessManager.sendOxyData(oxygenSaturation);
}

}

0 comments on commit b11c355

Please sign in to comment.