Allow deletion of measurements
This commit is contained in:
parent
f731927dcd
commit
01a3aac91b
@ -18,6 +18,7 @@
|
|||||||
88404DE32A31F20E00D30244 /* Int+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88404DE22A31F20E00D30244 /* Int+Extensions.swift */; };
|
88404DE32A31F20E00D30244 /* Int+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88404DE22A31F20E00D30244 /* Int+Extensions.swift */; };
|
||||||
88404DE52A31F23E00D30244 /* UInt16+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88404DE42A31F23E00D30244 /* UInt16+Extensions.swift */; };
|
88404DE52A31F23E00D30244 /* UInt16+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88404DE42A31F23E00D30244 /* UInt16+Extensions.swift */; };
|
||||||
88404DE92A31F7D500D30244 /* Data+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88404DE82A31F7D500D30244 /* Data+Extensions.swift */; };
|
88404DE92A31F7D500D30244 /* Data+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88404DE82A31F7D500D30244 /* Data+Extensions.swift */; };
|
||||||
|
88404DEB2A37BE3000D30244 /* DeviceWakeCause.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88404DEA2A37BE3000D30244 /* DeviceWakeCause.swift */; };
|
||||||
88CDE04F2A2508E900114294 /* TempTrackApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88CDE04E2A2508E900114294 /* TempTrackApp.swift */; };
|
88CDE04F2A2508E900114294 /* TempTrackApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88CDE04E2A2508E900114294 /* TempTrackApp.swift */; };
|
||||||
88CDE0512A2508E900114294 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88CDE0502A2508E900114294 /* ContentView.swift */; };
|
88CDE0512A2508E900114294 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88CDE0502A2508E900114294 /* ContentView.swift */; };
|
||||||
88CDE0532A2508EA00114294 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 88CDE0522A2508EA00114294 /* Assets.xcassets */; };
|
88CDE0532A2508EA00114294 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 88CDE0522A2508EA00114294 /* Assets.xcassets */; };
|
||||||
@ -50,6 +51,7 @@
|
|||||||
88404DE22A31F20E00D30244 /* Int+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Int+Extensions.swift"; sourceTree = "<group>"; };
|
88404DE22A31F20E00D30244 /* Int+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Int+Extensions.swift"; sourceTree = "<group>"; };
|
||||||
88404DE42A31F23E00D30244 /* UInt16+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UInt16+Extensions.swift"; sourceTree = "<group>"; };
|
88404DE42A31F23E00D30244 /* UInt16+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UInt16+Extensions.swift"; sourceTree = "<group>"; };
|
||||||
88404DE82A31F7D500D30244 /* Data+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Extensions.swift"; sourceTree = "<group>"; };
|
88404DE82A31F7D500D30244 /* Data+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Extensions.swift"; sourceTree = "<group>"; };
|
||||||
|
88404DEA2A37BE3000D30244 /* DeviceWakeCause.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceWakeCause.swift; sourceTree = "<group>"; };
|
||||||
88CDE04B2A2508E900114294 /* TempTrack.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TempTrack.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
88CDE04B2A2508E900114294 /* TempTrack.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TempTrack.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||||
88CDE04E2A2508E900114294 /* TempTrackApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TempTrackApp.swift; sourceTree = "<group>"; };
|
88CDE04E2A2508E900114294 /* TempTrackApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TempTrackApp.swift; sourceTree = "<group>"; };
|
||||||
88CDE0502A2508E900114294 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
|
88CDE0502A2508E900114294 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
|
||||||
@ -154,6 +156,7 @@
|
|||||||
88CDE0732A28AEE500114294 /* DeviceManagerDelegate.swift */,
|
88CDE0732A28AEE500114294 /* DeviceManagerDelegate.swift */,
|
||||||
88CDE05E2A250F5200114294 /* DeviceState.swift */,
|
88CDE05E2A250F5200114294 /* DeviceState.swift */,
|
||||||
88CDE06C2A28A92000114294 /* DeviceInfo.swift */,
|
88CDE06C2A28A92000114294 /* DeviceInfo.swift */,
|
||||||
|
88404DEA2A37BE3000D30244 /* DeviceWakeCause.swift */,
|
||||||
);
|
);
|
||||||
path = Bluetooth;
|
path = Bluetooth;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@ -283,6 +286,7 @@
|
|||||||
E253A9222A2B39B700EC6B28 /* Color+Extensions.swift in Sources */,
|
E253A9222A2B39B700EC6B28 /* Color+Extensions.swift in Sources */,
|
||||||
88CDE0612A25108100114294 /* BluetoothClient.swift in Sources */,
|
88CDE0612A25108100114294 /* BluetoothClient.swift in Sources */,
|
||||||
88CDE0742A28AEE500114294 /* DeviceManagerDelegate.swift in Sources */,
|
88CDE0742A28AEE500114294 /* DeviceManagerDelegate.swift in Sources */,
|
||||||
|
88404DEB2A37BE3000D30244 /* DeviceWakeCause.swift in Sources */,
|
||||||
88CDE06D2A28A92000114294 /* DeviceInfo.swift in Sources */,
|
88CDE06D2A28A92000114294 /* DeviceInfo.swift in Sources */,
|
||||||
88404DD82A2F381B00D30244 /* HistoryList.swift in Sources */,
|
88404DD82A2F381B00D30244 /* HistoryList.swift in Sources */,
|
||||||
88CDE07E2A28AFF400114294 /* DeviceInfoView.swift in Sources */,
|
88CDE07E2A28AFF400114294 /* DeviceInfoView.swift in Sources */,
|
||||||
|
@ -14,8 +14,8 @@
|
|||||||
"kind" : "remoteSourceControl",
|
"kind" : "remoteSourceControl",
|
||||||
"location" : "https://github.com/SFSafeSymbols/SFSafeSymbols",
|
"location" : "https://github.com/SFSafeSymbols/SFSafeSymbols",
|
||||||
"state" : {
|
"state" : {
|
||||||
"revision" : "7cca2d60925876b5953a2cf7341cd80fbeac983c",
|
"revision" : "2bcd249b49178247e6b52bac7d67d6e338a40cee",
|
||||||
"version" : "4.1.1"
|
"version" : "4.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
<key>TempTrack.xcscheme_^#shared#^_</key>
|
<key>TempTrack.xcscheme_^#shared#^_</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>orderHint</key>
|
<key>orderHint</key>
|
||||||
<integer>1</integer>
|
<integer>0</integer>
|
||||||
</dict>
|
</dict>
|
||||||
</dict>
|
</dict>
|
||||||
</dict>
|
</dict>
|
||||||
|
@ -24,7 +24,7 @@ final class BluetoothClient: ObservableObject {
|
|||||||
@Published
|
@Published
|
||||||
private(set) var deviceState: DeviceState = .disconnected {
|
private(set) var deviceState: DeviceState = .disconnected {
|
||||||
didSet {
|
didSet {
|
||||||
print("State: \(deviceState.text)")
|
print("State: \(deviceState)")
|
||||||
if case .configured = deviceState {
|
if case .configured = deviceState {
|
||||||
startRegularUpdates()
|
startRegularUpdates()
|
||||||
} else {
|
} else {
|
||||||
@ -38,6 +38,11 @@ final class BluetoothClient: ObservableObject {
|
|||||||
didSet {
|
didSet {
|
||||||
updateDeviceTimeIfNeeded()
|
updateDeviceTimeIfNeeded()
|
||||||
collectRecordedData()
|
collectRecordedData()
|
||||||
|
if let deviceInfo, let runningTransfer {
|
||||||
|
runningTransfer.update(info: deviceInfo)
|
||||||
|
let next = runningTransfer.nextRequest()
|
||||||
|
addRequest(next)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +100,6 @@ final class BluetoothClient: ObservableObject {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
let next = openRequests.removeFirst()
|
let next = openRequests.removeFirst()
|
||||||
//print("Starting request \(next)")
|
|
||||||
|
|
||||||
guard connection.send(next.serialized) else {
|
guard connection.send(next.serialized) else {
|
||||||
print("Failed to start request \(next)")
|
print("Failed to start request \(next)")
|
||||||
@ -106,10 +110,20 @@ final class BluetoothClient: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addRequest(_ request: BluetoothRequest) {
|
func addRequest(_ request: BluetoothRequest) {
|
||||||
// TODO: Check if request already exists
|
defer {
|
||||||
openRequests.append(request)
|
|
||||||
performNextRequest()
|
performNextRequest()
|
||||||
}
|
}
|
||||||
|
let type = request.byte
|
||||||
|
if let runningRequest, runningRequest.byte == type {
|
||||||
|
print("Skipping duplicate request \(request)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard !openRequests.contains(where: { $0.byte == type }) else {
|
||||||
|
print("Skipping duplicate request \(request)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
openRequests.append(request)
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: Device time
|
// MARK: Device time
|
||||||
|
|
||||||
@ -134,12 +148,15 @@ final class BluetoothClient: ObservableObject {
|
|||||||
@discardableResult
|
@discardableResult
|
||||||
func collectRecordedData() -> Bool {
|
func collectRecordedData() -> Bool {
|
||||||
guard runningTransfer == nil else {
|
guard runningTransfer == nil else {
|
||||||
|
print("Transfer already running")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
guard !openRequests.contains(where: { if case .getRecordingData = $0 { return true }; return false }) else {
|
guard !openRequests.contains(where: { if case .getRecordingData = $0 { return true }; return false }) else {
|
||||||
|
print("Transfer already in scheduled")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
guard let info = deviceInfo else {
|
guard let info = deviceInfo else {
|
||||||
|
print("No device info to start transfer")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
guard info.numberOfStoredMeasurements > 0 else {
|
guard info.numberOfStoredMeasurements > 0 else {
|
||||||
@ -149,38 +166,27 @@ final class BluetoothClient: ObservableObject {
|
|||||||
let transfer = TemperatureDataTransfer(info: info)
|
let transfer = TemperatureDataTransfer(info: info)
|
||||||
runningTransfer = transfer
|
runningTransfer = transfer
|
||||||
let next = transfer.nextRequest()
|
let next = transfer.nextRequest()
|
||||||
|
print("Starting transfer")
|
||||||
addRequest(next)
|
addRequest(next)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private func didReceive(data: Data, offset: Int, count: Int) {
|
private func didReceive(data: Data, offset: Int, count: Int) {
|
||||||
guard let runningTransfer else {
|
guard let runningTransfer else {
|
||||||
|
print("No running transfer to process device data")
|
||||||
return // TODO: Start new transfer?
|
return // TODO: Start new transfer?
|
||||||
}
|
}
|
||||||
runningTransfer.add(data: data, offset: offset, count: count)
|
runningTransfer.add(data: data, offset: offset, count: count)
|
||||||
continueTransfer()
|
|
||||||
}
|
|
||||||
|
|
||||||
private func continueTransfer() {
|
|
||||||
guard let runningTransfer else {
|
|
||||||
return // TODO: Start new transfer?
|
|
||||||
}
|
|
||||||
let next = runningTransfer.nextRequest()
|
let next = runningTransfer.nextRequest()
|
||||||
addRequest(next)
|
addRequest(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func decode(info: Data) {
|
private func decode(info: Data) {
|
||||||
guard let newInfo = try? DeviceInfo(info: info) else {
|
guard let newInfo = try? DeviceInfo(info: info) else {
|
||||||
|
print("Failed to decode device info")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
self.deviceInfo = newInfo
|
self.deviceInfo = newInfo
|
||||||
if let runningTransfer {
|
|
||||||
runningTransfer.update(info: newInfo)
|
|
||||||
let next = runningTransfer.nextRequest()
|
|
||||||
addRequest(next)
|
|
||||||
} else if newInfo.numberOfStoredMeasurements > 0 {
|
|
||||||
collectRecordedData()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,15 +215,31 @@ extension BluetoothClient: DeviceManagerDelegate {
|
|||||||
case .success:
|
case .success:
|
||||||
break
|
break
|
||||||
case .responseInProgress:
|
case .responseInProgress:
|
||||||
|
print("Device is busy for \(runningRequest)")
|
||||||
// Retry the request
|
// Retry the request
|
||||||
addRequest(runningRequest)
|
addRequest(runningRequest)
|
||||||
return
|
return
|
||||||
|
case .invalidNumberOfBytesToDelete:
|
||||||
|
guard case .clearRecordingBuffer = runningRequest else {
|
||||||
|
// If clearing the recording buffer fails due to byte mismatch,
|
||||||
|
// then requesting new info will resolve the mismatch, and the transfer will be resumed
|
||||||
|
addRequest(.getInfo)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
print("Request \(runningRequest) received non-matching responde about number of bytes to delete")
|
||||||
|
case .responseTooLarge:
|
||||||
|
guard case .getRecordingData = runningRequest else {
|
||||||
|
// If requesting bytes fails due to the response size,
|
||||||
|
// then requesting new info will update the response size, and the transfer will be resumed
|
||||||
|
addRequest(.getInfo)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
print("Unexpectedly exceeded payload size for request \(runningRequest)")
|
||||||
default:
|
default:
|
||||||
print("Unknown response \(data[0]) for request \(runningRequest)")
|
print("Unknown response \(data[0]) for request \(runningRequest)")
|
||||||
// If clearing the recording buffer fails due to byte mismatch,
|
// If clearing the recording buffer fails due to byte mismatch,
|
||||||
// then requesting new info will resolve the mismatch, and the transfer will be resumed
|
// then requesting new info will resolve the mismatch, and the transfer will be resumed
|
||||||
// If requesting bytes fails due to the response size,
|
|
||||||
// then requesting new info will update the response size, and the transfer will be resumed
|
|
||||||
addRequest(.getInfo)
|
addRequest(.getInfo)
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -240,6 +262,7 @@ extension BluetoothClient: DeviceManagerDelegate {
|
|||||||
|
|
||||||
private func didClearDeviceStorage() {
|
private func didClearDeviceStorage() {
|
||||||
guard let runningTransfer else {
|
guard let runningTransfer else {
|
||||||
|
print("No running transfer after clearing device storage")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
runningTransfer.completeTransfer()
|
runningTransfer.completeTransfer()
|
||||||
|
@ -33,6 +33,8 @@ struct DeviceInfo {
|
|||||||
|
|
||||||
let hasDeviceStartTimeSet: Bool
|
let hasDeviceStartTimeSet: Bool
|
||||||
|
|
||||||
|
let wakeupReason: DeviceWakeCause
|
||||||
|
|
||||||
// MARK: Storage
|
// MARK: Storage
|
||||||
|
|
||||||
let storageSize: Int
|
let storageSize: Int
|
||||||
@ -74,6 +76,7 @@ extension DeviceInfo {
|
|||||||
let deviceStartTimeSeconds = try data.decodeFourByteInteger()
|
let deviceStartTimeSeconds = try data.decodeFourByteInteger()
|
||||||
self.sensor0 = try data.decodeSensor()
|
self.sensor0 = try data.decodeSensor()
|
||||||
self.sensor1 = try data.decodeSensor()
|
self.sensor1 = try data.decodeSensor()
|
||||||
|
self.wakeupReason = .init(rawValue: try data.getByte()) ?? .WAKEUP_UNDEFINED
|
||||||
|
|
||||||
if deviceStartTimeSeconds != 0 {
|
if deviceStartTimeSeconds != 0 {
|
||||||
self.hasDeviceStartTimeSet = true
|
self.hasDeviceStartTimeSet = true
|
||||||
@ -101,6 +104,7 @@ extension DeviceInfo {
|
|||||||
numberOfSecondsRunning: 20,
|
numberOfSecondsRunning: 20,
|
||||||
deviceStartTime: .now.addingTimeInterval(-20755),
|
deviceStartTime: .now.addingTimeInterval(-20755),
|
||||||
hasDeviceStartTimeSet: true,
|
hasDeviceStartTimeSet: true,
|
||||||
|
wakeupReason: .WAKEUP_EXT0,
|
||||||
storageSize: 10000,
|
storageSize: 10000,
|
||||||
transferBlockSize: 180)
|
transferBlockSize: 180)
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,6 @@ final class DeviceManager: NSObject, CBCentralManagerDelegate {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
device.writeValue(data, for: characteristic, type: .withResponse)
|
device.writeValue(data, for: characteristic, type: .withResponse)
|
||||||
//DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) { }
|
|
||||||
return self.read()
|
return self.read()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,27 +26,21 @@ enum DeviceState {
|
|||||||
case .bluetoothEnabled:
|
case .bluetoothEnabled:
|
||||||
return "Bluetooth enabled"
|
return "Bluetooth enabled"
|
||||||
case .scanning:
|
case .scanning:
|
||||||
return "Scanning for devices..."
|
return "Scanning..."
|
||||||
case .connecting(let device):
|
case .connecting(let device):
|
||||||
guard let name = device.name else {
|
guard let name = device.name else {
|
||||||
return "Connecting to device..."
|
return "Connecting..."
|
||||||
}
|
}
|
||||||
return "Connecting to \(name)..."
|
return "Connecting to \(name)..."
|
||||||
case .discoveringServices(let device):
|
case .discoveringServices:
|
||||||
guard let name = device.name else {
|
return "Discovering service..."
|
||||||
return "Setting up device..."
|
case .discoveringCharacteristic:
|
||||||
}
|
return "Discovering characteristic..."
|
||||||
return "Setting up \(name)..."
|
|
||||||
case .discoveringCharacteristic(let device):
|
|
||||||
guard let name = device.name else {
|
|
||||||
return "Setting up device..."
|
|
||||||
}
|
|
||||||
return "Setting up \(name)..."
|
|
||||||
case .configured(let device, _):
|
case .configured(let device, _):
|
||||||
guard let name = device.name else {
|
guard let name = device.name else {
|
||||||
return "Connected"
|
return "Connected"
|
||||||
}
|
}
|
||||||
return "Connected to \(name)"
|
return name
|
||||||
case .disconnected:
|
case .disconnected:
|
||||||
return "Not connected"
|
return "Not connected"
|
||||||
}
|
}
|
||||||
|
77
TempTrack/Bluetooth/DeviceWakeCause.swift
Normal file
77
TempTrack/Bluetooth/DeviceWakeCause.swift
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
enum DeviceWakeCause: UInt8 {
|
||||||
|
|
||||||
|
/// In case of deep sleep, reset was not caused by exit from deep sleep
|
||||||
|
case WAKEUP_UNDEFINED = 0
|
||||||
|
|
||||||
|
/// Not a wakeup cause, used to disable all wakeup sources with esp_sleep_disable_wakeup_source
|
||||||
|
case WAKEUP_ALL = 1
|
||||||
|
|
||||||
|
/// Wakeup caused by external signal using RTC_IO
|
||||||
|
case WAKEUP_EXT0 = 2
|
||||||
|
|
||||||
|
/// Wakeup caused by external signal using RTC_CNTL
|
||||||
|
case WAKEUP_EXT1 = 3
|
||||||
|
|
||||||
|
/// Wakeup caused by timer
|
||||||
|
case WAKEUP_TIMER = 4
|
||||||
|
|
||||||
|
/// Wakeup caused by touchpad
|
||||||
|
case WAKEUP_TOUCHPAD = 5
|
||||||
|
|
||||||
|
/// Wakeup caused by ULP program
|
||||||
|
case WAKEUP_ULP = 6
|
||||||
|
|
||||||
|
/// Wakeup caused by GPIO (light sleep only on ESP32, S2 and S3)
|
||||||
|
case WAKEUP_GPIO = 7
|
||||||
|
|
||||||
|
/// Wakeup caused by UART (light sleep only)
|
||||||
|
case WAKEUP_UART = 8
|
||||||
|
|
||||||
|
/// Wakeup caused by WIFI (light sleep only)
|
||||||
|
case WAKEUP_WIFI = 9
|
||||||
|
|
||||||
|
/// Wakeup caused by COCPU int
|
||||||
|
case WAKEUP_COCPU = 10
|
||||||
|
|
||||||
|
/// Wakeup caused by COCPU crash
|
||||||
|
case WAKEUP_COCPU_TRAP_TRIG = 11
|
||||||
|
|
||||||
|
/// Wakeup caused by BT (light sleep only)
|
||||||
|
case WAKEUP_BT = 12
|
||||||
|
}
|
||||||
|
|
||||||
|
extension DeviceWakeCause {
|
||||||
|
|
||||||
|
var text: String {
|
||||||
|
switch self {
|
||||||
|
case .WAKEUP_UNDEFINED:
|
||||||
|
return "Power On"
|
||||||
|
case .WAKEUP_ALL:
|
||||||
|
return ""
|
||||||
|
case .WAKEUP_EXT0:
|
||||||
|
return "Button"
|
||||||
|
case .WAKEUP_EXT1:
|
||||||
|
return "EXT1"
|
||||||
|
case .WAKEUP_TIMER:
|
||||||
|
return "Timer"
|
||||||
|
case .WAKEUP_TOUCHPAD:
|
||||||
|
return "Touch"
|
||||||
|
case .WAKEUP_ULP:
|
||||||
|
return "ELP"
|
||||||
|
case .WAKEUP_GPIO:
|
||||||
|
return "GPIO"
|
||||||
|
case .WAKEUP_UART:
|
||||||
|
return "UART"
|
||||||
|
case .WAKEUP_WIFI:
|
||||||
|
return "WiFi"
|
||||||
|
case .WAKEUP_COCPU:
|
||||||
|
return "CoCPU Interupt"
|
||||||
|
case .WAKEUP_COCPU_TRAP_TRIG:
|
||||||
|
return "CoCPU Crash"
|
||||||
|
case .WAKEUP_BT:
|
||||||
|
return "Bluetooth"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -126,7 +126,6 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
.padding()
|
.padding()
|
||||||
.sheet(isPresented: $showDeviceInfo) {
|
.sheet(isPresented: $showDeviceInfo) {
|
||||||
//.bottomSheet(isPresented: $showDeviceInfo, height: 650) {
|
|
||||||
if let info = bluetoothClient.deviceInfo {
|
if let info = bluetoothClient.deviceInfo {
|
||||||
DeviceInfoView(info: info, isPresented: $showDeviceInfo)
|
DeviceInfoView(info: info, isPresented: $showDeviceInfo)
|
||||||
} else {
|
} else {
|
||||||
|
@ -6,6 +6,13 @@ enum DeviceInfoError: Error {
|
|||||||
|
|
||||||
extension Data {
|
extension Data {
|
||||||
|
|
||||||
|
mutating func getByte() throws -> UInt8 {
|
||||||
|
guard count >= 1 else {
|
||||||
|
throw DeviceInfoError.missingData
|
||||||
|
}
|
||||||
|
return removeFirst()
|
||||||
|
}
|
||||||
|
|
||||||
mutating func decodeUInt16() throws -> UInt16 {
|
mutating func decodeUInt16() throws -> UInt16 {
|
||||||
guard count >= 2 else {
|
guard count >= 2 else {
|
||||||
throw DeviceInfoError.missingData
|
throw DeviceInfoError.missingData
|
||||||
@ -29,15 +36,4 @@ extension Data {
|
|||||||
let byte3 = removeFirst()
|
let byte3 = removeFirst()
|
||||||
return (Int(byte3) << 24) | (Int(byte2) << 16) | (Int(byte1) << 8) | Int(byte0)
|
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,32 @@ extension Date {
|
|||||||
self.init(timeIntervalSince1970: TimeInterval(seconds))
|
self.init(timeIntervalSince1970: TimeInterval(seconds))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var shortTimePassedText: String {
|
||||||
|
let secs = secondsToNow
|
||||||
|
guard secs > 1 else {
|
||||||
|
return "Now"
|
||||||
|
}
|
||||||
|
guard secs >= 60 else {
|
||||||
|
return "\(secs) s ago"
|
||||||
|
}
|
||||||
|
let minutes = secs / 60
|
||||||
|
guard minutes >= 60 else {
|
||||||
|
return "\(minutes) min ago"
|
||||||
|
}
|
||||||
|
let hours = minutes / 60
|
||||||
|
guard hours > 1 else {
|
||||||
|
return "1 hour ago"
|
||||||
|
}
|
||||||
|
guard hours >= 60 else {
|
||||||
|
return "\(hours) hours ago"
|
||||||
|
}
|
||||||
|
let days = hours / 24
|
||||||
|
guard days > 1 else {
|
||||||
|
return "1 day ago"
|
||||||
|
}
|
||||||
|
return "\(days) days ago"
|
||||||
|
}
|
||||||
|
|
||||||
var timePassedText: String {
|
var timePassedText: String {
|
||||||
let secs = secondsToNow
|
let secs = secondsToNow
|
||||||
guard secs > 1 else {
|
guard secs > 1 else {
|
||||||
|
@ -83,6 +83,10 @@ final class TemperatureStorage: ObservableObject {
|
|||||||
String(format: "%08d.bin", index)
|
String(format: "%08d.bin", index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func fileUrl(for dateIndex: Int) -> URL {
|
||||||
|
storageFolder.appendingPathComponent(fileName(for: dateIndex))
|
||||||
|
}
|
||||||
|
|
||||||
private func fileUrl(for fileName: String) -> URL {
|
private func fileUrl(for fileName: String) -> URL {
|
||||||
storageFolder.appendingPathComponent(fileName)
|
storageFolder.appendingPathComponent(fileName)
|
||||||
}
|
}
|
||||||
@ -148,6 +152,21 @@ final class TemperatureStorage: ObservableObject {
|
|||||||
updateLastMeasurements(measurements)
|
updateLastMeasurements(measurements)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func removeMeasurements(for dateIndex: Int) {
|
||||||
|
let fileUrl = fileUrl(for: dateIndex)
|
||||||
|
guard fm.fileExists(atPath: fileUrl.path) else {
|
||||||
|
print("No measurements for \(fileUrl.lastPathComponent)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
try fm.removeItem(at: fileUrl)
|
||||||
|
dailyMeasurementCounts = dailyMeasurementCounts.filter { $0.dateIndex != dateIndex }
|
||||||
|
recentMeasurements = recentMeasurements.filter { $0.date.dateIndex != dateIndex }
|
||||||
|
} catch {
|
||||||
|
print("Failed to delete \(fileUrl.lastPathComponent): \(error)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
- Returns: The number of new points
|
- Returns: The number of new points
|
||||||
*/
|
*/
|
||||||
@ -245,7 +264,7 @@ final class TemperatureStorage: ObservableObject {
|
|||||||
private extension Array where Element == TemperatureMeasurement {
|
private extension Array where Element == TemperatureMeasurement {
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
mutating func insert(_ measurement: TemperatureMeasurement) -> Bool {
|
mutating func insertIntoSorted(_ measurement: TemperatureMeasurement) -> Bool {
|
||||||
guard !contains(measurement) else {
|
guard !contains(measurement) else {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,10 @@ final class TemperatureDataTransfer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func add(data: Data, offset: Int, count: Int) {
|
func add(data: Data, offset: Int, count: Int) {
|
||||||
|
guard currentByteIndex == offset else {
|
||||||
|
print("Discarding \(data.count) bytes at offset \(offset), expected \(currentByteIndex)")
|
||||||
|
return
|
||||||
|
}
|
||||||
dataBuffer.append(data)
|
dataBuffer.append(data)
|
||||||
currentByteIndex += data.count
|
currentByteIndex += data.count
|
||||||
processBytes()
|
processBytes()
|
||||||
|
@ -28,7 +28,7 @@ struct TemperatureSensor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var updateText: String {
|
var updateText: String {
|
||||||
date.timePassedText
|
date.shortTimePassedText
|
||||||
}
|
}
|
||||||
|
|
||||||
static func temperatureIcon(_ temp: Double?) -> SFSymbol {
|
static func temperatureIcon(_ temp: Double?) -> SFSymbol {
|
||||||
@ -60,3 +60,17 @@ extension TemperatureSensor {
|
|||||||
self.date = Date().addingTimeInterval(-TimeInterval(secondsAgo))
|
self.date = Date().addingTimeInterval(-TimeInterval(secondsAgo))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Data {
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -21,14 +21,14 @@ struct DeviceInfoView: View {
|
|||||||
private var runTimeString: String {
|
private var runTimeString: String {
|
||||||
let number = info.numberOfSecondsRunning
|
let number = info.numberOfSecondsRunning
|
||||||
guard number >= 60 else {
|
guard number >= 60 else {
|
||||||
return "\(number) seconds"
|
return "\(number) s"
|
||||||
}
|
}
|
||||||
let minutes = number / 60
|
let minutes = number / 60
|
||||||
guard minutes > 1 else {
|
guard minutes > 1 else {
|
||||||
return "1 minute"
|
return "1 min"
|
||||||
}
|
}
|
||||||
guard minutes >= 60 else {
|
guard minutes >= 60 else {
|
||||||
return "\(minutes) minutes"
|
return "\(minutes) min"
|
||||||
}
|
}
|
||||||
let hours = minutes / 60
|
let hours = minutes / 60
|
||||||
guard hours > 1 else {
|
guard hours > 1 else {
|
||||||
@ -49,7 +49,7 @@ struct DeviceInfoView: View {
|
|||||||
guard secs > 1 else {
|
guard secs > 1 else {
|
||||||
return "Now"
|
return "Now"
|
||||||
}
|
}
|
||||||
return "In \(secs) seconds"
|
return "In \(secs) s"
|
||||||
}
|
}
|
||||||
|
|
||||||
private var storageIcon: SFSymbol {
|
private var storageIcon: SFSymbol {
|
||||||
@ -74,13 +74,7 @@ struct DeviceInfoView: View {
|
|||||||
HStack {
|
HStack {
|
||||||
Image(systemSymbol: sensor.temperatureIcon)
|
Image(systemSymbol: sensor.temperatureIcon)
|
||||||
.frame(width: 30)
|
.frame(width: 30)
|
||||||
Text(sensor.temperatureText)
|
Text("\(sensor.temperatureText) (\(sensor.updateText))")
|
||||||
}
|
|
||||||
HStack {
|
|
||||||
Image(systemSymbol: .arrowTriangle2Circlepath)
|
|
||||||
.frame(width: 30)
|
|
||||||
Text(sensor.updateText)
|
|
||||||
Spacer()
|
|
||||||
}
|
}
|
||||||
HStack {
|
HStack {
|
||||||
Image(systemSymbol: .tag)
|
Image(systemSymbol: .tag)
|
||||||
@ -112,41 +106,40 @@ struct DeviceInfoView: View {
|
|||||||
guard abs(offset) > 1 else {
|
guard abs(offset) > 1 else {
|
||||||
return "No clock offset"
|
return "No clock offset"
|
||||||
}
|
}
|
||||||
return "Offset: \(offset) seconds"
|
return "Offset: \(offset) s"
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationView {
|
NavigationView {
|
||||||
VStack(alignment: .leading, spacing: 5) {
|
VStack(alignment: .leading, spacing: 5) {
|
||||||
VStack(alignment: .leading, spacing: 5) {
|
VStack(alignment: .leading, spacing: 5) {
|
||||||
Text("Recording")
|
Text("System")
|
||||||
.font(.headline)
|
.font(.headline)
|
||||||
HStack {
|
HStack {
|
||||||
Image(systemSymbol: .power)
|
Image(systemSymbol: .power)
|
||||||
.frame(width: 30)
|
.frame(width: 30)
|
||||||
Text(df.string(from: info.deviceStartTime))
|
Text("\(df.string(from: info.deviceStartTime)) (\(runTimeString))")
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
HStack {
|
|
||||||
Image(systemSymbol: .clock)
|
|
||||||
.frame(width: 30)
|
|
||||||
Text("\(runTimeString)")
|
|
||||||
}
|
|
||||||
HStack {
|
HStack {
|
||||||
Image(systemSymbol: .clockBadgeExclamationmark)
|
Image(systemSymbol: .clockBadgeExclamationmark)
|
||||||
.frame(width: 30)
|
.frame(width: 30)
|
||||||
Text(clockOffsetText)
|
Text(clockOffsetText)
|
||||||
}
|
}
|
||||||
HStack {
|
HStack {
|
||||||
Image(systemSymbol: .stopwatch)
|
Image(systemSymbol: .autostartstop)
|
||||||
.frame(width: 30)
|
.frame(width: 30)
|
||||||
Text("Every \(info.measurementInterval) seconds")
|
Text("Wakeup: \(info.wakeupReason.text)")
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
VStack(alignment: .leading, spacing: 5) {
|
||||||
|
Text("Recording")
|
||||||
|
.font(.headline)
|
||||||
HStack {
|
HStack {
|
||||||
Image(systemSymbol: .arrowTriangle2Circlepath)
|
Image(systemSymbol: .stopwatch)
|
||||||
.frame(width: 30)
|
.frame(width: 30)
|
||||||
Text(nextUpdateText)
|
Text("\(nextUpdateText) (Every \(info.measurementInterval) s)")
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -192,7 +185,6 @@ struct DeviceInfoView: View {
|
|||||||
struct DeviceInfoView_Previews: PreviewProvider {
|
struct DeviceInfoView_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
DeviceInfoView(info: .mock, isPresented: .constant(true))
|
DeviceInfoView(info: .mock, isPresented: .constant(true))
|
||||||
.previewLayout(.fixed(width: 375, height: 650))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,12 +12,24 @@ struct HistoryList: View {
|
|||||||
TemperatureDayOverview(storage: storage, dateIndex: day.dateIndex)
|
TemperatureDayOverview(storage: storage, dateIndex: day.dateIndex)
|
||||||
}) {
|
}) {
|
||||||
HistoryListRow(entry: day)
|
HistoryListRow(entry: day)
|
||||||
|
.swipeActions(edge: .trailing, allowsFullSwipe: true) {
|
||||||
|
Button {
|
||||||
|
deleteRow(for: day.dateIndex)
|
||||||
|
} label: {
|
||||||
|
Label("Delete", systemSymbol: .pencil)
|
||||||
|
}
|
||||||
|
.tint(.purple)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.navigationTitle("History")
|
.navigationTitle("History")
|
||||||
.navigationBarTitleDisplayMode(.large)
|
.navigationBarTitleDisplayMode(.large)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func deleteRow(for dateIndex: Int) {
|
||||||
|
storage.removeMeasurements(for: dateIndex)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HistoryList_Previews: PreviewProvider {
|
struct HistoryList_Previews: PreviewProvider {
|
||||||
|
Loading…
Reference in New Issue
Block a user