Add icon, set device time
This commit is contained in:
43
TempTrack/Extensions/Data+Extensions.swift
Normal file
43
TempTrack/Extensions/Data+Extensions.swift
Normal 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)
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
27
TempTrack/Extensions/Int+Extensions.swift
Normal file
27
TempTrack/Extensions/Int+Extensions.swift
Normal 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)
|
||||
}
|
||||
}
|
20
TempTrack/Extensions/UInt16+Extensions.swift
Normal file
20
TempTrack/Extensions/UInt16+Extensions.swift
Normal 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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user