-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Dev 1.1.1 x22 battery status #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -353,6 +353,12 @@ final class DeviceDataManager: CarbStoreDelegate, CarbStoreSyncDelegate, DoseSto | |
| case .success(let (status, date)): | ||
| self.updateReservoirVolume(status.reservoir, at: date, withTimeLeft: nil) | ||
| let battery = BatteryStatus(voltage: status.batteryVolts, status: BatteryIndicator(batteryStatus: status.batteryStatus)) | ||
|
|
||
| //Non MySentry Battery Status #141 | ||
| if let sentrySupported = self.pumpState?.pumpModel?.hasMySentry , !sentrySupported { | ||
| self.setBatteryStatusforNonMySentryPumps(currVoltage: status.batteryVolts) | ||
| } | ||
|
|
||
| nsPumpStatus = NightscoutUploadKit.PumpStatus(clock: date, pumpID: status.pumpID, iob: nil, battery: battery, suspended: status.suspended, bolusing: status.bolusing, reservoir: status.reservoir) | ||
| case .failure(let error): | ||
| self.troubleshootPumpComms(using: device) | ||
|
|
@@ -364,6 +370,38 @@ final class DeviceDataManager: CarbStoreDelegate, CarbStoreSyncDelegate, DoseSto | |
| } | ||
| } | ||
|
|
||
| /// NonMySentry Battery Calculation for Alkaline and Lithuim #141 | ||
| /// | ||
| /// - parameter currVoltage: Current Voltage Reading from Pump | ||
| /// | ||
| public var x22BatteryPercentRemaining : Double = -1 | ||
|
|
||
| private func setBatteryStatusforNonMySentryPumps(currVoltage : Double){ | ||
| var minVoltage : Double | ||
| var maxVoltage : Double | ||
| var batteryNotification : Double | ||
|
|
||
| // if Lithium set min and max linear voltages | ||
| if (self.batteryChemistry == .lithium){ | ||
| minVoltage = 1.32 | ||
| maxVoltage = 1.58 | ||
| batteryNotification = 0.12 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point at which we display 0% battery should be equal the point at which a notification should be made, no?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intent of this Notification was to "warn" a user of an impending battery failure or intermittent communications failures due to low battery. This is a 8 to 12 hour warning prior to battery = 0. |
||
| }else{ | ||
| // if Alkaline (default) set min and max linear voltages | ||
| minVoltage = 1.26 | ||
| maxVoltage = 1.58 | ||
| batteryNotification = 0.19 | ||
| } | ||
|
|
||
| // Linear EQ ((currVoltage - minVoltage)/(maxVoltage - minVoltage)) | ||
| self.x22BatteryPercentRemaining = ((currVoltage - minVoltage)/(maxVoltage - minVoltage)) | ||
|
|
||
| // Notify if <= batteryNotification setpoint | ||
| if self.x22BatteryPercentRemaining <= (batteryNotification){ | ||
| NotificationManager.sendPumpBatteryLowNotification() | ||
| } | ||
| } | ||
|
|
||
| /// Send a bolus command and handle the result | ||
| /// | ||
| /// - parameter units: The number of units to deliver | ||
|
|
@@ -698,6 +736,13 @@ final class DeviceDataManager: CarbStoreDelegate, CarbStoreSyncDelegate, DoseSto | |
| UserDefaults.standard.preferredInsulinDataSource = preferredInsulinDataSource | ||
| } | ||
| } | ||
|
|
||
| /// The Default battery chemistry is Alkaline | ||
| var batteryChemistry = UserDefaults.standard.batteryChemistry ?? .alkaline { | ||
| didSet { | ||
| UserDefaults.standard.batteryChemistry = batteryChemistry | ||
| } | ||
| } | ||
|
|
||
| // MARK: G5 Transmitter | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // | ||
| // BatteryChemistryType.swift | ||
| // Loop | ||
| // | ||
| // Created by Jerermy Lucas on 11/15/16 pattern derived from Nathan Racklyeft. | ||
| // Copyright © 2016 LoopKit Authors. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| enum BatteryChemistryType: Int, CustomStringConvertible { | ||
| case alkaline = 0 | ||
| case lithium | ||
|
|
||
| var description: String { | ||
| switch self { | ||
| case .alkaline: | ||
| return NSLocalizedString("Alkaline", comment: "Describing the battery chemistry as Alkaline") | ||
| case .lithium: | ||
| return NSLocalizedString("Lithium", comment: "Describing the battery chemistry as Lithium") | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // | ||
| // BatteryTypeSelectionTableViewController.swift | ||
| // Loop | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was it not possible to re-use RadioSelectionTableViewController?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am sure it may be. I am just not swift enough to fully comprehend how. So I leveraged the existing pattern Nate had provided. |
||
| // | ||
| // Created by Jerermy Lucas on 11/15/16 pattern derived from Nathan Racklyeft. | ||
| // Copyright © 2016 LoopKit Authors. All rights reserved. | ||
| // | ||
|
|
||
| import UIKit | ||
|
|
||
|
|
||
| protocol BatteryTypeSelectionTableViewControllerDelegate: class { | ||
| func batteryTypeSelectionTableViewControllerDidChangeSelectedIndex(_ controller: BatteryTypeSelectionTableViewController) | ||
| } | ||
|
|
||
|
|
||
| class BatteryTypeSelectionTableViewController: UITableViewController, IdentifiableClass { | ||
|
|
||
| var options = [String]() | ||
|
|
||
| var selectedIndex: Int? { | ||
| didSet { | ||
| if let oldValue = oldValue, oldValue != selectedIndex { | ||
| tableView.cellForRow(at: IndexPath(row: oldValue, section: 0))?.accessoryType = .none | ||
| } | ||
|
|
||
| if let selectedIndex = selectedIndex, oldValue != selectedIndex { | ||
| tableView.cellForRow(at: IndexPath(row: selectedIndex, section: 0))?.accessoryType = .checkmark | ||
|
|
||
| delegate?.batteryTypeSelectionTableViewControllerDidChangeSelectedIndex(self) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var contextHelp: String? | ||
|
|
||
| weak var delegate: BatteryTypeSelectionTableViewControllerDelegate? | ||
|
|
||
| convenience init() { | ||
| self.init(style: .grouped) | ||
| } | ||
|
|
||
| // MARK: - Table view data source | ||
|
|
||
| override func numberOfSections(in tableView: UITableView) -> Int { | ||
| return 1 | ||
| } | ||
|
|
||
| override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
| return options.count | ||
| } | ||
|
|
||
| override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
| let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell") | ||
|
|
||
| cell.textLabel?.text = options[indexPath.row] | ||
| cell.accessoryType = selectedIndex == indexPath.row ? .checkmark : .none | ||
|
|
||
| return cell | ||
| } | ||
|
|
||
| override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { | ||
| return contextHelp | ||
| } | ||
|
|
||
| override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
| selectedIndex = indexPath.row | ||
|
|
||
| tableView.deselectRow(at: indexPath, animated: true) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| extension BatteryTypeSelectionTableViewController { | ||
| typealias T = BatteryTypeSelectionTableViewController | ||
|
|
||
| static func insulinDataSource(_ value: BatteryChemistryType) -> T { | ||
| let vc = T() | ||
|
|
||
| vc.selectedIndex = value.rawValue | ||
| vc.options = (0..<2).flatMap({ BatteryChemistryType(rawValue: $0) }).map { String(describing: $0) } | ||
| vc.contextHelp = NSLocalizedString("Alkaline and Lithium batteries decay at differing rates. Alkaline tend to have a linear voltage drop over time whereas lithium cell batteries tend to maintain voltage until the end of their lifespan. Under normal usage in a Non-MySentry compatible Minimed (x22/x15) insulin pump running Loop, Alkaline batteries last approximately 4 to 5 days. Lithium batteries last between 7 and 8 days. This selection will use different battery voltage decay rates for each of the battery chemistry types and alert the user when a battery is approximately 8 to 10 hours from failure.", comment: "Instructions on selecting battery chemistry type") | ||
|
|
||
| return vc | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we already have a battery % for x23 devices, let's track battery % as a single ivar.
Updating that ivar can trigger both battery change event (
AnalyticsManager.sharedManager.pumpBatteryWasReplaced()) and notification (NotificationManager.sendPumpBatteryLowNotification()), and then the StatusTableViewController can check one place for battery percent, instead of checking bothdataManager.latestPumpStatusFromMySentry.batteryRemainingPercentanddataManager.x22BatteryPercentRemainingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use magic number sentinels; use an optional.
Since percentage is derived from chemistry, and chemistry can be changed at any time, this shouldn't be stored at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand it you are saying I should write the Battery % Remaining to dataManager.latestPumpStatusFromMySentry.batteryRemainingPercent. I totally agree reuse of the already existing structure would be great, however I am unsure how to write to the batteryRemainingPercent var.
latestPumpStatusFromMySentry?.batteryRemainingPercent = ((currVoltage - minVoltage)/(maxVoltage - minVoltage))I receive an error
Cannot assign to property: 'batteryRemainingPercent' is a 'let' constant