Newer Older
1246 lines | 44.628kb
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
            // track("\(name) - Request sent!")
782
        } else {
783
            track("Request delayed for: \(commandQueue.first!.hexEncodedStringValue)")
784
            btSerial.write( commandQueue.first! )
785
            commandQueue.removeFirst()
Bogdan Timofte authored 2 months ago
786
            scheduleDataDumpRequest(after: 1, reason: "queued command")
Bogdan Timofte authored 2 months ago
787
        }
788
    }
789

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

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

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

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

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

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

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

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

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

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

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

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

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

            
987
        if restoreSignature != restoredChargeRecordSignature {
988
            chargeRecordMeasurements.restorePersistedChargeSessionSamplesIfNeeded(
Bogdan Timofte authored a month ago
989
                from: activeSession
Bogdan Timofte authored a month ago
990
            )
991
            if activeSession.aggregatedSamples.isEmpty == false {
992
                restoredChargeRecordSignature = restoreSignature
993
                didChange = true
994
            }
995
        }
996

            
997
        if chargeRecordState != .active {
998
            chargeRecordState = .active
999
            didChange = true
1000
        }
1001

            
1002
        let resolvedChargeAH = max(chargeRecordAH, activeSession.measuredChargeAh)
1003
        if resolvedChargeAH != chargeRecordAH {
1004
            chargeRecordAH = resolvedChargeAH
1005
            didChange = true
1006
        }
1007

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

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

            
1020
        if chargeRecordStopThreshold != activeSession.stopThresholdAmps {
1021
            chargeRecordStopThreshold = activeSession.stopThresholdAmps
1022
            didChange = true
1023
        }
1024

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

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

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

            
1054
        if didChange {
1055
            objectWillChange.send()
1056
        }
1057
    }
1058

            
1059
    private func captureLiveMeasurements(at timestamp: Date, in destination: Measurements) {
1060
        if supportsRecordingView {
1061
            destination.captureEnergyValue(
1062
                timestamp: timestamp,
1063
                value: recordedWH,
1064
                groupID: .max
1065
            )
1066
        } else if let energySample = currentEnergySample() {
1067
            destination.captureEnergyValue(
1068
                timestamp: timestamp,
1069
                value: energySample.value,
1070
                groupID: energySample.groupID
1071
            )
1072
        }
1073

            
1074
        destination.addValues(
1075
            timestamp: timestamp,
1076
            power: power,
1077
            voltage: voltage,
1078
            current: current,
1079
            temperature: displayedTemperatureValue,
1080
            rssi: Double(btSerial.averageRSSI)
1081
        )
Bogdan Timofte authored a month ago
1082
    }
1083

            
1084
    func restoreChargeMonitoringIfNeeded(from activeSession: ChargeSessionSummary) {
1085
        restoreChargeRecordIfNeeded(from: activeSession)
1086

            
1087
        guard restoredChargeSessionID != activeSession.id else {
1088
            return
1089
        }
1090

            
1091
        restoredChargeSessionID = activeSession.id
1092
        enableAutoConnect = true
1093

            
1094
        guard operationalState < .peripheralConnectionPending else {
1095
            return
1096
        }
1097

            
1098
        track("\(name) - Restoring active charge session and reconnecting to meter")
1099
        btSerial.connect()
1100
    }
Bogdan Timofte authored 2 months ago
1101

            
1102
    func nextScreen() {
1103
        switch model {
1104
        case .UM25C:
Bogdan Timofte authored 2 months ago
1105
            commandQueue.append(UMProtocol.nextScreen)
Bogdan Timofte authored 2 months ago
1106
        case .UM34C:
Bogdan Timofte authored 2 months ago
1107
            commandQueue.append(UMProtocol.nextScreen)
Bogdan Timofte authored 2 months ago
1108
        case .TC66C:
Bogdan Timofte authored 2 months ago
1109
            commandQueue.append(TC66Protocol.nextPage)
Bogdan Timofte authored 2 months ago
1110
        }
1111
    }
1112

            
1113
    func rotateScreen() {
1114
        switch model {
1115
        case .UM25C:
Bogdan Timofte authored 2 months ago
1116
            commandQueue.append(UMProtocol.rotateScreen)
Bogdan Timofte authored 2 months ago
1117
        case .UM34C:
Bogdan Timofte authored 2 months ago
1118
            commandQueue.append(UMProtocol.rotateScreen)
Bogdan Timofte authored 2 months ago
1119
        case .TC66C:
Bogdan Timofte authored 2 months ago
1120
            commandQueue.append(TC66Protocol.rotateScreen)
Bogdan Timofte authored 2 months ago
1121
        }
1122
    }
1123

            
1124
    func previousScreen() {
1125
        switch model {
1126
        case .UM25C:
Bogdan Timofte authored 2 months ago
1127
            commandQueue.append(UMProtocol.previousScreen)
Bogdan Timofte authored 2 months ago
1128
        case .UM34C:
Bogdan Timofte authored 2 months ago
1129
            commandQueue.append(UMProtocol.previousScreen)
Bogdan Timofte authored 2 months ago
1130
        case .TC66C:
Bogdan Timofte authored 2 months ago
1131
            commandQueue.append(TC66Protocol.previousPage)
Bogdan Timofte authored 2 months ago
1132
        }
1133
    }
1134

            
1135
    func clear() {
Bogdan Timofte authored 2 months ago
1136
        guard supportsDataGroupCommands else { return }
Bogdan Timofte authored 2 months ago
1137
        noteInitiatedVolatileMemoryResetIfNeeded(for: selectedDataGroup)
Bogdan Timofte authored 2 months ago
1138
        commandQueue.append(UMProtocol.clearCurrentGroup)
Bogdan Timofte authored 2 months ago
1139
    }
Bogdan Timofte authored a month ago
1140

            
1141
    func resetMeterCountersForNewSession() {
1142
        guard supportsDataGroupCommands else { return }
1143

            
1144
        clear()
1145

            
1146
        if let record = dataGroupRecords[Int(selectedDataGroup)] {
1147
            record.ah = 0
1148
            record.wh = 0
1149
        }
1150
        recordedAH = 0
1151
        recordedWH = 0
1152
        recording = false
1153
        objectWillChange.send()
1154
    }
Bogdan Timofte authored 2 months ago
1155

            
1156
    func clear(group id: UInt8) {
Bogdan Timofte authored 2 months ago
1157
        guard supportsDataGroupCommands else { return }
Bogdan Timofte authored 2 months ago
1158
        commandQueue.append(UMProtocol.selectDataGroup(id))
Bogdan Timofte authored 2 months ago
1159
        noteInitiatedVolatileMemoryResetIfNeeded(for: id)
1160
        commandQueue.append(UMProtocol.clearCurrentGroup)
Bogdan Timofte authored 2 months ago
1161
        commandQueue.append(UMProtocol.selectDataGroup(selectedDataGroup))
Bogdan Timofte authored 2 months ago
1162
    }
1163

            
1164
    func selectDataGroup ( id: UInt8) {
Bogdan Timofte authored 2 months ago
1165
        guard supportsDataGroupCommands else { return }
Bogdan Timofte authored 2 months ago
1166
        track("\(name) - \(id)")
1167
        selectedDataGroup = id
Bogdan Timofte authored 2 months ago
1168
        objectWillChange.send()
Bogdan Timofte authored 2 months ago
1169
        commandQueue.append(UMProtocol.selectDataGroup(selectedDataGroup))
Bogdan Timofte authored 2 months ago
1170
    }
1171

            
1172
    private func setSceeenBrightness ( to value: UInt8) {
1173
        track("\(name) - \(value)")
Bogdan Timofte authored 2 months ago
1174
        guard supportsUMSettings else { return }
Bogdan Timofte authored 2 months ago
1175
        commandQueue.append(UMProtocol.setScreenBrightness(value))
Bogdan Timofte authored 2 months ago
1176
    }
1177
    private func setScreenSaverTimeout ( to value: UInt8) {
1178
        track("\(name) - \(value)")
Bogdan Timofte authored 2 months ago
1179
        guard supportsUMSettings else { return }
Bogdan Timofte authored 2 months ago
1180
        commandQueue.append(UMProtocol.setScreenSaverTimeout(value))
Bogdan Timofte authored 2 months ago
1181
    }
1182
    func setrecordingTreshold ( to value: UInt8) {
Bogdan Timofte authored 2 months ago
1183
        guard supportsRecordingThreshold else { return }
Bogdan Timofte authored 2 months ago
1184
        commandQueue.append(UMProtocol.setRecordingThreshold(value))
Bogdan Timofte authored 2 months ago
1185
    }
1186

            
1187
    /**
1188
     Connect to meter.
1189
     1. It calls BluetoothSerial.connect
1190
     */
1191
    func connect() {
1192
        enableAutoConnect = true
1193
        btSerial.connect()
1194
    }
1195

            
1196
    /**
1197
     Disconnect from meter.
1198
        It calls BluetoothSerial.disconnect
1199
     */
1200
    func disconnect() {
1201
        enableAutoConnect = false
1202
        btSerial.disconnect()
1203
    }
1204
}
1205

            
1206
extension Meter : SerialPortDelegate {
1207

            
1208
    func opertionalStateChanged(to serialPortOperationalState: BluetoothSerial.OperationalState) {
Bogdan Timofte authored 2 months ago
1209
        let applyStateChange = {
1210
            self.lastSeen = Date()
1211
            switch serialPortOperationalState {
1212
            case .peripheralNotConnected:
1213
                self.operationalState = .peripheralNotConnected
1214
            case .peripheralConnectionPending:
1215
                self.operationalState = .peripheralConnectionPending
1216
            case .peripheralConnected:
Bogdan Timofte authored 2 months ago
1217
                self.noteConnectionEstablished(at: Date())
Bogdan Timofte authored 2 months ago
1218
                self.operationalState = .peripheralConnected
1219
            case .peripheralReady:
1220
                self.operationalState = .peripheralReady
1221
            }
1222
        }
1223

            
1224
        if Thread.isMainThread {
1225
            applyStateChange()
1226
        } else {
1227
            DispatchQueue.main.async(execute: applyStateChange)
Bogdan Timofte authored 2 months ago
1228
        }
1229
    }
1230

            
1231
    func didReceiveData(_ data: Data) {
Bogdan Timofte authored 2 months ago
1232
        let applyData = {
1233
            self.lastSeen = Date()
Bogdan Timofte authored 2 months ago
1234
            if self.operationalState < .comunicating {
1235
                self.operationalState = .comunicating
1236
            }
Bogdan Timofte authored 2 months ago
1237
            self.parseData(from: data)
1238
        }
1239

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