Skip to content

Commit

Permalink
fix for MiaoMiao. Commit 7e23c0b introduced a fix for when MM loses c…
Browse files Browse the repository at this point in the history
…onnection in the mid of receiving packets (see commit message). Now it goes wrong when the MM is about 3-4 feet away. Then it may happen that all the packets are not received within 4 seconds, resulting in buffer being reset.

This changes resets the timestamp of last packet each time there's a new packet, and also 4 seconds is further reduced to 3, to avoid we run again into problem 1.
  • Loading branch information
JohanDegraeve committed Aug 17, 2020
1 parent 859d085 commit 3f49128
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ class CGMMiaoMiaoTransmitter:BluetoothTransmitter, CGMTransmitter {
private var resendPacketCounter:Int = 0

/// used when processing MiaoMiao data packet
private var timestampFirstPacketReception:Date
private var timestampLastPacketReception:Date

/// receive buffer for miaomiao packets
private var rxBuffer:Data

/// how long to wait for next packet before sending startreadingcommand
private static let maxWaitForpacketInSeconds = 5.0
private static let maxWaitForpacketInSeconds = 3.0

/// length of header added by MiaoMiao in front of data dat is received from Libre sensor
private let miaoMiaoHeaderLength = 18
Expand Down Expand Up @@ -94,7 +94,7 @@ class CGMMiaoMiaoTransmitter:BluetoothTransmitter, CGMTransmitter {

// initialize rxbuffer
rxBuffer = Data()
timestampFirstPacketReception = Date()
timestampLastPacketReception = Date()

// initialize timeStampLastBgReading
self.timeStampLastBgReading = timeStampLastBgReading ?? Date(timeIntervalSince1970: 0)
Expand Down Expand Up @@ -143,11 +143,14 @@ class CGMMiaoMiaoTransmitter:BluetoothTransmitter, CGMTransmitter {
if let value = characteristic.value {

//check if buffer needs to be reset
if (Date() > timestampFirstPacketReception.addingTimeInterval(CGMMiaoMiaoTransmitter.maxWaitForpacketInSeconds - 1)) {
if (Date() > timestampLastPacketReception.addingTimeInterval(CGMMiaoMiaoTransmitter.maxWaitForpacketInSeconds)) {
trace("in peripheral didUpdateValueFor, more than %{public}@ seconds since last update - or first update since app launch, resetting buffer", log: log, category: ConstantsLog.categoryCGMMiaoMiao, type: .info, CGMMiaoMiaoTransmitter.maxWaitForpacketInSeconds.description)
resetRxBuffer()
}

// set timestampLastPacketReception to now, this gives the MM again maxWaitForpacketInSeconds seconds to send the next packet
timestampLastPacketReception = Date()

//add new packet to buffer
rxBuffer.append(value)

Expand Down Expand Up @@ -350,7 +353,7 @@ class CGMMiaoMiaoTransmitter:BluetoothTransmitter, CGMTransmitter {
/// reset rxBuffer, reset startDate, set resendPacketCounter to 0
private func resetRxBuffer() {
rxBuffer = Data()
timestampFirstPacketReception = Date()
timestampLastPacketReception = Date()
resendPacketCounter = 0
}

Expand Down

0 comments on commit 3f49128

Please sign in to comment.