Add icon, set device time

This commit is contained in:
Christoph Hagen
2023-06-08 14:57:40 +02:00
parent 147cd6a306
commit 0f2246cbe5
15 changed files with 335 additions and 195 deletions

View File

@ -0,0 +1,43 @@
import Foundation
enum DeviceInfoError: Error {
case missingData
}
extension Data {
mutating func decodeUInt16() throws -> UInt16 {
guard count >= 2 else {
throw DeviceInfoError.missingData
}
let low = removeFirst()
let high = removeFirst()
return UInt16(high: high, low: low)
}
mutating func decodeTwoByteInteger() throws -> Int {
try decodeUInt16().integer
}
mutating func decodeFourByteInteger() throws -> Int {
guard count >= 4 else {
throw DeviceInfoError.missingData
}
let byte0 = removeFirst()
let byte1 = removeFirst()
let byte2 = removeFirst()
let byte3 = removeFirst()
return (Int(byte3) << 24) | (Int(byte2) << 16) | (Int(byte1) << 8) | Int(byte0)
}
mutating func decodeSensor() throws -> TemperatureSensor? {
guard count >= 11 else {
throw DeviceInfoError.missingData
}
let address = Array(self[startIndex..<startIndex+8])
removeFirst(8)
let temperatureByte = removeFirst()
let time = try decodeUInt16()
return .init(address: address, valueByte: temperatureByte, secondsAgo: time)
}
}

View File

@ -62,4 +62,12 @@ extension Date {
var startOfNextDay: Date {
startOfDay.addingTimeInterval(86400)
}
func adding(seconds: Int) -> Date {
addingTimeInterval(TimeInterval(seconds))
}
var nearestSecond: Date {
.init(seconds: seconds)
}
}

View File

@ -0,0 +1,27 @@
import Foundation
extension Int {
var twoByteData: Data {
let value = UInt16(clamping: self)
return Data([value.low, value.high])
}
var fourByteData: Data {
let value = UInt32(clamping: self)
return Data([
UInt8(value & 0xFF),
UInt8((value >> 8) & 0xFF),
UInt8((value >> 16) & 0xFF),
UInt8((value >> 24) & 0xFF)
])
}
init(uint32 byte0: UInt8, _ byte1: UInt8, _ byte2: UInt8, _ byte3: UInt8) {
self = (Int(byte0) << 24) | (Int(byte1) << 16) | (Int(byte2) << 8) | Int(byte3)
}
init(high: UInt8, low: UInt8) {
self = Int(high) << 8 + Int(low)
}
}

View File

@ -0,0 +1,20 @@
import Foundation
extension UInt16 {
init(high: UInt8, low: UInt8) {
self = UInt16(high) << 8 + UInt16(low)
}
var low: UInt8 {
UInt8(clamping: self)
}
var high: UInt8 {
UInt8(clamping: self >> 8)
}
var integer: Int {
.init(self)
}
}