TempTrack-iOS/TempTrack/Temperature/TemperatureSensor.swift
2023-06-08 09:52:20 +02:00

63 lines
1.5 KiB
Swift

import Foundation
import SFSafeSymbols
struct TemperatureSensor {
let address: [UInt8]
let value: TemperatureValue
let date: Date
var optionalValue: Double? {
value.optionalValue
}
var hexAddress: String {
String(format: "%02X %02X %02X %02X %02X %02X %02X %02X",
address[0], address[1], address[2], address[3],
address[4], address[5], address[6], address[7])
}
var temperatureIcon: SFSymbol {
TemperatureSensor.temperatureIcon(optionalValue)
}
var temperatureText: String {
value.text
}
var updateText: String {
date.timePassedText
}
static func temperatureIcon(_ temp: Double?) -> SFSymbol {
guard let temp else {
return .thermometerMediumSlash
}
guard temp > 0 else {
return .thermometerSnowflake
}
guard temp > 15 else {
return .thermometerLow
}
guard temp > 25 else {
return .thermometerMedium
}
return .thermometerHigh
}
}
extension TemperatureSensor {
init?(address: [UInt8], valueByte: UInt8, secondsAgo: UInt16) {
guard address.contains(where: { $0 != 0 }) else {
// Empty address
return nil
}
self.address = address
self.value = .init(byte: valueByte)
self.date = Date().addingTimeInterval(-TimeInterval(secondsAgo))
}
}