Skip to content

Commit

Permalink
Merge pull request #10 from combustion-inc/develop
Browse files Browse the repository at this point in the history
Release 0.7.4
  • Loading branch information
jjohnstz authored Feb 1, 2022
2 parents 064f94f + 9be2387 commit c6510af
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 15 deletions.
10 changes: 8 additions & 2 deletions Sources/CombustionBLE/AdvertisingData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ extension AdvertisingData {

serialNumber = value


// Temperatures (8 13-bit) values
let tempData = data.subdata(in: Constants.TEMPERATURE_RANGE)
temperatures = ProbeTemperatures.fromRawData(data: tempData)
Expand All @@ -88,9 +87,16 @@ extension AdvertisingData {

extension AdvertisingData {
// Fake data initializer for previews
public init?(fakeSerial: UInt32) {
public init(fakeSerial: UInt32) {
type = .PROBE
temperatures = ProbeTemperatures.withFakeData()
serialNumber = fakeSerial
}

// Fake data initializer for Simulated Probe
public init(fakeSerial: UInt32, fakeTemperatures: ProbeTemperatures) {
type = .PROBE
temperatures = fakeTemperatures
serialNumber = fakeSerial
}
}
6 changes: 0 additions & 6 deletions Sources/CombustionBLE/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ extension Device {
if(connectionState != .connected) {
DeviceManager.shared.connectToDevice(self)
}

// Update the DeviceManager's record for this device
DeviceManager.shared.devices[self.id] = self
}

/// Mark that app should no longer attempt to maintain a connection to this device.
Expand All @@ -96,9 +93,6 @@ extension Device {

// Disconnect if connected
DeviceManager.shared.disconnectFromDevice(self)

// Update the DeviceManager's record for this device
DeviceManager.shared.devices[self.id] = self
}

func updateConnectionState(_ state: ConnectionState) {
Expand Down
23 changes: 17 additions & 6 deletions Sources/CombustionBLE/DeviceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DeviceManager : ObservableObject {

/// Dictionary of discovered devices.
/// key = string representation of device identifier (UUID)
@Published public var devices : [String: Device] = [String: Device]()
@Published public private(set) var devices : [String: Device] = [String: Device]()


/// Dictionary of discovered probes (subset of devices).
Expand All @@ -50,6 +50,10 @@ public class DeviceManager : ObservableObject {
}
}

public func addSimulatedProbe() {
addDevice(device: SimulatedProbe())
}

/// Private initializer to enforce singleton
private init() {
BleManager.shared.delegate = self
Expand All @@ -62,7 +66,6 @@ public class DeviceManager : ObservableObject {
}
}


/// Adds a device to the local list.
/// - parameter device: Add device to list of known devices.
private func addDevice(device: Device) {
Expand Down Expand Up @@ -99,13 +102,21 @@ public class DeviceManager : ObservableObject {
}

func connectToDevice(_ device: Device) {
// print("Connect to : \(device.serialNumber)")
BleManager.shared.connect(id: device.id)
if let _ = device as? SimulatedProbe, let uuid = UUID(uuidString: device.id) {
didConnectTo(id: uuid)
}
else {
BleManager.shared.connect(id: device.id)
}
}

func disconnectFromDevice(_ device: Device) {
// print("Disconnect from : \(device.serialNumber)")
BleManager.shared.disconnect(id: device.id)
if let _ = device as? SimulatedProbe, let uuid = UUID(uuidString: device.id) {
didDisconnectFrom(id: uuid)
}
else {
BleManager.shared.disconnect(id: device.id)
}
}

/// Request log messages from the specified device.
Expand Down
2 changes: 1 addition & 1 deletion Sources/CombustionBLE/Probe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ extension Probe {
logsUpToDate = true
}

print("Updating status! Temperature log size: \(temperatureLog.dataPoints.count)")
// print("Updating status! Temperature log size: \(temperatureLog.dataPoints.count)")

lastUpdateTime = Date()
}
Expand Down
15 changes: 15 additions & 0 deletions Sources/CombustionBLE/ProbeTemperatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,19 @@ extension ProbeTemperatures {
]
return ProbeTemperatures(values: temperatures)
}

// Generates randome data for Simulated Probe
static func withRandomData() -> ProbeTemperatures {
let temperatures : [Double] = [
Double.random(in: 45.0 ..< 60.0),
Double.random(in: 45.0 ..< 60.0),
Double.random(in: 45.0 ..< 60.0),
Double.random(in: 45.0 ..< 60.0),
Double.random(in: 45.0 ..< 60.0),
Double.random(in: 45.0 ..< 60.0),
Double.random(in: 45.0 ..< 60.0),
Double.random(in: 45.0 ..< 60.0),
]
return ProbeTemperatures(values: temperatures)
}
}
84 changes: 84 additions & 0 deletions Sources/CombustionBLE/SimulatedProbe.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SimulatedProbe.swift
// Simulated Probe

/*--
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

class SimulatedProbe: Probe {
init() {
let advertising = AdvertisingData(fakeSerial: UInt32.random(in: 0 ..< UINT32_MAX),
fakeTemperatures: ProbeTemperatures.withRandomData())
super.init(advertising, RSSI: SimulatedProbe.randomeRSSI(), id: UUID())

firmareVersion = "v1.2.3"

// Create timer to update probe with fake advertising packets
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in
self?.updateFakeAdvertising()
}

// Create timer to update probe with fake status notifications
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in
self?.updateFakeStatus()
}
}

public override var name: String {
var nameStr = super.name
nameStr.removeLast(4)
return String(format: "SIM-\(nameStr)")
}

static func randomeRSSI() -> NSNumber {
return NSNumber(value: Int.random(in: -80 ..< -40))
}

private func updateFakeAdvertising() {
let advertising = AdvertisingData(fakeSerial: UInt32.random(in: 0 ..< UINT32_MAX),
fakeTemperatures: ProbeTemperatures.withRandomData())
updateWithAdvertising(advertising, RSSI: SimulatedProbe.randomeRSSI())
}

private func updateFakeStatus() {
guard connectionState == .connected else { return }

let firstSeq = temperatureLog.dataPoints.first?.sequenceNum ?? 0

let lastSequence: UInt32
if let last = temperatureLog.dataPoints.last?.sequenceNum {
lastSequence = last + 1
}
else {
lastSequence = 0
}

let deviceStatus = DeviceStatus(minSequenceNumber: firstSeq,
maxSequenceNumber: lastSequence,
temperatures: ProbeTemperatures.withRandomData())

updateProbeStatus(deviceStatus: deviceStatus)
}
}

0 comments on commit c6510af

Please sign in to comment.