From 709f7bfa32dbfff94a884fcaaf29dcb9b10b38bc Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Tue, 19 Dec 2023 14:09:27 +0100 Subject: [PATCH] fix: adjusted chart value in the Sensors popup to the local temperature (C/F) (#1730) --- Modules/Sensors/popup.swift | 2 +- Modules/Sensors/values.swift | 122 ++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 61 deletions(-) diff --git a/Modules/Sensors/popup.swift b/Modules/Sensors/popup.swift index 55797772eb2..96a411237aa 100644 --- a/Modules/Sensors/popup.swift +++ b/Modules/Sensors/popup.swift @@ -330,7 +330,7 @@ internal class SensorView: NSStackView { } public func addHistoryPoint(_ sensor: Sensor_p) { - self.chartView.update(sensor.value) + self.chartView.update(sensor.localValue) } private func open() { diff --git a/Modules/Sensors/values.swift b/Modules/Sensors/values.swift index 3371a5050ab..0073b56db73 100644 --- a/Modules/Sensors/values.swift +++ b/Modules/Sensors/values.swift @@ -44,6 +44,7 @@ public protocol Sensor_p { var isComputed: Bool { get } var average: Bool { get } + var localValue: Double { get } var unit: String { get } var formattedValue: String { get } var formattedMiniValue: String { get } @@ -136,75 +137,73 @@ public struct Sensor: Sensor_p, Codable { public var average: Bool = false public var unit: String { - get { - switch self.type { - case .temperature: - return UnitTemperature.current.symbol - case .voltage: - return "V" - case .power: - return "W" - case .energy: - return "Wh" - case .current: - return "A" - case .fan: - return "RPM" - } + switch self.type { + case .temperature: + return UnitTemperature.current.symbol + case .voltage: + return "V" + case .power: + return "W" + case .energy: + return "Wh" + case .current: + return "A" + case .fan: + return "RPM" } } public var formattedValue: String { - get { - switch self.type { - case .temperature: - return temperature(value) - case .voltage: - let val = value >= 100 ? "\(Int(value))" : String(format: "%.3f", value) - return "\(val)\(unit)" - case .power, .energy: - let val = value >= 100 ? "\(Int(value))" : String(format: "%.2f", value) - return "\(val)\(unit)" - case .current: - let val = value >= 100 ? "\(Int(value))" : String(format: "%.2f", value) - return "\(val)\(unit)" - case .fan: - return "\(Int(value)) \(unit)" - } + switch self.type { + case .temperature: + return temperature(value) + case .voltage: + let val = value >= 100 ? "\(Int(value))" : String(format: "%.3f", value) + return "\(val)\(unit)" + case .power, .energy: + let val = value >= 100 ? "\(Int(value))" : String(format: "%.2f", value) + return "\(val)\(unit)" + case .current: + let val = value >= 100 ? "\(Int(value))" : String(format: "%.2f", value) + return "\(val)\(unit)" + case .fan: + return "\(Int(value)) \(unit)" } } public var formattedPopupValue: String { - get { - switch self.type { - case .temperature: - return temperature(value, fractionDigits: 1) - case .voltage: - let val = value >= 100 ? "\(Int(value))" : String(format: "%.3f", value) - return "\(val)\(unit)" - case .power, .energy: - let val = value >= 100 ? "\(Int(value))" : String(format: "%.2f", value) - return "\(val)\(unit)" - case .current: - let val = value >= 100 ? "\(Int(value))" : String(format: "%.2f", value) - return "\(val)\(unit)" - case .fan: - return "\(Int(value)) \(unit)" - } + switch self.type { + case .temperature: + return temperature(value, fractionDigits: 1) + case .voltage: + let val = value >= 100 ? "\(Int(value))" : String(format: "%.3f", value) + return "\(val)\(unit)" + case .power, .energy: + let val = value >= 100 ? "\(Int(value))" : String(format: "%.2f", value) + return "\(val)\(unit)" + case .current: + let val = value >= 100 ? "\(Int(value))" : String(format: "%.2f", value) + return "\(val)\(unit)" + case .fan: + return "\(Int(value)) \(unit)" } } public var formattedMiniValue: String { - get { - switch self.type { - case .temperature: - return temperature(value).replacingOccurrences(of: "C", with: "").replacingOccurrences(of: "F", with: "") - case .voltage, .power, .energy, .current: - let val = value >= 9.95 ? "\(Int(round(value)))" : String(format: "%.1f", value) - return "\(val)\(unit)" - case .fan: - return "\(Int(value))" - } + switch self.type { + case .temperature: + return temperature(value).replacingOccurrences(of: "C", with: "").replacingOccurrences(of: "F", with: "") + case .voltage, .power, .energy, .current: + let val = value >= 9.95 ? "\(Int(round(value)))" : String(format: "%.1f", value) + return "\(val)\(unit)" + case .fan: + return "\(Int(value))" } } + public var localValue: Double { + if self.type == .temperature { + return Double(self.formattedMiniValue.digits) ?? self.value + } + return self.value + } public var state: Bool { Store.shared.bool(key: "sensor_\(self.key)", defaultValue: false) @@ -254,13 +253,16 @@ public struct Fan: Sensor_p, Codable { public var unit: String = "RPM" public var formattedValue: String { - "\(Int(value)) RPM" + "\(Int(self.value)) RPM" } public var formattedMiniValue: String { - "\(Int(value))" + "\(Int(self.value))" } public var formattedPopupValue: String { - "\(Int(value)) RPM" + "\(Int(self.value)) RPM" + } + public var localValue: Double { + return self.value } public var state: Bool {