2023-06-03 08:15:00 +02:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
struct DeviceInfo {
|
|
|
|
|
|
|
|
let receivedDate: Date
|
|
|
|
|
|
|
|
/// The number of bytes recorded by the tracker
|
|
|
|
let numberOfRecordedBytes: Int
|
|
|
|
|
|
|
|
/// The number of measurements already performed
|
2023-06-08 14:57:40 +02:00
|
|
|
let numberOfStoredMeasurements: Int
|
|
|
|
|
|
|
|
/// The measurements since device start
|
|
|
|
let totalNumberOfMeasurements: Int
|
2023-06-03 08:15:00 +02:00
|
|
|
|
|
|
|
/// The interval between measurements (in seconds)
|
|
|
|
let measurementInterval: Int
|
|
|
|
|
|
|
|
let nextMeasurement: Date
|
|
|
|
|
|
|
|
let sensor0: TemperatureSensor?
|
|
|
|
|
|
|
|
let sensor1: TemperatureSensor?
|
|
|
|
|
2023-06-08 09:52:20 +02:00
|
|
|
// MARK: Device time
|
2023-06-03 08:15:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
The number of seconds the device has been powered on
|
|
|
|
*/
|
2023-06-08 09:52:20 +02:00
|
|
|
let numberOfSecondsRunning: Int
|
|
|
|
|
|
|
|
let deviceStartTime: Date
|
|
|
|
|
|
|
|
let hasDeviceStartTimeSet: Bool
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
2023-06-08 14:57:40 +02:00
|
|
|
|
|
|
|
var clockOffset: TimeInterval {
|
|
|
|
// Measurements are performed on device start (-1) and also count next measurement (+1)
|
|
|
|
let nextMeasurementTime = deviceStartTime.adding(seconds: (totalNumberOfMeasurements) * measurementInterval)
|
|
|
|
return nextMeasurement.timeIntervalSince(nextMeasurementTime)
|
|
|
|
}
|
2023-06-03 08:15:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension DeviceInfo {
|
|
|
|
|
2023-06-08 14:57:40 +02:00
|
|
|
init(info: Data) throws {
|
|
|
|
var data = info
|
|
|
|
|
|
|
|
let date = Date().nearestSecond
|
|
|
|
self.receivedDate = date
|
|
|
|
self.numberOfRecordedBytes = try data.decodeTwoByteInteger()
|
|
|
|
self.nextMeasurement = date.adding(seconds: try data.decodeTwoByteInteger())
|
|
|
|
self.measurementInterval = try data.decodeTwoByteInteger()
|
|
|
|
self.numberOfStoredMeasurements = try data.decodeTwoByteInteger()
|
|
|
|
self.totalNumberOfMeasurements = try data.decodeFourByteInteger()
|
|
|
|
self.transferBlockSize = try data.decodeTwoByteInteger()
|
|
|
|
self.storageSize = try data.decodeTwoByteInteger()
|
|
|
|
let secondsSincePowerOn = try data.decodeFourByteInteger()
|
2023-06-08 09:52:20 +02:00
|
|
|
self.numberOfSecondsRunning = secondsSincePowerOn
|
2023-06-08 14:57:40 +02:00
|
|
|
let deviceStartTimeSeconds = try data.decodeFourByteInteger()
|
|
|
|
self.sensor0 = try data.decodeSensor()
|
|
|
|
self.sensor1 = try data.decodeSensor()
|
|
|
|
|
2023-06-08 09:52:20 +02:00
|
|
|
if deviceStartTimeSeconds != 0 {
|
|
|
|
self.hasDeviceStartTimeSet = true
|
|
|
|
self.deviceStartTime = Date(seconds: deviceStartTimeSeconds)
|
2023-06-08 14:57:40 +02:00
|
|
|
|
2023-06-08 09:52:20 +02:00
|
|
|
} else {
|
|
|
|
self.hasDeviceStartTimeSet = false
|
2023-06-08 14:57:40 +02:00
|
|
|
self.deviceStartTime = Date(seconds: date.seconds - secondsSincePowerOn) // Round to nearest second
|
2023-06-08 09:52:20 +02:00
|
|
|
}
|
2023-06-03 08:15:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DeviceInfo {
|
|
|
|
|
|
|
|
static var mock: DeviceInfo {
|
|
|
|
.init(
|
|
|
|
receivedDate: Date(),
|
|
|
|
numberOfRecordedBytes: 123,
|
2023-06-08 14:57:40 +02:00
|
|
|
numberOfStoredMeasurements: 234,
|
|
|
|
totalNumberOfMeasurements: 345,
|
2023-06-03 08:15:00 +02:00
|
|
|
measurementInterval: 60,
|
|
|
|
nextMeasurement: .now.addingTimeInterval(5),
|
|
|
|
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-08 09:52:20 +02:00
|
|
|
numberOfSecondsRunning: 20,
|
2023-06-08 14:57:40 +02:00
|
|
|
deviceStartTime: .now.addingTimeInterval(-20755),
|
|
|
|
hasDeviceStartTimeSet: true,
|
2023-06-08 09:52:20 +02:00
|
|
|
storageSize: 10000,
|
|
|
|
transferBlockSize: 180)
|
2023-06-03 08:15:00 +02:00
|
|
|
}
|
|
|
|
}
|