Skip to content

Fix calibration decoding, and lagged backfill processing #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions G7SensorKit/G7CGMManager/G7BackfillMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ public struct G7BackfillMessage: Equatable {
return nil
}

timestamp = data[0..<4].toInt()

timestamp = data[0..<3].toInt()

let glucoseBytes = data[4..<6].to(UInt16.self)

if glucoseBytes != 0xffff {
glucose = glucoseBytes & 0xfff
glucoseIsDisplayOnly = (glucoseBytes & 0xf000) > 0
} else {
glucose = nil
glucoseIsDisplayOnly = false
}

glucoseIsDisplayOnly = data[7] & 0x10 != 0

algorithmState = AlgorithmState(rawValue: data[6])

if data[8] == 0x7f {
Expand Down
20 changes: 14 additions & 6 deletions G7SensorKit/G7CGMManager/G7Sensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ public final class G7Sensor: G7BluetoothManagerDelegate {
func peripheralDidDisconnect(_ manager: G7BluetoothManager, peripheralManager: G7PeripheralManager, wasRemoteDisconnect: Bool) {
if let sensorID = sensorID, sensorID == peripheralManager.peripheral.name {

// Sometimes we do not receive the backfillFinished message before disconnect
flushBackfillBuffer()

let suspectedEndOfSession: Bool

self.log.info("Sensor disconnected: wasRemoteDisconnect:%{public}@", String(describing: wasRemoteDisconnect))
Expand Down Expand Up @@ -249,18 +252,23 @@ public final class G7Sensor: G7BluetoothManagerDelegate {
}
}
case .backfillFinished:
if backfillBuffer.count > 0 {
delegateQueue.async {
self.delegate?.sensor(self, didReadBackfill: self.backfillBuffer)
self.backfillBuffer = []
}
}
flushBackfillBuffer()
default:
self.delegate?.sensor(self, logComms: response.hexadecimalString)
break
}
}

func flushBackfillBuffer() {
if backfillBuffer.count > 0 {
let backfill = backfillBuffer
self.backfillBuffer = []
delegateQueue.async {
self.delegate?.sensor(self, didReadBackfill: backfill)
}
}
}

func bluetoothManager(_ manager: G7BluetoothManager, didReceiveBackfillResponse response: Data) {

log.debug("Received backfill response: %{public}@", response.hexadecimalString)
Expand Down
17 changes: 17 additions & 0 deletions G7SensorKitTests/G7GlucoseMessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ final class G7GlucoseMessageTests: XCTestCase {
let data = Data(hexadecimalString: "cf5802008f00060f10")!
let message = G7BackfillMessage(data: data)!
XCTAssertEqual(153807, message.timestamp)
XCTAssertEqual(143, message.glucose)
XCTAssertEqual(.known(.ok), message.algorithmState)
XCTAssertNil(message.condition)
XCTAssertEqual(false, message.glucoseIsDisplayOnly)
XCTAssertEqual(true, message.hasReliableGlucose)
}

func testBackfillTimestampWithHighByte() {
let data = Data(hexadecimalString: "f20e0d00ba00060ffb")!
let message = G7BackfillMessage(data: data)!
XCTAssertEqual(855794, message.timestamp)
}

func testBackfillCalibration() {
let data = Data(hexadecimalString: "f63d00008500061efe")!
let message = G7BackfillMessage(data: data)!
XCTAssertEqual(true, message.glucoseIsDisplayOnly)
}

}
Expand Down