diff --git a/Sources/CombustionBLE/AdvertisingData.swift b/Sources/CombustionBLE/BleData/AdvertisingData.swift similarity index 74% rename from Sources/CombustionBLE/AdvertisingData.swift rename to Sources/CombustionBLE/BleData/AdvertisingData.swift index dd591cd..b896f88 100644 --- a/Sources/CombustionBLE/AdvertisingData.swift +++ b/Sources/CombustionBLE/BleData/AdvertisingData.swift @@ -33,54 +33,6 @@ public enum CombustionProductType: UInt8 { case NODE = 0x02 } -/// Probe colors -public enum ProbeColor: UInt8, CaseIterable { - case COLOR1 = 0x00 - case COLOR2 = 0x01 - case COLOR3 = 0x02 - case COLOR4 = 0x03 - case COLOR5 = 0x04 - case COLOR6 = 0x05 - case COLOR7 = 0x06 - case COLOR8 = 0x07 - - private enum Constants { - static let PRODE_COLOR_MASK: UInt8 = 0x7 - static let PRODE_COLOR_SHIFT: UInt8 = 2 - } - - static func fromRawData(data: Data) -> ProbeColor { - let modeIdColorBytes = [UInt8](data) - - let rawProbeColor = (modeIdColorBytes[0] & (Constants.PRODE_COLOR_MASK << Constants.PRODE_COLOR_SHIFT)) >> Constants.PRODE_COLOR_SHIFT - return ProbeColor(rawValue: rawProbeColor) ?? .COLOR1 - } -} - -/// Probe IDs -public enum ProbeID: UInt8, CaseIterable { - case ID1 = 0x00 - case ID2 = 0x01 - case ID3 = 0x02 - case ID4 = 0x03 - case ID5 = 0x04 - case ID6 = 0x05 - case ID7 = 0x06 - case ID8 = 0x07 - - private enum Constants { - static let PRODE_ID_MASK: UInt8 = 0x7 - static let PRODE_ID_SHIFT: UInt8 = 5 - } - - static func fromRawData(data: Data) -> ProbeID { - let modeIdColorBytes = [UInt8](data) - - let rawProbeID = (modeIdColorBytes[0] & (Constants.PRODE_ID_MASK << Constants.PRODE_ID_SHIFT)) >> Constants.PRODE_ID_SHIFT - return ProbeID(rawValue: rawProbeID) ?? .ID1 - } -} - /// Struct containing advertising data received from device. public struct AdvertisingData { @@ -94,6 +46,8 @@ public struct AdvertisingData { public let id: ProbeID /// Probe Color public let color: ProbeColor + /// Probe mode + public let mode: ProbeMode private enum Constants { // Locations of data in advertising packets @@ -140,10 +94,12 @@ extension AdvertisingData { let modeIdColorData = data.subdata(in: Constants.MODE_COLOR_ID_RANGE) id = ProbeID.fromRawData(data: modeIdColorData) color = ProbeColor.fromRawData(data: modeIdColorData) + mode = ProbeMode.fromRawData(data: modeIdColorData) } else { id = .ID1 color = .COLOR1 + mode = .Normal } } } @@ -157,6 +113,7 @@ extension AdvertisingData { serialNumber = fakeSerial id = .ID1 color = .COLOR1 + mode = .Normal } // Fake data initializer for Simulated Probe @@ -166,5 +123,6 @@ extension AdvertisingData { serialNumber = fakeSerial id = .ID1 color = .COLOR1 + mode = .Normal } } diff --git a/Sources/CombustionBLE/DeviceStatus.swift b/Sources/CombustionBLE/BleData/DeviceStatus.swift similarity index 95% rename from Sources/CombustionBLE/DeviceStatus.swift rename to Sources/CombustionBLE/BleData/DeviceStatus.swift index 527b36e..73b997a 100644 --- a/Sources/CombustionBLE/DeviceStatus.swift +++ b/Sources/CombustionBLE/BleData/DeviceStatus.swift @@ -38,6 +38,8 @@ public struct DeviceStatus { public let id: ProbeID /// Probe Color public let color: ProbeColor + /// Probe mode + public let mode: ProbeMode private enum Constants { // Locations of data in status packet @@ -71,10 +73,12 @@ extension DeviceStatus { let modeIdColorData = data.subdata(in: Constants.MODE_COLOR_ID_RANGE) id = ProbeID.fromRawData(data: modeIdColorData) color = ProbeColor.fromRawData(data: modeIdColorData) + mode = ProbeMode.fromRawData(data: modeIdColorData) } else { id = .ID1 color = .COLOR1 + mode = .Normal } } } diff --git a/Sources/CombustionBLE/BleData/ProbeColor.swift b/Sources/CombustionBLE/BleData/ProbeColor.swift new file mode 100644 index 0000000..860ed3d --- /dev/null +++ b/Sources/CombustionBLE/BleData/ProbeColor.swift @@ -0,0 +1,50 @@ +// ProbeColor.swift + +/*-- +MIT License + +Copyright (c) 2021 Combustion Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +--*/ + +import Foundation + +public enum ProbeColor: UInt8, CaseIterable { + case COLOR1 = 0x00 + case COLOR2 = 0x01 + case COLOR3 = 0x02 + case COLOR4 = 0x03 + case COLOR5 = 0x04 + case COLOR6 = 0x05 + case COLOR7 = 0x06 + case COLOR8 = 0x07 + + private enum Constants { + static let PRODE_COLOR_MASK: UInt8 = 0x7 + static let PRODE_COLOR_SHIFT: UInt8 = 2 + } + + static func fromRawData(data: Data) -> ProbeColor { + let modeIdColorBytes = [UInt8](data) + + let rawProbeColor = (modeIdColorBytes[0] & (Constants.PRODE_COLOR_MASK << Constants.PRODE_COLOR_SHIFT)) >> Constants.PRODE_COLOR_SHIFT + return ProbeColor(rawValue: rawProbeColor) ?? .COLOR1 + } +} diff --git a/Sources/CombustionBLE/BleData/ProbeID.swift b/Sources/CombustionBLE/BleData/ProbeID.swift new file mode 100644 index 0000000..742ad93 --- /dev/null +++ b/Sources/CombustionBLE/BleData/ProbeID.swift @@ -0,0 +1,50 @@ +// ProbeID.swift + +/*-- +MIT License + +Copyright (c) 2021 Combustion Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +--*/ + +import Foundation + +public enum ProbeID: UInt8, CaseIterable { + case ID1 = 0x00 + case ID2 = 0x01 + case ID3 = 0x02 + case ID4 = 0x03 + case ID5 = 0x04 + case ID6 = 0x05 + case ID7 = 0x06 + case ID8 = 0x07 + + private enum Constants { + static let PRODE_ID_MASK: UInt8 = 0x7 + static let PRODE_ID_SHIFT: UInt8 = 5 + } + + static func fromRawData(data: Data) -> ProbeID { + let modeIdColorBytes = [UInt8](data) + + let rawProbeID = (modeIdColorBytes[0] & (Constants.PRODE_ID_MASK << Constants.PRODE_ID_SHIFT)) >> Constants.PRODE_ID_SHIFT + return ProbeID(rawValue: rawProbeID) ?? .ID1 + } +} diff --git a/Sources/CombustionBLE/BleData/ProbeMode.swift b/Sources/CombustionBLE/BleData/ProbeMode.swift new file mode 100644 index 0000000..ddb0728 --- /dev/null +++ b/Sources/CombustionBLE/BleData/ProbeMode.swift @@ -0,0 +1,45 @@ +// ProbeMode.swift + +/*-- +MIT License + +Copyright (c) 2021 Combustion Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +--*/ + +import Foundation + +public enum ProbeMode: UInt8, CaseIterable { + case Normal = 0x00 + case InstantRead = 0x01 + case Reserved = 0x02 + case Error = 0x03 + + private enum Constants { + static let PRODE_MODE_MASK: UInt8 = 0x3 + } + + static func fromRawData(data: Data) -> ProbeMode { + let modeIdColorBytes = [UInt8](data) + + let rawProbeID = modeIdColorBytes[0] & (Constants.PRODE_MODE_MASK) + return ProbeMode(rawValue: rawProbeID) ?? .Normal + } +} diff --git a/Sources/CombustionBLE/ProbeTemperatures.swift b/Sources/CombustionBLE/BleData/ProbeTemperatures.swift similarity index 100% rename from Sources/CombustionBLE/ProbeTemperatures.swift rename to Sources/CombustionBLE/BleData/ProbeTemperatures.swift diff --git a/Sources/CombustionBLE/Probe.swift b/Sources/CombustionBLE/Probe.swift index 9f6e375..77893d5 100644 --- a/Sources/CombustionBLE/Probe.swift +++ b/Sources/CombustionBLE/Probe.swift @@ -85,15 +85,18 @@ extension Probe { /// Updates the Device based on newly-received DeviceStatus message. Requests missing records. func updateProbeStatus(deviceStatus: DeviceStatus) { + currentTemperatures = deviceStatus.temperatures minSequenceNumber = deviceStatus.minSequenceNumber maxSequenceNumber = deviceStatus.maxSequenceNumber id = deviceStatus.id color = deviceStatus.color - // Log the temperature data point - temperatureLog.appendDataPoint(dataPoint: - LoggedProbeDataPoint.fromDeviceStatus(deviceStatus: - deviceStatus)) + // Log the temperature data point for "Normal" status updates + if(deviceStatus.mode == .Normal) { + temperatureLog.appendDataPoint(dataPoint: + LoggedProbeDataPoint.fromDeviceStatus(deviceStatus: + deviceStatus)) + } // Check for missing records if let missingSequence = temperatureLog.firstMissingIndex(sequenceRangeStart: deviceStatus.minSequenceNumber, diff --git a/Sources/CombustionBLE/SimulatedProbe.swift b/Sources/CombustionBLE/SimulatedProbe.swift index 7955424..3fa65f1 100644 --- a/Sources/CombustionBLE/SimulatedProbe.swift +++ b/Sources/CombustionBLE/SimulatedProbe.swift @@ -79,7 +79,8 @@ class SimulatedProbe: Probe { maxSequenceNumber: lastSequence, temperatures: ProbeTemperatures.withRandomData(), id: .ID1, - color: .COLOR1) + color: .COLOR1, + mode: .Normal) updateProbeStatus(deviceStatus: deviceStatus) }