Skip to content

Commit

Permalink
atom : fix for new sensor detection
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanDegraeve committed Feb 20, 2021
1 parent 8e23d69 commit daad6c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
39 changes: 27 additions & 12 deletions xdrip/BluetoothTransmitter/CGM/Libre/Atom/CGMAtomTransmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class CGMAtomTransmitter:BluetoothTransmitter, CGMTransmitter {
/// firmware, if received
private var firmWare: String?

/// current sensor serial number, if nil then it's not known yet
private var sensorSerialNumber:String?

// MARK: - Initialization
/// - parameters:
/// - address: if already connected before, then give here the address that was received during previous connect, if not give nil
Expand All @@ -71,14 +74,18 @@ class CGMAtomTransmitter:BluetoothTransmitter, CGMTransmitter {
/// - cGMTransmitterDelegate : a CGMTransmitterDelegate
/// - cGMAtomTransmitterDelegate : a CGMAtomTransmitterDelegate
/// - firmWare : firmWare if known
init(address:String?, name: String?, bluetoothTransmitterDelegate: BluetoothTransmitterDelegate, cGMAtomTransmitterDelegate : CGMAtomTransmitterDelegate, cGMTransmitterDelegate:CGMTransmitterDelegate, webOOPEnabled: Bool?, nonFixedSlopeEnabled: Bool?, firmWare: String?) {
/// - sensorSerialNumber : sensor serial number, if nil then it's not yet known
init(address:String?, name: String?, bluetoothTransmitterDelegate: BluetoothTransmitterDelegate, cGMAtomTransmitterDelegate : CGMAtomTransmitterDelegate, cGMTransmitterDelegate:CGMTransmitterDelegate, sensorSerialNumber:String?, webOOPEnabled: Bool?, nonFixedSlopeEnabled: Bool?, firmWare: String?) {

// assign addressname and name or expected devicename
var newAddressAndName:BluetoothTransmitter.DeviceAddressAndName = BluetoothTransmitter.DeviceAddressAndName.notYetConnected(expectedName: expectedDeviceNameAtom)
if let address = address {
newAddressAndName = BluetoothTransmitter.DeviceAddressAndName.alreadyConnectedBefore(address: address, name: name)
}

// initialize sensorSerialNumber
self.sensorSerialNumber = sensorSerialNumber

// assign firmWare
self.firmWare = firmWare

Expand Down Expand Up @@ -291,19 +298,9 @@ class CGMAtomTransmitter:BluetoothTransmitter, CGMTransmitter {

}

// assign self.serialNumber
// assign self.sensorSerialNumberAsData - not yet calculating the serialNumber, because the value depends on the patchInfo which we receive only in a second step
self.sensorSerialNumberAsData = value[2..<10]

guard let sensorSerialNumberAsData = sensorSerialNumberAsData, let libreSensorSerialNumber = LibreSensorSerialNumber(withUID: sensorSerialNumberAsData, with: LibreSensorType.type(patchInfo: patchInfo)) else {

trace("in peripheral didUpdateValueFor, could not create libreSensorSerialNumber", log: self.log, category: ConstantsLog.categoryCGMBubble, type: .info)

return

}

// call to delegate, received sensor serialNumber
cGMAtomTransmitterDelegate?.received(serialNumber: libreSensorSerialNumber.serialNumber, from: self)

case .patchInfo:

Expand All @@ -328,7 +325,9 @@ class CGMAtomTransmitter:BluetoothTransmitter, CGMTransmitter {
}

if let patchInfo = patchInfo {

trace(" received patchInfo %{public}@ ", log: log, category: ConstantsLog.categoryCGMBubble, type: .info, patchInfo)

}

// send libreSensorType to delegate
Expand All @@ -344,6 +343,22 @@ class CGMAtomTransmitter:BluetoothTransmitter, CGMTransmitter {
// call to delegate, received sensor serialNumber
cGMAtomTransmitterDelegate?.received(serialNumber: libreSensorSerialNumber.serialNumber, from: self)

if self.sensorSerialNumber != libreSensorSerialNumber.serialNumber {

// assign self.sensorSerialNumber to libreSensorSerialNumber.serialNumber
self.sensorSerialNumber = libreSensorSerialNumber.serialNumber

// call delegate, to inform that a new sensor is detected
cgmTransmitterDelegate?.newSensorDetected()

}

} else {

trace("in peripheral didUpdateValueFor, could not create libreSensorSerialNumber", log: self.log, category: ConstantsLog.categoryCGMBubble, type: .info)

return

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class BluetoothPeripheralManager: NSObject {

// create an instance of CGMAtomTransmitter, CGMAtomTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble
// add it to the array of bluetoothTransmitters
bluetoothTransmitters.insert(CGMAtomTransmitter(address: atom.blePeripheral.address, name: atom.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMAtomTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, webOOPEnabled: atom.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: atom.blePeripheral.nonFixedSlopeEnabled, firmWare: atom.firmware), at: index)
bluetoothTransmitters.insert(CGMAtomTransmitter(address: atom.blePeripheral.address, name: atom.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMAtomTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: atom.blePeripheral.sensorSerialNumber, webOOPEnabled: atom.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: atom.blePeripheral.nonFixedSlopeEnabled, firmWare: atom.firmware), at: index)

// if AtomType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true
if bluetoothPeripheralType.category() == .CGM {
Expand Down Expand Up @@ -655,7 +655,7 @@ class BluetoothPeripheralManager: NSObject {

if let cgmTransmitterDelegate = cgmTransmitterDelegate {

newTransmitter = CGMAtomTransmitter(address: atom.blePeripheral.address, name: atom.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMAtomTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, webOOPEnabled: atom.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: atom.blePeripheral.nonFixedSlopeEnabled, firmWare: atom.firmware)
newTransmitter = CGMAtomTransmitter(address: atom.blePeripheral.address, name: atom.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMAtomTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: atom.blePeripheral.sensorSerialNumber, webOOPEnabled: atom.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: atom.blePeripheral.nonFixedSlopeEnabled, firmWare: atom.firmware)

} else {

Expand Down Expand Up @@ -891,7 +891,7 @@ class BluetoothPeripheralManager: NSObject {
fatalError("in createNewTransmitter, AtomType, cgmTransmitterDelegate is nil")
}

return CGMAtomTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMAtomTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, webOOPEnabled: nil, nonFixedSlopeEnabled: nil, firmWare: nil)
return CGMAtomTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMAtomTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: nil, webOOPEnabled: nil, nonFixedSlopeEnabled: nil, firmWare: nil)

case .DropletType:

Expand Down

0 comments on commit daad6c6

Please sign in to comment.