Newer Older
1262 lines | 45.942kb
Bogdan Timofte authored 2 months ago
1
//
2
//  File.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 03/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8
//MARK: Store and documentation: https://www.aliexpress.com/item/32968303350.html
9
//MARK: Protocol: https://sigrok.org/wiki/RDTech_UM_series
10
//MARK: Pithon Code: https://github.com/rfinnie/rdserialtool
11
//MARK: HM-10 Code: https://github.com/hoiberg/HM10-BluetoothSerial-iOS
12
//MARK: Package dependency https://github.com/krzyzanowskim/CryptoSwift
13

            
14
import CoreBluetooth
15
import SwiftUI
16

            
17
/**
18
 Supprted USB Meters
19
 # UM25C
20
 # TC66
21
 * Reverse Engineering
22
 [UM Series](https://sigrok.org/wiki/RDTech_UM_series)
23
 [TC66C](https://sigrok.org/wiki/RDTech_TC66C)
24
 */
Bogdan Timofte authored a month ago
25
enum Model: CaseIterable, Hashable {
Bogdan Timofte authored 2 months ago
26
    case UM25C
27
    case UM34C
28
    case TC66C
29
}
30

            
Bogdan Timofte authored 2 months ago
31
enum TemperatureUnitPreference: String, CaseIterable, Identifiable {
32
    case celsius
33
    case fahrenheit
34

            
35
    var id: String { rawValue }
36

            
37
    var title: String {
38
        switch self {
39
        case .celsius:
40
            return "Celsius"
41
        case .fahrenheit:
42
            return "Fahrenheit"
43
        }
44
    }
45

            
46
    var symbol: String {
47
        switch self {
48
        case .celsius:
49
            return "℃"
50
        case .fahrenheit:
51
            return "℉"
52
        }
53
    }
54
}
55

            
Bogdan Timofte authored 2 months ago
56
private extension TemperatureUnitPreference {
57
    var localeTitle: String {
58
        switch self {
59
        case .celsius:
60
            return "System (Celsius)"
61
        case .fahrenheit:
62
            return "System (Fahrenheit)"
63
        }
64
    }
65
}
66

            
Bogdan Timofte authored 2 months ago
67
enum ChargeRecordState {
68
    case waitingForStart
69
    case active
70
    case completed
71
}
72

            
Bogdan Timofte authored 2 months ago
73
class Meter : NSObject, ObservableObject, Identifiable {
Bogdan Timofte authored a month ago
74
    private struct ChargeRecordRestoreSignature: Equatable {
75
        let sessionID: UUID
76
        let sampleCount: Int
77
        let lastSampleTimestamp: Date?
78
    }
79

            
Bogdan Timofte authored 2 months ago
80

            
Bogdan Timofte authored 2 months ago
81
    private static func shouldLogOperationalStateTransition(from oldValue: OperationalState, to newValue: OperationalState) -> Bool {
82
        switch (oldValue, newValue) {
83
        case (.comunicating, .dataIsAvailable), (.dataIsAvailable, .comunicating):
84
            return false
85
        default:
86
            return true
87
        }
88
    }
89

            
Bogdan Timofte authored 2 months ago
90
    enum OperationalState: Int, Comparable {
91
        case notPresent
92
        case peripheralNotConnected
93
        case peripheralConnectionPending
94
        case peripheralConnected
95
        case peripheralReady
96
        case comunicating
97
        case dataIsAvailable
98

            
99
        static func < (lhs: OperationalState, rhs: OperationalState) -> Bool {
100
            return lhs.rawValue < rhs.rawValue
101
        }
102
    }
103

            
104
    @Published var operationalState = OperationalState.peripheralNotConnected {
105
        didSet {
Bogdan Timofte authored 2 months ago
106
            guard operationalState != oldValue else { return }
Bogdan Timofte authored 2 months ago
107
            if Self.shouldLogOperationalStateTransition(from: oldValue, to: operationalState) {
108
                track("\(name) - Operational state changed from \(oldValue) to \(operationalState)")
109
            }
Bogdan Timofte authored 2 months ago
110
            switch operationalState {
111
            case .notPresent:
Bogdan Timofte authored 2 months ago
112
                cancelPendingDataDumpRequest(reason: "meter missing")
Bogdan Timofte authored 2 months ago
113
                break
114
            case .peripheralNotConnected:
Bogdan Timofte authored 2 months ago
115
                cancelPendingDataDumpRequest(reason: "peripheral disconnected")
Bogdan Timofte authored 2 months ago
116
                handleMeasurementDiscontinuity(at: Date())
Bogdan Timofte authored 2 months ago
117
                if !commandQueue.isEmpty {
118
                    track("\(name) - Clearing \(commandQueue.count) queued commands after disconnect")
119
                    commandQueue.removeAll()
120
                }
Bogdan Timofte authored 2 months ago
121
                if enableAutoConnect {
122
                    track("\(name) - Reconnecting...")
123
                    btSerial.connect()
124
                }
125
            case .peripheralConnectionPending:
Bogdan Timofte authored 2 months ago
126
                cancelPendingDataDumpRequest(reason: "connection pending")
Bogdan Timofte authored 2 months ago
127
                break
128
            case .peripheralConnected:
Bogdan Timofte authored 2 months ago
129
                cancelPendingDataDumpRequest(reason: "services not ready yet")
Bogdan Timofte authored 2 months ago
130
                break
131
            case .peripheralReady:
Bogdan Timofte authored 2 months ago
132
                scheduleDataDumpRequest(after: 0.5, reason: "peripheral ready")
Bogdan Timofte authored 2 months ago
133
            case .comunicating:
134
                break
135
            case .dataIsAvailable:
136
                break
137
            }
138
        }
139
    }
140

            
141
    static func operationalColor(for state: OperationalState) -> Color {
142
        switch state {
143
        case .notPresent:
144
            return .red
145
        case .peripheralNotConnected:
146
            return .blue
147
        case .peripheralConnectionPending:
148
            return .yellow
149
        case .peripheralConnected:
150
            return .yellow
151
        case .peripheralReady:
152
            return .orange
153
        case .comunicating:
154
            return .orange
155
        case .dataIsAvailable:
156
            return .green
157
        }
158
    }
159

            
160
    private var wdTimer: Timer?
161

            
Bogdan Timofte authored 2 months ago
162
    @Published var lastSeen: Date? {
Bogdan Timofte authored 2 months ago
163
        didSet {
164
            wdTimer?.invalidate()
Bogdan Timofte authored 2 months ago
165
            guard lastSeen != nil else { return }
166
            appData.noteMeterSeen(at: lastSeen!, macAddress: btSerial.macAddress.description)
Bogdan Timofte authored 2 months ago
167
            if operationalState == .peripheralNotConnected {
168
                wdTimer = Timer.scheduledTimer(withTimeInterval: 20, repeats: false, block: {_ in
169
                    track("\(self.name) - Lost advertisments...")
170
                    self.operationalState = .notPresent
171
                })
172
            } else if operationalState == .notPresent {
173
               operationalState = .peripheralNotConnected
174
            }
175
        }
176
    }
177

            
178
    var uuid: UUID
179
    var model: Model
180
    var modelString: String
181

            
Bogdan Timofte authored 2 months ago
182
    private var isSyncingNameFromStore = false
183

            
184
    @Published var name: String {
Bogdan Timofte authored 2 months ago
185
        didSet {
Bogdan Timofte authored 2 months ago
186
            guard !isSyncingNameFromStore else { return }
187
            guard oldValue != name else { return }
188
            appData.setMeterName(name, for: btSerial.macAddress.description)
Bogdan Timofte authored 2 months ago
189
        }
190
    }
Bogdan Timofte authored 2 months ago
191

            
Bogdan Timofte authored 2 months ago
192
    var preferredTabIdentifier: String = "home"
193

            
Bogdan Timofte authored 2 months ago
194
    @Published private(set) var lastConnectedAt: Date?
Bogdan Timofte authored 2 months ago
195

            
196
    var color : Color {
197
        get {
Bogdan Timofte authored 2 months ago
198
            return model.color
Bogdan Timofte authored 2 months ago
199
        }
200
    }
201

            
Bogdan Timofte authored 2 months ago
202
    var capabilities: MeterCapabilities {
203
        model.capabilities
204
    }
205

            
Bogdan Timofte authored 2 months ago
206
    var availableDataGroupIDs: [UInt8] {
Bogdan Timofte authored 2 months ago
207
        capabilities.availableDataGroupIDs
Bogdan Timofte authored 2 months ago
208
    }
209

            
210
    var supportsDataGroupCommands: Bool {
Bogdan Timofte authored 2 months ago
211
        capabilities.supportsDataGroupCommands
Bogdan Timofte authored 2 months ago
212
    }
213

            
Bogdan Timofte authored 2 months ago
214
    var supportsRecordingView: Bool {
215
        capabilities.supportsRecordingView
216
    }
217

            
Bogdan Timofte authored 2 months ago
218
    var supportsUMSettings: Bool {
Bogdan Timofte authored 2 months ago
219
        capabilities.supportsScreenSettings
Bogdan Timofte authored 2 months ago
220
    }
221

            
222
    var supportsRecordingThreshold: Bool {
Bogdan Timofte authored 2 months ago
223
        capabilities.supportsRecordingThreshold
Bogdan Timofte authored 2 months ago
224
    }
225

            
Bogdan Timofte authored 2 months ago
226
    var reportsCurrentScreenIndex: Bool {
227
        capabilities.reportsCurrentScreenIndex
228
    }
229

            
230
    var showsDataGroupEnergy: Bool {
231
        capabilities.showsDataGroupEnergy
232
    }
233

            
234
    var highlightsActiveDataGroup: Bool {
235
        if model == .TC66C {
236
            return hasObservedActiveDataGroup
237
        }
238
        return capabilities.highlightsActiveDataGroup
239
    }
240

            
Bogdan Timofte authored 2 months ago
241
    var supportsFahrenheit: Bool {
Bogdan Timofte authored 2 months ago
242
        capabilities.supportsFahrenheit
Bogdan Timofte authored 2 months ago
243
    }
244

            
Bogdan Timofte authored 2 months ago
245
    var supportsManualTemperatureUnitSelection: Bool {
246
        model == .TC66C
247
    }
248

            
Bogdan Timofte authored 2 months ago
249
    var supportsChargerDetection: Bool {
Bogdan Timofte authored 2 months ago
250
        capabilities.supportsChargerDetection
251
    }
252

            
Bogdan Timofte authored 2 months ago
253
    var dataGroupsTitle: String {
254
        capabilities.dataGroupsTitle
255
    }
256

            
Bogdan Timofte authored 2 months ago
257
    var documentedWorkingVoltage: String {
258
        capabilities.documentedWorkingVoltage
259
    }
260

            
Bogdan Timofte authored 2 months ago
261
    var chargerTypeDescription: String {
262
        capabilities.chargerTypeDescription(for: chargerTypeIndex)
Bogdan Timofte authored 2 months ago
263
    }
264

            
Bogdan Timofte authored 2 months ago
265
    var temperatureUnitDescription: String {
266
        if supportsManualTemperatureUnitSelection {
Bogdan Timofte authored 2 months ago
267
            return "Device-defined"
Bogdan Timofte authored 2 months ago
268
        }
Bogdan Timofte authored 2 months ago
269
        return systemTemperatureUnitPreference.localeTitle
Bogdan Timofte authored 2 months ago
270
    }
271

            
272
    var primaryTemperatureDescription: String {
Bogdan Timofte authored 2 months ago
273
        let value = displayedTemperatureValue.format(decimalDigits: 0)
Bogdan Timofte authored 2 months ago
274
        if supportsManualTemperatureUnitSelection {
Bogdan Timofte authored 2 months ago
275
            return "\(value)°"
Bogdan Timofte authored 2 months ago
276
        }
Bogdan Timofte authored 2 months ago
277
        return "\(value)\(systemTemperatureUnitPreference.symbol)"
Bogdan Timofte authored 2 months ago
278
    }
279

            
280
    var secondaryTemperatureDescription: String? {
Bogdan Timofte authored 2 months ago
281
        nil
282
    }
283

            
284
    var displayedTemperatureValue: Double {
285
        if supportsManualTemperatureUnitSelection {
286
            return temperatureCelsius
287
        }
288
        switch systemTemperatureUnitPreference {
289
        case .celsius:
290
            return displayedTemperatureCelsius
291
        case .fahrenheit:
292
            return displayedTemperatureFahrenheit
293
        }
294
    }
295

            
296
    private var displayedTemperatureCelsius: Double {
297
        if supportsManualTemperatureUnitSelection {
298
            switch tc66TemperatureUnitPreference {
299
            case .celsius:
300
                return temperatureCelsius
301
            case .fahrenheit:
302
                return (temperatureCelsius - 32) * 5 / 9
303
            }
304
        }
305
        return temperatureCelsius
306
    }
307

            
308
    private var displayedTemperatureFahrenheit: Double {
309
        if supportsManualTemperatureUnitSelection {
310
            switch tc66TemperatureUnitPreference {
311
            case .celsius:
312
                return (temperatureCelsius * 9 / 5) + 32
313
            case .fahrenheit:
314
                return temperatureCelsius
315
            }
316
        }
317
        if supportsFahrenheit, temperatureFahrenheit.isFinite {
318
            return temperatureFahrenheit
319
        }
320
        return (temperatureCelsius * 9 / 5) + 32
321
    }
322

            
323
    private var systemTemperatureUnitPreference: TemperatureUnitPreference {
324
        let locale = Locale.autoupdatingCurrent
325
        if #available(iOS 16.0, *) {
326
            switch locale.measurementSystem {
327
            case .us:
328
                return .fahrenheit
329
            default:
330
                return .celsius
331
            }
332
        }
333

            
334
        let regionCode = locale.regionCode ?? ""
335
        let fahrenheitRegions: Set<String> = ["US", "BS", "BZ", "KY", "PW", "LR", "FM", "MH"]
336
        return fahrenheitRegions.contains(regionCode) ? .fahrenheit : .celsius
Bogdan Timofte authored 2 months ago
337
    }
338

            
Bogdan Timofte authored 2 months ago
339
    var currentScreenDescription: String {
Bogdan Timofte authored 2 months ago
340
        guard reportsCurrentScreenIndex else {
341
            return "Page Controls"
342
        }
Bogdan Timofte authored 2 months ago
343
        if let label = capabilities.screenDescription(for: currentScreen) {
344
            return "Screen \(currentScreen): \(label)"
345
        }
346
        return "Screen \(currentScreen)"
347
    }
348

            
Bogdan Timofte authored 2 months ago
349
    var deviceModelName: String {
Bogdan Timofte authored 2 months ago
350
        if !reportedModelName.isEmpty {
351
            return reportedModelName
352
        }
353
        return model.canonicalName
Bogdan Timofte authored 2 months ago
354
    }
355

            
Bogdan Timofte authored 2 months ago
356
    var deviceModelSummary: String {
Bogdan Timofte authored 2 months ago
357
        let baseName = deviceModelName
Bogdan Timofte authored 2 months ago
358
        if modelNumber != 0 {
359
            return "\(baseName) (\(modelNumber))"
360
        }
361
        return baseName
362
    }
363

            
Bogdan Timofte authored 2 months ago
364
    var recordingDurationDescription: String {
365
        let totalSeconds = Int(recordingDuration)
366
        let hours = totalSeconds / 3600
367
        let minutes = (totalSeconds % 3600) / 60
368
        let seconds = totalSeconds % 60
369

            
370
        if hours > 0 {
371
            return String(format: "%d:%02d:%02d", hours, minutes, seconds)
372
        }
373
        return String(format: "%02d:%02d", minutes, seconds)
374
    }
375

            
Bogdan Timofte authored 2 months ago
376
    var chargeRecordDurationDescription: String {
377
        let totalSeconds = Int(chargeRecordDuration)
378
        let hours = totalSeconds / 3600
379
        let minutes = (totalSeconds % 3600) / 60
380
        let seconds = totalSeconds % 60
381

            
382
        if hours > 0 {
383
            return String(format: "%d:%02d:%02d", hours, minutes, seconds)
384
        }
385
        return String(format: "%02d:%02d", minutes, seconds)
386
    }
387

            
388
    var chargeRecordTimeRange: ClosedRange<Date>? {
389
        guard let start = chargeRecordStartTimestamp else { return nil }
390
        let end = chargeRecordEndTimestamp ?? chargeRecordLastTimestamp
391
        guard let end else { return nil }
392
        return start...end
393
    }
394

            
395
    var chargeRecordStatusText: String {
396
        switch chargeRecordState {
397
        case .waitingForStart:
398
            return "Waiting"
399
        case .active:
400
            return "Active"
401
        case .completed:
402
            return "Completed"
403
        }
404
    }
405

            
406
    var chargeRecordStatusColor: Color {
407
        switch chargeRecordState {
408
        case .waitingForStart:
409
            return .secondary
410
        case .active:
411
            return .red
412
        case .completed:
413
            return .green
414
        }
415
    }
416

            
Bogdan Timofte authored 2 months ago
417
    var dataGroupsHint: String? {
Bogdan Timofte authored 2 months ago
418
        if model == .TC66C {
419
            if hasObservedActiveDataGroup {
420
                return "The active memory is inferred from the totals that are currently increasing."
421
            }
422
            return "The device exposes two read-only memories. The active memory is inferred once one total starts increasing."
423
        }
424
        return capabilities.dataGroupsHint
425
    }
426

            
427
    func dataGroupLabel(for id: UInt8) -> String {
428
        supportsDataGroupCommands ? "\(id)" : "M\(Int(id) + 1)"
Bogdan Timofte authored 2 months ago
429
    }
430

            
431
    var recordingThresholdHint: String? {
432
        capabilities.recordingThresholdHint
433
    }
434

            
Bogdan Timofte authored 2 months ago
435
    var btSerial: BluetoothSerial
Bogdan Timofte authored 2 months ago
436

            
Bogdan Timofte authored a month ago
437
    let measurements = Measurements()
438
    let chargeRecordMeasurements = Measurements()
Bogdan Timofte authored 2 months ago
439

            
Bogdan Timofte authored a month ago
440
    private let minimumLivePollingInterval: TimeInterval = 0.4
Bogdan Timofte authored 2 months ago
441
    private var commandQueue: [Data] = []
442
    private var dataDumpRequestTimestamp = Date()
Bogdan Timofte authored 2 months ago
443
    private var pendingDataDumpWorkItem: DispatchWorkItem?
Bogdan Timofte authored 2 months ago
444

            
445
    class DataGroupRecord {
Bogdan Timofte authored 2 months ago
446
        var ah: Double
447
        var wh: Double
Bogdan Timofte authored 2 months ago
448
        init(ah: Double, wh: Double) {
449
            self.ah = ah
450
            self.wh = wh
451
        }
452
    }
Bogdan Timofte authored 2 months ago
453
    private(set) var selectedDataGroup: UInt8 = 0
454
    private(set) var dataGroupRecords: [Int : DataGroupRecord] = [:]
455
    private(set) var chargeRecordAH: Double = 0
456
    private(set) var chargeRecordWH: Double = 0
457
    private(set) var chargeRecordDuration: TimeInterval = 0
Bogdan Timofte authored 2 months ago
458
    @Published var chargeRecordStopThreshold: Double = 0.05
459
    @Published var tc66TemperatureUnitPreference: TemperatureUnitPreference = .celsius {
460
        didSet {
461
            guard supportsManualTemperatureUnitSelection else { return }
462
            guard oldValue != tc66TemperatureUnitPreference else { return }
Bogdan Timofte authored 2 months ago
463
            appData.setTemperatureUnitPreference(tc66TemperatureUnitPreference, for: btSerial.macAddress.description)
Bogdan Timofte authored 2 months ago
464
        }
465
    }
Bogdan Timofte authored 2 months ago
466

            
467
    @Published var screenBrightness: Int = -1 {
468
        didSet {
469
            if oldValue != screenBrightness {
470
                screenBrightnessTimestamp = Date()
471
                if oldValue != -1 {
472
                    setSceeenBrightness(to: UInt8(screenBrightness))
473
                }
474
            }
475
        }
476
    }
477
    private var screenBrightnessTimestamp = Date()
478

            
479
    @Published var screenTimeout: Int = -1 {
480
        didSet {
481
            if oldValue != screenTimeout {
482
                screenTimeoutTimestamp = Date()
483
                if oldValue != -1 {
484
                    setScreenSaverTimeout(to: UInt8(screenTimeout))
485
                }
486
            }
487
        }
488
    }
489
    private var screenTimeoutTimestamp = Date()
490

            
Bogdan Timofte authored 2 months ago
491
    private(set) var voltage: Double = 0
492
    private(set) var current: Double = 0
493
    private(set) var power: Double = 0
494
    private(set) var temperatureCelsius: Double = 0
495
    private(set) var temperatureFahrenheit: Double = 0
496
    private(set) var usbPlusVoltage: Double = 0
497
    private(set) var usbMinusVoltage: Double = 0
498
    private(set) var recordedAH: Double = 0
499
    private(set) var recordedWH: Double = 0
500
    private(set) var recording: Bool = false
Bogdan Timofte authored 2 months ago
501
    @Published var recordingTreshold: Double = 0 {
Bogdan Timofte authored 2 months ago
502
        didSet {
Bogdan Timofte authored 2 months ago
503
            guard recordingTreshold != oldValue else { return }
504
            if isApplyingRecordingThresholdFromDevice {
505
                return
Bogdan Timofte authored 2 months ago
506
            }
Bogdan Timofte authored 2 months ago
507
            recordingThresholdTimestamp = Date()
508
            guard recordingThresholdLoadedFromDevice else { return }
509
            setrecordingTreshold(to: (recordingTreshold * 100).uInt8Value)
Bogdan Timofte authored 2 months ago
510
        }
Bogdan Timofte authored 2 months ago
511
    }
Bogdan Timofte authored 2 months ago
512
    private(set) var currentScreen: UInt16 = 0
513
    private(set) var recordingDuration: UInt32 = 0
514
    private(set) var loadResistance: Double = 0
515
    private(set) var modelNumber: UInt16 = 0
516
    private(set) var chargerTypeIndex: UInt16 = 0
517
    private(set) var reportedModelName: String = ""
518
    private(set) var firmwareVersion: String = ""
519
    private(set) var serialNumber: UInt32 = 0
520
    private(set) var bootCount: UInt32 = 0
Bogdan Timofte authored 2 months ago
521
    private var enableAutoConnect: Bool = false
Bogdan Timofte authored 2 months ago
522
    private var recordingThresholdTimestamp = Date()
523
    private var recordingThresholdLoadedFromDevice = false
524
    private var isApplyingRecordingThresholdFromDevice = false
Bogdan Timofte authored 2 months ago
525
    private(set) var chargeRecordState = ChargeRecordState.waitingForStart
Bogdan Timofte authored 2 months ago
526
    private var chargeRecordStartTimestamp: Date?
527
    private var chargeRecordEndTimestamp: Date?
528
    private var chargeRecordLastTimestamp: Date?
529
    private var chargeRecordLastCurrent: Double = 0
530
    private var chargeRecordLastPower: Double = 0
Bogdan Timofte authored 2 months ago
531
    private let volatileMemoryDecreaseEpsilon = 0.0005
532
    private let initiatedVolatileMemoryResetGraceWindow: TimeInterval = 12
533
    private var hasSeenUMSnapshot = false
Bogdan Timofte authored 2 months ago
534
    private var hasObservedActiveDataGroup = false
535
    private var hasSeenTC66Snapshot = false
Bogdan Timofte authored 2 months ago
536
    private var pendingVolatileMemoryResetIgnoreCount = 0
537
    private var pendingVolatileMemoryResetDeadline: Date?
Bogdan Timofte authored 2 months ago
538
    private var liveDataChanged = false
Bogdan Timofte authored a month ago
539
    private var restoredChargeSessionID: UUID?
Bogdan Timofte authored a month ago
540
    private var restoredChargeRecordSignature: ChargeRecordRestoreSignature?
Bogdan Timofte authored a month ago
541
    private var lastRecorderObservationAt: Date?
Bogdan Timofte authored 2 months ago
542

            
Bogdan Timofte authored 2 months ago
543
    @discardableResult
544
    private func setIfChanged<T: Equatable>(_ keyPath: ReferenceWritableKeyPath<Meter, T>, to value: T) -> Bool {
545
        guard self[keyPath: keyPath] != value else { return false }
546
        self[keyPath: keyPath] = value
547
        liveDataChanged = true
548
        return true
549
    }
550

            
551
    private func updateDataGroupRecord(index: Int, ah: Double, wh: Double) {
552
        if let existing = dataGroupRecords[index] {
553
            if existing.ah != ah { existing.ah = ah; liveDataChanged = true }
554
            if existing.wh != wh { existing.wh = wh; liveDataChanged = true }
555
        } else {
556
            dataGroupRecords[index] = DataGroupRecord(ah: ah, wh: wh)
557
            liveDataChanged = true
558
        }
559
    }
560

            
Bogdan Timofte authored 2 months ago
561
    init ( model: Model, with serialPort: BluetoothSerial ) {
562
        uuid = serialPort.peripheral.identifier
563
        //dataStore.meterUUIDS.append(serialPort.peripheral.identifier)
564
        modelString = serialPort.peripheral.name!
565
        self.model = model
566
        btSerial = serialPort
Bogdan Timofte authored 2 months ago
567
        name = appData.meterName(for: serialPort.macAddress.description) ?? serialPort.macAddress.description
Bogdan Timofte authored 2 months ago
568
        lastSeen = appData.lastSeen(for: serialPort.macAddress.description)
569
        lastConnectedAt = appData.lastConnected(for: serialPort.macAddress.description)
Bogdan Timofte authored 2 months ago
570
        super.init()
571
        btSerial.delegate = self
Bogdan Timofte authored 2 months ago
572
        reloadTemperatureUnitPreference()
Bogdan Timofte authored 2 months ago
573
        //name = dataStore.meterNames[macAddress.description] ?? peripheral.name!
574
        for index in stride(from: 0, through: 9, by: 1) {
575
            dataGroupRecords[index] = DataGroupRecord(ah:0, wh: 0)
576
        }
577
    }
Bogdan Timofte authored 2 months ago
578

            
579
    func reloadTemperatureUnitPreference() {
580
        guard supportsManualTemperatureUnitSelection else { return }
Bogdan Timofte authored 2 months ago
581
        let persistedPreference = appData.temperatureUnitPreference(for: btSerial.macAddress.description)
Bogdan Timofte authored 2 months ago
582
        if tc66TemperatureUnitPreference != persistedPreference {
583
            tc66TemperatureUnitPreference = persistedPreference
584
        }
585
    }
Bogdan Timofte authored 2 months ago
586

            
Bogdan Timofte authored 2 months ago
587
    func updateNameFromStore(_ newName: String) {
588
        guard newName != name else { return }
589
        isSyncingNameFromStore = true
590
        name = newName
591
        isSyncingNameFromStore = false
592
    }
593

            
Bogdan Timofte authored 2 months ago
594
    private func noteConnectionEstablished(at date: Date) {
595
        lastConnectedAt = date
596
        appData.noteMeterConnected(at: date, macAddress: btSerial.macAddress.description)
597
    }
598

            
Bogdan Timofte authored 2 months ago
599
    private func handleMeasurementDiscontinuity(at timestamp: Date) {
600
        measurements.markDiscontinuity(at: timestamp)
Bogdan Timofte authored a month ago
601
        chargeRecordMeasurements.markDiscontinuity(at: timestamp)
Bogdan Timofte authored 2 months ago
602

            
603
        guard chargeRecordState == .active else { return }
604
        chargeRecordLastTimestamp = nil
605
        chargeRecordLastCurrent = 0
606
        chargeRecordLastPower = 0
607
    }
608

            
Bogdan Timofte authored 2 months ago
609
    private func currentEnergySample() -> (groupID: UInt8, value: Double)? {
610
        guard showsDataGroupEnergy else { return nil }
611

            
612
        if model == .TC66C && !hasObservedActiveDataGroup {
613
            return nil
614
        }
615

            
616
        let groupID = selectedDataGroup
617
        guard let record = dataGroupRecords[Int(groupID)] else { return nil }
618
        return (groupID, record.wh)
619
    }
620

            
Bogdan Timofte authored a month ago
621
    private func currentChargeSample() -> (groupID: UInt8, value: Double)? {
622
        guard showsDataGroupEnergy else { return nil }
623

            
624
        if model == .TC66C && !hasObservedActiveDataGroup {
625
            return nil
626
        }
627

            
628
        let groupID = selectedDataGroup
629
        guard let record = dataGroupRecords[Int(groupID)] else { return nil }
630
        return (groupID, record.ah)
631
    }
632

            
633
    func chargingMonitorSnapshot(at observedAt: Date) -> ChargingMonitorSnapshot? {
Bogdan Timofte authored a month ago
634
        let usesNativeRecordingCounters = supportsRecordingView
635
        let nativeChargeCounter = usesNativeRecordingCounters ? recordedAH : nil
636
        let nativeEnergyCounter = usesNativeRecordingCounters ? recordedWH : nil
637

            
638
        return ChargingMonitorSnapshot(
Bogdan Timofte authored a month ago
639
            meterMACAddress: btSerial.macAddress.description,
640
            meterName: name,
641
            meterModel: deviceModelSummary,
642
            observedAt: observedAt,
643
            voltageVolts: voltage,
644
            currentAmps: current,
645
            powerWatts: power,
Bogdan Timofte authored a month ago
646
            selectedDataGroup: usesNativeRecordingCounters ? nil : (currentEnergySample()?.groupID ?? currentChargeSample()?.groupID),
647
            meterChargeCounterAh: nativeChargeCounter ?? currentChargeSample()?.value,
648
            meterEnergyCounterWh: nativeEnergyCounter ?? currentEnergySample()?.value,
Bogdan Timofte authored a month ago
649
            meterRecordingDurationSeconds: usesNativeRecordingCounters ? TimeInterval(recordingDuration) : nil,
Bogdan Timofte authored a month ago
650
            fallbackStopThresholdAmps: supportsRecordingThreshold ? recordingTreshold : chargeRecordStopThreshold
Bogdan Timofte authored a month ago
651
        )
652
    }
653

            
Bogdan Timofte authored a month ago
654
    var recordingBootedAt: Date? {
655
        guard supportsRecordingView else { return nil }
656
        guard let lastRecorderObservationAt else { return nil }
657
        return lastRecorderObservationAt.addingTimeInterval(-TimeInterval(recordingDuration))
658
    }
659

            
Bogdan Timofte authored a month ago
660
    var chargingMonitorSnapshot: ChargingMonitorSnapshot? {
661
        chargingMonitorSnapshot(at: Date())
662
    }
663

            
Bogdan Timofte authored 2 months ago
664
    private func cancelPendingDataDumpRequest(reason: String) {
665
        guard let pendingDataDumpWorkItem else { return }
666
        track("\(name) - Cancel scheduled data request (\(reason))")
667
        pendingDataDumpWorkItem.cancel()
668
        self.pendingDataDumpWorkItem = nil
669
    }
670

            
671
    private func scheduleDataDumpRequest(after delay: TimeInterval, reason: String) {
672
        cancelPendingDataDumpRequest(reason: "reschedule")
673

            
674
        let workItem = DispatchWorkItem { [weak self] in
675
            guard let self else { return }
676
            self.pendingDataDumpWorkItem = nil
677
            self.dataDumpRequest()
678
        }
679
        pendingDataDumpWorkItem = workItem
680
        track("\(name) - Schedule data request in \(String(format: "%.2f", delay))s (\(reason))")
681
        DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: workItem)
682
    }
Bogdan Timofte authored 2 months ago
683

            
Bogdan Timofte authored a month ago
684
    private func scheduleNextLiveDataDumpRequest() {
685
        let elapsedSinceLastRequest = Date().timeIntervalSince(dataDumpRequestTimestamp)
686
        let delay = max(minimumLivePollingInterval - elapsedSinceLastRequest, 0)
687
        scheduleDataDumpRequest(after: delay, reason: "continuous live polling")
688
    }
689

            
Bogdan Timofte authored 2 months ago
690
    private func noteInitiatedVolatileMemoryResetIfNeeded(for groupID: UInt8) {
691
        guard groupID == 0 else { return }
692
        pendingVolatileMemoryResetIgnoreCount += 1
693
        pendingVolatileMemoryResetDeadline = Date().addingTimeInterval(initiatedVolatileMemoryResetGraceWindow)
694
        track("\(name) - Will ignore the next volatile memory drop caused by an app reset request.")
695
    }
696

            
697
    private func pendingInitiatedVolatileMemoryResetIsActive(at timestamp: Date) -> Bool {
698
        guard let pendingVolatileMemoryResetDeadline else { return false }
699
        guard pendingVolatileMemoryResetIgnoreCount > 0 else {
700
            self.pendingVolatileMemoryResetDeadline = nil
701
            return false
702
        }
703
        guard timestamp <= pendingVolatileMemoryResetDeadline else {
704
            track("\(name) - Expiring stale volatile memory reset ignore state.")
705
            pendingVolatileMemoryResetIgnoreCount = 0
706
            self.pendingVolatileMemoryResetDeadline = nil
707
            return false
708
        }
709
        return true
710
    }
711

            
712
    private func shouldIgnoreVolatileMemoryDrop(at timestamp: Date) -> Bool {
713
        guard pendingInitiatedVolatileMemoryResetIsActive(at: timestamp) else { return false }
714
        pendingVolatileMemoryResetIgnoreCount -= 1
715
        if pendingVolatileMemoryResetIgnoreCount == 0 {
716
            pendingVolatileMemoryResetDeadline = nil
717
        }
718
        track("\(name) - Ignoring volatile memory drop after an app-initiated reset.")
719
        return true
720
    }
721

            
722
    private func didUMVolatileMemoryDecrease(in snapshot: UMSnapshot) -> Bool {
723
        guard hasSeenUMSnapshot else { return false }
724
        guard let previousRecord = dataGroupRecords[0], let nextRecord = snapshot.dataGroupRecords[0] else {
725
            return false
726
        }
727

            
728
        return nextRecord.ah < (previousRecord.ah - volatileMemoryDecreaseEpsilon)
729
            || nextRecord.wh < (previousRecord.wh - volatileMemoryDecreaseEpsilon)
730
    }
731

            
732
    private func didUMDeviceReboot(with snapshot: UMSnapshot, at timestamp: Date) -> Bool {
733
        defer { hasSeenUMSnapshot = true }
734

            
735
        guard didUMVolatileMemoryDecrease(in: snapshot) else { return false }
736
        guard !shouldIgnoreVolatileMemoryDrop(at: timestamp) else { return false }
737

            
738
        track("\(name) - Inferred UM reboot because volatile memory dropped.")
739
        return true
740
    }
741

            
742
    private func didTC66DeviceReboot(with snapshot: TC66Snapshot) -> Bool {
743
        guard hasSeenTC66Snapshot else { return false }
744
        guard snapshot.bootCount != bootCount else { return false }
745

            
746
        track("\(name) - Inferred TC66 reboot because bootCount changed from \(bootCount) to \(snapshot.bootCount).")
747
        return true
748
    }
749

            
750
    private func updateChargerTypeLatch(observedIndex: UInt16, didDetectDeviceReset: Bool) {
751
        if didDetectDeviceReset, chargerTypeIndex != 0 {
Bogdan Timofte authored 2 months ago
752
            setIfChanged(\.chargerTypeIndex, to: 0)
Bogdan Timofte authored 2 months ago
753
        }
754

            
755
        guard supportsChargerDetection else { return }
756

            
757
        if chargerTypeIndex == 0 {
Bogdan Timofte authored 2 months ago
758
            setIfChanged(\.chargerTypeIndex, to: observedIndex)
Bogdan Timofte authored 2 months ago
759
            return
760
        }
761

            
762
        guard observedIndex != 0, observedIndex != chargerTypeIndex else { return }
763
        track("\(name) - Ignoring charger type change from \(chargerTypeIndex) to \(observedIndex) until the device reboots.")
764
    }
Bogdan Timofte authored 2 months ago
765

            
766
    func dataDumpRequest() {
Bogdan Timofte authored 2 months ago
767
        guard operationalState >= .peripheralReady else {
768
            track("\(name) - Skip data request while state is \(operationalState)")
769
            return
770
        }
Bogdan Timofte authored 2 months ago
771
        if commandQueue.isEmpty {
772
            switch model {
773
            case .UM25C:
Bogdan Timofte authored 2 months ago
774
                btSerial.write(UMProtocol.snapshotRequest, expectedResponseLength: 130)
Bogdan Timofte authored 2 months ago
775
            case .UM34C:
Bogdan Timofte authored 2 months ago
776
                btSerial.write(UMProtocol.snapshotRequest, expectedResponseLength: 130)
Bogdan Timofte authored 2 months ago
777
            case .TC66C:
Bogdan Timofte authored 2 months ago
778
                btSerial.write(TC66Protocol.snapshotRequest, expectedResponseLength: 192)
Bogdan Timofte authored 2 months ago
779
            }
780
            dataDumpRequestTimestamp = Date()
781
        } else {
782
            track("Request delayed for: \(commandQueue.first!.hexEncodedStringValue)")
783
            btSerial.write( commandQueue.first! )
784
            commandQueue.removeFirst()
Bogdan Timofte authored 2 months ago
785
            scheduleDataDumpRequest(after: 1, reason: "queued command")
Bogdan Timofte authored 2 months ago
786
        }
787
    }
788

            
789
    /**
790
     received data parser
791
     - parameter buffer cotains response for data dump request
792
     - Decription metod for TC66C AES ECB response  found [here](https:github.com/krzyzanowskim/CryptoSwift/issues/693)
793
     */
794
    func parseData ( from buffer: Data) {
Bogdan Timofte authored 2 months ago
795
        liveDataChanged = false
Bogdan Timofte authored 2 months ago
796
        switch model {
797
        case .UM25C:
Bogdan Timofte authored 2 months ago
798
            do {
799
                apply(umSnapshot: try UMProtocol.parseSnapshot(from: buffer, model: model))
800
            } catch {
801
                track("\(name) - Error: \(error)")
802
            }
Bogdan Timofte authored 2 months ago
803
        case .UM34C:
Bogdan Timofte authored 2 months ago
804
            do {
805
                apply(umSnapshot: try UMProtocol.parseSnapshot(from: buffer, model: model))
806
            } catch {
807
                track("\(name) - Error: \(error)")
808
            }
Bogdan Timofte authored 2 months ago
809
        case .TC66C:
Bogdan Timofte authored 2 months ago
810
            do {
811
                apply(tc66Snapshot: try TC66Protocol.parseSnapshot(from: buffer))
812
            } catch {
813
                track("\(name) - Error: \(error)")
814
            }
Bogdan Timofte authored 2 months ago
815
        }
Bogdan Timofte authored 2 months ago
816
        updateChargeRecord(at: dataDumpRequestTimestamp)
Bogdan Timofte authored a month ago
817
        captureLiveMeasurements(at: dataDumpRequestTimestamp, in: measurements)
818
        if chargeRecordState != .waitingForStart {
Bogdan Timofte authored a month ago
819
            captureLiveMeasurements(
820
                at: dataDumpRequestTimestamp,
821
                in: chargeRecordMeasurements,
822
                includesTemperature: false
823
            )
Bogdan Timofte authored 2 months ago
824
        }
Bogdan Timofte authored a month ago
825
        appData.observeChargeSnapshot(from: self, observedAt: dataDumpRequestTimestamp)
Bogdan Timofte authored 2 months ago
826
        if operationalState != .dataIsAvailable {
827
            operationalState = .dataIsAvailable
828
        } else if liveDataChanged {
829
            objectWillChange.send()
830
        }
Bogdan Timofte authored a month ago
831
        scheduleNextLiveDataDumpRequest()
Bogdan Timofte authored 2 months ago
832
    }
833

            
Bogdan Timofte authored 2 months ago
834
    private func apply(umSnapshot snapshot: UMSnapshot) {
Bogdan Timofte authored 2 months ago
835
        let didDetectDeviceReset = didUMDeviceReboot(with: snapshot, at: dataDumpRequestTimestamp)
Bogdan Timofte authored a month ago
836
        lastRecorderObservationAt = dataDumpRequestTimestamp
Bogdan Timofte authored 2 months ago
837
        setIfChanged(\.modelNumber, to: snapshot.modelNumber)
838
        setIfChanged(\.voltage, to: snapshot.voltage)
839
        setIfChanged(\.current, to: snapshot.current)
840
        setIfChanged(\.power, to: snapshot.power)
841
        setIfChanged(\.temperatureCelsius, to: snapshot.temperatureCelsius)
842
        setIfChanged(\.temperatureFahrenheit, to: snapshot.temperatureFahrenheit)
843
        setIfChanged(\.selectedDataGroup, to: snapshot.selectedDataGroup)
Bogdan Timofte authored 2 months ago
844
        for (index, record) in snapshot.dataGroupRecords {
Bogdan Timofte authored 2 months ago
845
            updateDataGroupRecord(index: index, ah: record.ah, wh: record.wh)
Bogdan Timofte authored 2 months ago
846
        }
Bogdan Timofte authored 2 months ago
847
        setIfChanged(\.usbPlusVoltage, to: snapshot.usbPlusVoltage)
848
        setIfChanged(\.usbMinusVoltage, to: snapshot.usbMinusVoltage)
Bogdan Timofte authored 2 months ago
849
        updateChargerTypeLatch(observedIndex: snapshot.chargerTypeIndex, didDetectDeviceReset: didDetectDeviceReset)
Bogdan Timofte authored 2 months ago
850
        setIfChanged(\.recordedAH, to: snapshot.recordedAH)
851
        setIfChanged(\.recordedWH, to: snapshot.recordedWH)
Bogdan Timofte authored 2 months ago
852

            
853
        if recordingThresholdTimestamp < dataDumpRequestTimestamp || !recordingThresholdLoadedFromDevice {
854
            recordingThresholdLoadedFromDevice = true
855
            if recordingTreshold != snapshot.recordingThreshold {
856
                isApplyingRecordingThresholdFromDevice = true
857
                recordingTreshold = snapshot.recordingThreshold
858
                isApplyingRecordingThresholdFromDevice = false
859
            }
860
        } else {
861
            track("\(name) - Skip updating recordingThreshold (changed after request).")
862
        }
Bogdan Timofte authored 2 months ago
863
        setIfChanged(\.recordingDuration, to: snapshot.recordingDuration)
864
        setIfChanged(\.recording, to: snapshot.recording)
Bogdan Timofte authored 2 months ago
865

            
Bogdan Timofte authored 2 months ago
866
        if screenTimeoutTimestamp < dataDumpRequestTimestamp {
Bogdan Timofte authored 2 months ago
867
            if screenTimeout != snapshot.screenTimeout {
868
                screenTimeout = snapshot.screenTimeout
Bogdan Timofte authored 2 months ago
869
            }
870
        } else {
871
            track("\(name) - Skip updating screenTimeout (changed after request).")
872
        }
873

            
874
        if screenBrightnessTimestamp < dataDumpRequestTimestamp {
Bogdan Timofte authored 2 months ago
875
            if screenBrightness != snapshot.screenBrightness {
876
                screenBrightness = snapshot.screenBrightness
Bogdan Timofte authored 2 months ago
877
            }
878
        } else {
879
            track("\(name) - Skip updating screenBrightness (changed after request).")
880
        }
881

            
Bogdan Timofte authored 2 months ago
882
        setIfChanged(\.currentScreen, to: snapshot.currentScreen)
883
        setIfChanged(\.loadResistance, to: snapshot.loadResistance)
Bogdan Timofte authored 2 months ago
884
    }
885

            
Bogdan Timofte authored 2 months ago
886
    private func apply(tc66Snapshot snapshot: TC66Snapshot) {
Bogdan Timofte authored 2 months ago
887
        let didDetectDeviceReset = didTC66DeviceReboot(with: snapshot)
Bogdan Timofte authored 2 months ago
888
        if hasSeenTC66Snapshot {
889
            inferTC66ActiveDataGroup(from: snapshot)
890
        } else {
891
            hasSeenTC66Snapshot = true
892
        }
Bogdan Timofte authored 2 months ago
893
        setIfChanged(\.reportedModelName, to: snapshot.modelName)
894
        setIfChanged(\.firmwareVersion, to: snapshot.firmwareVersion)
895
        setIfChanged(\.serialNumber, to: snapshot.serialNumber)
896
        setIfChanged(\.bootCount, to: snapshot.bootCount)
Bogdan Timofte authored 2 months ago
897
        updateChargerTypeLatch(observedIndex: 0, didDetectDeviceReset: didDetectDeviceReset)
Bogdan Timofte authored 2 months ago
898
        setIfChanged(\.voltage, to: snapshot.voltage)
899
        setIfChanged(\.current, to: snapshot.current)
900
        setIfChanged(\.power, to: snapshot.power)
901
        setIfChanged(\.loadResistance, to: snapshot.loadResistance)
Bogdan Timofte authored 2 months ago
902
        for (index, record) in snapshot.dataGroupRecords {
Bogdan Timofte authored 2 months ago
903
            updateDataGroupRecord(index: index, ah: record.ah, wh: record.wh)
Bogdan Timofte authored 2 months ago
904
        }
Bogdan Timofte authored 2 months ago
905
        setIfChanged(\.temperatureCelsius, to: snapshot.temperatureCelsius)
906
        setIfChanged(\.usbPlusVoltage, to: snapshot.usbPlusVoltage)
907
        setIfChanged(\.usbMinusVoltage, to: snapshot.usbMinusVoltage)
Bogdan Timofte authored 2 months ago
908
    }
Bogdan Timofte authored 2 months ago
909

            
910
    private func inferTC66ActiveDataGroup(from snapshot: TC66Snapshot) {
911
        let candidate = snapshot.dataGroupRecords.compactMap { entry -> (UInt8, Double)? in
912
            let index = entry.key
913
            let record = entry.value
914
            guard let previous = dataGroupRecords[index] else { return nil }
915
            let deltaAH = max(record.ah - previous.ah, 0)
916
            let deltaWH = max(record.wh - previous.wh, 0)
917
            let score = deltaAH + deltaWH
918
            guard score > 0 else { return nil }
919
            return (UInt8(index), score)
920
        }
921
        .max { lhs, rhs in lhs.1 < rhs.1 }
922

            
923
        if let candidate {
924
            selectedDataGroup = candidate.0
925
            hasObservedActiveDataGroup = true
926
        }
927
    }
928

            
929
    private func updateChargeRecord(at timestamp: Date) {
930
        switch chargeRecordState {
931
        case .waitingForStart:
932
            guard current > chargeRecordStopThreshold else { return }
933
            chargeRecordState = .active
934
            chargeRecordStartTimestamp = timestamp
935
            chargeRecordEndTimestamp = timestamp
936
            chargeRecordLastTimestamp = timestamp
937
            chargeRecordLastCurrent = current
938
            chargeRecordLastPower = power
939
        case .active:
940
            if let lastTimestamp = chargeRecordLastTimestamp {
941
                let deltaSeconds = max(timestamp.timeIntervalSince(lastTimestamp), 0)
942
                chargeRecordAH += chargeRecordLastCurrent * deltaSeconds / 3600
943
                chargeRecordWH += chargeRecordLastPower * deltaSeconds / 3600
944
                chargeRecordDuration += deltaSeconds
945
            }
946
            chargeRecordEndTimestamp = timestamp
947
            chargeRecordLastTimestamp = timestamp
948
            chargeRecordLastCurrent = current
949
            chargeRecordLastPower = power
950
            if current <= chargeRecordStopThreshold {
951
                chargeRecordState = .completed
952
            }
953
        case .completed:
954
            break
955
        }
956
    }
957

            
958
    func resetChargeRecord() {
959
        chargeRecordAH = 0
960
        chargeRecordWH = 0
961
        chargeRecordDuration = 0
962
        chargeRecordState = .waitingForStart
963
        chargeRecordStartTimestamp = nil
964
        chargeRecordEndTimestamp = nil
965
        chargeRecordLastTimestamp = nil
966
        chargeRecordLastCurrent = 0
967
        chargeRecordLastPower = 0
Bogdan Timofte authored a month ago
968
        restoredChargeSessionID = nil
969
        restoredChargeRecordSignature = nil
970
        chargeRecordMeasurements.resetSeries()
Bogdan Timofte authored 2 months ago
971
        objectWillChange.send()
Bogdan Timofte authored 2 months ago
972
    }
973

            
974
    func resetChargeRecordGraph() {
975
        resetChargeRecord()
976
    }
Bogdan Timofte authored a month ago
977

            
Bogdan Timofte authored a month ago
978
    func restoreChargeRecordIfNeeded(
979
        from activeSession: ChargeSessionSummary,
980
        replacingLiveBufferIfNeeded: Bool = false
981
    ) {
Bogdan Timofte authored a month ago
982
        var didChange = false
983
        let restoreSignature = ChargeRecordRestoreSignature(
984
            sessionID: activeSession.id,
985
            sampleCount: activeSession.aggregatedSamples.count,
Bogdan Timofte authored a month ago
986
            lastSampleTimestamp: activeSession.aggregatedSamples.last?.timestamp
Bogdan Timofte authored a month ago
987
        )
988

            
989
        if restoreSignature != restoredChargeRecordSignature {
Bogdan Timofte authored a month ago
990
            let shouldRefreshPersistedBuffer = replacingLiveBufferIfNeeded
Bogdan Timofte authored a month ago
991
            restoreTrace("meter=\(name) charge-record-restore-start session=\(activeSession.id.uuidString) status=\(activeSession.status.rawValue) samples=\(restoreSignature.sampleCount) lastTs=\(restoreSignature.lastSampleTimestamp?.description ?? "nil") replaceLive=\(shouldRefreshPersistedBuffer) state=\(chargeRecordState) existingPower=\(chargeRecordMeasurements.power.samplePoints.count)")
Bogdan Timofte authored a month ago
992
            let didRestorePersistedSamples = chargeRecordMeasurements.restorePersistedChargeSessionSamplesIfNeeded(
993
                from: activeSession,
Bogdan Timofte authored a month ago
994
                replacingLiveBufferIfNeeded: shouldRefreshPersistedBuffer
Bogdan Timofte authored a month ago
995
            )
Bogdan Timofte authored a month ago
996
            restoreTrace("meter=\(name) charge-record-restore-result session=\(activeSession.id.uuidString) didRestore=\(didRestorePersistedSamples) priorSignatureSamples=\(restoredChargeRecordSignature?.sampleCount.description ?? "nil")")
997
            if didRestorePersistedSamples || activeSession.aggregatedSamples.isEmpty == false {
Bogdan Timofte authored a month ago
998
                restoredChargeRecordSignature = restoreSignature
Bogdan Timofte authored a month ago
999
            }
1000
            if didRestorePersistedSamples {
Bogdan Timofte authored a month ago
1001
                didChange = true
1002
            }
1003
        }
1004

            
1005
        if chargeRecordState != .active {
1006
            chargeRecordState = .active
1007
            didChange = true
1008
        }
1009

            
1010
        let resolvedChargeWH = max(chargeRecordWH, activeSession.measuredEnergyWh)
1011
        if resolvedChargeWH != chargeRecordWH {
1012
            chargeRecordWH = resolvedChargeWH
1013
            didChange = true
1014
        }
1015

            
1016
        let resolvedDuration = max(chargeRecordDuration, max(activeSession.effectiveDuration, 0))
1017
        if resolvedDuration != chargeRecordDuration {
1018
            chargeRecordDuration = resolvedDuration
1019
            didChange = true
1020
        }
1021

            
1022
        if chargeRecordStopThreshold != activeSession.stopThresholdAmps {
1023
            chargeRecordStopThreshold = activeSession.stopThresholdAmps
1024
            didChange = true
1025
        }
1026

            
1027
        if let chargeRecordStartTimestamp {
1028
            let restoredStart = min(chargeRecordStartTimestamp, activeSession.startedAt)
1029
            if restoredStart != chargeRecordStartTimestamp {
1030
                self.chargeRecordStartTimestamp = restoredStart
1031
                didChange = true
1032
            }
1033
        } else {
1034
            chargeRecordStartTimestamp = activeSession.startedAt
1035
            didChange = true
1036
        }
1037

            
1038
        if let chargeRecordEndTimestamp {
1039
            let restoredEnd = max(chargeRecordEndTimestamp, activeSession.lastObservedAt)
1040
            if restoredEnd != chargeRecordEndTimestamp {
1041
                self.chargeRecordEndTimestamp = restoredEnd
1042
                didChange = true
1043
            }
1044
        } else {
1045
            chargeRecordEndTimestamp = activeSession.lastObservedAt
1046
            didChange = true
1047
        }
1048

            
Bogdan Timofte authored a month ago
1049
        if let selectedDataGroup = activeSession.selectedDataGroup {
Bogdan Timofte authored a month ago
1050
            if self.selectedDataGroup != selectedDataGroup {
1051
                self.selectedDataGroup = selectedDataGroup
1052
                didChange = true
1053
            }
Bogdan Timofte authored a month ago
1054
        }
Bogdan Timofte authored a month ago
1055

            
1056
        if didChange {
1057
            objectWillChange.send()
1058
        }
1059
    }
1060

            
Bogdan Timofte authored a month ago
1061
    private func captureLiveMeasurements(
1062
        at timestamp: Date,
1063
        in destination: Measurements,
1064
        includesTemperature: Bool = true
1065
    ) {
Bogdan Timofte authored a month ago
1066
        if supportsRecordingView {
1067
            destination.captureEnergyValue(
1068
                timestamp: timestamp,
1069
                value: recordedWH,
1070
                groupID: .max
1071
            )
1072
        } else if let energySample = currentEnergySample() {
1073
            destination.captureEnergyValue(
1074
                timestamp: timestamp,
1075
                value: energySample.value,
1076
                groupID: energySample.groupID
1077
            )
1078
        }
1079

            
1080
        destination.addValues(
1081
            timestamp: timestamp,
1082
            power: power,
1083
            voltage: voltage,
1084
            current: current,
Bogdan Timofte authored a month ago
1085
            temperature: includesTemperature ? displayedTemperatureValue : nil,
Bogdan Timofte authored a month ago
1086
            rssi: Double(btSerial.averageRSSI)
1087
        )
Bogdan Timofte authored a month ago
1088
    }
1089

            
1090
    func restoreChargeMonitoringIfNeeded(from activeSession: ChargeSessionSummary) {
Bogdan Timofte authored a month ago
1091
        let shouldReplaceLiveBuffer = restoredChargeSessionID != activeSession.id
1092
        if shouldReplaceLiveBuffer {
1093
            restoreTrace("meter=\(name) charge-monitoring-restore-request session=\(activeSession.id.uuidString) status=\(activeSession.status.rawValue) replaceLive=\(shouldReplaceLiveBuffer) restoredSession=\(restoredChargeSessionID?.uuidString ?? "nil")")
1094
        }
1095
        restoreChargeRecordIfNeeded(
1096
            from: activeSession,
1097
            replacingLiveBufferIfNeeded: shouldReplaceLiveBuffer
1098
        )
Bogdan Timofte authored a month ago
1099

            
1100
        guard restoredChargeSessionID != activeSession.id else {
1101
            return
1102
        }
1103

            
1104
        restoredChargeSessionID = activeSession.id
Bogdan Timofte authored a month ago
1105

            
1106
        guard activeSession.status == .active else {
1107
            restoreTrace("meter=\(name) charge-monitoring-restore-no-reconnect session=\(activeSession.id.uuidString) status=\(activeSession.status.rawValue)")
1108
            return
1109
        }
1110

            
Bogdan Timofte authored a month ago
1111
        enableAutoConnect = true
1112

            
1113
        guard operationalState < .peripheralConnectionPending else {
1114
            return
1115
        }
1116

            
1117
        track("\(name) - Restoring active charge session and reconnecting to meter")
1118
        btSerial.connect()
1119
    }
Bogdan Timofte authored 2 months ago
1120

            
1121
    func nextScreen() {
1122
        switch model {
1123
        case .UM25C:
Bogdan Timofte authored 2 months ago
1124
            commandQueue.append(UMProtocol.nextScreen)
Bogdan Timofte authored 2 months ago
1125
        case .UM34C:
Bogdan Timofte authored 2 months ago
1126
            commandQueue.append(UMProtocol.nextScreen)
Bogdan Timofte authored 2 months ago
1127
        case .TC66C:
Bogdan Timofte authored 2 months ago
1128
            commandQueue.append(TC66Protocol.nextPage)
Bogdan Timofte authored 2 months ago
1129
        }
1130
    }
1131

            
1132
    func rotateScreen() {
1133
        switch model {
1134
        case .UM25C:
Bogdan Timofte authored 2 months ago
1135
            commandQueue.append(UMProtocol.rotateScreen)
Bogdan Timofte authored 2 months ago
1136
        case .UM34C:
Bogdan Timofte authored 2 months ago
1137
            commandQueue.append(UMProtocol.rotateScreen)
Bogdan Timofte authored 2 months ago
1138
        case .TC66C:
Bogdan Timofte authored 2 months ago
1139
            commandQueue.append(TC66Protocol.rotateScreen)
Bogdan Timofte authored 2 months ago
1140
        }
1141
    }
1142

            
1143
    func previousScreen() {
1144
        switch model {
1145
        case .UM25C:
Bogdan Timofte authored 2 months ago
1146
            commandQueue.append(UMProtocol.previousScreen)
Bogdan Timofte authored 2 months ago
1147
        case .UM34C:
Bogdan Timofte authored 2 months ago
1148
            commandQueue.append(UMProtocol.previousScreen)
Bogdan Timofte authored 2 months ago
1149
        case .TC66C:
Bogdan Timofte authored 2 months ago
1150
            commandQueue.append(TC66Protocol.previousPage)
Bogdan Timofte authored 2 months ago
1151
        }
1152
    }
1153

            
1154
    func clear() {
Bogdan Timofte authored 2 months ago
1155
        guard supportsDataGroupCommands else { return }
Bogdan Timofte authored 2 months ago
1156
        noteInitiatedVolatileMemoryResetIfNeeded(for: selectedDataGroup)
Bogdan Timofte authored 2 months ago
1157
        commandQueue.append(UMProtocol.clearCurrentGroup)
Bogdan Timofte authored 2 months ago
1158
    }
Bogdan Timofte authored a month ago
1159

            
1160
    func resetMeterCountersForNewSession() {
1161
        guard supportsDataGroupCommands else { return }
1162

            
1163
        clear()
1164

            
1165
        if let record = dataGroupRecords[Int(selectedDataGroup)] {
1166
            record.ah = 0
1167
            record.wh = 0
1168
        }
1169
        recordedAH = 0
1170
        recordedWH = 0
1171
        recording = false
1172
        objectWillChange.send()
1173
    }
Bogdan Timofte authored 2 months ago
1174

            
1175
    func clear(group id: UInt8) {
Bogdan Timofte authored 2 months ago
1176
        guard supportsDataGroupCommands else { return }
Bogdan Timofte authored 2 months ago
1177
        commandQueue.append(UMProtocol.selectDataGroup(id))
Bogdan Timofte authored 2 months ago
1178
        noteInitiatedVolatileMemoryResetIfNeeded(for: id)
1179
        commandQueue.append(UMProtocol.clearCurrentGroup)
Bogdan Timofte authored 2 months ago
1180
        commandQueue.append(UMProtocol.selectDataGroup(selectedDataGroup))
Bogdan Timofte authored 2 months ago
1181
    }
1182

            
1183
    func selectDataGroup ( id: UInt8) {
Bogdan Timofte authored 2 months ago
1184
        guard supportsDataGroupCommands else { return }
Bogdan Timofte authored 2 months ago
1185
        selectedDataGroup = id
Bogdan Timofte authored 2 months ago
1186
        objectWillChange.send()
Bogdan Timofte authored 2 months ago
1187
        commandQueue.append(UMProtocol.selectDataGroup(selectedDataGroup))
Bogdan Timofte authored 2 months ago
1188
    }
1189

            
1190
    private func setSceeenBrightness ( to value: UInt8) {
Bogdan Timofte authored 2 months ago
1191
        guard supportsUMSettings else { return }
Bogdan Timofte authored 2 months ago
1192
        commandQueue.append(UMProtocol.setScreenBrightness(value))
Bogdan Timofte authored 2 months ago
1193
    }
1194
    private func setScreenSaverTimeout ( to value: UInt8) {
Bogdan Timofte authored 2 months ago
1195
        guard supportsUMSettings else { return }
Bogdan Timofte authored 2 months ago
1196
        commandQueue.append(UMProtocol.setScreenSaverTimeout(value))
Bogdan Timofte authored 2 months ago
1197
    }
1198
    func setrecordingTreshold ( to value: UInt8) {
Bogdan Timofte authored 2 months ago
1199
        guard supportsRecordingThreshold else { return }
Bogdan Timofte authored 2 months ago
1200
        commandQueue.append(UMProtocol.setRecordingThreshold(value))
Bogdan Timofte authored 2 months ago
1201
    }
1202

            
1203
    /**
1204
     Connect to meter.
1205
     1. It calls BluetoothSerial.connect
1206
     */
1207
    func connect() {
1208
        enableAutoConnect = true
1209
        btSerial.connect()
1210
    }
1211

            
1212
    /**
1213
     Disconnect from meter.
1214
        It calls BluetoothSerial.disconnect
1215
     */
1216
    func disconnect() {
1217
        enableAutoConnect = false
1218
        btSerial.disconnect()
1219
    }
1220
}
1221

            
1222
extension Meter : SerialPortDelegate {
1223

            
1224
    func opertionalStateChanged(to serialPortOperationalState: BluetoothSerial.OperationalState) {
Bogdan Timofte authored 2 months ago
1225
        let applyStateChange = {
1226
            self.lastSeen = Date()
1227
            switch serialPortOperationalState {
1228
            case .peripheralNotConnected:
1229
                self.operationalState = .peripheralNotConnected
1230
            case .peripheralConnectionPending:
1231
                self.operationalState = .peripheralConnectionPending
1232
            case .peripheralConnected:
Bogdan Timofte authored 2 months ago
1233
                self.noteConnectionEstablished(at: Date())
Bogdan Timofte authored 2 months ago
1234
                self.operationalState = .peripheralConnected
1235
            case .peripheralReady:
1236
                self.operationalState = .peripheralReady
1237
            }
1238
        }
1239

            
1240
        if Thread.isMainThread {
1241
            applyStateChange()
1242
        } else {
1243
            DispatchQueue.main.async(execute: applyStateChange)
Bogdan Timofte authored 2 months ago
1244
        }
1245
    }
1246

            
1247
    func didReceiveData(_ data: Data) {
Bogdan Timofte authored 2 months ago
1248
        let applyData = {
1249
            self.lastSeen = Date()
Bogdan Timofte authored 2 months ago
1250
            if self.operationalState < .comunicating {
1251
                self.operationalState = .comunicating
1252
            }
Bogdan Timofte authored 2 months ago
1253
            self.parseData(from: data)
1254
        }
1255

            
1256
        if Thread.isMainThread {
1257
            applyData()
1258
        } else {
1259
            DispatchQueue.main.async(execute: applyData)
1260
        }
Bogdan Timofte authored 2 months ago
1261
    }
1262
}