TempTrack-iOS/TempTrack/Bluetooth/DeviceInfo.swift

108 lines
3.4 KiB
Swift
Raw Normal View History

2023-06-03 08:15:00 +02:00
import Foundation
struct DeviceInfo {
2023-07-03 13:28:51 +02:00
/// The unique ID generated by the device to distinguish between power cycles
let uniqueIdOfPowerCycle: Int
2023-06-03 08:15:00 +02:00
/// The number of bytes recorded by the tracker
let numberOfRecordedBytes: Int
2023-07-03 13:28:51 +02:00
/// The sum of all recorded bytes
let dataChecksum: UInt16
2023-06-03 08:15:00 +02:00
/// The number of measurements already performed
2023-06-08 14:57:40 +02:00
let numberOfStoredMeasurements: Int
2023-06-03 08:15:00 +02:00
/// The interval between measurements (in seconds)
let measurementInterval: Int
let sensor0: TemperatureSensor?
let sensor1: TemperatureSensor?
// MARK: Device time
2023-06-03 08:15:00 +02:00
2023-06-13 17:14:57 +02:00
let wakeupReason: DeviceWakeCause
let time: DeviceTime
2023-06-13 17:14:57 +02:00
// MARK: Storage
let storageSize: Int
/// The maximum number of bytes which can be requested
let transferBlockSize: Int
2023-06-03 08:15:00 +02:00
var storageFillRatio: Double {
Double(numberOfRecordedBytes) / Double(storageSize)
}
var storageFillPercentage: Int {
Int((storageFillRatio * 100).rounded())
}
var currentMeasurementStartTime: Date {
time.nextMeasurement.addingTimeInterval(-Double(numberOfStoredMeasurements * measurementInterval))
2023-06-08 14:57:40 +02:00
}
2023-06-14 17:52:43 +02:00
2023-06-03 08:15:00 +02:00
}
extension DeviceInfo {
2023-06-08 14:57:40 +02:00
init(info: Data) throws {
let date = Date()
2023-06-08 14:57:40 +02:00
var data = info
2023-07-03 13:28:51 +02:00
self.uniqueIdOfPowerCycle = try data.decodeFourByteInteger()
2023-06-08 14:57:40 +02:00
self.numberOfRecordedBytes = try data.decodeTwoByteInteger()
let secondsUntilNextMeasurement = try data.decodeTwoByteInteger()
2023-06-08 14:57:40 +02:00
self.measurementInterval = try data.decodeTwoByteInteger()
self.numberOfStoredMeasurements = try data.decodeTwoByteInteger()
let totalNumberOfMeasurements = try data.decodeFourByteInteger()
2023-06-08 14:57:40 +02:00
self.transferBlockSize = try data.decodeTwoByteInteger()
self.storageSize = try data.decodeTwoByteInteger()
let secondsSincePowerOn = try data.decodeFourByteInteger()
2023-07-03 13:28:51 +02:00
let startSecondsOfCurrentRecording = try data.decodeFourByteInteger()
self.dataChecksum = try data.decodeUInt16()
self.wakeupReason = .init(rawValue: try data.getByte()) ?? .WAKEUP_UNDEFINED
self.sensor0 = try data.decodeSensor()
self.sensor1 = try data.decodeSensor()
guard data.isEmpty else {
log.error("\(data.count) bytes remaining in device info buffer")
throw DeviceInfoError.missingData
}
self.time = .init(
date: date,
secondsSincePowerOn: secondsSincePowerOn,
totalNumberOfMeasurements: totalNumberOfMeasurements,
2023-07-03 13:28:51 +02:00
secondsUntilNextMeasurement: secondsUntilNextMeasurement,
secondsOfFirstMeasurement: startSecondsOfCurrentRecording)
2023-06-03 08:15:00 +02:00
}
}
extension DeviceInfo {
static var mock: DeviceInfo {
.init(
2023-07-03 13:28:51 +02:00
uniqueIdOfPowerCycle: .random(in: 0...Int(UInt32.max)),
2023-06-03 08:15:00 +02:00
numberOfRecordedBytes: 123,
2023-07-03 13:28:51 +02:00
dataChecksum: .random(in: .min...UInt16.max),
2023-06-08 14:57:40 +02:00
numberOfStoredMeasurements: 234,
2023-06-03 08:15:00 +02:00
measurementInterval: 60,
sensor0: .init(address: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], value: .value(21.0), date: .now.addingTimeInterval(-2)),
sensor1: .init(address: [0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09], value: .value(19.0), date: .now.addingTimeInterval(-4)),
2023-06-13 17:14:57 +02:00
wakeupReason: .WAKEUP_EXT0,
time: .mock,
storageSize: 10000,
transferBlockSize: 180)
2023-06-03 08:15:00 +02:00
}
}
2023-07-03 13:28:51 +02:00
extension DeviceInfo: Codable {
}