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:
@@ -8,9 +8,9 @@
|
||||
|
||||
import UIKit
|
||||
|
||||
class MainViewController: UIViewController, SwitchesViewControllerDelegate
|
||||
class MainViewController: UIViewController, SwitchesViewControllerDelegate, ServerMultiplexDelegate
|
||||
{
|
||||
fileprivate var _server: WemoServer
|
||||
fileprivate var _serverMultiplex: ServerMultiplex = ServerMultiplex()
|
||||
fileprivate var _visualizationController: VisualizationViewController = VisualizationViewController()
|
||||
fileprivate var _switchesController: SwitchesViewController = SwitchesViewController()
|
||||
fileprivate var _headerView: HeaderView = HeaderView()
|
||||
@@ -18,10 +18,10 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate
|
||||
|
||||
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
|
||||
{
|
||||
let url = URL(string: "http://midna.xionsf.com:5000")!
|
||||
_server = WemoServer(url)
|
||||
|
||||
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
|
||||
|
||||
_serverMultiplex.delegate = self
|
||||
_serverMultiplex.addServer(WemoServer(URL(string: "http://midna.xionsf.com:5000")!))
|
||||
}
|
||||
|
||||
required init?(coder aDecoder: NSCoder)
|
||||
@@ -104,16 +104,11 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate
|
||||
|
||||
_headerView.xionLogoView.beginAnimating()
|
||||
|
||||
if (!_server.connected) {
|
||||
if _serverMultiplex.devices.count == 0 {
|
||||
// Do initial refresh
|
||||
_updateConnectivityStatus(.connecting)
|
||||
_server.connect { (error: Error?) -> Void in
|
||||
if (error == nil) {
|
||||
self._reloadDevices()
|
||||
self._startUpdatingDevices()
|
||||
} else {
|
||||
self._updateConnectivityStatus(.error)
|
||||
}
|
||||
}
|
||||
_serverMultiplex.refreshDevices()
|
||||
_startUpdatingDevices()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,25 +129,14 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate
|
||||
return true
|
||||
}
|
||||
|
||||
// MARK: API
|
||||
|
||||
var devices: [WemoDevice] = []
|
||||
{
|
||||
didSet
|
||||
{
|
||||
_switchesController.devices = devices
|
||||
_updateVisualization(false)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: SwitchesViewControllerDelegate
|
||||
|
||||
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [WemoDevice])
|
||||
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [AnyDevice])
|
||||
{
|
||||
_updateVisualization(true)
|
||||
|
||||
for device in devices {
|
||||
_server.toggleDevice(device, state: device.state, completion: { (error: Error?) -> Void in })
|
||||
_serverMultiplex.toggleDeviceState(device, state: device.state, completion: { (error: Error?) -> Void in })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,14 +163,10 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate
|
||||
|
||||
internal func _updateVisualization(_ animated: Bool)
|
||||
{
|
||||
var activatedDevicesCount = 0
|
||||
for device in self.devices {
|
||||
if (device.state == .on) {
|
||||
activatedDevicesCount += 1
|
||||
}
|
||||
}
|
||||
let activatedDevicesCount = _serverMultiplex.devices.filter { $0.state == .on }.count
|
||||
let totalDeviceCount = _serverMultiplex.devices.count
|
||||
|
||||
let percentageActivated = (self.devices.count > 0 ? Float(activatedDevicesCount) / Float(self.devices.count) : 0.0)
|
||||
let percentageActivated = (totalDeviceCount > 0 ? Float(activatedDevicesCount) / Float(totalDeviceCount) : 0.0)
|
||||
if (percentageActivated != _visualizationController.percentActivated) {
|
||||
_visualizationController.setPercentActivated(percentageActivated, animated: animated)
|
||||
}
|
||||
@@ -201,21 +181,6 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate
|
||||
}
|
||||
}
|
||||
|
||||
internal func _reloadDevices()
|
||||
{
|
||||
_server.fetchDevices({ (devices: [WemoDevice], error: Error?) -> Void in
|
||||
DispatchQueue.main.async { () -> Void in
|
||||
if (error == nil) {
|
||||
self.devices = devices
|
||||
self._updateConnectivityStatus(.connected)
|
||||
} else {
|
||||
self.devices = []
|
||||
self._updateConnectivityStatus(.error)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
internal func _startUpdatingDevices()
|
||||
{
|
||||
_updateDevices = true
|
||||
@@ -223,7 +188,7 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate
|
||||
let interval = DispatchTime.now() + Double(Int64(10 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
|
||||
DispatchQueue.main.asyncAfter(deadline: interval) { () -> Void in
|
||||
if (self._updateDevices) {
|
||||
self._reloadDevices()
|
||||
self._serverMultiplex.refreshDevices()
|
||||
self._startUpdatingDevices()
|
||||
}
|
||||
}
|
||||
@@ -233,4 +198,28 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate
|
||||
{
|
||||
_updateDevices = false
|
||||
}
|
||||
|
||||
// MARK: Server Multiplex Delegate
|
||||
|
||||
func serverMultiplex(_ multiplex: ServerMultiplex, didAddDevices devices: [AnyDevice])
|
||||
{
|
||||
_switchesController.devices = devices
|
||||
_updateVisualization(false)
|
||||
|
||||
// Update connection status too, if applicable
|
||||
if _serverMultiplex.numServers == _serverMultiplex.numberOfConnectedServers() {
|
||||
_updateConnectivityStatus(.connected)
|
||||
}
|
||||
}
|
||||
|
||||
func serverMultiplex(_ multiplex: ServerMultiplex, devicesStateChanged devices: [AnyDevice])
|
||||
{
|
||||
_switchesController.devicesStateChanged(devices)
|
||||
}
|
||||
|
||||
func serverMultiplex(_ multiplex: ServerMultiplex, didEncounterError error: Error)
|
||||
{
|
||||
_updateConnectivityStatus(.error)
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,12 +12,7 @@ import UIKit
|
||||
|
||||
protocol SwitchesViewControllerDelegate: class
|
||||
{
|
||||
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [WemoDevice])
|
||||
}
|
||||
|
||||
extension SwitchesViewControllerDelegate
|
||||
{
|
||||
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [WemoDevice]) {}
|
||||
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [AnyDevice])
|
||||
}
|
||||
|
||||
class SwitchesViewController: UIViewController,
|
||||
@@ -117,12 +112,12 @@ class SwitchesViewController: UIViewController,
|
||||
|
||||
// MARK: API
|
||||
|
||||
var devices: [WemoDevice] = []
|
||||
var devices: [AnyDevice] = []
|
||||
{
|
||||
didSet
|
||||
{
|
||||
// sort devices by name
|
||||
self.devices.sort(by: { (d1: WemoDevice, d2: WemoDevice) -> Bool in
|
||||
self.devices.sort(by: { (d1: Device, d2: Device) -> Bool in
|
||||
return (d1.name.compare(d2.name, options: .caseInsensitive, range: nil, locale: nil) == .orderedAscending)
|
||||
})
|
||||
|
||||
@@ -132,6 +127,19 @@ class SwitchesViewController: UIViewController,
|
||||
}
|
||||
}
|
||||
|
||||
public func devicesStateChanged(_ changedDevices: [AnyDevice])
|
||||
{
|
||||
var snapshot = _collectionViewDataSource.snapshot()
|
||||
changedDevices.forEach { changedDevice in
|
||||
if let existingDevice = (self.devices.first { $0.hashValue == changedDevice.hashValue }) {
|
||||
existingDevice.state = changedDevice.state
|
||||
snapshot.reloadItems([ existingDevice.hashValue ])
|
||||
}
|
||||
}
|
||||
|
||||
_collectionViewDataSource.apply(snapshot, animatingDifferences: true)
|
||||
}
|
||||
|
||||
// MARK: UICollectionView
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath, identifier: Int) -> UICollectionViewCell?
|
||||
|
||||
Reference in New Issue
Block a user