reverted changes in coredata, need to create a new model version first
This commit is contained in:
parent
3de5f40fc9
commit
5fc96ea44c
|
@ -37,5 +37,8 @@ enum M5StackTextColor:UInt32, CaseIterable {
|
|||
return Texts_Common.yellow
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
init?(forUInt32: UInt32) {
|
||||
self.init(rawValue: forUInt32)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,14 @@
|
|||
import Foundation
|
||||
import CoreData
|
||||
|
||||
|
||||
/// M5Stack
|
||||
///
|
||||
/// M5Stack has
|
||||
/// - an address (received form M5Stack),
|
||||
/// - a name (also received from M5Stack),
|
||||
/// - shouldConnect with default value true, if true then xdrip will automatically try to connect at app launch
|
||||
/// - blePassword : optional, if this value is set, then it means this M5Stack does have an internally stored password created during M5Stack launch. If it is not set, then the password in the userdefaults will be used. If that is also nil, then the xdrip app can not authenticate towards the M5Stack
|
||||
/// - M5StackName : optional. A reference to a userdefined name
|
||||
public class M5Stack: NSManagedObject {
|
||||
|
||||
/// create M5Stack, shouldconnect default value = true
|
||||
|
|
|
@ -524,7 +524,7 @@ extension UserDefaults {
|
|||
get {
|
||||
let textColorAsInt = integer(forKey: Key.m5StackTextColor.rawValue)
|
||||
if textColorAsInt > 0 {
|
||||
return M5StackTextColor(rawValue: UInt32(textColorAsInt))
|
||||
return M5StackTextColor(forUInt32: UInt32(textColorAsInt))
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -18,6 +18,9 @@ class M5StackManager: NSObject {
|
|||
/// to access m5Stack entity in coredata
|
||||
private var m5StackAccessor: M5StackAccessor
|
||||
|
||||
/// reference to BgReadingsAccessor
|
||||
private var bgReadingsAccessor: BgReadingsAccessor
|
||||
|
||||
/// if scan is called, and a connection is successfully made to a new device, then a new M5Stack must be created, and this function will be called. It is owned by the UIViewController that calls the scan function
|
||||
private var callBackAfterDiscoveringDevice: ((M5Stack) -> Void)?
|
||||
|
||||
|
@ -31,6 +34,7 @@ class M5StackManager: NSObject {
|
|||
// initialize properties
|
||||
self.coreDataManager = coreDataManager
|
||||
self.m5StackAccessor = M5StackAccessor(coreDataManager: coreDataManager)
|
||||
self.bgReadingsAccessor = BgReadingsAccessor(coreDataManager: coreDataManager)
|
||||
|
||||
super.init()
|
||||
|
||||
|
@ -39,16 +43,57 @@ class M5StackManager: NSObject {
|
|||
for m5Stack in m5Stacks {
|
||||
if m5Stack.shouldconnect {
|
||||
// create an instance of M5StackBluetoothTransmitter, M5StackBluetoothTransmitter will automatically try to connect to the M5Stack with the address that is stored in m5Stack
|
||||
self.m5StacksBlueToothTransmitters[m5Stack] = M5StackBluetoothTransmitter(m5Stack: m5Stack, delegateFixed: self)
|
||||
self.m5StacksBlueToothTransmitters[m5Stack] = M5StackBluetoothTransmitter(m5Stack: m5Stack, delegateFixed: self, blePassword: UserDefaults.standard.m5StackBlePassword)
|
||||
} else {
|
||||
// shouldn't connect, so don't create an instance of M5StackBluetoothTransmitter
|
||||
self.m5StacksBlueToothTransmitters[m5Stack] = (M5StackBluetoothTransmitter?).none
|
||||
}
|
||||
}
|
||||
|
||||
// when user changes M5Stack related settings, then the transmitter need to get that info
|
||||
addObservers()
|
||||
|
||||
}
|
||||
|
||||
// MARK: - public helper functions
|
||||
|
||||
/// will send latest reading to all M5Stacks, only if it's less than 5 minutes old
|
||||
public func sendLatestReading() {
|
||||
|
||||
// get reading of latest 5 minutes
|
||||
let bgReadingToSend = bgReadingsAccessor.getLatestBgReadings(limit: 1, fromDate: Date(timeIntervalSinceNow: -5 * 60), forSensor: nil, ignoreRawData: true, ignoreCalculatedValue: false)
|
||||
|
||||
// check that there's at least 1 reading available
|
||||
guard bgReadingToSend.count >= 1 else {
|
||||
trace("in sendLatestReading, there's no recent reading to send", log: self.log, type: .info)
|
||||
return
|
||||
}
|
||||
|
||||
// send the reading to all M5Stacks
|
||||
for m5StackBlueToothTransmitter in m5StacksBlueToothTransmitters.values {
|
||||
m5StackBlueToothTransmitter?.writeBgReadingInfo(bgReading: bgReadingToSend[0])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - private helper functions
|
||||
|
||||
/// when user changes M5Stack related settings, then the transmitter need to get that info, add observers
|
||||
private func addObservers() {
|
||||
|
||||
UserDefaults.standard.addObserver(self, forKeyPath: UserDefaults.Key.m5StackWiFiName1.rawValue, options: .new, context: nil)
|
||||
UserDefaults.standard.addObserver(self, forKeyPath: UserDefaults.Key.m5StackWiFiName2.rawValue, options: .new, context: nil)
|
||||
UserDefaults.standard.addObserver(self, forKeyPath: UserDefaults.Key.m5StackWiFiName3.rawValue, options: .new, context: nil)
|
||||
UserDefaults.standard.addObserver(self, forKeyPath: UserDefaults.Key.m5StackWiFiPassword1.rawValue, options: .new, context: nil)
|
||||
UserDefaults.standard.addObserver(self, forKeyPath: UserDefaults.Key.m5StackWiFiPassword2.rawValue, options: .new, context: nil)
|
||||
UserDefaults.standard.addObserver(self, forKeyPath: UserDefaults.Key.m5StackWiFiPassword3.rawValue, options: .new, context: nil)
|
||||
UserDefaults.standard.addObserver(self, forKeyPath: UserDefaults.Key.m5StackBlePassword.rawValue, options: .new, context: nil)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - conform to M5StackManaging
|
||||
|
||||
extension M5StackManager: M5StackManaging {
|
||||
|
||||
|
||||
|
@ -57,7 +102,7 @@ extension M5StackManager: M5StackManaging {
|
|||
|
||||
callBackAfterDiscoveringDevice = callback
|
||||
|
||||
tempM5StackBlueToothTransmitterWhileScanningForNewM5Stack = M5StackBluetoothTransmitter(m5Stack: nil, delegateFixed: self)
|
||||
tempM5StackBlueToothTransmitterWhileScanningForNewM5Stack = M5StackBluetoothTransmitter(m5Stack: nil, delegateFixed: self, blePassword: UserDefaults.standard.m5StackBlePassword)
|
||||
|
||||
_ = tempM5StackBlueToothTransmitterWhileScanningForNewM5Stack?.startScanning()
|
||||
|
||||
|
@ -94,7 +139,7 @@ extension M5StackManager: M5StackManaging {
|
|||
} else {
|
||||
|
||||
// this can be the case where initially shouldconnect was set to false, and user sets it to true via uiviewcontroller, uiviewcontroller calls this function, connect should automatially be initiated
|
||||
let newBlueToothTransmitter = M5StackBluetoothTransmitter(m5Stack: m5Stack, delegateFixed: self)
|
||||
let newBlueToothTransmitter = M5StackBluetoothTransmitter(m5Stack: m5Stack, delegateFixed: self, blePassword: UserDefaults.standard.m5StackBlePassword)
|
||||
|
||||
m5StacksBlueToothTransmitters[m5Stack] = newBlueToothTransmitter
|
||||
|
||||
|
@ -103,7 +148,7 @@ extension M5StackManager: M5StackManaging {
|
|||
} else {
|
||||
|
||||
// I don't think this code will be used, because value m5Stack should always be in m5StacksBlueToothTransmitters, anyway let's add it
|
||||
let newBlueToothTransmitter = M5StackBluetoothTransmitter(m5Stack: m5Stack, delegateFixed: self)
|
||||
let newBlueToothTransmitter = M5StackBluetoothTransmitter(m5Stack: m5Stack, delegateFixed: self, blePassword: UserDefaults.standard.m5StackBlePassword)
|
||||
|
||||
m5StacksBlueToothTransmitters[m5Stack] = newBlueToothTransmitter
|
||||
|
||||
|
@ -133,7 +178,7 @@ extension M5StackManager: M5StackManaging {
|
|||
}
|
||||
|
||||
if createANewOneIfNecesssary {
|
||||
let newTransmitter = M5StackBluetoothTransmitter(m5Stack: m5Stack, delegateFixed: self)
|
||||
let newTransmitter = M5StackBluetoothTransmitter(m5Stack: m5Stack, delegateFixed: self, blePassword: UserDefaults.standard.m5StackBlePassword)
|
||||
m5StacksBlueToothTransmitters[m5Stack] = newTransmitter
|
||||
return newTransmitter
|
||||
}
|
||||
|
@ -163,6 +208,8 @@ extension M5StackManager: M5StackManaging {
|
|||
}
|
||||
}
|
||||
|
||||
// MARK: - conform to M5StackBluetoothDelegate
|
||||
|
||||
extension M5StackManager: M5StackBluetoothDelegate {
|
||||
|
||||
func didConnect(forM5Stack m5Stack: M5Stack?, address: String?, name: String?, bluetoothTransmitter : M5StackBluetoothTransmitter) {
|
||||
|
@ -181,7 +228,7 @@ extension M5StackManager: M5StackBluetoothDelegate {
|
|||
|
||||
// it's an already known m5Stack, not storing this, on the contrary disconnecting because maybe it's an m5stack already known for which user has preferred not to connect to
|
||||
// If we're actually waiting for a new scan result, then there's an instance of M5StacksBlueToothTransmitter stored in tempM5StackBlueToothTransmitterWhileScanningForNewM5Stack - but this one stopped scanning, so let's recreate an instance of M5StacksBlueToothTransmitter
|
||||
tempM5StackBlueToothTransmitterWhileScanningForNewM5Stack = M5StackBluetoothTransmitter(m5Stack: nil, delegateFixed: self)
|
||||
tempM5StackBlueToothTransmitterWhileScanningForNewM5Stack = M5StackBluetoothTransmitter(m5Stack: nil, delegateFixed: self, blePassword: UserDefaults.standard.m5StackBlePassword)
|
||||
_ = tempM5StackBlueToothTransmitterWhileScanningForNewM5Stack?.startScanning()
|
||||
|
||||
return
|
||||
|
|
|
@ -44,8 +44,9 @@ final class M5StackBluetoothTransmitter: BluetoothTransmitter, BluetoothTransmit
|
|||
/// - parameters:
|
||||
/// - m5Stack : an instance of M5Stack, if nil then this instance is created to scan for a new M5Stack, address and name not yet known, so not possible yet to create an M5Stack instance
|
||||
/// - delegate : there's two delegates, one public, one private. The private one will be assigned during object creation. There's two because two other classes want to receive feedback : M5StackManager and UIViewControllers. There's only one instance of M5StackManager and it's always the same. An instance will be assigned to m5StackBluetoothTransmitterDelegateFixed. There can be different UIViewController' and they can change, an instance will be assigned to m5StackBluetoothTransmitterDelegateVariable
|
||||
/// - blePassword : only if set in userdefaults
|
||||
init(m5Stack: M5Stack?, delegateFixed: M5StackBluetoothDelegate) {
|
||||
/// - blePassword : optional. If nil then xdrip will send a M5StackReadBlePassWordTxMessage to the M5Stack, so this would be a case where the M5Stack (all M5Stacks managed by xdrip) do not have a fixed blepassword
|
||||
/// The blepassword in the M5Stack object gets priority over the blePassword in the parameter list
|
||||
init(m5Stack: M5Stack?, delegateFixed: M5StackBluetoothDelegate, blePassword: String?) {
|
||||
|
||||
// assign addressname and name or expected devicename
|
||||
var newAddressAndName:BluetoothTransmitter.DeviceAddressAndName = BluetoothTransmitter.DeviceAddressAndName.notYetConnected(expectedName: "M5Stack")
|
||||
|
@ -53,7 +54,14 @@ final class M5StackBluetoothTransmitter: BluetoothTransmitter, BluetoothTransmit
|
|||
newAddressAndName = BluetoothTransmitter.DeviceAddressAndName.alreadyConnectedBefore(address: m5Stack.address, name: m5Stack.name)
|
||||
}
|
||||
|
||||
self.blePassword = m5Stack?.blepassword
|
||||
if let m5Stack = m5Stack, let blePassword = m5Stack.blepassword {
|
||||
// m5Stack object is not nil and does have a blePassword, use that value
|
||||
self.blePassword = blePassword
|
||||
} else {
|
||||
// use blePassword that was in the parameter list, possibily nil value
|
||||
self.blePassword = blePassword
|
||||
}
|
||||
|
||||
self.m5Stack = m5Stack
|
||||
|
||||
self.m5StackBluetoothTransmitterDelegateFixed = delegateFixed
|
||||
|
|
|
@ -318,6 +318,7 @@ final class M5StackViewController: UIViewController {
|
|||
if shouldConnect {
|
||||
|
||||
// device should not automaticaly connect, which means, each time the app restarts, it will not try to connect to this M5Stack
|
||||
// if user clicks cancel button (ie goes back to previous view controller without clicking done, then this value will not be saved
|
||||
shouldConnect = false
|
||||
|
||||
// normally there should be a bluetoothTransmitter
|
||||
|
@ -331,6 +332,7 @@ final class M5StackViewController: UIViewController {
|
|||
} else {
|
||||
|
||||
// device should automatically connect, this will be stored in coredata (only after clicking done button), which means, each time the app restarts, it will try to connect to this M5Stack
|
||||
// if user clicks cancel button (ie goes back to previous view controller without clicking done, then this value will not be saved
|
||||
shouldConnect = true
|
||||
|
||||
// connect,
|
||||
|
|
|
@ -361,6 +361,8 @@ final class RootViewController: UIViewController {
|
|||
|
||||
dexcomShareUploadManager?.upload()
|
||||
|
||||
m5StackManager?.sendLatestReading()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1450,6 +1452,8 @@ extension RootViewController:NightScoutFollowerDelegate {
|
|||
bgReadingSpeaker.speakNewReading()
|
||||
}
|
||||
|
||||
m5StackManager?.sendLatestReading()
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue