Skip to content

Commit

Permalink
Merge branch 'main' into apple-health-full-sync
Browse files Browse the repository at this point in the history
  • Loading branch information
bassrock committed Mar 4, 2023
2 parents a998b00 + 5dd262f commit 42babcd
Show file tree
Hide file tree
Showing 18 changed files with 216 additions and 220 deletions.
2 changes: 1 addition & 1 deletion App/Modules/AppleSync/AppleCalendarExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private class AppleCalendarExportService {
event.startDate = timestamp
event.endDate = timestamp + durationMinutes * 60

if withAlarm || true {
if withAlarm {
let alarm = EKAlarm(relativeOffset: 0)
event.alarms = [alarm]
}
Expand Down
56 changes: 15 additions & 41 deletions App/Modules/BellmanAlarm/BellmanAlarm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ private func bellmanAlarmMiddelware(service: LazyService<BellmanAlarmService>, s

let alarm = state.isAlarm(glucoseValue: glucose.glucoseValue)
DirectLog.info("alarm: \(alarm)")

let isSnoozed = state.isSnoozed(alarm: alarm)
DirectLog.info("isSnoozed: \(isSnoozed)")

guard !isSnoozed else {
break
}
Expand Down Expand Up @@ -108,7 +108,7 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
setStayConnected(stayConnected: false)

managerQueue.sync {
self.disconnect()
disconnect()
}
}

Expand All @@ -124,13 +124,13 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
return
}

managerQueue.sync {
managerQueue.async {
self.notify(type: .withoutResponse)
}
}

func centralManagerDidUpdateState(_ central: CBCentralManager) {
guard manager != nil else {
guard let manager else {
DirectLog.error("Guard: manager is nil")
return
}
Expand Down Expand Up @@ -167,8 +167,6 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
}

func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
DirectLog.info("DidFailToConnect peripheral: \(peripheral)")

if let error = error {
DirectLog.error("DidFailToConnect error: \(error)")
}
Expand All @@ -183,8 +181,6 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
}

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
DirectLog.info("DidDisconnectPeripheral peripheral: \(peripheral)")

if let error = error {
DirectLog.error("DidDisconnectPeripheral error: \(error)")
}
Expand All @@ -199,15 +195,11 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
DirectLog.info("DidConnect peripheral: \(peripheral)")

setConnectionState(connectionState: .connected)
peripheral.discoverServices([commandServiceUUID, deviceServiceUUID])
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
DirectLog.info("DidDiscoverServices peripheral: \(peripheral)")

if let error = error {
DirectLog.error("DidDiscoverServices error: \(error)")
}
Expand All @@ -223,8 +215,6 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
DirectLog.info("DidDiscoverCharacteristicsFor peripheral: \(peripheral)")

if let error = error {
DirectLog.error("DidDiscoverCharacteristicsFor error: \(error)")
}
Expand Down Expand Up @@ -263,8 +253,6 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
}

func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
DirectLog.info("DidUpdateNotificationStateFor peripheral: \(peripheral)")

if let error = error {
DirectLog.error("DidUpdateNotificationStateFor error: \(error)")
}
Expand All @@ -283,8 +271,6 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
}

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
DirectLog.info("DidWriteValueFor peripheral: \(peripheral)")

if let error = error {
DirectLog.error("DidWriteValueFor error: \(error)")
}
Expand All @@ -299,8 +285,6 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
DirectLog.info("DidUpdateValueFor peripheral: \(peripheral)")

if let error = error {
DirectLog.error("DidUpdateValueFor error: \(error)")
}
Expand All @@ -314,18 +298,6 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
DirectLog.info("DidUpdateValueFor data.count: \(data.count)")
}

func connect() {
DirectLog.info("Connect")

setConnectionState(connectionState: .connecting)

if let peripheral = peripheral {
connect(peripheral)
} else {
find()
}
}

// MARK: Private

private weak var subject: PassthroughSubject<DirectAction, DirectError>?
Expand Down Expand Up @@ -397,8 +369,6 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
}

private func find() {
DirectLog.info("Find")

guard manager.state == .poweredOn else {
DirectLog.error("Guard: manager.state \(manager.state.rawValue) is not .poweredOn")
return
Expand All @@ -422,17 +392,23 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
}
}
}

private func connect() {
setConnectionState(connectionState: .connecting)

private func connect(_ peripheral: CBPeripheral) {
DirectLog.info("Connect: \(peripheral)")
if let peripheral = peripheral {
connect(peripheral)
} else {
find()
}
}

private func connect(_ peripheral: CBPeripheral) {
self.peripheral = peripheral
manager.connect(peripheral, options: nil)
}

private func disconnect() {
DirectLog.info("Disconnect")

if manager.isScanning {
manager.stopScan()
}
Expand All @@ -446,8 +422,6 @@ private class BellmanAlarmService: NSObject, CBCentralManagerDelegate, CBPeriphe
}

private func notify(type: CBCharacteristicWriteType) {
DirectLog.info("Notify")

setShouldNotify(shouldNotify: false)

if let peripheral = peripheral, let writeCharacteristic = writeCharacteristic {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class BubbleConnection: SensorBluetoothConnection, IsTransmitter {
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
DirectLog.info("Peripheral: \(peripheral)")

sendUpdate(error: error)

if let services = peripheral.services {
Expand All @@ -46,8 +44,6 @@ class BubbleConnection: SensorBluetoothConnection, IsTransmitter {
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
DirectLog.info("Peripheral: \(peripheral)")

sendUpdate(error: error)

if let characteristics = service.characteristics {
Expand All @@ -67,8 +63,6 @@ class BubbleConnection: SensorBluetoothConnection, IsTransmitter {
}

func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
DirectLog.info("Peripheral: \(peripheral)")

sendUpdate(error: error)

guard let writeCharacteristic = writeCharacteristic else {
Expand All @@ -79,8 +73,6 @@ class BubbleConnection: SensorBluetoothConnection, IsTransmitter {
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
DirectLog.info("Peripheral: \(peripheral)")

guard let value = characteristic.value else {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Libre2Connection: SensorBluetoothConnection, IsSensor {
override func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
DirectLog.info("Found peripheral: \(peripheral.name ?? "-")")

guard manager != nil else {
guard let manager else {
DirectLog.error("Guard: manager is nil")
return
}
Expand All @@ -69,8 +69,6 @@ class Libre2Connection: SensorBluetoothConnection, IsSensor {
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
DirectLog.info("Peripheral: \(peripheral)")

sendUpdate(error: error)

if let services = peripheral.services {
Expand All @@ -83,8 +81,6 @@ class Libre2Connection: SensorBluetoothConnection, IsSensor {
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
DirectLog.info("Peripheral: \(peripheral)")

sendUpdate(error: error)

if let characteristics = service.characteristics {
Expand All @@ -111,8 +107,6 @@ class Libre2Connection: SensorBluetoothConnection, IsSensor {
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
DirectLog.info("Peripheral: \(peripheral)")

sendUpdate(error: error)

guard let value = characteristic.value else {
Expand Down Expand Up @@ -155,7 +149,7 @@ class Libre2Connection: SensorBluetoothConnection, IsSensor {
}
}

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
managerQueue.asyncAfter(deadline: .now() + .seconds(2)) {
self.resetBuffer()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ class LibreLinkConnection: Libre2Connection {
}

override func find() {
DirectLog.info("Find")

guard manager != nil else {
guard let manager else {
DirectLog.error("Guard: manager is nil")
return
}

guard let serviceUUID else {
DirectLog.error("Guard: serviceUUID is nil")
return
}

guard manager.state == .poweredOn else {
DirectLog.error("Guard: manager.state \(manager.state.rawValue) is not .poweredOn")
Expand All @@ -71,7 +74,7 @@ class LibreLinkConnection: Libre2Connection {
connect(connectedPeripheral)

} else {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) {
managerQueue.asyncAfter(deadline: .now() + .seconds(5)) {
self.find()
}
}
Expand Down
Loading

0 comments on commit 42babcd

Please sign in to comment.