HistoryPage+PumpOpsSession.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // HistoryPage+PumpOpsSession.swift
  3. // RileyLinkKit
  4. //
  5. // Copyright © 2017 Pete Schwamb. All rights reserved.
  6. //
  7. extension HistoryPage {
  8. /// Returns TimestampedHistoryEvents from this page occuring after a given date
  9. ///
  10. /// - Parameters:
  11. /// - start: The date to filter events occuring on or after
  12. /// - timeZone: The current time zone offset of the pump
  13. /// - model: The pump model
  14. /// - Returns: A tuple containing:
  15. /// - events: The matching events
  16. /// - hasMoreEvents: Whether the next page likely contains events after the specified start date
  17. /// - cancelledEarly:
  18. func timestampedEvents(after start: Date, timeZone: TimeZone, model: PumpModel) -> (events: [TimestampedHistoryEvent], hasMoreEvents: Bool, cancelledEarly: Bool) {
  19. // Start with some time in the future, to account for the condition when the pump's clock is ahead
  20. // of ours by a small amount.
  21. var timeCursor = Date(timeIntervalSinceNow: TimeInterval(minutes: 60))
  22. var events = [TimestampedHistoryEvent]()
  23. var timeAdjustmentInterval: TimeInterval = 0
  24. var seenEventData = Set<Data>()
  25. var lastEvent: PumpEvent?
  26. for event in self.events.reversed() {
  27. if let event = event as? TimestampedPumpEvent, !seenEventData.contains(event.rawData) {
  28. seenEventData.insert(event.rawData)
  29. var timestamp = event.timestamp
  30. timestamp.timeZone = timeZone
  31. if let changeTimeEvent = event as? ChangeTimePumpEvent, let newTimeEvent = lastEvent as? NewTimePumpEvent {
  32. timeAdjustmentInterval += (newTimeEvent.timestamp.date?.timeIntervalSince(changeTimeEvent.timestamp.date!))!
  33. }
  34. if let alarm = event as? PumpAlarmPumpEvent, alarm.alarmType.indicatesUnrecoverableClockIssue {
  35. NSLog("Found device reset battery issue in history (%@). Ending history fetch.", String(describing: event))
  36. return (events: events, hasMoreEvents: false, cancelledEarly: true)
  37. }
  38. if let date = timestamp.date?.addingTimeInterval(timeAdjustmentInterval) {
  39. let shouldCheckDateForCompletion = !event.isDelayedAppend(with: model)
  40. if shouldCheckDateForCompletion {
  41. if date <= start {
  42. // Success, we have all the events we need
  43. //NSLog("Found event at or before startDate(%@)", date as NSDate, String(describing: eventTimestampDeltaAllowance), startDate as NSDate)
  44. return (events: events, hasMoreEvents: false, cancelledEarly: false)
  45. } else if date.timeIntervalSince(timeCursor) > TimeInterval(minutes: 60) {
  46. // Appears that pump lost time; we can't build up a valid timeline from this point back.
  47. // TODO: Convert logging
  48. NSLog("Found event (%@) out of order in history. Ending history fetch.", date as NSDate)
  49. return (events: events, hasMoreEvents: false, cancelledEarly: true)
  50. }
  51. timeCursor = date
  52. }
  53. events.insert(TimestampedHistoryEvent(pumpEvent: event, date: date), at: 0)
  54. }
  55. }
  56. lastEvent = event
  57. }
  58. return (events: events, hasMoreEvents: true, cancelledEarly: false)
  59. }
  60. }
  61. extension PumpAlarmType {
  62. var indicatesUnrecoverableClockIssue: Bool {
  63. switch self {
  64. case .deviceResetBatteryIssue17, .deviceResetBatteryIssue21:
  65. return true
  66. default:
  67. return false
  68. }
  69. }
  70. }