ServerMultiplex: Introduces a server mutiplexer

- Also moves all the existing Wemo* stuff over to support the multiplexer
- Moves MainViewController to be the owner of the multiplexer, and its delegate
This commit is contained in:
2020-03-14 18:33:02 -07:00
parent f1f86ced29
commit 918818cd99
8 changed files with 335 additions and 83 deletions

View File

@@ -22,8 +22,10 @@ enum ConnectionError : Error
case serverUnavailable
}
class WemoServer
class WemoServer : Server
{
private var devices: [WemoDevice] = []
fileprivate(set) var baseURL: URL
fileprivate(set) var connected: Bool = false
@@ -31,7 +33,7 @@ class WemoServer
fileprivate var _errorStream: StandardErrorOutputStream = StandardErrorOutputStream()
fileprivate var _operationQueue: OperationQueue = OperationQueue()
init(_ url: URL)
required init(_ url: URL)
{
self.baseURL = url
@@ -68,29 +70,32 @@ class WemoServer
completion(nil)
}
func fetchDevices(_ completion: @escaping ([WemoDevice], Error?) -> Void)
func fetchDevices(_ completion: @escaping (Result<[AnyDevice], Error>) -> Void)
{
if (self.connected) {
let op = FetchDevicesOperation(baseURL: self.baseURL, session: _urlSession)
weak var weakOp = op
op.completionBlock = {
guard let strongOp = weakOp else { completion([], nil) ; return }
guard let strongOp = weakOp else { return }
if let error = strongOp.error {
self._logError("Error fetching devices", error: error)
completion(.failure(error))
} else {
self.devices = strongOp.devices
}
completion(strongOp.devices, strongOp.error)
self.devices = strongOp.devices
completion(.success(self.devices.map { AnyDevice($0) }))
}
_operationQueue.addOperation(op)
} else {
let err = ConnectionError.serverUnavailable
completion([], err)
completion(.failure(ConnectionError.serverUnavailable))
}
}
func toggleDevice(_ device: WemoDevice, state: WemoDevice.State, completion: @escaping (Error?) -> Void)
func toggleDevice(_ device: AnyDevice, state: DeviceState, completion: @escaping (Error?) -> Void)
{
if (self.connected) {
if self.connected, let device = findDevice(device) {
let op = ToggleDeviceOperation(baseURL: self.baseURL, session: _urlSession, device: device, state: state)
weak var weakOp = op
op.completionBlock = {
@@ -108,12 +113,22 @@ class WemoServer
}
}
func responsibleForDevice(_ device: AnyDevice) -> Bool
{
return self.devices.contains { $0.serial == device.serial }
}
// MARK: Internal
internal func _logError(_ description: String, error: Error)
{
print("ERROR: \(description) \(error)", to: &_errorStream)
}
internal func findDevice(_ device: AnyDevice) -> WemoDevice?
{
return self.devices.first { $0.serial == device.serial }
}
}
internal class WemoOperation : Operation
@@ -188,9 +203,9 @@ internal class FetchDevicesOperation : WemoOperation
internal class ToggleDeviceOperation : WemoOperation
{
var device: WemoDevice
var state: WemoDevice.State
var state: DeviceState
init(baseURL: URL, session: URLSession, device: WemoDevice, state: WemoDevice.State)
init(baseURL: URL, session: URLSession, device: WemoDevice, state: DeviceState)
{
self.device = device
self.state = state