Files
Cable/Cable/SavedBattery.swift
Stefan Lange-Hegermann 28ad6dd10c battery persistence
2025-10-21 09:55:43 +02:00

45 lines
1.2 KiB
Swift

import Foundation
import SwiftData
@Model
class SavedBattery {
@Attribute(.unique) var id: UUID
var name: String
var nominalVoltage: Double
var capacityAmpHours: Double
private var chemistryRawValue: String
var system: ElectricalSystem?
var timestamp: Date
init(
id: UUID = UUID(),
name: String,
nominalVoltage: Double = 12.8,
capacityAmpHours: Double = 100,
chemistry: BatteryConfiguration.Chemistry = .lithiumIronPhosphate,
system: ElectricalSystem? = nil,
timestamp: Date = Date()
) {
self.id = id
self.name = name
self.nominalVoltage = nominalVoltage
self.capacityAmpHours = capacityAmpHours
self.chemistryRawValue = chemistry.rawValue
self.system = system
self.timestamp = timestamp
}
var chemistry: BatteryConfiguration.Chemistry {
get {
BatteryConfiguration.Chemistry(rawValue: chemistryRawValue) ?? .lithiumIronPhosphate
}
set {
chemistryRawValue = newValue.rawValue
}
}
var energyWattHours: Double {
nominalVoltage * capacityAmpHours
}
}