-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from DrAma999/develop
Added Custom operator Tests passes
- Loading branch information
Showing
20 changed files
with
1,147 additions
and
138 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
Sources/LittleBlueTooth/Classes/CustomOperator/JustLittleBlueTooth.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// JustLittleBlueTooth.swift | ||
// LittleBlueTooth | ||
// | ||
// Created by Andrea Finollo on 28/08/2020. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
public let StartLittleBlueTooth = Just(()).setFailureType(to: LittleBluetoothError.self) |
85 changes: 85 additions & 0 deletions
85
Sources/LittleBlueTooth/Classes/CustomOperator/Listen.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// Listen.swift | ||
// LittleBlueTooth | ||
// | ||
// Created by Andrea Finollo on 26/08/2020. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import os.log | ||
#if TEST | ||
import CoreBluetoothMock | ||
#else | ||
import CoreBluetooth | ||
#endif | ||
|
||
|
||
// MARK: - Listen | ||
|
||
extension Publisher where Self.Failure == LittleBluetoothError { | ||
|
||
/// Returns a publisher with the `LittleBlueToothCharacteristic` where the notify command has been activated. | ||
/// After starting the listen command you should subscribe to the `listenPublisher` to be notified. | ||
/// - parameter littleBluetooth: the `LittleBlueTooth` instance | ||
/// - parameter characteristic: Characteristc you want to be notified. | ||
/// - returns: A publisher with the `LittleBlueToothCharacteristic` where the notify command has been activated. | ||
/// - important: This publisher only activate the notification on a specific characteristic, it will not send notified values. | ||
/// After starting the listen command you should subscribe to the `listenPublisher` to be notified. | ||
public func enableListen(for littleBluetooth: LittleBlueTooth, | ||
from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<LittleBlueToothCharacteristic, LittleBluetoothError> { | ||
|
||
func enableListen<Upstream: Publisher>(upstream: Upstream, | ||
for littleBluetooth: LittleBlueTooth, | ||
from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<LittleBlueToothCharacteristic, LittleBluetoothError> where Upstream.Failure == LittleBluetoothError { | ||
return upstream | ||
.flatMapLatest { _ in | ||
littleBluetooth.enableListen(from: characteristic) | ||
} | ||
} | ||
|
||
return enableListen(upstream: self, | ||
for: littleBluetooth, | ||
from: characteristic) | ||
} | ||
|
||
/// Returns a shared publisher for listening to a specific characteristic. | ||
/// - parameter littleBluetooth: the `LittleBlueTooth` instance | ||
/// - parameter characteristic: Characteristc you want to be notified. | ||
/// - returns: A shared publisher that will send out values of the type defined by the generic type. | ||
/// - important: The type of the value must be conform to `Readable` | ||
public func startListen<T: Readable>(for littleBluetooth: LittleBlueTooth, | ||
from charact: LittleBlueToothCharacteristic) -> AnyPublisher<T, LittleBluetoothError> { | ||
|
||
func startListen<T: Readable, Upstream: Publisher>(upstream: Upstream, | ||
for littleBluetooth: LittleBlueTooth, | ||
from charact: LittleBlueToothCharacteristic) -> AnyPublisher<T, LittleBluetoothError> where Upstream.Failure == LittleBluetoothError { | ||
return upstream | ||
.flatMapLatest { _ in | ||
littleBluetooth.startListen(from: charact) | ||
} | ||
} | ||
|
||
return startListen(upstream: self, | ||
for: littleBluetooth, | ||
from: charact) | ||
} | ||
|
||
/// Disable listen from a specific characteristic | ||
/// - parameter characteristic: characteristic you want to stop listen | ||
/// - returns: A publisher with that informs you about the successful or failed task | ||
public func disableListen(for littleBluetooth: LittleBlueTooth, | ||
from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<LittleBlueToothCharacteristic, LittleBluetoothError> { | ||
func disableListen<Upstream: Publisher>(upstream: Upstream, | ||
for littleBluetooth: LittleBlueTooth, | ||
from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<LittleBlueToothCharacteristic, LittleBluetoothError> where Upstream.Failure == LittleBluetoothError { | ||
return upstream | ||
.flatMapLatest { _ in | ||
littleBluetooth.disableListen(from: characteristic) | ||
} | ||
} | ||
return disableListen(upstream: self, | ||
for: littleBluetooth, | ||
from: characteristic) | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
Sources/LittleBlueTooth/Classes/CustomOperator/ReadAndWrite.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// | ||
// ReadAndWrite.swift | ||
// LittleBlueTooth | ||
// | ||
// Created by Andrea Finollo on 26/08/2020. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import os.log | ||
#if TEST | ||
import CoreBluetoothMock | ||
#else | ||
import CoreBluetooth | ||
#endif | ||
|
||
extension Publisher where Self.Failure == LittleBluetoothError { | ||
// MARK: - RSSI | ||
/// Returns a publisher with the `Int`value of the RSSI. | ||
/// - parameter littleBluetooth: the `LittleBlueTooth` instance | ||
/// - returns: A publisher with the `Int` value of the RSSI.. | ||
public func readRSSI(for littleBluetooth: LittleBlueTooth) -> AnyPublisher<Int, LittleBluetoothError> { | ||
|
||
func readRSSI<Upstream: Publisher>(upstream: Upstream, | ||
for littleBluetooth: LittleBlueTooth) -> AnyPublisher<Int, LittleBluetoothError> where Upstream.Failure == LittleBluetoothError { | ||
return upstream | ||
.flatMapLatest { _ in | ||
littleBluetooth.readRSSI() | ||
} | ||
} | ||
return readRSSI(upstream: self, | ||
for: littleBluetooth) | ||
} | ||
|
||
// MARK: - Read | ||
|
||
/// Read a value from a specific charteristic | ||
/// - parameter littleBluetooth: the `LittleBlueTooth` instance | ||
/// - parameter characteristic: characteristic where you want to read | ||
/// - returns: A publisher with the value you want to read. | ||
/// - important: The type of the value must be conform to `Readable` | ||
public func read<T: Readable>(for littleBluetooth: LittleBlueTooth, | ||
from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<T, LittleBluetoothError> { | ||
|
||
func read<T: Readable, Upstream: Publisher>(upstream: Upstream, | ||
for littleBluetooth: LittleBlueTooth, | ||
from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<T, LittleBluetoothError> where Upstream.Failure == LittleBluetoothError { | ||
return upstream | ||
.flatMapLatest { _ in | ||
littleBluetooth.read(from: characteristic) | ||
} | ||
} | ||
|
||
return read(upstream: self, | ||
for: littleBluetooth, | ||
from: characteristic) | ||
} | ||
|
||
// MARK: - Write | ||
|
||
/// Write a value to a specific charteristic | ||
/// - parameter littleBluetooth: the `LittleBlueTooth` instance | ||
/// - parameter characteristic: characteristic where you want to write | ||
/// - parameter value: The value you want to write | ||
/// - parameter response: An optional `Bool` value that will look for error after write operation | ||
/// - returns: A publisher with that informs you about eventual error | ||
/// - important: The type of the value must be conform to `Writable` | ||
public func write<T: Writable>(for littleBluetooth: LittleBlueTooth, | ||
to characteristic: LittleBlueToothCharacteristic, | ||
value: T, | ||
response: Bool = true) -> AnyPublisher<Void, LittleBluetoothError> { | ||
|
||
func write<T: Writable, Upstream: Publisher>(upstream: Upstream, | ||
for littleBluetooth: LittleBlueTooth, | ||
to characteristic: LittleBlueToothCharacteristic, | ||
value: T, | ||
response: Bool = true) -> AnyPublisher<Void, LittleBluetoothError> where Upstream.Failure == LittleBluetoothError { | ||
return upstream | ||
.flatMapLatest { _ in | ||
littleBluetooth.write(to: characteristic, value: value, response: response) | ||
} | ||
} | ||
|
||
return write(upstream: self, | ||
for: littleBluetooth, | ||
to: characteristic, | ||
value: value, | ||
response: response) | ||
} | ||
|
||
/// Write a value to a specific charteristic and wait for a response | ||
/// - parameter littleBluetooth: the `LittleBlueTooth` instance | ||
/// - parameter characteristic: characteristic where you want to write and listen | ||
/// - parameter value: The value you want to write must conform to `Writable` | ||
/// - returns: A publisher with that post and error or the response of the write requests. | ||
/// - important: Written value must conform to `Writable`, response must conform to `Readable` | ||
public func writeAndListen<W: Writable, R: Readable>(for littleBluetooth: LittleBlueTooth, | ||
from characteristic: LittleBlueToothCharacteristic, | ||
value: W) -> AnyPublisher<R, LittleBluetoothError> { | ||
func writeAndListen<W: Writable, R: Readable, Upstream: Publisher>(upstream: Upstream, | ||
for littleBluetooth: LittleBlueTooth, | ||
from characteristic: LittleBlueToothCharacteristic, | ||
value: W) -> AnyPublisher<R, LittleBluetoothError> where Upstream.Failure == LittleBluetoothError { | ||
return upstream | ||
.flatMapLatest { _ in | ||
littleBluetooth.writeAndListen(from: characteristic, | ||
value: value) | ||
} | ||
} | ||
return writeAndListen(upstream: self, | ||
for: littleBluetooth, | ||
from: characteristic, | ||
value: value) | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.