fixes for M5Stick

This commit is contained in:
Johan Degraeve 2019-12-21 16:37:32 +01:00
parent 55bb9d7bee
commit d6fc94ca5d
12 changed files with 109 additions and 62 deletions

View File

@ -1247,8 +1247,6 @@
F8EA6CA421B9A25B0082976B /* classes */ = {
isa = PBXGroup;
children = (
F804870A2336D90200EBDDB7 /* M5Stack+CoreDataClass.swift */,
F804870B2336D90200EBDDB7 /* M5Stack+CoreDataProperties.swift */,
F8B3A79122635A25004BA588 /* AlertEntry+CoreDataClass.swift */,
F8B3A79222635A25004BA588 /* AlertEntry+CoreDataProperties.swift */,
F8B3A78C22622953004BA588 /* AlertType+CoreDataClass.swift */,
@ -1257,6 +1255,8 @@
F85DC2E721CFE2F500B9F74A /* BgReading+CoreDataProperties.swift */,
F85DC2F021CFE3D400B9F74A /* Calibration+CoreDataClass.swift */,
F867E25D2252ADAB000FD265 /* Calibration+CoreDataProperties.swift */,
F804870A2336D90200EBDDB7 /* M5Stack+CoreDataClass.swift */,
F804870B2336D90200EBDDB7 /* M5Stack+CoreDataProperties.swift */,
F85DC2F121CFE3D400B9F74A /* Sensor+CoreDataClass.swift */,
F85DC2E921CFE2F500B9F74A /* Sensor+CoreDataProperties.swift */,
);

View File

@ -19,8 +19,11 @@ extension M5Stack: BluetoothPeripheral {
return address
}
// get the type of BluetoothPeripheral: "M5Strack", ...
// get the type of BluetoothPeripheral: "M5Stack", ...
func bluetoothPeripheralType() -> BluetoothPeripheralType {
if isM5StickC {
return .M5StickCType
}
return .M5StackType
}

View File

@ -9,6 +9,9 @@ final class M5StackBluetoothTransmitter: BluetoothTransmitter {
// MARK: - public properties
/// M5StackBluetoothTransmitter can be used for bluetoothPeripheralType's M5Stack or M5StickC. We need to store the type for which it is being used
public let bluetoothPeripheralType: BluetoothPeripheralType
// MARK: - private properties
/// service to be discovered
@ -39,7 +42,8 @@ final class M5StackBluetoothTransmitter: BluetoothTransmitter {
/// - name : if already connected before, then give here the name that was received during previous connect, if not give nil
/// - delegate : the M5StackBluetoothTransmitterDelegate
/// - 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
init(address:String?, name: String?, delegate: M5StackBluetoothTransmitterDelegate, blePassword: String?) {
/// - bluetoothPeripheralType : M5Stack or M5StickC
init(address:String?, name: String?, delegate: M5StackBluetoothTransmitterDelegate, blePassword: String?, bluetoothPeripheralType: BluetoothPeripheralType) {
// assign addressname and name, assume it's not been connected before
var newAddressAndName:BluetoothTransmitter.DeviceAddressAndName = BluetoothTransmitter.DeviceAddressAndName.notYetConnected(expectedName: "M5Stack")
@ -52,6 +56,9 @@ final class M5StackBluetoothTransmitter: BluetoothTransmitter {
// assign blePassword
self.blePassword = blePassword
// assign bluetoothPeripheralType
self.bluetoothPeripheralType = bluetoothPeripheralType
// call super
super.init(addressAndName: newAddressAndName, CBUUID_Advertisement: nil, servicesCBUUIDs: [CBUUID(string: CBUUID_Service)], CBUUID_ReceiveCharacteristic: CBUUID_TxRxCharacteristic, CBUUID_WriteCharacteristic: CBUUID_TxRxCharacteristic, startScanningAfterInit: false, bluetoothTransmitterDelegate: delegate)

View File

@ -22,6 +22,9 @@ enum ConstantsM5Stack {
/// github url with repository for M5Stack that supports bluetooth
static let githubURLM5Stack = "https://github.com/JohanDegraeve/M5_NightscoutMon"
/// github url with repository for M5StickC that supports bluetooth
static let githubURLM5StickC = "https://github.com/JohanDegraeve/M5_StickC_xdrip_iOS"
}
enum M5StackColor:UInt16, CaseIterable {

View File

@ -20,24 +20,25 @@ class M5StackAccessor {
// MARK: Public functions
/// gets all M5Stack nstances from coredata. Includes M5Stick instances which is derived from M5Stack
func getM5Stacks() -> [M5Stack] {
// create fetchRequest
let fetchRequest: NSFetchRequest<M5Stack> = M5Stack.fetchRequest()
// create fetchRequest to get M5Stack's as M5Stack classes
let m5StackFetchRequest: NSFetchRequest<M5Stack> = M5Stack.fetchRequest()
// fetch the M5Stacks
var m5Stacks = [M5Stack]()
var m5StackArray = [M5Stack]()
coreDataManager.mainManagedObjectContext.performAndWait {
do {
// Execute Fetch Request
m5Stacks = try fetchRequest.execute()
m5StackArray = try m5StackFetchRequest.execute()
} catch {
let fetchError = error as NSError
trace("in getM5Stacks, Unable to Execute m5Stacks Fetch Request : %{public}@", log: self.log, type: .error, fetchError.localizedDescription)
}
}
return m5Stacks
return m5StackArray
}
}

View File

@ -59,7 +59,7 @@ class BluetoothPeripheralManager: NSObject {
// create an instance of M5StackBluetoothTransmitter, M5StackBluetoothTransmitter will automatically try to connect to the M5Stack with the address that is stored in m5Stack
// add it to the array of bluetoothTransmitters
bluetoothTransmitters.append(M5StackBluetoothTransmitter(address: m5Stack.address, name: m5Stack.name, delegate: self, blePassword: m5Stack.blepassword))
bluetoothTransmitters.append(M5StackBluetoothTransmitter(address: m5Stack.address, name: m5Stack.name, delegate: self, blePassword: m5Stack.blepassword, bluetoothPeripheralType: m5Stack.isM5StickC ? .M5StickCType : .M5StackType))
} else {
@ -173,7 +173,7 @@ class BluetoothPeripheralManager: NSObject {
case .M5StackType, .M5StickCType:
return M5StackBluetoothTransmitter(address: nil, name: nil, delegate: self, blePassword: UserDefaults.standard.m5StackBlePassword)
return M5StackBluetoothTransmitter(address: nil, name: nil, delegate: self, blePassword: UserDefaults.standard.m5StackBlePassword, bluetoothPeripheralType: type)
}
@ -188,8 +188,8 @@ class BluetoothPeripheralManager: NSObject {
case .M5StackType, .M5StickCType:
if bluetoothTransmitter is M5StackBluetoothTransmitter {
return .M5StackType
if let bluetoothTransmitter = bluetoothTransmitter as? M5StackBluetoothTransmitter {
return bluetoothTransmitter.bluetoothPeripheralType
}
}
@ -198,6 +198,7 @@ class BluetoothPeripheralManager: NSObject {
// normally we shouldn't get here, but we need to return a value
fatalError("BluetoothPeripheralManager : getTransmitterType did not find a valid type")
}
// MARK:- override observe function
@ -400,7 +401,7 @@ extension BluetoothPeripheralManager: BluetoothPeripheralManaging {
blePassword = UserDefaults.standard.m5StackBlePassword
}
newTransmitter = M5StackBluetoothTransmitter(address: m5Stack.address, name: m5Stack.name, delegate: self, blePassword: blePassword)
newTransmitter = M5StackBluetoothTransmitter(address: m5Stack.address, name: m5Stack.name, delegate: self, blePassword: blePassword, bluetoothPeripheralType: bluetoothPeripheral.bluetoothPeripheralType())
}
}

View File

@ -2,5 +2,8 @@
"authenticationFailureWarning" = "Authentication to M5Stack Failed, either set the pre-configured password in the Settings, or, if the M5Stack does not have a preconfigured password then reset the M5Stack. M5Stack will disconnect now. You can make a new attempt by clicking ";
"blePasswordMissingWarning" = "You need to set the password in the Settings";
"m5StackResetRequiredWarning" = "M5Stack must be reset in order to generate a new temporary password. When done click ";
"m5StackSoftWareHelpCellText" = "Where to find M5Stack software ?";
"m5StackSoftWhereHelpCellText" = "Where to find M5Stack software ?";
"m5StickCSoftWhereHelpCellText" = "Where to find M5StickC software ?";
"m5StackSoftWareHelpText" = "Go to";
"m5StackViewscreenTitle" = "M5Stack";
"m5StickCViewscreenTitle" = "M5StickC";

View File

@ -5,8 +5,12 @@ class Texts_M5StackView {
static private let filename = "M5StackView"
static let screenTitle: String = {
return NSLocalizedString("screenTitle", tableName: filename, bundle: Bundle.main, value: "M5Stack", comment: "when M5 stack list is shown, title of the view")
static let m5StackViewscreenTitle: String = {
return NSLocalizedString("m5StackViewscreenTitle", tableName: filename, bundle: Bundle.main, value: "M5Stack", comment: "when M5Stack list is shown, title of the view")
}()
static let m5StickCViewscreenTitle: String = {
return NSLocalizedString("m5StickCViewscreenTitle", tableName: filename, bundle: Bundle.main, value: "M5StickC", comment: "when M5Stickc list is shown, title of the view")
}()
static let authenticationFailureWarning: String = {
@ -21,8 +25,12 @@ class Texts_M5StackView {
return NSLocalizedString("m5StackResetRequiredWarning", tableName: filename, bundle: Bundle.main, value: "You need to reset the M5Stack in order to get a new temporary password. When done click'", comment: "in case M5Stack authentication failed, and M5Stack is generating a random password")
}()
static let m5StackSoftWareHelpCellText: String = {
return NSLocalizedString("m5StackSoftWareHelpCellText", tableName: filename, bundle: Bundle.main, value: "Where to find M5Stack software ?", comment: "In list of M5Stacks, the last line allows to show info where to find M5Stack software, this is the text in the cell")
static let m5StackSoftWhereHelpCellText: String = {
return NSLocalizedString("m5StackSoftWhereHelpCellText", tableName: filename, bundle: Bundle.main, value: "Where to find M5Stack software ?", comment: "In m5Stack view, one line allows to show info where to find M5Stack software, this is the text in the cell")
}()
static let m5StickCSoftWhereHelpCellText: String = {
return NSLocalizedString("m5StickCSoftWhereHelpCellText", tableName: filename, bundle: Bundle.main, value: "Where to find M5StickC software ?", comment: "In m5StickC view, one line allows to show info where to find M5Stack software, this is the text in the cell")
}()
static let m5StackSoftWareHelpText: String = {

View File

@ -41,7 +41,15 @@ class BluetoothPeripheralViewController: UIViewController {
/// action for scan Button, to scan for BluetoothPeripheral
@IBAction func scanButtonAction(_ sender: UIButton) {
self.scanForBluetoothPeripheral(type: expectedBluetoothPeripheralType)
if let expectedBluetoothPeripheralType = expectedBluetoothPeripheralType {
self.scanForBluetoothPeripheral(type: expectedBluetoothPeripheralType)
} else {
fatalError("in scanButtonAction, expectedBluetoothPeripheralType is nil")
}
}
/// action for cancelbutton
@ -91,7 +99,7 @@ class BluetoothPeripheralViewController: UIViewController {
/// needed to support the bluetooth peripheral type specific attributes
private var bluetoothPeripheralViewModel: BluetoothPeripheralViewModel!
private var expectedBluetoothPeripheralType: BluetoothPeripheralType!
private var expectedBluetoothPeripheralType: BluetoothPeripheralType?
// MARK:- public functions
@ -122,7 +130,11 @@ class BluetoothPeripheralViewController: UIViewController {
super.viewDidLoad()
// here the tableView is not nil, we can safely call bluetoothPeripheralViewModel.configure, this one requires a non-nil tableView
bluetoothPeripheralViewModel = expectedBluetoothPeripheralType.viewModel()
// get a viewModel instance for the expectedBluetoothPeripheralType
bluetoothPeripheralViewModel = expectedBluetoothPeripheralType?.viewModel()
// configure the bluetoothPeripheralViewModel
bluetoothPeripheralViewModel?.configure(bluetoothPeripheral: bluetoothPeripheralAsNSObject, bluetoothPeripheralManager: self.bluetoothPeripheralManager, tableView: tableView, bluetoothPeripheralViewController: self, settingRowOffset: Setting.allCases.count, bluetoothTransmitterDelegate: self)
// still need to assign the delegate in the transmitter object
@ -241,16 +253,11 @@ class BluetoothPeripheralViewController: UIViewController {
}
/// user clicks scan button
/// - parameter :
/// - type is optional here because it's called from within IBAction scanButtonAction
private func scanForBluetoothPeripheral(type: BluetoothPeripheralType?) {
private func scanForBluetoothPeripheral(type: BluetoothPeripheralType) {
// if bluetoothPeripheralASNSObject is not nil, then there's already a BluetoothPeripheral for which scanning has started or which is already known from a previous scan (either connected or not connected) (bluetoothPeripheralASNSObject should be nil because if it is not, the scanbutton should not even be enabled, anyway let's check).
guard bluetoothPeripheralAsNSObject == nil else {return}
// should never be called with type == nil
guard let type = type else {return}
bluetoothPeripheralManager.startScanningForNewDevice(type: type, callback: { (bluetoothPeripheral) in
// assign internal bluetoothPeripheralASNSObject to new bluetoothPeripheral
@ -300,9 +307,9 @@ class BluetoothPeripheralViewController: UIViewController {
guard let bluetoothPeripheralASNSObject = bluetoothPeripheralAsNSObject else {return}
// textToAdd is either 'address' + the address, or 'alias' + the alias, depending if alias has a value
var textToAdd = Text_BluetoothPeripheralView.address + bluetoothPeripheralASNSObject.getAddress()
var textToAdd = Text_BluetoothPeripheralView.address + " " + bluetoothPeripheralASNSObject.getAddress()
if let alias = aliasTemporaryValue {
textToAdd = Text_BluetoothPeripheralView.bluetoothPeripheralAlias + alias
textToAdd = Text_BluetoothPeripheralView.bluetoothPeripheralAlias + " " + alias
}
// first ask user if ok to delete and if yes delete
@ -510,7 +517,9 @@ extension BluetoothPeripheralViewController: UITableViewDataSource, UITableViewD
// it's a setting not defined here but in a BluetoothPeripheralViewModel
// bluetoothPeripheralViewModel should not be nil here, otherwise user wouldn't be able to click a row which is higher than maximum
if let bluetoothPeripheralViewModel = bluetoothPeripheralViewModel, let bluetoothPeripheral = bluetoothPeripheralAsNSObject {
bluetoothPeripheralViewModel.userDidSelectRow(withSettingRawValue: indexPath.row - Setting.allCases.count, for: bluetoothPeripheral, bluetoothPeripheralManager: bluetoothPeripheralManager, doneButtonOutlet: doneButtonOutlet)
}
return

View File

@ -10,20 +10,23 @@ class M5StackBluetoothPeripheralViewModel {
/// - these are attributes specific to M5Stack, the generic ones are defined in BluetoothPeripheralViewController
public enum Setting:Int, CaseIterable {
/// helptext for M5Stack software
case m5StackHelpText = 0
/// ble password
case blePassword = 0
case blePassword = 1
/// textColor
case textColor = 1
case textColor = 2
/// backGroundColor
case backGroundColor = 2
case backGroundColor = 3
/// rotation
case rotation = 3
case rotation = 4
/// case brightness
case brightness = 4
case brightness = 5
}
@ -62,7 +65,7 @@ class M5StackBluetoothPeripheralViewModel {
private weak var tableView: UITableView?
/// reference to BluetoothPeripheralViewController that will own this M5StackBluetoothPeripheralViewModel - needed to present stuff etc
private weak var bluetoothPeripheralViewController: BluetoothPeripheralViewController?
private(set) weak var bluetoothPeripheralViewController: BluetoothPeripheralViewController?
private weak var bluetoothTransmitterDelegate: BluetoothTransmitterDelegate?
@ -71,6 +74,13 @@ class M5StackBluetoothPeripheralViewModel {
// MARK: - public functions
/// get screenTitle
///
/// because screentitle is different for M5Stick, this function allows override by M5Stick specific viewmodel
public func m5StackcreenTitle() -> String {
return Texts_M5StackView.m5StackViewscreenTitle
}
/// - implements the update functions defined in protocol BluetoothPeripheralViewModelProtocol
/// - this function is defined to allow override by M5StickC specific model class, because
public func userDidSelectM5StackRow(withSettingRawValue rawValue: Int, for bluetoothPeripheral: BluetoothPeripheral, bluetoothPeripheralManager: BluetoothPeripheralManaging, doneButtonOutlet: UIBarButtonItem) {
@ -79,6 +89,11 @@ class M5StackBluetoothPeripheralViewModel {
switch setting {
case .m5StackHelpText:
let alert = UIAlertController(title: Texts_HomeView.info, message: Texts_M5StackView.m5StackSoftWareHelpText + " " + ConstantsM5Stack.githubURLM5Stack, actionHandler: nil)
bluetoothPeripheralViewController?.present(alert, animated: true, completion: nil)
case .blePassword:
break
@ -288,6 +303,11 @@ class M5StackBluetoothPeripheralViewModel {
// configure the cell depending on setting
switch setting {
case .m5StackHelpText:
cell.textLabel?.text = Texts_M5StackView.m5StackSoftWhereHelpCellText
cell.detailTextLabel?.text = nil
cell.accessoryType = .disclosureIndicator
case .blePassword:
cell.textLabel?.text = Texts_Common.password
cell.detailTextLabel?.text = m5Stack.blepassword
@ -531,7 +551,7 @@ extension M5StackBluetoothPeripheralViewModel: BluetoothPeripheralViewModel {
}
func screenTitle() -> String {
Texts_M5StackView.screenTitle
return m5StackcreenTitle()
}
/// - parameters :

View File

@ -3,6 +3,10 @@ import UIKit
class M5StickCBluetoothPeripheralViewModel : M5StackBluetoothPeripheralViewModel {
override func m5StackcreenTitle() -> String {
return Texts_M5StackView.m5StickCViewscreenTitle
}
override func updateM5Stack(cell: UITableViewCell, withSettingRawValue rawValue: Int, for bluetoothPeripheral: BluetoothPeripheral) {
// verify that rawValue is within range of setting
@ -16,6 +20,11 @@ class M5StickCBluetoothPeripheralViewModel : M5StackBluetoothPeripheralViewModel
// M5StickC doesn't support brightness
cell.accessoryType = .none
} else if setting == .m5StackHelpText {
// specific text for M5StickC in the cell
cell.textLabel?.text = Texts_M5StackView.m5StickCSoftWhereHelpCellText
}
}
@ -31,6 +40,12 @@ class M5StickCBluetoothPeripheralViewModel : M5StackBluetoothPeripheralViewModel
// On M5StickC, user can't change the brightness, so do nothing
return
} else if setting == .m5StackHelpText {
let alert = UIAlertController(title: Texts_HomeView.info, message: Texts_M5StackView.m5StackSoftWareHelpText + " " + ConstantsM5Stack.githubURLM5StickC, actionHandler: nil)
bluetoothPeripheralViewController?.present(alert, animated: true, completion: nil)
} else {
super.userDidSelectM5StackRow(withSettingRawValue: rawValue, for: bluetoothPeripheral, bluetoothPeripheralManager: bluetoothPeripheralManager, doneButtonOutlet: doneButtonOutlet)

View File

@ -168,25 +168,13 @@ final class BluetoothPeripheralsViewController: UIViewController {
extension BluetoothPeripheralsViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// add 1 for the row that will show help info
return bluetoothPeripheralManager.getBluetoothPeripherals().count + 1
return bluetoothPeripheralManager.getBluetoothPeripherals().count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: SettingsTableViewCell.reuseIdentifier, for: indexPath) as? SettingsTableViewCell else { fatalError("Unexpected Table View Cell") }
// the last row is the help info
if indexPath.row == bluetoothPeripheralManager.getBluetoothPeripherals().count {
cell.textLabel?.text = Texts_M5StackView.m5StackSoftWareHelpCellText
cell.detailTextLabel?.text = nil
cell.accessoryType = .disclosureIndicator
return cell
}
// textLabel should be the user defined alias of the BluetoothPeripheral, or if user defined alias == nil, then the devicename
cell.textLabel?.text = bluetoothPeripheralManager.getBluetoothPeripherals()[indexPath.row].getAlias()
if cell.textLabel?.text == nil {
@ -218,19 +206,8 @@ extension BluetoothPeripheralsViewController: UITableViewDataSource, UITableView
tableView.deselectRow(at: indexPath, animated: true)
// the last row is the help info
if indexPath.row == bluetoothPeripheralManager.getBluetoothPeripherals().count {
self.performSegue(withIdentifier: BluetoothPeripheralViewController.SegueIdentifiers.BluetoothPeripheralsToBluetoothPeripheralSegueIdentifier.rawValue, sender: bluetoothPeripheralManager.getBluetoothPeripherals()[indexPath.row])
let alert = UIAlertController(title: Texts_HomeView.info, message: Texts_M5StackView.m5StackSoftWareHelpText + " " + ConstantsM5Stack.githubURLM5Stack, actionHandler: nil)
self.present(alert, animated: true, completion: nil)
} else {
self.performSegue(withIdentifier: BluetoothPeripheralViewController.SegueIdentifiers.BluetoothPeripheralsToBluetoothPeripheralSegueIdentifier.rawValue, sender: bluetoothPeripheralManager.getBluetoothPeripherals()[indexPath.row])
}
}
}