small changes
This commit is contained in:
parent
51fe942fa5
commit
d5722c47f8
|
@ -491,10 +491,10 @@ extension CalibratorProtocol {
|
|||
/// - bgReading : reading that needs to be updated - inout parameter to improve performance
|
||||
private func updateCalculatedValue(for bgReading:BgReading) {
|
||||
if (bgReading.calculatedValue < 10) {
|
||||
bgReading.calculatedValue = Constants.BgReadingAlgorithms.bgReadingErrorValue
|
||||
bgReading.calculatedValue = Constants.CalibrationAlgorithms.bgReadingErrorValue
|
||||
bgReading.hideSlope = true
|
||||
} else {
|
||||
bgReading.calculatedValue = min(Constants.BgReadingAlgorithms.maximumBgReadingCalculatedValue, max(Constants.BgReadingAlgorithms.minimumBgReadingCalculatedValue, bgReading.calculatedValue))
|
||||
bgReading.calculatedValue = min(Constants.CalibrationAlgorithms.maximumBgReadingCalculatedValue, max(Constants.CalibrationAlgorithms.minimumBgReadingCalculatedValue, bgReading.calculatedValue))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -529,11 +529,11 @@ extension CalibratorProtocol {
|
|||
/// - withSensor : the currently active sensor, optional
|
||||
private func calculateAgeAdjustedRawValue(for bgReading:BgReading, withSensor sensor:Sensor?) {
|
||||
if let sensor = sensor {
|
||||
let adjustfor:Double = Constants.BgReadingAlgorithms.ageAdjustmentTime - (bgReading.timeStamp.toMillisecondsAsDouble() - sensor.startDate.toMillisecondsAsDouble())
|
||||
let adjustfor:Double = Constants.CalibrationAlgorithms.ageAdjustmentTime - (bgReading.timeStamp.toMillisecondsAsDouble() - sensor.startDate.toMillisecondsAsDouble())
|
||||
if (adjustfor <= 0 || !ageAdjustMentNeeded) {
|
||||
bgReading.ageAdjustedRawValue = bgReading.rawData
|
||||
} else {
|
||||
bgReading.ageAdjustedRawValue = ((Constants.BgReadingAlgorithms.ageAdjustmentFactor * (adjustfor / Constants.BgReadingAlgorithms.ageAdjustmentTime)) * bgReading.rawData) + bgReading.rawData
|
||||
bgReading.ageAdjustedRawValue = ((Constants.CalibrationAlgorithms.ageAdjustmentFactor * (adjustfor / Constants.CalibrationAlgorithms.ageAdjustmentTime)) * bgReading.rawData) + bgReading.rawData
|
||||
}
|
||||
} else {
|
||||
bgReading.ageAdjustedRawValue = bgReading.rawData
|
||||
|
|
|
@ -12,7 +12,7 @@ struct Constants {
|
|||
static let maxSlopeInMinutes = 21
|
||||
}
|
||||
|
||||
enum BgReadingAlgorithms {
|
||||
enum CalibrationAlgorithms {
|
||||
// age adjustment constants, only for non Libre
|
||||
static let ageAdjustmentTime = 86400000 * 1.9
|
||||
static let ageAdjustmentFactor = 0.45
|
||||
|
@ -56,4 +56,5 @@ struct Constants {
|
|||
enum DexcomG5 {
|
||||
static let batteryReadPeriodInHours = 12.0
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,7 +29,6 @@ final class CGMG4xDripTransmitter: BluetoothTransmitter, BluetoothTransmitterDel
|
|||
/// - address: if already connected before, then give here the address that was received during previous connect, if not give nil
|
||||
/// - transmitterID: expected transmitterID, 5 characters
|
||||
init(address:String?, transmitterID:String, delegate:CGMTransmitterDelegate) {
|
||||
|
||||
// assign addressname and name or expected devicename
|
||||
var newAddressAndName:BluetoothTransmitter.DeviceAddressAndName = BluetoothTransmitter.DeviceAddressAndName.notYetConnected(expectedName: nil)
|
||||
if let address = address {
|
||||
|
|
|
@ -41,16 +41,13 @@ class CGMMiaoMiaoTransmitter:BluetoothTransmitter, BluetoothTransmitterDelegate,
|
|||
|
||||
|
||||
// MARK: - Initialization
|
||||
|
||||
init(addressAndName: BluetoothTransmitter.DeviceAddressAndName, delegate:CGMTransmitterDelegate, timeStampLastBgReading:Date) {
|
||||
|
||||
/// - parameters:
|
||||
/// - address: if already connected before, then give here the address that was received during previous connect, if not give nil
|
||||
init(address:String?, delegate:CGMTransmitterDelegate, timeStampLastBgReading:Date) {
|
||||
// assign addressname and name or expected devicename
|
||||
var newAddressAndName:BluetoothTransmitter.DeviceAddressAndName
|
||||
switch addressAndName {
|
||||
case .alreadyConnectedBefore(let newAddress):
|
||||
newAddressAndName = BluetoothTransmitter.DeviceAddressAndName.alreadyConnectedBefore(address: newAddress)
|
||||
case .notYetConnected:
|
||||
newAddressAndName = BluetoothTransmitter.DeviceAddressAndName.notYetConnected(expectedName: expectedDeviceNameMiaoMiao)
|
||||
var newAddressAndName:BluetoothTransmitter.DeviceAddressAndName = BluetoothTransmitter.DeviceAddressAndName.notYetConnected(expectedName: expectedDeviceNameMiaoMiao)
|
||||
if let address = address {
|
||||
newAddressAndName = BluetoothTransmitter.DeviceAddressAndName.alreadyConnectedBefore(address: address)
|
||||
}
|
||||
|
||||
// assign CGMTransmitterDelegate
|
||||
|
@ -64,6 +61,7 @@ class CGMMiaoMiaoTransmitter:BluetoothTransmitter, BluetoothTransmitterDelegate,
|
|||
self.timeStampLastBgReading = timeStampLastBgReading
|
||||
|
||||
super.init(addressAndName: newAddressAndName, CBUUID_Advertisement: nil, CBUUID_Service: CBUUID_Service_MiaoMiao, CBUUID_ReceiveCharacteristic: CBUUID_ReceiveCharacteristic_MiaoMiao, CBUUID_WriteCharacteristic: CBUUID_WriteCharacteristic_MiaoMiao)
|
||||
|
||||
bluetoothTransmitterDelegate = self
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,9 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
/// peripheral, gets value during connect
|
||||
private var peripheral: CBPeripheral?
|
||||
|
||||
/// will be used to monitor progress
|
||||
private var timeStampLastStatusUpdate:Date
|
||||
|
||||
/// if never connected before to the device, then possibily we expect a specific device name. For example for G5, if transmitter id is ABCDEF, we expect as devicename DexcomEF. For an xDrip bridge, we don't expect a specific devicename, in which case the value stays nil
|
||||
/// the value is only used during first time connection to a new device. Once we've connected at least once, we know the final name (eg xBridge) and will store this name in the name attribute, the expectedName value can then be ignored
|
||||
private var expectedName:String?
|
||||
|
@ -44,6 +47,9 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
/// the receive Characteristic
|
||||
private var receiveCharacteristic:CBCharacteristic?
|
||||
|
||||
/// used in BluetoothTransmitter class, eg if after calling discoverServices new method is called and time is exceed, then cancel connection
|
||||
let maxTimeToWaitForPeripheralResponse = 5.0
|
||||
|
||||
// MARK: - Initialization
|
||||
|
||||
/// - parameters:
|
||||
|
@ -69,6 +75,9 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
self.CBUUID_WriteCharacteristic = CBUUID_WriteCharacteristic
|
||||
self.CBUUID_ReceiveCharacteristic = CBUUID_ReceiveCharacteristic
|
||||
|
||||
//initialize timeStampLastStatusUpdate
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
super.init()
|
||||
|
||||
initialize()
|
||||
|
@ -100,7 +109,12 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
os_log(" peripheral is already connected, will not start scanning", log: log, type: .info)
|
||||
return .alreadyConnected
|
||||
case .connecting:
|
||||
if Date() > Date(timeInterval: maxTimeToWaitForPeripheralResponse, since: timeStampLastStatusUpdate) {
|
||||
os_log(" status connecting, but waiting more than %{public}d seconds, will disconnect", log: log, type: .info, maxTimeToWaitForPeripheralResponse)
|
||||
disconnect()
|
||||
} else {
|
||||
os_log(" peripheral is currently connecting, will not start scanning", log: log, type: .info)
|
||||
}
|
||||
return .connecting
|
||||
default:()
|
||||
}
|
||||
|
@ -115,7 +129,7 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
// try to start the scanning
|
||||
if let centralManager = centralManager {
|
||||
if centralManager.isScanning {
|
||||
os_log("in startScanning but already scanning", log: log, type: .info)
|
||||
os_log(" already scanning", log: log, type: .info)
|
||||
return .alreadyScanning
|
||||
}
|
||||
switch centralManager.state {
|
||||
|
@ -217,6 +231,8 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
|
||||
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
|
||||
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
// devicename needed unwrapped for logging
|
||||
var deviceName = "unknown"
|
||||
if let temp = peripheral.name {
|
||||
|
@ -252,6 +268,8 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
var services:[CBUUID]?
|
||||
if CBUUID_Service.count > 0 {
|
||||
services = [CBUUID(string: CBUUID_Service)]
|
||||
|
@ -264,6 +282,8 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
if let error = error {
|
||||
os_log("failed to connect with error: %{public}@, will try again", log: log, type: .error , error.localizedDescription)
|
||||
} else {
|
||||
|
@ -277,11 +297,13 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
|
||||
/// if new state is powered on and if address is known then try to retrieveperipherals, if that fails start scanning
|
||||
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
os_log("in centralManagerDidUpdateState, new state is %{public}@", log: log, type: .info, "\(central.state.toString())")
|
||||
|
||||
/// in case status changed to powered on and if device address known then try either to retrieveperipherals, or if that doesn't succeed, start scanning
|
||||
if central.state == .poweredOn {
|
||||
if (address != nil) {
|
||||
if (deviceAddress != nil) {
|
||||
if !retrievePeripherals(central) {
|
||||
_ = startScanning()
|
||||
}
|
||||
|
@ -292,6 +314,8 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
if let error = error {
|
||||
os_log("Did disconnect peripheral with error: %{public}@", log: log, type: .error , error.localizedDescription)
|
||||
}
|
||||
|
@ -309,6 +333,8 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
os_log("didDiscoverServices", log: log, type: .info)
|
||||
if let error = error {
|
||||
os_log(" didDiscoverServices error: %{public}@", log: log, type: .error , "\(error.localizedDescription)")
|
||||
|
@ -323,6 +349,8 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
os_log("didDiscoverCharacteristicsFor", log: log, type: .info)
|
||||
if let error = error {
|
||||
os_log(" didDiscoverCharacteristicsFor error: %{public}@", log: log, type: .error , error.localizedDescription)
|
||||
|
@ -347,6 +375,8 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
if let error = error {
|
||||
os_log("didWriteValueFor characteristic %{public}@, characteristic description %{public}@, error = %{public}@", log: log, type: .error, String(describing: characteristic.uuid), String(characteristic.debugDescription), error.localizedDescription)
|
||||
} else {
|
||||
|
@ -355,6 +385,8 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
if let error = error {
|
||||
os_log("didUpdateNotificationStateFor characteristic %{public}@, characteristic description %{public}@, error = %{public}@, no further processing", log: log, type: .error, String(describing: characteristic.uuid), String(characteristic.debugDescription), error.localizedDescription)
|
||||
} else {
|
||||
|
@ -363,6 +395,8 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
|
||||
timeStampLastStatusUpdate = Date()
|
||||
|
||||
if let error = error {
|
||||
os_log("didUpdateValueFor characteristic %{public}@, characteristic description %{public}@, error = %{public}@, no further processing", log: log, type: .error, String(describing: characteristic.uuid), String(characteristic.debugDescription), error.localizedDescription)
|
||||
} else {
|
||||
|
@ -371,10 +405,13 @@ class BluetoothTransmitter: NSObject, CBCentralManagerDelegate, CBPeripheralDele
|
|||
}
|
||||
|
||||
// MARK: methods to get address and name
|
||||
|
||||
/// read device address
|
||||
func address() -> String? {
|
||||
return deviceAddress
|
||||
}
|
||||
|
||||
/// read device name
|
||||
func name() -> String? {
|
||||
return deviceName
|
||||
}
|
||||
|
|
|
@ -60,9 +60,9 @@ If necessary, each of the functions in the protocols CBCentralManagerDelegate an
|
|||
The protocol **BluetoothTransmitterDelegate** defines functions that allow to pass bluetooth activity information from the
|
||||
BluetoothTransmitter class to a specific transmitter class. Example when a disconnect occurs, the BlueToothTransmitter class
|
||||
handles the reconnect but the delegate class can for instance show the connection status to the user. It will be informed about
|
||||
the connection status via the function centralManagerDidConnect in the BluetoothTransmitterDelegate
|
||||
the connection status via the function centralManagerDidConnect in the BluetoothTransmitterDelegate.
|
||||
|
||||
**CGMTransmitter** defines functions that CGM transmitter classes need to implement.
|
||||
The **CGMTransmitter** protocol defines functions that CGM transmitter classes need to implement.
|
||||
|
||||
The CGM transmitter communicates back to the caller via the **CGMTransmitterDelegate** protocol.<br>
|
||||
Needs to be conformed to, for instance by a view controller, or manager, .. whatever<br>
|
||||
|
|
|
@ -48,6 +48,7 @@ final class RootHomeViewController: UIViewController, CGMTransmitterDelegate {
|
|||
if let storedAddress = UserDefaults.standard.bluetoothDeviceAddress {
|
||||
address = storedAddress
|
||||
}
|
||||
//test = CGMMiaoMiaoTransmitter(address: address, delegate: self, timeStampLastBgReading: timeStampLastBgReading)
|
||||
//test = CGMG4xDripTransmitter(address: address, transmitterID: "6LSDU", delegate:self)
|
||||
test = CGMG5Transmitter(address: address, transmitterID: "406QWK", delegate: self)
|
||||
|
||||
|
|
Loading…
Reference in New Issue