import Foundation /// Zustand des Solarladereglers. struct SolarState: Equatable, Codable, Sendable { var batteryVoltage: Double? var pvVoltage: Double? var pvCurrent: Double? var pvPower: Double? var controllerTemperature: Double? var isBatteryCharging: Bool? var isBatteryDischarging: Bool? var isControllerActive: Bool? var isCurrentLimited: Bool? var hasReading: Bool { batteryVoltage != nil || pvVoltage != nil || pvCurrent != nil || pvPower != nil } /// Kurzer Klartext, wie bei den übrigen Geräten als "Zustand" angezeigt. var stateText: String? { guard isControllerActive != nil else { return nil } if isBatteryCharging == true { return "Lädt" } if isControllerActive == true { return "Aktiv" } return "Inaktiv" } func snapshot(deviceID: UUID, rssi: Int?) -> DeviceSnapshot { var snapshot = DeviceSnapshot(deviceID: deviceID, timestamp: Date(), rssi: rssi) snapshot.metrics = [ Metric("pv_power", "Solarleistung", pvPower, unit: "W", precision: 0, primary: true), Metric("pv_voltage", "PV-Spannung", pvVoltage, unit: "V", precision: 1), Metric("pv_current", "PV-Strom", pvCurrent, unit: "A", precision: 1), Metric("battery_voltage", "Batteriespannung", batteryVoltage, unit: "V", precision: 2), ] if let controllerTemperature { snapshot.metrics.append( Metric("controller_temperature", "Reglertemperatur", controllerTemperature, unit: "°C", precision: 0) ) } snapshot.state = stateText if isCurrentLimited == true { snapshot.offReasons = ["PV-Strombegrenzung aktiv"] } return snapshot } }